我们利用计算机技术,通过建模分析、优化参数等手段,从历史金融数据中挖掘出影响投资的指标,使用程序进行自动交易来获得“超额”的收益,这种投资方法就叫做量化交易。
现在,很多量化机构将人工智能和机器学习与量化策略相结合。国内的一些顶尖私募,比如:九坤、幻方、朱雀等都在使用AI量化策略,从各大公司的招聘公告上也可以看出这点。
数据来源:Tushare金融大数据开放社区 运行准备:点击上方官网,注册tushare账户,获取token
获取数据
#导入库 import tushare as ts import pandas as pd import numpy as np import time,random pro = ts.pro_api(token='你自己的token') # L上市 D退市 P暂停上市 # ts_code 股票代码 exchange 交易所 SSE上交所 SZSE 深交所 # list_date 上市日期 delist_date 退市日期 stock_L = pro.stock_basic(exchange='', list_status='L', fields='ts_code,exchange,list_date,delist_date') stock_D = pro.stock_basic(exchange='', list_status='D', fields='ts_code,exchange,list_date,delist_date') stock_P = pro.stock_basic(exchange='', list_status='P', fields='ts_code,exchange,list_date,delist_date') #整合所有股票 data=pd.concat([stock_L,stock_D,stock_P],axis=0).reset_index(drop=True) result = pd.DataFrame() count=0 for ts_code in data.ts_code.tolist(): start_time=time.time() df = pro.daily_basic(ts_code=ts_code, trade_date='', start_date='20060101',fields='ts_code,trade_date,pe') result=pd.concat([result,df],axis=0).reset_index(drop=True) count+=1 if count%200: pass else: end_time=time.time() tm=end_time-start_time #限制60秒内最多请求200次 while tm<=60 : time.sleep(60-tm+1) end_time=time.time() tm=end_time-start_time print(count,"耗时 %s 秒"%tm) #获取到的数据保存到本地,也可以存到数据库里持续更新 result.to_csv("PE监控.csv",index=False)
注意:做这个图的时候还是沿用原来的指数规则,2020/7/22上证指数进行了修正
可视化绘图
#导入库 import warnings warnings.filterwarnings("ignore") import numpy as np import pandas as pd import matplotlib.pyplot as plt plt.style.use('seaborn') %matplotlib inline #导入之前获取的数据 result=pd.read_csv("PE监控.csv") #等权重处理 data=pd.pivot_table(result,values='pe',index='trade_date',aggfunc='mean') #绘图 data.plot(figsize=(15,6),subplots=True)
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句