#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]
# draw pnl and tag pnl importlib.reload(rschLib) [dtesByTrade, pnlByTrade] = rschLib.getPnlFast(r, 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,
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)
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)
# analyze tags rschLib.analyzeTradeTags(tradesUsed, result['rGood10'], result['idxGood10'], '盈利前10%交易',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['rBad30'], result['idxBad30'], '亏损前30%交易',strategy_name, dtes, tkrs, offStart) rschLib.analyzeTradeTags(tradesUsed, result['rDieting'], result['idxDieting'], '亏损前30%交易',strategy_name, dtes, tkrs, offStart) # get tag names tnames, t1,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.getPnlFast(r, 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) # 求非涨停,以及伴随有tagName标签的交易,绘制价格变化曲线 idxTradableAndHasTag = list(np.intersect1d(np.nonzero(idxTradable)[0], np.array(idxOverLapTagList[i]))) rschLib.drawPriceChange(r[idxTradableAndHasTag,:], strategy_name, timeLabels=timeLabels, title=tagName, tp=tradeArea) # control group and optimal group [rawInTime, rawOtTime] = rschLib.getDefaultTradeTime(offStart) idxTradableRaw = np.isfinite(r_withnan[:, rawInTime]) [dtesByTradeRaw, pnlByTradeRaw] = rschLib.getPnlFast(r, idxTradableRaw, dtes, tkrs, name, tradesUsed, rawInTime, rawOtTime, dayOff, timeAsFloat, toDatabase='yes', strategy_name=strategy_name) [dtesPnlAggrRaw, pnlAggrRaw, numTradesRaw] = rschLib.aggregatePnlAndDtes(dtesByTradeRaw, pnlByTradeRaw)