返回列表 发布新帖

【done】小白求助:券商版里面的行业轮动示例需要修改哪些函数?

761 1
发表于 2024-4-1 12:39:06 | 显示全部楼层 阅读模式
运行提示:get_history_data版本较老,推荐使用get_market_data_ex替代,配合download_history_data补充昨日以前的K线数据。

问题是:应该如何修改?还有里面是否还有其他需要修改的函数?例如get_sector?还有set_universe?

  1. #coding=gbk

  2. #导入常用库
  3. import numpy as np
  4. import math

  5. #初始化函数,用于初始化策略
  6. def init(C):
  7.         MarketPosition={} #初始化持仓字典
  8.         C.MarketPosition=MarketPosition #将持仓字典存储在全局变量C中
  9.         index_universe=['399381.SZ','399382.SZ','399383.SZ','399384.SZ','399385.SZ','399386.SZ'] #行业指数列表
  10.         index_stocks=[] #存储行业指数下的所有股票
  11.         for index in index_universe: #遍历每个行业指数
  12.                 for stock in C.get_sector(index): #获取该行业下的所有股票
  13.                         index_stocks.append(stock) #将股票添加到列表中
  14.         C.set_universe(index_universe+index_stocks) #设定股票池,包括行业指数和行业内的所有股票
  15.         C.day=20 #设置回测周期
  16.         C.ratio=0.8 #设定资金比例
  17.         C.holding_amount=5 #设定持仓数量为5只股票
  18.         C.accountid='testS' #设定账户ID
  19.         
  20. def handlebar(C):
  21.         buy_condition=False #买入初始值为False
  22.         sell_condition=False #卖出初始值为False
  23.         d=C.barpos #获取当前K线值
  24.         lastdate=timetag_to_datetime(C.get_bar_timetag(d-1),'%Y%m%d') #获取上一根K线的日期
  25.         date=timetag_to_datetime(C.get_bar_timetag(d),'%Y%m%d') #获取当前K线日期
  26.         print(date)
  27.         index_list=['399381.SZ','399382.SZ','399383.SZ','399384.SZ','399385.SZ','399386.SZ'] #设定行业指数列表
  28.         return_index=[] #建立空列表,用于存储行业指数收益率
  29.         weight=C.ratio/C.holding_amount #计算每只股票的权重
  30.         size_dict={} #设立空字典,存储股票市值
  31.         if (float(date[-4:-2])!=float(lastdate[-4:-2])): #根据与钱一日的月份数据是否相同判断是否新的一个月
  32.                 his=C.get_history_data(21,'1d','close') #获取过去21个交易日的收盘价数据
  33.                 for k in list(his.keys()): #遍历历史数据
  34.                         if len(his[k])==0: #如果某只股票没有历史数据
  35.                                 del his[k] #删除该股票
  36.                 for index in index_list: #遍历行业指数列表
  37.                         ratio=0 #初始化收益率
  38.                         try:
  39.                                 ratio=(his[index][-2]-his[index][0])/his[index][0] #计算过去20个交易日的收益率
  40.                         except KeyError: #如果索引错误
  41.                                 print('key error:'+index) #打印错误信息
  42.                         except IndexError: #如果列表索引越界
  43.                                 print('list index out of range:'+index) #打印错误信息
  44.                         return_index.append(ratio) #将收益率添加在列表中
  45.                 #获取指定数内收益表现最好的行业
  46.                 best_index=index_list[np.argmax(return_index)]
  47.                 #获取最佳行业指数下当天有交易的股票
  48.                 index_stock=C.get_sector(best_index)
  49.                 stock_available=[] #建立空集,用于存储当天有交易股票
  50.                 for stock in index_stock:
  51.                         if C.is_suspended_stock(stock)==False:
  52.                                 stock_available.append(stock)
  53.                 for stock in stock_available:
  54.                         if stock in list(his.keys()):
  55.                                 #目前历史流通股本取不到,暂用总股本
  56.                                 if len(his[stock])>=2:
  57.                                         stocksize=his[stock][-2]*float(C.get_financial_data(['CAPITALSTRUCTURE.total_capital'],[stock],lastdate,date).iloc[0,-1])
  58.                                         size_dict[stock]=stocksize
  59.                                 elif len(his[stock])==1:
  60.                                         stocksize=his[stock][-1]*float(C.get_financial_data(['CAPITALSTRUCTURE.total_capital'],[stock],lastdate,date).iloc[0,-1])
  61.                                         size_dict[stock]=stocksize
  62.                                 else:
  63.                                         return
  64.                 size_sorted=sorted(list(size_dict.items()),key=lambda item:item[1])
  65.                 pre_holding=[]
  66.                 for tuple in size_sorted[-C.holding_amount:]:
  67.                         pre_holding.append(tuple[0])
  68.                         print('买入备选',pre_holding)
  69.                 #函数下单
  70.                 if len(pre_holding)>0:
  71.                         sellshort_list=[]
  72.                         for stock in list(C.MarketPosition.keys()):
  73.                                 if stock not in pre_holding and (stock in list(his.keys())):
  74.                                         order_shares(stock,-C.MarketPosition[stock],'lastest',histock[-1],C,C.accountid)
  75.                                         print('sell',stock)
  76.                                         sell_condition=True
  77.                                         sellshort_list.append(stock)
  78.                                 if len(sellshort_list)>0:
  79.                                         for stock in sellshort_list:
  80.                                                 del C.MarketPosition[stock]
  81.                                 for stock in pre_holding:
  82.                                         if stock not in list(C.MarketPosition.keys()):
  83.                                                 Lots=math.floor(C.ratio*(1.0/len(pre_holding))*C.capital/(his[stock][-1]*100))
  84.                                                 order_shares(stock,Lots*100,'lastest',his[stock][-1],C,C.accountid)
  85.                                                 print('buy',stock)
  86.                                                 buy_condition=True
  87.                                                 C.MarketPosition[stock]=Lots*100
  88.                 C.paint('do_buy',int(buy_condition),-1,0)
  89.                 C.paint('do_sell',int(sell_cindition),-1,0)
  90.                
复制代码

评论1

心如止水
发表于 2024-4-1 17:59:21 | 显示全部楼层
您好,只要能用就不用改的,不维护了不代表不能用哈

回复

您需要登录后才可以回帖 登录 | 立即注册

客服专线

400-080-8112

用思考的速度交易,用真诚的态度合作,我们是认真的!
  • 关注公众号
  • 添加微信客服
Copyright © 2001-2025 迅投QMT社区 版权所有 All Rights Reserved. 蜀ICP备19002686号-2
关灯 快速发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表