獲取指數池:
def init(ContextInfo):#設置股票池stock300 =ContextInfo.get_stock_list_in_sector('滬深300')ContextInfo.stock300_weight = {}stock300_symbol = []stock300_weightlist = [] ContextInfo.index_code = ContextInfo.stockcode+"."+ContextInfo.market#重新制作股票池for key in stock300:# 保留權重大于0.35%的成份股if (ContextInfo.get_weight_in_index(ContextInfo.index_code, key) / 100) > 0.0035:stock300_symbol.append(key)ContextInfo.stock300_weight[key] = ContextInfo.get_weight_in_index(ContextInfo.index_code, key) / 100stock300_weightlist.append(ContextInfo.get_weight_in_index(ContextInfo.index_code, key) / 100)print('選擇的成分股權重總和為: ', np.sum(stock300_weightlist)) ContextInfo.set_universe(stock300_symbol)#print ContextInfo.stock300_weight# 資產配置的初始權重,配比為0.6-0.8-1.0ContextInfo.ratio = 0.8#賬號ContextInfo.accountid = "testS"
擇時買賣:
# 獲取過去5天的價格數據,若連續上漲則為強勢股,調倉到(權重+0.2)的倉位if all(diff>0) and holdings[stock] < buytarget_num:buy_num = buytarget_num - holdings[stock]order_shares(stock,buy_num*100,'fix',pre_close,ContextInfo,ContextInfo.accountid)buy_sum += 1#print "買入",stock,buy_num# 獲取過去5天的價格數據,若連續下跌則為弱勢股,調倉到(權重-0.2)的倉位elif all(diff<0) and holdings[stock] > selltarget_num:sell_num = holdings[stock] - selltarget_numorder_shares(stock,(-1.0)*sell_num*100,'fix',pre_close,ContextInfo,ContextInfo.accountid)sell_sum += 1#print "賣出",stock,sell_num
策略代碼:?
#coding:gbk
'''
回測模型示例(非實盤交易策略)本策略以0.8為初始權重跟蹤指數標的滬深300中權重大于0.35%的成份股.
個股所占的百分比為(0.8*成份股權重)*100%.然后根據個股是否:
1.連續上漲5天 2.連續下跌5天
來判定個股是否為強勢股/弱勢股,并對其把權重由0.8調至1.0或0.6'''
#在指數(例如HS300)日線下運行
import numpy as npdef init(ContextInfo):#設置股票池stock300 =ContextInfo.get_stock_list_in_sector('滬深300')ContextInfo.stock300_weight = {}stock300_symbol = []stock300_weightlist = [] ContextInfo.index_code = ContextInfo.stockcode+"."+ContextInfo.marketfor key in stock300:# 保留權重大于0.35%的成份股if (ContextInfo.get_weight_in_index(ContextInfo.index_code, key) / 100) > 0.0035:stock300_symbol.append(key)ContextInfo.stock300_weight[key] = ContextInfo.get_weight_in_index(ContextInfo.index_code, key) / 100stock300_weightlist.append(ContextInfo.get_weight_in_index(ContextInfo.index_code, key) / 100)print('選擇的成分股權重總和為: ', np.sum(stock300_weightlist)) ContextInfo.set_universe(stock300_symbol)#print ContextInfo.stock300_weight# 資產配置的初始權重,配比為0.6-0.8-1.0ContextInfo.ratio = 0.8#賬號ContextInfo.accountid = "testS"def handlebar(ContextInfo):buy_sum = 0sell_sum = 0index = ContextInfo.barposrealtimetag = ContextInfo.get_bar_timetag(index)print(timetag_to_datetime(realtimetag, '%Y%m%d %H:%M:%S'))dict_close=ContextInfo.get_history_data(7,'1d','close',3)print('測試1',ContextInfo.get_history_data(7,'1d','close'))print('測試',ContextInfo.get_history_data(7,'1d','close',3))print('測試',ContextInfo.get_history_data(7,'1d','close',2))print('測試',ContextInfo.get_history_data(7,'1d','close',1))#持倉市值holdvalue = 0#持倉holdings=get_holdings(ContextInfo.accountid,"STOCK")#剩余資金surpluscapital=get_avaliablecost(ContextInfo.accountid,"STOCK")for stock in ContextInfo.stock300_weight:if stock in holdings:if len(dict_close[stock]) == 7:holdvalue += dict_close[stock][-2] * holdings[stock]for stock in ContextInfo.stock300_weight:# 若沒有倉位則按照初始權重開倉if stock not in holdings and stock in list(dict_close.keys()):if len(dict_close[stock]) == 7:pre_close = dict_close[stock][-1]buy_num = int(ContextInfo.stock300_weight[stock] * ( holdvalue + surpluscapital ) *ContextInfo.ratio / pre_close /100)order_shares(stock,buy_num*100,'fix',pre_close,ContextInfo,ContextInfo.accountid)buy_sum += 1#print "買入",stock,buy_numelif stock in list(dict_close.keys()):if len(dict_close[stock]) == 7:diff = np.array(dict_close[stock][1:6]) - np.array(dict_close[stock][:-2])pre_close = dict_close[stock][-1]buytarget_num = int(ContextInfo.stock300_weight[stock] * ( holdvalue + surpluscapital ) * (ContextInfo.ratio + 0.2)/ pre_close /100)selltarget_num = int(ContextInfo.stock300_weight[stock] * ( holdvalue + surpluscapital ) *(ContextInfo.ratio - 0.2)/ pre_close /100)# 獲取過去5天的價格數據,若連續上漲則為強勢股,調倉到(權重+0.2)的倉位if all(diff>0) and holdings[stock] < buytarget_num:buy_num = buytarget_num - holdings[stock]order_shares(stock,buy_num*100,'fix',pre_close,ContextInfo,ContextInfo.accountid)buy_sum += 1#print "買入",stock,buy_num# 獲取過去5天的價格數據,若連續下跌則為弱勢股,調倉到(權重-0.2)的倉位elif all(diff<0) and holdings[stock] > selltarget_num:sell_num = holdings[stock] - selltarget_numorder_shares(stock,(-1.0)*sell_num*100,'fix',pre_close,ContextInfo,ContextInfo.accountid)sell_sum += 1#print "賣出",stock,sell_numif not ContextInfo.do_back_test:ContextInfo.paint('buy_num', buy_sum, -1, 0)ContextInfo.paint('sell_num', sell_sum, -1, 0)def get_holdings(accountid,datatype):holdinglist={}resultlist=get_trade_detail_data(accountid,datatype,"POSITION")for obj in resultlist:holdinglist[obj.m_strInstrumentID+"."+obj.m_strExchangeID]=obj.m_nVolume/100return holdinglistdef get_avaliablecost(accountid,datatype):result=0resultlist=get_trade_detail_data(accountid,datatype,"ACCOUNT")for obj in resultlist:result=obj.m_dAvailablereturn result