欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

多因子选股+指标择时 python3.7+tushare pro

程序员文章站 2024-01-19 08:09:46
import tushare as tsimport pandas as pdimport timets.set_token('**************************')pro=ts.pro_api()# 获取股票池hs300_all_stock = []stocks = []df = pro.index_weight(index_code='399300.SZ', start_date='202011011')hs300_all_stock = df['con_co...
import tushare as ts
import pandas as pd
import time

ts.set_token('**************************')
pro=ts.pro_api()
# 获取股票池
hs300_all_stock = []

stocks = []

df = pro.index_weight(index_code='399300.SZ', start_date='202011011')

hs300_all_stock = df['con_code'].values.tolist()

index_num = []

for i in range(len(hs300_all_stock)):
	
	index_num.append(i)

data=pd.DataFrame(index=index_num,columns=['code','turn_over','total_mv','attack'])

data.code = hs300_all_stock
# 获取股票池所有股票信息    len(hs300_all_stock)
for num in range(100):
		#turn_over:换手率  total_mv:总市值   attack:攻击波
	stock_data = pro.bak_daily(ts_code = data.code[num],start_date = '20201110',\
	end_date = '20201111',fields='turn_over,total_mv,attack')
	data.turn_over[num] = stock_data['turn_over'][0]
	data.total_mv[num] = stock_data['total_mv'][0]
	data.attack[num] = stock_data['attack'][0]
	time.sleep(6)
data.to_excel('沪深300换手率冲击波1.xlsx')
# 通过市值升序排列 取100个,通过换手率降序排列取出前30,通过攻击波取前10名,短线操作
data.sort_values(by=["total_mv"],ascending=True)
data1 = data.head(int(len(data)/3))  

data1.sort_values(by=["turn_over"],ascending=False)
data2 = data1.head(int(len(data1)/3))    

data2.sort_values(by=["attack"],ascending=False)
data3 = data2.head(int(len(data2)/3))  

stocks = data3['code'].values.tolist()   
print(stocks)
# 获取十日平均,和5日平均,及当前价
for stock in stocks:
	df1 = pro.daily(ts_code=stock, start_date='20201001', end_date='20201111')
	
	df1=df1.sort_values(by=["trade_date"],ascending=True)

	df1.index = df1.iloc[:,1]

	df1.index = pd.to_datetime(df1.index,format='%Y-%m-%d')

	data4 = pd.DataFrame(index=df1.index,columns=['close','mean5','mean10'])
	
	data4['close'] = df1.close
	
	data4['mean5']=data4.close.rolling(5).mean()
	
	data4['mean10']=data4.close.rolling(10).mean()

	if data4['close'][-1] < data4['mean5'][-1] and data4['close'][-1] < data4['mean5'][-1]:
		
		stocks.remove(stock)

print(stocks)

本文地址:https://blog.csdn.net/zhaojieming1990/article/details/109616715