导报:
import alpha
import akshare as ak
import sql
import time
import pandas as pd
import PySimpleGUI as sg
import tkinter as tk
import matplotlib.pyplot as plt
plt.rcParams['font.family']='SimHei'
plt.rcParams['axes.unicode_minus']=False
创建tkinter
root=tk.Tk()
root.title('alpha1到200计算')
root.geometry('500x200')
按键功能:
def culu_all_alpha_to_sql():
'''
计算全部的alpha alpha1到alpha191
'''
stock=sg.popup_get_text('输入股票代码比如sh600111')
start_date=sg.popup_get_text('输入开始人气比如20210101')
table_name=stock+'alpha'
#连接数据库
sql.learn_mysql().concnet_mysql()
#删除数据表
sql.learn_mysql().drop_table(table_name=table_name)
#建立数据表
sql.learn_mysql().create_table(table_name=table_name)
#添加标题
sql.learn_mysql().add_table_columns_name(table_name=table_name,columns='date',endcode='varchar(225)')
sql.learn_mysql().add_table_columns_name(table_name=table_name,columns='alpha',endcode='varchar(225)')
sql.learn_mysql().add_table_columns_name(table_name=table_name,columns='value',endcode='varchar(225)')
sql.learn_mysql().drop_table_columns_name(table_name=table_name,columns='id')
#sql.learn_mysql().desc_table(table_name=table_name)
#获取股票数据
df=ak.stock_zh_a_daily(symbol=stock,start_date=start_date)
#该名称
loc=time.localtime()
#周末不计算
if loc.tm_wday<7:
year=loc.tm_year
mo=loc.tm_mon
daily=loc.tm_mday
if mo<=9:
mo='0'+str(mo)
if daily<=9:
daily='0'+str(daily)
date='{}-{}-{}'.format(year,mo,daily)
df.rename(columns={'data':'data','open':'openPrice','close':'closePrice','low':'lowestPrice','high':'highestPrice','volume':'turnoverVol'},inplace=True)
for i in range(1,192):
if i<=9:
i='00'+str(i)
elif i<=99 and int(i)>9:
i='0'+str(i)
else:
pass
try:
alpha_value=eval('alpha.alpha{}'.format(i))(data=df)
bj=i
sql.learn_mysql().insert_alpha(table_name=table_name,value=[date,str(bj),str(alpha_value)])
except:
print('计算失败')
sql.learn_mysql().show_table_context(table_name=table_name)
else:
print('周末不计算')
def culu_all_alpha_to_excel():
'''
计算全部的alpha alpha1到alpha191
保持到本地excel
'''
stock=sg.popup_get_text('输入股票代码比如sh600111')
start_date=sg.popup_get_text('输入开始人气比如20210101')
df=ak.stock_zh_a_daily(symbol=stock,start_date=start_date)
#该名称
loc=time.localtime()
#周末不计算
if loc.tm_wday<6:
year=loc.tm_year
mo=loc.tm_mon
daily=loc.tm_mday
if mo<=9:
mo='0'+str(mo)
if daily<=9:
daily='0'+str(daily)
date='{}-{}-{}'.format(year,mo,daily)
df.rename(columns={'data':'data','open':'openPrice','close':'closePrice','low':'lowestPrice','high':'highestPrice','volume':'turnoverVol'},inplace=True)
date_list=[]
type_list=[]
alpha_value_list=[]
for i in range(1,192):
if i<=9:
i='00'+str(i)
elif i<=99 and int(i)>9:
i='0'+str(i)
else:
pass
try:
alpha_value=eval('alpha.alpha{}'.format(i))(data=df)
date_list.append(date)
type_list.append(i)
alpha_value_list.append(alpha_value)
except:
print('计算失败')
data=pd.DataFrame({'时间':date_list,'alpha':type_list,'值':alpha_value_list})
data.to_excel(r'C:\Users\Administrator\Desktop\alpha因子\{}.xlsx'.format(stock))
print(data)
#计算保持到csv
def culu_all_alpha_to_csv():
'''
计算全部的alpha alpha1到alpha191
保持到本地csv
'''
stock=sg.popup_get_text('输入股票代码比如sh600111')
start_date=sg.popup_get_text('输入开始人气比如20210101')
df=ak.stock_zh_a_daily(symbol=stock,start_date=start_date)
#该名称
loc=time.localtime()
#周末不计算
if loc.tm_wday<6:
year=loc.tm_year
mo=loc.tm_mon
daily=loc.tm_mday
if mo<=9:
mo='0'+str(mo)
if daily<=9:
daily='0'+str(daily)
date='{}-{}-{}'.format(year,mo,daily)
df.rename(columns={'data':'data','open':'openPrice','close':'closePrice','low':'lowestPrice','high':'highestPrice','volume':'turnoverVol'},inplace=True)
date_list=[]
type_list=[]
alpha_value_list=[]
for i in range(1,192):
if i<=9:
i='00'+str(i)
elif i<=99 and int(i)>9:
i='0'+str(i)
else:
pass
try:
alpha_value=eval('alpha.alpha{}'.format(i))(data=df)
date_list.append(date)
type_list.append(i)
alpha_value_list.append(alpha_value)
except:
print('计算失败')
data=pd.DataFrame({'时间':date_list,'alpha':type_list,'值':alpha_value_list})
data.to_csv(r'C:\Users\Administrator\Desktop\alpha因子\{}.csv'.format(stock))
print(data)
#滚动的alpha计算
def culu_roll_alpha():
'''
只需要输入股票代码
滚动的alpha计算1到191
i选择alpha及,比如1是alpha1
'''
stock=sg.popup_get_text('输入股票代码比如sh600111')
start_date=sg.popup_get_text('输入开始人气比如20210101')
i=int(sg.popup_get_text('选择alpha几,比如1'))
import time
loc=time.localtime()
year=loc.tm_year
mo=loc.tm_mon
daily=loc.tm_mday
if mo<=9:
mo='0'+str(mo)
if daily<=9:
daily='0'+str(daily)
end_date='{}{}{}'.format(year,mo,daily)
list_date=pd.date_range(start=start_date,end=end_date)
#获取数据
df=ak.stock_zh_a_daily(symbol=stock,start_date=start_date)
df.rename(columns={'date':'date','open':'openPrice','close':'closePrice','low':'lowestPrice','high':'highestPrice','volume':'turnoverVol'},inplace=True)
df.index=pd.to_datetime(df['date'])
result_list=[]
lx_list=[]
date_list=[]
#进行滚动计算,自己也可用该
if i<=9:
i='00'+str(i)
elif i<=99 and int(i)>9:
i='0'+str(i)
else:
pass
for m in list_date:
df1=df[start_date:m]
try:
result=eval('alpha.alpha{}'.format(i))(data=df1)
result_list.append(result)
lx_list.append(i)
date_list.append(m)
except:
print('计算失败')
data=pd.DataFrame({'时间':date_list,'值':result_list,'alpha类型':lx_list})
print(data)
data.to_excel(r'C:\Users\Administrator\Desktop\alpha因子\滚动{}alpha_{}.xlsx'.format(stock,i))
return data,df
def plot_alpha():
'''
'''
df,stock=culu_roll_alpha()
plt.subplot(212)
plt.title('alpha绘制')
plt.plot(df['时间'],df['值'],label='alpha历史滚动',color='m')
plt.grid()
plt.subplot(211)
plt.plot(stock['date'],stock['closePrice'],label='收盘价走势',color='g')
plt.title('收盘价')
plt.xlabel('时间')
plt.ylabel('值')
plt.legend()
plt.grid()
plt.show()
tkinter按键功能绑定:
button1=tk.Button(root,text='计算当日全部的量化因子alpha,1到200因子保持到数据库',command=culu_all_alpha_to_sql,fg='red',font=18)
button2=tk.Button(root,text='计算当日全部的量化因子alpha,1到200因子保持到excel表',command=culu_all_alpha_to_excel,fg='red',font=18)
button3=tk.Button(root,text='计算当日全部的量化因子alpha,1到200因子保持到csv表',command=culu_all_alpha_to_csv,fg='red',font=18)
button4=tk.Button(root,text='计算历史滚动量化因子alpha,1到200因子保持到excel表',command=culu_roll_alpha,fg='red',font=18)
button5=tk.Button(root,text='alpha历史滚动图绘制',command=plot_alpha,fg='red',font=18)
button1.pack()
button2.pack()
button3.pack()
button4.pack()
button5.pack()
root.mainloop()