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)
import matplotlib.dates as mdates from scipy.spatial import ConvexHull, convex_hull_plot_2d import talib import importlib import rschLib np.set_printoptions(formatter={'float_kind': "{:.6f}".format}) dbt = rschLib.db_tinySoftData() dtes, tkrs, name, open_mtx, high_mtx, low_mtx, close_mtx, belong, shenwan1, shenwan2, shenwan3, vol_mtx, amount_mtx = rschLib.loadDailyBarMtx( ) # get time labels timeAsFloat, timeLabels, maxM, dayOff, dayTimeAsFloat = rschLib.getTimeLabels( maxD) # get trades 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] r = r_withnan.copy() r[np.isfinite(r) == False] = 0 # draw price change idxTradable = np.isfinite(r_withnan[:, tradeArea[0]]) result = rschLib.getTradeAnalysisSampleGroups(r, idxTradable, tradeArea) rschLib.drawPriceChange(r[idxTradable, :], strategy_name, timeLabels=timeLabels, tp=tradeArea) rschLib.drawPriceChange(result['rGood10'], strategy_name,
client = pymongo.MongoClient('localhost', 27017) db = client.quanLiang dbt = client.tinySoftData dtes, tkrs, name, open_mtx, high_mtx, low_mtx, close_mtx,belong, shenwan1, shenwan2, shenwan3, vol_mtx, amount_mtx = rschLib.loadDailyBarMtx() # In[304]: maxD = 5 inTime = 234 otTime = 474 tradeArea=[inTime,otTime] timeAsFloat, timeLabels, maxM, dayOff, dayTimeAsFloat = rschLib.getTimeLabels(maxD) importlib.reload(rschLib) tradesUsed, Po, r, Sale1 = rschLib.getTradesFast(strategy_name, name, tkrs, dtes, maxD, dayTimeAsFloat) # In[124]: R = open_mtx[:, 1:]/close_mtx[:,:-1]-1 R = np.hstack((np.zeros((R.shape[0],1)), R)) # In[210]: dicttkrs = dict(zip(tkrs, range(len(tkrs)))) dictdtes = dict(zip(dtes, range(len(dtes))))