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

Obtain 'close' data before SMA kicks in

  • I have defined two SMA's in my init like so:

    def log(self, txt, dt=None):
        ''' Logging function for this strategy'''
        dt = dt or self.datas[0]
        print('%s, %s' % (dt.isoformat(), txt))
    def __init__(self):
        self.dataclose = self.datas[0].close
        self.sma1 = bt.ind.SMA(self.datas[0], period = 8)
        self.sma2 = bt.ind.SMA(self.datas[0], period = 21)
        self.trade_hist = []

    as well as an empty list 'self.trade_hist' in order to accumulate all close prices at each 'next'.

    I append the close prices to this list in the 'next' method, like so:


    When I log 'self.trade_hist' in the 'next' method, the list only starts filling up with the close prices starting from the 21st data point (i.e. after the slowest SMA kicks in). This is the output:

    2017-01-21, [925.06]
    2017-01-22, [925.06, 931.22]
    2017-01-23, [925.06, 931.22, 910.7]
    2017-01-24, [925.06, 931.22, 910.7, 887.0]
    2017-01-25, [925.06, 931.22, 910.7, 887.0, 896.0]

    and so on... I want to append the close prices starting from the first data point (2017-01-01), before the SMA's kick in.


  • Use strategy prenext(), it is called from the very beginning of the data feed.

  • Thanks a lot

Log in to reply