def generatePAndLPlot(self): plotter = PAndLPlotter(self.settings) datastore = datastorefactory.get_datastore() account = Account(self.settings) positionSizer = PercentOfEquityPositionSizer(self.settings) outfile = "equitycurve.csv" plotter.plot(datastore, account, positionSizer, self.trades, outfile) # TODO try/finally datastore.close()
def getStatsNew(self): datastore = PreloadedProxyDataStore(datastorefactory.get_datastore()) try: maxValidRStr = self.settings.get("TradeManager", "maxValidR") except: maxValidRStr = "None" if maxValidRStr == "None": maxValidR = None else: maxValidR = float(maxValidRStr) numtrades = float(len(self.trades)) numwins = 0.0 totalpandl = 0.0 totalabspandl = 0.0 totalwin = 0.0 totalloss = 0.0 timeintrade=0 timeinwin=0 timeinloss=0 rs = list() drawdownsizes = dict() drawdownsizes["5%"] = None drawdownsizes["10%"] = None drawdownsizes["25%"] = None drawdownsizes["25%"] = None drawdownsizes["50%"] = None drawdownsizes["75%"] = None drawdownsizes["90%"] = None drawdownsizes["optimal f"] = None leverage_stats = list() self.trades = sorted(self.trades, key=lambda trade: trade.getEntryDate()) best_final_equity = 0 for risk in (0.005, 0.01, 0.015, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, \ 0.12, 0.14, 0.15, 0.16, 0.18, 0.2, 0.25, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, \ 1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 4.0,): trades = copy.deepcopy(self.trades) account = Account(self.settings) max_equity = account.cash posSizer = PercentOfEquityPositionSizer(self.settings, risk, 4.0, 10) pandlPlotter = PAndLPlotter(self.settings) equityCurve = pandlPlotter.generate_equity_curve(datastore, account, \ posSizer, trades) max_drawdown = 0 final_equity = 0 for data in equityCurve: equity = data["equity"] if equity < max_equity: drawdown = (max_equity-equity)/max_equity if drawdown > max_drawdown: max_drawdown = drawdown if equity > max_equity: max_equity = equity final_equity = equity leverage_stat = dict() leverage_stat["risk"] = risk leverage_stat["drawdown"] = max_drawdown leverage_stat["final_equity"] = final_equity leverage_stats.append(leverage_stat) keys = list() if final_equity > best_final_equity: keys.append("optimal f") best_final_equity = equity if max_drawdown < .9: keys.append("90%") if max_drawdown < .75: keys.append("75%") if max_drawdown < .5: keys.append("50%") if max_drawdown < 0.25: keys.append("25%") if max_drawdown < 0.1: keys.append("10%") if max_drawdown < 0.05: keys.append("5%") for key in keys: if drawdownsizes[key] == None or drawdownsizes[key][1] < final_equity: drawdownsizes[key] = (risk, final_equity) for trade in self.trades: pandl = trade.pandlR() if pandl == 0: pandl = 0 rs.append(0) abspandl = 0 else: # disregard outrageous R values, per max valid R setting if maxValidR == None or pandl <= maxValidR: rs.append(pandl) abspandl = trade.pandlPercent() # disregard outrageous R values, per max valid R setting if maxValidR == None or pandl <= maxValidR: totalpandl = totalpandl + pandl totalabspandl = totalabspandl + abspandl if pandl > 0: numwins = numwins + 1.0 totalwin = totalwin + pandl timeinwin = timeinwin + (trade.time_in_trade()/60) else: totalloss = totalloss - pandl timeinloss = timeinloss + (trade.time_in_trade()/60) timeintrade = timeintrade + (trade.time_in_trade()/60) if numtrades > 0: averageWin=0 averageLoss=0 averageDuration=0 averageWinDuration=0 averageLossDuration=0 if numwins > 0: averageWin = (totalwin/numwins) averageWinDuration = timeinwin/(60.0*24.0*float(numwins)) if numwins<numtrades: averageLoss = (totalloss/(numtrades-numwins)) averageLossDuration = timeinloss/(60.0*24.0*float(numtrades-numwins)) if numtrades > 0: averageDuration = timeintrade/(60.0*24.0*float(numtrades)) rdev = numpy.std(rs) # TODO move this somewhere sensible self.generatePAndLPlot() self.generateTradesCSV() return Stats(numtrades,(totalabspandl/numtrades), (totalpandl/numtrades), (numwins/numtrades), \ averageWin = averageWin, averageLoss = averageLoss, averageDuration=averageDuration, \ averageWinDuration=averageWinDuration, averageLossDuration=averageLossDuration, rdev=rdev, \ drawdownsizes = drawdownsizes, leverage_stats = leverage_stats) else: return Stats(0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 0.0)