# In[45]: plt.plot(tradesCount) # In[46]: plt.plot(dtes2Label(dtesUsed), np.cumsum(pnl1), 'k') plt.plot(dtes2Label(dtesUsed), np.cumsum(pnl2), 'b') plt.plot(dtes2Label(dtesUsed), np.cumsum(pnl3), 'r') plt.plot(dtes2Label(dtesUsed), np.cumsum(pnl4), 'c') plt.plot(dtes2Label(dtesUsed), np.cumsum(pnl5), 'm') plt.legend([ '第一天入场到第一天收盘', '第一天收盘到第二天开盘', '第二天开盘到第二天收盘', '第二天收盘到第三天开盘', '第三天开盘到第三天收盘' ]) plt.grid() # In[47]: rschLib.drawPNL(dtesUsed, pnl3 + pnl4 + pnl5, dtes, strategy_name, toDatabase='yes') # In[48]: rschLib.saveOffStart(strategy_name, off_start) # In[ ]:
#draw pnl and tag pnl importlib.reload(rschLib) [dtesByTrade, pnlByTrade] = rschLib.getPnlWithCache(dtes, tkrs, name, tradesUsed, inTime, otTime, dayOff, timeAsFloat, toDatabase='yes', strategy_name=strategy_name) [dtesPnlAggr, pnlAggr, numTrades] = rschLib.aggregatePnlAndDtes(dtesByTrade, pnlByTrade) rschLib.drawPNL(dtesPnlAggr, pnlAggr, dtes, strategy_name, toDatabase='yes') # In[41]: #importlib.reload(rschLib) #%load_ext line_profiler #%lprun -f rschLib.getPnl rschLib.getPnl(dtes,tkrs, name, tradesUsed, inTime, otTime, dayOff, timeAsFloat) # In[43]: #draw pnl and tag pnl tnames, tagNamesEn, t2 = rschLib.getTagNames() importlib.reload(rschLib) for i in range(len(tnames)): tagName = tnames[i] [dtesWithTag, pnlWithTag,
idxTradable, dtes, tkrs, name, tradesUsed, inTime, otTime, dayOff, timeAsFloat, toDatabase='yes', strategy_name=strategy_name) [dtesPnlAggr, pnlAggr, numTrades] = rschLib.aggregatePnlAndDtes(dtesByTrade, pnlByTrade) rschLib.drawPNL(dtesPnlAggr, pnlAggr, dtes, strategy_name, showFigure='yes', toDatabase='yes') for i in range(len(tnames)): tagName = tnames[i] [dtesWithTag, pnlWithTag, n] = rschLib.aggregatePnlAndDtes(dtesByTrade[idxOverLapTagList[i]], pnlByTrade[idxOverLapTagList[i]]) rschLib.drawPNL(dtesWithTag, pnlWithTag, dtes, strategy_name, showFigure='yes', toDatabase='yes', dateStart=dtesPnlAggr[0], pnlType=tagName)
def analyzeStrategy(strategy_name, offStart, dtes, name, tkrs): timeAsFloat, timeLabels, maxM, dayOff, dayTimeAsFloat = rschLib.getTimeLabels( maxD) R = open_mtx[:, 1:] / close_mtx[:, :-1] - 1 #使用收盘到开盘的回报率来修正分红和拆股 R = np.hstack((np.zeros((R.shape[0], 1)), R)) tradesUsed, r_withnan = rschLib.getTradesFast(strategy_name, name, tkrs, dtes, maxD, dayTimeAsFloat, R) # get trade samples by good/bad trades tradeArea = [inTime, otTime] idxTradable = np.isfinite(r_withnan[:, tradeArea[0]]) r = r_withnan.copy() r[np.isfinite(r) == False] = 0 result = rschLib.getTradeAnalysisSampleGroups(r, idxTradable, tradeArea) # draw price change rschLib.drawPriceChange(r[idxTradable, :], strategy_name, timeLabels=timeLabels, tp=tradeArea) rschLib.drawPriceChange(result['rGood10'], strategy_name, timeLabels=timeLabels, title='盈利前10%交易', tp=tradeArea) #rschLib.drawPriceChange(result['rGood20'], strategy_name, timeLabels=timeLabels, title='盈利前20%交易', tp=tradeArea) rschLib.drawPriceChange(result['rGood30'], strategy_name, timeLabels=timeLabels, title='盈利前30%交易', tp=tradeArea) rschLib.drawPriceChange(result['rBad10'], strategy_name, timeLabels=timeLabels, title='亏损前10%交易', tp=tradeArea) #rschLib.drawPriceChange(result['rBad20'], strategy_name, timeLabels=timeLabels, title='亏损前20%交易', tp=tradeArea) rschLib.drawPriceChange(result['rBad30'], strategy_name, timeLabels=timeLabels, title='亏损前30%交易', tp=tradeArea) # analyze tags #rschLib.analyzeTradeTags(tradesUsed, result['rGood10'], result['idxGood10'], '盈利前10%交易',strategy_name, dtes, tkrs, offStart) #rschLib.analyzeTradeTags(tradesUsed, result['rGood20'], result['idxGood20'], '盈利前20%交易',strategy_name, dtes, tkrs, offStart) #rschLib.analyzeTradeTags(tradesUsed, result['rGood30'], result['idxGood30'], '盈利前30%交易',strategy_name, dtes, tkrs, offStart) #rschLib.analyzeTradeTags(tradesUsed, result['rBad10'], result['idxBad10'], '亏损前10%交易',strategy_name, dtes, tkrs, offStart) #rschLib.analyzeTradeTags(tradesUsed, result['rBad20'], result['idxBad20'], '亏损前20%交易',strategy_name, dtes, tkrs, offStart) #rschLib.analyzeTradeTags(tradesUsed, result['rBad30'], result['idxBad30'], '亏损前30%交易',strategy_name, dtes, tkrs, offStart) #get tag names tnames, tagNamesEn, t2 = rschLib.getTagNames() idxOverLapTagList = rschLib.analyzeTradeTags(tradesUsed, r, list(range(len(tradesUsed))), '所有交易', strategy_name, dtes, tkrs, offStart, "d:\\pklWeeklyUpdate\\") #draw pnl and tag pnl importlib.reload(rschLib) [dtesByTrade, pnlByTrade] = rschLib.getPnlFast(r, dtes, tkrs, name, tradesUsed, inTime, otTime, dayOff, timeAsFloat, toDatabase='yes', strategy_name=strategy_name) [dtesPnlAggr, pnlAggr, numTrades] = rschLib.aggregatePnlAndDtes(dtesByTrade, pnlByTrade) rschLib.drawPNL(dtesPnlAggr, pnlAggr, dtes, strategy_name, showFigure='no', toDatabase='yes') for i in range(len(tnames)): tagName = tnames[i] [dtesWithTag, pnlWithTag, n] = rschLib.aggregatePnlAndDtes(dtesByTrade[idxOverLapTagList[i]], pnlByTrade[idxOverLapTagList[i]]) rschLib.drawPNL(dtesWithTag, pnlWithTag, dtes, strategy_name, showFigure='no', toDatabase='yes', dateStart=dtesPnlAggr[0], pnlType=tagName) rschLib.drawPNL(dtesWithTag, pnlWithTag, dtes, strategy_name + '+' + tagNamesEn[i], showFigure='no', toDatabase='yes', dateStart=dtesPnlAggr[0], pnlType='pnl') #analysis of number of trades vs performance importlib.reload(rschLib) rschLib.pnlVsNumtrades(pnlAggr, numTrades, strategy_name, toDatabase='yes') rschLib.saveOffStart(strategy_name, offStart)
rschLib.drawPriceChange(rBad2, strategy_name, timeLabels=timeLabels, title='亏损前20%交易', tp=tradeArea) rschLib.drawPriceChange(rBad3, strategy_name, timeLabels=timeLabels, title='亏损前30%交易', tp=tradeArea) # In[83]: importlib.reload(rschLib) [dtesPnl,pnl, numTrades]=rschLib.getPnl(dtes,tkrs, name, trades, inTime, otTime, dayOff, timeAsFloat, toDatabase='yes') # In[81]: importlib.reload(rschLib) rschLib.drawPNL(dtesPnl, pnl, dtes, strategy_name, toDatabase='yes') # In[57]: importlib.reload(rschLib) rschLib.pnlVsNumtrades(pnl, numTrades) # In[58]: # %load_ext line_profiler # #%lprun -f getPnl getPnl()
def analyzeStrategy(strategy_name, offStart, dtes, name, tkrs): timeAsFloat, timeLabels, maxM, dayOff = rschLib.getTimeLabels(maxD) trades, tradesUsed, Po, r = rschLib.getTradesWithPklCache( strategy_name, name, tkrs, dtes, maxD, maxM) # get trade samples by good/bad trades tradeArea = [inTime, otTime] result = rschLib.getTradeAnalysisSampleGroups(r, tradeArea) # draw price change rschLib.drawPriceChange(r, strategy_name, timeLabels=timeLabels, tp=tradeArea) rschLib.drawPriceChange(result['rGood10'], strategy_name, timeLabels=timeLabels, title='盈利前10%交易', tp=tradeArea) rschLib.drawPriceChange(result['rGood20'], strategy_name, timeLabels=timeLabels, title='盈利前20%交易', tp=tradeArea) rschLib.drawPriceChange(result['rGood30'], strategy_name, timeLabels=timeLabels, title='盈利前30%交易', tp=tradeArea) rschLib.drawPriceChange(result['rBad10'], strategy_name, timeLabels=timeLabels, title='亏损前10%交易', tp=tradeArea) rschLib.drawPriceChange(result['rBad20'], strategy_name, timeLabels=timeLabels, title='亏损前20%交易', tp=tradeArea) rschLib.drawPriceChange(result['rBad30'], strategy_name, timeLabels=timeLabels, title='亏损前30%交易', tp=tradeArea) # analyze tags #rschLib.analyzeTradeTags(tradesUsed, result['rGood10'], result['idxGood10'], '盈利前10%交易',strategy_name, dtes, tkrs, offStart) #rschLib.analyzeTradeTags(tradesUsed, result['rGood20'], result['idxGood20'], '盈利前20%交易',strategy_name, dtes, tkrs, offStart) #rschLib.analyzeTradeTags(tradesUsed, result['rGood30'], result['idxGood30'], '盈利前30%交易',strategy_name, dtes, tkrs, offStart) #rschLib.analyzeTradeTags(tradesUsed, result['rBad10'], result['idxBad10'], '亏损前10%交易',strategy_name, dtes, tkrs, offStart) #rschLib.analyzeTradeTags(tradesUsed, result['rBad20'], result['idxBad20'], '亏损前20%交易',strategy_name, dtes, tkrs, offStart) #rschLib.analyzeTradeTags(tradesUsed, result['rBad30'], result['idxBad30'], '亏损前30%交易',strategy_name, dtes, tkrs, offStart) #get tag names tnames, tagNamesEn, t2 = rschLib.getTagNames() idxOverLapTagList = rschLib.analyzeTradeTags(tradesUsed, r, list(range(len(tradesUsed))), '所有交易', strategy_name, dtes, tkrs, offStart) #draw pnl and tag pnl importlib.reload(rschLib) [dtesByTrade, pnlByTrade] = rschLib.getPnl(dtes, tkrs, name, tradesUsed, inTime, otTime, dayOff, timeAsFloat, toDatabase='yes', strategy_name=strategy_name) [dtesPnlAggr, pnlAggr, numTrades] = rschLib.aggregatePnlAndDtes(dtesByTrade, pnlByTrade) rschLib.drawPNL(dtesPnlAggr, pnlAggr, dtes, strategy_name, toDatabase='yes') for i in range(len(tnames)): tagName = tnames[i] [dtesWithTag, pnlWithTag, n] = rschLib.aggregatePnlAndDtes(dtesByTrade[idxOverLapTagList[i]], pnlByTrade[idxOverLapTagList[i]]) rschLib.drawPNL(dtesWithTag, pnlWithTag, dtes, strategy_name, toDatabase='yes', dateStart=dtesPnlAggr[0], pnlType=tagName) rschLib.drawPNL(dtesWithTag, pnlWithTag, dtes, strategy_name + '+' + tagNamesEn[i], toDatabase='yes', dateStart=dtesPnlAggr[0], pnlType='pnl') #analysis of number of trades vs performance importlib.reload(rschLib) rschLib.pnlVsNumtrades(pnlAggr, numTrades, strategy_name, toDatabase='yes') rschLib.saveOffStart(strategy_name, offStart)