def _run_test_cerebro(stratcls, expected_results):
    if not isinstance(expected_results, list):
        expected_results = [expected_results]

    c = bt.Cerebro()
    c.addstrategy(stratcls)
    data = bt.feeds.YahooFinanceCSVData(
        dataname=getdatadir("orcl-1995-2014.txt"),
        fromdate=datetime.datetime(2000, 1, 1),
        todate=datetime.datetime(2001, 1, 2),
        reverse=False,
    )
    c.adddata(data)

    data = bt.feeds.YahooFinanceCSVData(
        dataname=getdatadir("orcl-1995-2014.txt"),
        fromdate=datetime.datetime(2000, 1, 1),
        todate=datetime.datetime(2001, 1, 2),
        reverse=False,
    )
    c.adddata(data)

    c.run()

    strategy = c.runstrats[0][0]
    for idx, ind in enumerate(strategy.getindicators()):
        label = labelizer.label(ind)
        assert (label == expected_results[idx])
示例#2
0
def test_github_issue37_plotaspectratio():
    cerebro = bt.Cerebro()

    data = bt.feeds.YahooFinanceCSVData(
        dataname=getdatadir("orcl-1995-2014.txt"),
        fromdate=datetime.datetime(2000, 1, 1),
        todate=datetime.datetime(2001, 2, 28),
        reverse=False,
    )

    cerebro.adddata(data)
    cerebro.broker.setcash(10000)
    cerebro.coc = True
    cerebro.broker.setcommission(commission=0.00075)
    cerebro.addstrategy(BokehTest)
    cerebro.run()

    plotconfig = {
        'id:rsi': dict(plotaspectratio=10, ),
    }

    b = Bokeh(style='bar',
              plot_mode='single',
              scheme=Tradimo(),
              plotconfig=plotconfig,
              output_mode='memory')
    output = cerebro.plot(b)

    assert output[0][0].figures[1].bfigure.aspect_ratio == 10
def test_github_issue30():
    cerebro = bt.Cerebro()

    cerebro.addstrategy(MyStrategy)

    data = bt.feeds.YahooFinanceCSVData(
        dataname=getdatadir("orcl-1995-2014.txt"),
        fromdate=datetime.datetime(2000, 1, 1),
        todate=datetime.datetime(2001, 2, 28),
        reverse=False,
    )
    cerebro.adddata(data)
    cerebro.addanalyzer(bt.analyzers.SharpeRatio)

    cerebro.run()

    b = backtrader_plotting.Bokeh(filename='chart.html',
                                  style='bar',
                                  scheme=Tradimo(),
                                  output_mode='memory')

    figs = cerebro.plot(b)

    assert isinstance(figs[0][0], backtrader_plotting.bokeh.bokeh.FigurePage)
    assert len(figs[0][0].figures) == 4
    assert len(figs[0][0].analyzers) == 1
def test_github_issue44():
    cerebro = bt.Cerebro()
    data = bt.feeds.YahooFinanceCSVData(
        dataname=getdatadir("20170319-20200319-0388.HK.csv"),
        fromdata=datetime.datetime(
            2020,
            2,
            19,
            0,
            0,
            0,
        ),
        todata=datetime.datetime(2020, 3, 19, 0, 0, 0),
        reverse=False)

    cerebro.addobserver(bt.observers.Benchmark,
                        data=data,
                        timeframe=bt.TimeFrame.NoTimeFrame)
    cerebro.addstrategy(MyStrategy)
    cerebro.adddata(data)
    cerebro.addsizer(bt.sizers.AllInSizer)
    cerebro.broker.setcash(100000)

    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
    import json
    strats = cerebro.run()[0]
    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())

    bo = Bokeh(style='bar', plot_mode='single', output_mode='memory')
    cerebro.plot(bo)
def test_github_issue63():
    cerebro = bt.Cerebro()

    dataname = getdatadir("orcl-1995-2014.txt")  # data feed

    data0 = bt.feeds.BacktraderCSVData(dataname=dataname, name='data0')
    cerebro.adddata(data0)

    data1 = bt.feeds.BacktraderCSVData(dataname=dataname, name='data1')
    data1.addfilter(close_changer)
    data1.compensate(data0)
    data1.plotinfo.plotmaster = data0
    cerebro.adddata(data1)

    cerebro.addstrategy(St)  # sample strategy

    cerebro.addobserver(bt.obs.Broker)  # removed below with stdstats=False
    cerebro.addobserver(bt.obs.Trades)  # removed below with stdstats=False

    cerebro.broker.set_coc(True)
    cerebro.run(stdstats=False)  # execute

    bo = Bokeh(style='bar', plot_mode='single', output_mode='memory')
    fig_pages = cerebro.plot(bo, volume=False)

    assert len(fig_pages[0][0].figures) == 3
示例#6
0
def test_github_issue10():
    data = bt.feeds.YahooFinanceCSVData(
        dataname=getdatadir("orcl-1995-2014.txt"),
        fromdate=datetime.datetime(2000, 1, 1),
        todate=datetime.datetime(2001, 2, 28),
    )

    bokeh_plot(data)
示例#7
0
def cerebro_no_optreturn() -> bt.Cerebro:
    cerebro = bt.Cerebro(optreturn=False)

    datapath = getdatadir('orcl-1995-2014.txt')
    data = bt.feeds.YahooFinanceCSVData(
        dataname=datapath,
        fromdate=datetime.datetime(1998, 1, 1),
        todate=datetime.datetime(2000, 12, 31),
        reverse=False,
        swapcloses=True,
    )
    cerebro.adddata(data)

    cerebro.addanalyzer(bt.analyzers.TradeAnalyzer)
    cerebro.addanalyzer(bt.analyzers.SharpeRatio, compression=2)
    cerebro.addanalyzer(bt.analyzers.TimeDrawDown)

    return cerebro
def _run_cerebro(stratcls):
    cerebro = bt.Cerebro()

    cerebro.addstrategy(stratcls)

    datas = [
        'nvda-1999-2014.txt',
        'orcl-1995-2014.txt',
    ]

    for dname in datas:
        data = bt.feeds.YahooFinanceCSVData(
            dataname=getdatadir(dname),
            fromdate=datetime.datetime(2000, 1, 1),
            todate=datetime.datetime(2001, 2, 28),
            reverse=False,
        )
        cerebro.adddata(data)

    cerebro.addanalyzer(bt.analyzers.SharpeRatio)
    cerebro.run()
    b = backtrader_plotting.Bokeh(style='bar', scheme=Tradimo(), output_mode='memory')
    return cerebro.plot(b)
示例#9
0
def test_std_backtest_2datas(cerebro: bt.Cerebro):
    datapath = getdatadir('nvda-1999-2014.txt')
    data = bt.feeds.YahooFinanceCSVData(
        dataname=datapath,
        fromdate=datetime.datetime(1998, 1, 1),
        todate=datetime.datetime(2000, 12, 31),
        reverse=False,
        swapcloses=True,
    )
    cerebro.adddata(data)

    cerebro.addstrategy(bt.strategies.MA_CrossOver)
    cerebro.run()

    s = backtrader_plotting.schemes.Blackly()
    b = Bokeh(style='bar',
              scheme=s,
              output_mode=_output_mode,
              merge_data_hovers=True)
    figs = cerebro.plot(b)

    assert len(figs) == 1
    assert_num_tabs(figs, 3)
    assert_num_figures(figs, 5)
示例#10
0
                                                 subplot=True,
                                                 plotmaster=sma)

    def next(self):
        pos = len(self.data)
        if pos == 45 or pos == 145:
            self.buy(self.datas[0], size=None)

        if pos == 116 or pos == 215:
            self.sell(self.datas[0], size=None)


if __name__ == '__main__':
    cerebro = bt.Cerebro()
    data = bt.feeds.YahooFinanceCSVData(
        dataname=getdatadir("20170319-20200319-0388.HK.csv"),
        fromdata=datetime.datetime(
            2020,
            2,
            19,
            0,
            0,
            0,
        ),
        todata=datetime.datetime(2020, 3, 19, 0, 0, 0),
        reverse=False)

    cerebro.addobserver(bt.observers.Benchmark,
                        data=data,
                        timeframe=bt.TimeFrame.NoTimeFrame)
    cerebro.addstrategy(MyStrategy)