为了做量化分析需要把获取的数据存入mysql,这样方便处理数据:
直接给上代码,下面看部分代码分享:
计算出一字板存入:
#! /usr/bin/python3
# -*- coding: utf-8 -*-
import pandas as pd
import tushare as ts
from sqlalchemy import create_engine
import pymysql
ts.set_token('youtoken')
pro = ts.pro_api()
day = input('请输入要查询的日期(例如20220101):')
tatle = '每日一字涨停'
name = '一字'+ day
df = pro.bak_daily(trade_date=day, fields='trade_date,ts_code,name,industry,pct_change,close,open,avg_price,vol_ratio,turn_over,vol,selling,buying,total_share,float_share,total_mv,high,low,pre_close')
df.columns = ('股票代码','交易日期','股票名称','涨跌幅','收盘价','开盘价','最高价','最低价','昨日收盘价','量比','换手率','成交量','内盘(主动卖,手)','外盘(主动买,手)','总股本(亿)','流通股本(亿)','所属行业','总市值','平均价')
def sum(a,b): # 条件函数
sums = a-b
return sums
df['开收盘价差'] = df.apply(lambda row:sum(row['收盘价'],row['开盘价']),axis=1)
df['高低价差'] = df.apply(lambda row:sum(row['最高价'],row['最低价']),axis=1)
df = df[df['开收盘价差']==0]
df = df[df['高低价差']==0]
df = df[df['涨跌幅']>9]
# df = df.set_index('股票代码')
engine = create_engine(f"mysql+pymysql://root:[email protected]:3306/每日一字涨停?charset=utf8")
df.to_sql(name=name, con=engine, schema=tatle, if_exists='replace', index=False,
chunksize=None, dtype=None)
print('*'*60)
print(df)
print(f'{day}日一字板数据已经导入到mysql的“每日一字涨停”中,请查看!!!')
print('*'*60)
计算出净流量大的股票
#! /usr/bin/python3
# -*- coding: utf-8 -*-
import pandas as pd
import tushare as ts
from sqlalchemy import create_engine
import pymysql
ts.set_token('youtoken')
pro = ts.pro_api()
def sum(a, b): # 用于计算总流入金额,总流出金额,总买入量及总卖出量
sums = a - b
return sums
def s(day,df_date):
tatle = "每日净量卖出"
name = "卖出"+day
df = pro.moneyflow(trade_date=day)
df.columns = ('股票代码', '交易日期', '小单买入量', '小单买入金额',
'小单卖出量', '小单卖出金额', '中单买入量', '中单买入金额',
'中单卖出量', '中单卖出金额', '大单买入量', '大单买入金额',
'大单卖出量', '大单卖出金额', '特大单买入量', '特大单买入金额',
'特大单卖出量', '特大单卖出金额', '净流入量', '净流入额')
df = df.set_index('股票代码')
df2 = df
df2['净卖出特大单'] = df2.apply(lambda row: sum(row['特大单卖出量'], row['特大单买入量']), axis=1)
df2['净卖出大单'] = df2.apply(lambda row: sum(row['大单卖出量'], row['大单买入量']), axis=1)
df2['净卖出中单'] = df2.apply(lambda row: sum(row['中单卖出量'], row['中单买入量']), axis=1)
df2['净卖出小单'] = df2.apply(lambda row: sum(row['小单卖出量'], row['小单买入量']), axis=1)
df2 = df2[df2['净卖出特大单'] > df2['净卖出大单']]
df2 = df2[df2['净卖出大单'] > df2['净卖出中单']]
df2 = df2[df2['净卖出中单'] > df2['净卖出小单']]
df2 = df2[df2['净卖出中单'] > 0]
date4 = df_date.merge(df2, on='股票代码').fillna(0)
# date5 = date4.set_index('股票代码')
engine = create_engine(f"mysql+pymysql://root:[email protected]:3306/每日净量卖出?charset=utf8")
date4.to_sql(name=name, con=engine, schema=tatle, if_exists='replace', index=False,
chunksize=None, dtype=None)
print('*'*60)
print(f'{day}日卖出数据已经导入到mysql的“每日净量卖出”中,请查看!!!')
print('*'*60)
# print(date4)
def b(day,df_date):
tatle = "每日净量买入"
name = "买入"+day
df = pro.moneyflow(trade_date=day)
df.columns = ('股票代码', '交易日期', '小单买入量', '小单买入金额',
'小单卖出量', '小单卖出金额', '中单买入量', '中单买入金额',
'中单卖出量', '中单卖出金额', '大单买入量', '大单买入金额',
'大单卖出量', '大单卖出金额', '特大单买入量', '特大单买入金额',
'特大单卖出量', '特大单卖出金额', '净流入量', '净流入额')
df = df.set_index('股票代码')
df1 = df
df1['净买入特大单'] = df1.apply(lambda row:sum(row['特大单买入量'],row['特大单卖出量']),axis=1)
df1['净买入大单'] = df1.apply(lambda row:sum(row['大单买入量'],row['大单卖出量']),axis=1)
df1['净买入中单'] = df1.apply(lambda row:sum(row['中单买入量'],row['中单卖出量']),axis=1)
df1['净买入小单'] = df1.apply(lambda row:sum(row['小单买入量'],row['小单卖出量']),axis=1)
df1 = df1[df1['净买入特大单'] > df1['净买入大单']]
df1 = df1[df1['净买入大单'] > df1['净买入中单']]
df1 = df1[df1['净买入中单'] > df1['净买入小单']]
df1 = df1[df1['净买入中单'] >0]
date2 = df_date.merge(df1,on ='股票代码').fillna(0)
# date3 = date2.set_index('股票代码')
engine = create_engine(f"mysql+pymysql://root:[email protected]:3306/每日净量买入?charset=utf8")
date2.to_sql(name=name, con=engine, schema=tatle, if_exists='replace', index=False,
chunksize=None, dtype=None)
print('*'*60)
print(f'{day}日买入情数据已经导入到mysql的“每日净量买入”中,请查看!!!')
print('*'*60)
if __name__ == '__main__':
while True:
day = input('请输入要查询的日期(例如20220101):')
df_date = pro.bak_daily(trade_date=day, fields='ts_code,name,industry')
df_date.columns = ('股票代码','股票名称','所属行业')
b(day,df_date)
s(day,df_date)
获取每日行情数据存入
#! /usr/bin/python3
# -*- coding: utf-8 -*-
import pandas as pd
import tushare as ts
from sqlalchemy import create_engine
import pymysql
ts.set_token('youtoken')
pro = ts.pro_api()
day = input('请输入要查询的日期(例如20220101):')
tatle = "每日行情"
name = "每日行情"+day
df = pro.bak_daily(trade_date=day, fields='trade_date,ts_code,name,industry,pct_change,close,open,avg_price,vol_ratio,turn_over,vol,selling,buying,total_share,float_share,total_mv,high,low,pre_close')
df.columns = ('股票代码','交易日期','股票名称','涨跌幅','收盘价','开盘价','最高价','最低价','昨日收盘价','量比','换手率','成交量','内盘(主动卖,手)','外盘(主动买,手)','总股本(亿)','流通股本(亿)','所属行业','总市值','平均价')
engine = create_engine(f"mysql+pymysql://root:[email protected]:3306/每日行情?charset=utf8")
df.to_sql(name=name, con=engine, schema=tatle, if_exists='replace', index=False,
chunksize=None, dtype=None)
print('*'*60)
print(f'{day}日行情数据已经导入到mysql的“每日行情”中,请查看!!!')
print('*'*60)