def read_func1(signalDataAddr, table_name): if signalDataAddr is not None: signalData = SourceDataDao.read_file_set_index(signalDataAddr) elif table_name is not None: signalData = SignalDataMSSQLDao.select_signal_data(table_name) else: raise ValueError('signal data address and table name is unknown!') # print('signalData') # print(signalData) return signalData
def main(): signalDataDf = SourceDataDao.read_file_set_index('D:\\0_Data\\Mom_basedon_HS300.h5') techList = [StockConst.MOM] sliceIdx = None numOfDailySignal = 4 sliceTotalNum = None sliceDict = {'sliceIdx': sliceIdx, 'numOfDailySignal': numOfDailySignal, 'sliceTotalNum': sliceTotalNum} allSelectStockDf = select_func_6(signalDataDf,techList,sliceDict)
def test_select_stock_func1(self): signalData = SourceDataDao.read_file_set_index( StockConst.ROOT + StockConst.SIGNAL_DATA_H5) techList = [StockConst.MOM] sliceIdx = None numOfDailySignal = 4 sliceTotalNum = None sliceDict = { 'sliceIdx': sliceIdx, 'numOfDailySignal': numOfDailySignal, 'sliceTotalNum': sliceTotalNum } groupedSignalData = BackTestMainNew.select_stock_func1( signalData, techList, sliceDict) currSignalData = groupedSignalData.xs('2005-01-05') entity = currSignalData.ix['000950.SZ'] print(entity)
def main(read_func,select_stock_func,trade_func,techList,sliceDict,signalDataAddr = '', dailyQuoteAddr = '', indexQuoteAddr='', benchmark='', startDate = '2004-01-01', endDate = '2005-12-31', doPlot=True, table_name=None): start = time.clock() # signalData = SourceDataDao.loadSignalData(signalDataAddr) # dailyQuote = SourceDataDao.loadNewDailyQuote(dailyQuoteAddr) # indexQuote = SourceDataDao.loadH5(indexQuoteAddr) #StockConst.root + StockConst.fakeIndexQuoteH5 # signalData = SourceDataDao.read_file_set_index(signalDataAddr) signalData = read_func(signalDataAddr, table_name) dailyQuote = SourceDataDao.read_file_set_index(dailyQuoteAddr) indexQuote = SourceDataDao.read_file_set_index(indexQuoteAddr) if signalDataAddr is not None: maxSignalDate = SourceDataDao.get_max_date(signalDataAddr) minSignalDate = SourceDataDao.get_min_date(signalDataAddr) else: maxSignalDate = SignalDataMSSQLDao.select_max_data(table_name) minSignalDate = SignalDataMSSQLDao.select_min_data(table_name) # print('print') # print(maxSignalDate) # print(minSignalDate) maxDailyQuoteDate = SourceDataDao.get_max_date(dailyQuoteAddr) minDailyQuoteDate = SourceDataDao.get_min_date(dailyQuoteAddr) maxIndexQuoteDate = SourceDataDao.get_max_date(indexQuoteAddr) minIndexQuoteDate = SourceDataDao.get_min_date(indexQuoteAddr) # print('minSignalDate:' + minSignalDate) # print('maxSignalDate:' + maxSignalDate) # # print('minDailyQuoteDate:' + minDailyQuoteDate) # print('maxDailyQuoteDate:' + maxDailyQuoteDate) # # print('minIndexQuoteDate:' + minIndexQuoteDate) # print('maxIndexQuoteDate:' + maxIndexQuoteDate) # if minSignalDate < minDailyQuoteDate: startDate = minDailyQuoteDate else: startDate = minSignalDate if maxSignalDate > maxDailyQuoteDate: endDate = maxDailyQuoteDate else: endDate = maxSignalDate # if startDate < minIndexQuoteDate: startDate = minIndexQuoteDate else: pass if endDate > maxIndexQuoteDate: endDate = maxIndexQuoteDate else: pass print('startDate:'+startDate) print('endDate:' + endDate) #TODO 暂时不需要 # dailyQuotePn = SourceDataDao.loadNewDailyQuoteToPn(dailyQuoteAddr) dailyQuotePn = None #参数 #初始资金 initialCash = 1000000 #1000000 #选股结果(外部传入) #select top 5 group by TradingDay order by Mom desc allSelectStock = select_stock_func(signalData,techList,sliceDict) print('allSelectStock:'+str(len(allSelectStock))) # print(allSelectStock) if len(allSelectStock) == 0: return #当前持仓 currHoldSet = {} #资金情况 capitalEntity = CapitalEntity.CapitalEntity(initialCash, 0, initialCash) lastCapitalEntity = copy.copy(capitalEntity) #初始化每日统计表 stockStatDailyDf = pd.DataFrame(index=pd.date_range(startDate, endDate), columns=['netValue','changePCT','buyCnt','sellCnt','prevHoldCnt','currHoldCnt','cannotSellCnt','cannotBuyCnt', 'usableCash','totalAsset','indexChangePCT','relativeChangePCT','relativeNetValue']) #初始化每日持仓表 stockHoldDailyList = [] stockTradeList = [] #从信号表中取得唯一性日期 dateList = SourceDataDao.select_date_from_signal(dailyQuote, startDate, endDate) #signalData dailyQuote # print(dateList) # lastTradingDay = None # lastTradingDayStr = None lastRelativeNetValue = 1 #昨日相对净值 start_loop = time.clock() for index, item in enumerate(dateList): #for date in dateList: #print('index:'+str(index)+' len:'+str(len(dateList))) # start_inloop0 = time.clock() #信号日 signalDay = dateList[index] # 非最后一天 if index < (len(dateList) - 1): #交易日 tradingDay = dateList[index + 1] tradingDayStr = DateUtil.datetime2_str(tradingDay) # tradingDayTsp = pd.Timestamp(tradingDayStr) # print(tradingDay) # print(tradingDayStr) # print(tradingDayTsp) # print(type(tradingDay)) # print(type(tradingDayStr)) # print(type(tradingDayTsp)) #print('dateStr:'+dateStr) # 最后一天 else: break # dailyQuote1day = dailyQuote[dailyQuote.index.get_level_values(0) == tradingDay] #TODO # start_slice = time.clock() # print(str(len(dailyQuote))) # print(type(tradingDay)) #交易日行情 dailyQuote1day = dailyQuote.xs(tradingDay) # print('dailyQuote1day:'+str(len(dailyQuote1day))) # end_slice = time.clock() # print("切片花费时间:%f s" % (end_slice - start_slice)) # dailyQuote1day = None #print(dateStr) #信号日选股 currSelectStock = allSelectStock.xs(signalDay) #.xs(signalDay) # print('currSelectStock:'+str(len(currSelectStock))) # if StockConst.isDebug: # print('tradingDayStr:'+tradingDayStr+' currSignalData:'+str(len(currSignalData))) #print(currSignalData) # end_inloop0 = time.clock() # print("inloop0花费时间:%f s" % (end_inloop0 - start_inloop0)) #交易方法(外部传入) # start_inloop1 = time.clock() dict = trade_func(currSelectStock, currHoldSet, dailyQuote, tradingDay, signalData, capitalEntity, dailyQuotePn, dailyQuote1day, lastCapitalEntity) # end_inloop1 = time.clock() # print("trade_func花费时间:%f s" % (end_inloop1 - start_inloop1)) actualBuyList = dict['actualBuyList'] actualSellList = dict['actualSellList'] prevHoldList = dict['prevHoldList'] cannotBuyList = dict['cannotBuyList'] cannotSellList = dict['cannotSellList'] stockTradeListDaily = dict['stockTradeList'] if len(stockTradeListDaily) > 0 : stockTradeList.extend(stockTradeListDaily) # print(currHoldSet) #3.计算当日市值 # start_inloop2 = time.clock() calculate_daily_mv(currHoldSet, capitalEntity, dailyQuote, tradingDay, stockHoldDailyList, initialCash) totalAsset = capitalEntity.get_total_asset() # end_inloop2 = time.clock() # print("calculateDailyMV花费时间:%f s" % (end_inloop2 - start_inloop2)) # start_inloop3 = time.clock() #4.个数 buyCnt = len(actualBuyList) sellCnt = len(actualSellList) prevHoldCnt = len(prevHoldList) currHoldCnt = len(currHoldSet) cannotSellCnt = len(cannotSellList) cannotBuyCnt = len(cannotBuyList) # print(currMV) #if StockConst.isDebug: #print("dateStr:" + dateStr + " changePCTBuy:" + str(changePCTBuy) + " changePCTSell:" + str(changePCTSell) + #" changePCTHold:" + str(changePCTHold)) #当日净值 netValue = totalAsset / initialCash #print("netValue:" + str(netValue)) #当日收益(%) dailyChangePCT = NumUtil.get_change_pct(lastCapitalEntity.get_total_asset(), totalAsset, 5) #当日可用现金 usableCash = capitalEntity.get_usable_cash() # 当日的指数 indexQuote1day = None indexChangePCT = 0 if indexQuote is not None: indexQuote1day = indexQuote.ix[(tradingDay, benchmark)] indexChangePCT = indexQuote1day['ChangePCT'] #相对收益=每日收益-指数收益 (%) relativeChangePCT = dailyChangePCT - indexChangePCT #相对净值=昨日相对净值*(1+相对收益/100) relativeNetValue = lastRelativeNetValue*(1+relativeChangePCT/100) #统计表 stockStatDailyDf.ix[tradingDayStr] = netValue,dailyChangePCT,buyCnt,sellCnt,prevHoldCnt,currHoldCnt,cannotSellCnt,cannotBuyCnt,usableCash,totalAsset,indexChangePCT,relativeChangePCT,relativeNetValue #昨日资产 lastCapitalEntity = copy.copy(capitalEntity) #昨日相对净值 lastRelativeNetValue = relativeNetValue # lastTradingDay = tradingDay # lastTradingDayStr = tradingDayStr # end_inloop3 = time.clock() # print("inloop3花费时间:%f s" % (end_inloop3 - start_inloop3)) end_loop = time.clock() print("for循环花费时间:%f s" % (end_loop - start_loop)) result = '' start_stat = time.clock() # 信号数据 #allSelectStock.to_csv(StockConst.root + '\export\allSelectStock.csv') # # 每日交易 # # print('每日交易:') # # print(stockTradeList) # stockTradeDailyDf = pd.DataFrame(stockTradeList) # stockTradeDailyDf.sort_values(by=['tradingDay'], ascending=True) # # stockTradeDailyDf.to_csv(StockConst.root + '\export\stockTradeDaily.csv') # # 每日持仓 # #print('每日持仓:') # stockHoldDailyDf = pd.DataFrame(stockHoldDailyList) # stockHoldDailyDf.sort_values(by=['tradingDay'],ascending=True) # # stockHoldDailyDf.to_csv(StockConst.root + '\export\stockHoldDaily.csv') start_stat_1 = time.clock() #每日统计(收益,净值,买入数,卖出数,持有数) stockStatDailyDf = stockStatDailyDf.dropna(how='all') result += '--' * 70 + '\n' result += '每日统计:' + '\n' result += stockStatDailyDf.to_string() + '\n' stockStatDailyDf.to_csv(StockConst.ROOT + '\export\stockStatDaily.csv') end_stat_1 = time.clock() print("统计1花费时间:%f s" % (end_stat_1 - start_stat_1)) start_stat_2 = time.clock() # 每年统计 result += '--' * 70 + '\n' yearDf = StockYearService.main(stockStatDailyDf) result += '每年统计:' + '\n' result += yearDf.to_string() + '\n' end_stat_2 = time.clock() print("统计2花费时间:%f s" % (end_stat_2 - start_stat_2)) start_stat_3 = time.clock() # 最大回撤 result += '--' * 70 + '\n' maxdrop = StockMaxDropNewService.get_max_drop(stockStatDailyDf) result += '最大回撤:' + '\n' result += SetUtil.dict_to_string(maxdrop) + '\n' # #每年的最大回撤 # maxdropDf = StockMaxDropNewService.getMaxDropForEachYear(stockStatDailyDf) # maxdropDf.sort_values(by=["year"]) # result += '每年的最大回撤:' + '\n' # result += maxdropDf.to_string() + '\n' end_stat_3 = time.clock() print("统计3花费时间:%f s" % (end_stat_3 - start_stat_3)) start_stat_4 = time.clock() #夏普比率 result += '--' * 70 + '\n' sharpRatio = SharpRatioNewService.get_sharp_ratio(stockStatDailyDf) result += '夏普比率:' + '\n' result += str(sharpRatio) + '\n' #每年的夏普比率 sharpRatioDf = SharpRatioNewService.get_sharp_ratio_for_each_year(stockStatDailyDf) sharpRatioDf.sort_values(by=["year"]) result += '每年的夏普比率:' + '\n' result += sharpRatioDf.to_string() + '\n' end_stat_4 = time.clock() print("统计4花费时间:%f s" % (end_stat_4 - start_stat_4)) end_stat = time.clock() print("统计花费时间:%f s" % (end_stat - start_stat)) # result += '回测完成' end = time.clock() print("main花费时间:%f s" % (end - start)) if doPlot: # # # 净值曲线图 # stockStatDailyDf['netValue'].plot() # plt.show() # # # # 相对净值曲线图 # stockStatDailyDf['relativeNetValue'].plot() # plt.show() pass # resultDic={ 'stockStatDailyDf': stockStatDailyDf, 'result': result, 'sharpRatio': sharpRatio } print(result) return resultDic