def test_ordered_optimize(cerebro: bt.Cerebro): cerebro.optstrategy(bt.strategies.MA_CrossOver, slow=[20], fast=[5, 10, 20]) res = cerebro.run(optreturn=True) def df(optresults): a = [x.analyzers.tradeanalyzer.get_analysis() for x in optresults] return sum([x.pnl.gross.total if 'pnl' in x else 0 for x in a]) usercolumns = { 'Profit & Loss': df, } b = Bokeh(style='bar', output_mode=_output_mode) browser = OptBrowser(b, res, usercolumns=usercolumns, sortcolumn='Profit & Loss') model = browser.build_optresult_model() # browser.start() def count_children(obj): numo = 1 if hasattr(obj, "children"): numo = count_children(obj.children) if hasattr(obj, '__len__'): numo += len(obj) return numo num = count_children(model) assert num == 3
def test_optimize_no_optreturn(cerebro_no_optreturn: bt.Cerebro): cerebro_no_optreturn.optstrategy(bt.strategies.MA_CrossOver, slow=[5, 10, 20], fast=[5, 10, 20]) res = cerebro_no_optreturn.run() s = backtrader_plotting.schemes.Blackly() b = Bokeh(style='bar', output_mode=_output_mode, scheme=s) browser = OptBrowser(b, res) model = browser.build_optresult_model() #browser.start() def count_children(obj): numo = 1 if hasattr(obj, "children"): numo = count_children(obj.children) if hasattr(obj, '__len__'): numo += len(obj) return numo num = count_children(model) assert num == 3
def test_optimize_2strat(cerebro: bt.Cerebro): cerebro.optstrategy(bt.strategies.MA_CrossOver, slow=[5, 10, 20], fast=[5, 10, 20]) cerebro.optstrategy(ToggleStrategy, modbuy=[12, 15], modsell=[17, 19]) res = cerebro.run() b = Bokeh(style='bar', output_mode=_output_mode) browser = OptBrowser(b, res) with pytest.raises(RuntimeError): browser.build_optresult_model()
def test_optimize(cerebro: bt.Cerebro): cerebro.optstrategy(bt.strategies.MA_CrossOver, slow=[5, 10, 20], fast=[5, 10, 20]) res = cerebro.run(optreturn=True) b = Bokeh(style='bar', output_mode=_output_mode) browser = OptBrowser(b, res) model = browser.build_optresult_model() # browser.start() def count_children(obj): numo = 1 if hasattr(obj, "children"): numo = count_children(obj.children) if hasattr(obj, '__len__'): numo += len(obj) return numo num = count_children(model) assert num == 3
if __name__ == '__main__': cerebro = bt.Cerebro(maxcpus=1) data = bt.feeds.YahooFinanceCSVData( dataname="datas/orcl-1995-2014.txt", fromdate=datetime.datetime(2000, 1, 1), todate=datetime.datetime(2001, 2, 28), reverse=False, swapcloses=True, ) cerebro.adddata(data) cerebro.addanalyzer(bt.analyzers.TradeAnalyzer) cerebro.optstrategy(MyStrategy, buydate=range(40, 180, 30)) result = cerebro.run(optreturn=False) def get_pnl_gross(strats): a = strats[0].analyzers.tradeanalyzer.get_analysis() return a.pnl.gross.total if 'pnl' in a else 0 b = Bokeh(style='bar', scheme=Tradimo()) browser = OptBrowser(b, result, usercolumns=dict(pnl=get_pnl_gross), sortcolumn='pnl', sortasc=False) browser.start()
def next(self): pos = len(self.data) if pos == self.p.buydate: self.buy(self.datas[0], size=None) if pos == self.p.buydate + self.p.holdtime: self.sell(self.datas[0], size=None) if __name__ == '__main__': cerebro = bt.Cerebro(maxcpus=1) data = bt.feeds.YahooFinanceCSVData( dataname="datas/orcl-1995-2014.txt", fromdate=datetime.datetime(2000, 1, 1), todate=datetime.datetime(2001, 2, 28), reverse=False, swapcloses=True, ) cerebro.adddata(data) cerebro.addanalyzer(bt.analyzers.TradeAnalyzer) cerebro.optstrategy(MyStrategy, buydate=range(40, 180, 30)) result = cerebro.run(optreturn=True) b = Bokeh(style='bar', scheme=Tradimo()) browser = OptBrowser(b, result) browser.start()
self.sell(self.datas[0], size=None) if __name__ == '__main__': cerebro = bt.Cerebro(maxcpus=1) data = bt.feeds.YahooFinanceCSVData( dataname="datas/orcl-1995-2014.txt", fromdate=datetime.datetime(2000, 1, 1), todate=datetime.datetime(2001, 2, 28), reverse=False, swapcloses=True, ) cerebro.adddata(data) cerebro.addanalyzer(bt.analyzers.TradeAnalyzer) cerebro.optstrategy(MyStrategy, buydate=range(40, 180, 30)) optres = cerebro.run(optreturn=False) def df(optresults): a = [x.analyzers.tradeanalyzer.get_analysis() for x in optresults] return sum([x.pnl.gross.total if 'pnl' in x else 0 for x in a]) usercolumns = {'Profit & Loss': df} b = Bokeh(style='bar', scheme=Tradimo()) browser = OptBrowser(b, optres, usercolumns=usercolumns, sortcolumn='Profit & Loss', sortasc=False) browser.start()