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/

    strategy next() out of alginment

    General Discussion
    2
    2
    90
    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.
    • A
      adamb032 last edited by

      I am noticing something a little weird, and not exactly sure whats happening. I have a simple SMA strategy with a 5/20 cross, just like in the example in my init function:

      self.sma_fast = self.p._movav(period=self.p.fast)
      self.sma_slow = self.p._movav(period=self.p.slow)
      

      in my next() function:

      self.logger.info(f'current pos: {self.position.size}')
      self.logger.info(f'{num2date(self.data.datetime[0])} \
                        close: {self.data.close[0]}')
      self.logger.info(f'current bar: {len(self)}')
      
      #so the indicators 
      self.logger.info(self.sma_fast.lines[0].array[len(self)])
      self.logger.info(self.sma_slow.lines[0].array[len(self)])
      

      i verified with my data, that the date, and close price are accurate for the given bar, however the indicators produce the average(5/20) with the next bar's closing price

      should this be the case? i would expect that they include up to the current bar. or am i misunderstanding how this works? thanks-

      vladisld 1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld @adamb032 last edited by

        @adamb032 The next() method runs after a candle has been closed when all final ohlc data for that candle is available.

        Does it answer your question?

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