For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Change backtrader.Strategy params depending on asset?

  • I just started playing with bt and have been struggling with this all afternoon.

    So my starting point is this post here.

    It works out of the box. So far so good.

    Now I try to change things a bit. My objective is to allow the indicator values
    to be different for the different assets. I figured one simple way of doing that would be to flip sma1 and sma2 in the code above for the 'cross' indicator some of the assets.

    import backtrader 
    class maCross(backtrader.Strategy):
        For an official backtrader blog on this topic please take a look at:
        oneplot = Force all datas to plot on the same master.
        params = (
        ('sma1', 80),
        ('sma2', 200),
        ('oneplot', True),
        ('list_plus', []),
        ('list_minus', [])
        def __init__(self):
            Create an dictionary of indicators so that we can dynamically add the
            indicators to the strategy using a loop. This mean the strategy will
            work with any number of data feeds. 
            self.inds = dict()
            for i, d in enumerate(self.datas):
                self.inds[d] = dict()
                self.inds[d]['sma1'] = backtrader.indicators.SMA(d.close, 
                self.inds[d]['sma2'] = backtrader.indicators.SMA(d.close, 
                if d._name in self.params.list_plus:
                    self.inds[d]['cross'] = backtrader.indicators.CrossOver(self.inds[d]['sma1'],
                if d._name in self.params.list_minus:
                    self.inds[d]['cross'] = backtrader.indicators.CrossOver(self.inds[d]['sma2'],
                if i > 0: #Check we are not on the first loop of data feed:
                    if self.p.oneplot == True:
                        d.plotinfo.plotmaster = self.datas[0]
        def next(self):
            for i, d in enumerate(self.datas):
                dt, dn =, d._name
                pos = self.getposition(d).size
                if not pos:  # no market / no orders
                    if self.inds[d]['cross'][0]:
              , size=1000)
                    elif self.inds[d]['cross'][0]:
                        self.sell(data=d, size=1000)
                    if self.inds[d]['cross'][0]:
              , size=1000)
                    elif self.inds[d]['cross'][0]:
                        self.sell(data=d, size=1000)
        def notify_trade(self, trade):
            dt =
            if trade.isclosed:
                print('{} {} Closed: PnL Gross {}, Net {}'.format(

    I set the parameters:

    cerebro.addstrategy(maCross, list_plus = ['CADCHF', 'EURUSD'], list_minus = ['GBPAUD'], oneplot=False)

    Now when I run the strategy, I get the print outs I would expect on the screen:


    So far so good ( runs without a warning/error).

    But is seems no trades are executed:

    The PnL is the initial one and the triangles on the plot are no longer there.

    I presume a rookie problem (I'm a rookie). Is it one that can be easily fixed?

  • @kav
    Everything is fixed. I had introduced a mistake in the next() (visible above). Please ignore my post!

Log in to reply