Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Obtain 'close' data before SMA kicks in

    General Code/Help
    2
    3
    503
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      maxpaton last edited by

      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].datetime.date(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:

      self.trade_hist.append(self.dataclose[0])
      

      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.

      Cheers

      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

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

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 1
        • M
          maxpaton last edited by

          Thanks a lot

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors