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/

    Is backtrader predefined indicator stateless?

    Indicators/Strategies/Analyzers
    2
    5
    149
    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.
    • Yacc Don
      Yacc Don last edited by

      Hi,
      I'm trying to implement some kind of strategy, which pick stock base on some criteria at some time, then trade those stock.

      So the data feed is dynamically changing.

      I run cerebro with preload=True, so it will always use new data.
      But it require the indicator to be stateless, which all the line caculate with new data feed, and don't keep any state from old data feed.

      So I'm wondering if Is backtrader predefined indicator (for example SMA, crossover) stateless. And if it's not stateless, is there any general way to reset the indicator to initial state?

      @backtrader @ab_trader

      Thanks

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

        @yacc-don I am not sure what is stateless indicator and what is always new data with the preload=True, but bt works the following way (unless you hacked it somehow):

        • all data feeds loaded once at the very beginning of the backtest, after cerebro initiated
        • indicators are initiated/calculated in the __init()__ in pseudo-batch mode (if possible)
        • cerebro goes bar by bar and calculates rest of the indicators indicators and perform strategy rules

        IIRC preload=True helps to run batch mode and speed up the backtest.

        @yacc-don said in Is backtrader predefined indicator stateless?:

        So the data feed is dynamically changing.

        If you plan to load and un-load data feeds during backtest, than bt is not doing this. See my clarification above.

        • 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
        • Yacc Don
          Yacc Don last edited by

          sorry, my mistake.

          I run cerebro with preload=False, then I change the content of data feed at some time.

          As for stateless and stateful, here is example.

          class stateful_crossover(bt.Indicator):
          
             def next(self):
                   if self.data0 < self.data1:
                          if self.state == OVER:  #check state caculate by old data
                                 self.line.crossover = -1
                          self.state = UNDER     #keep state
                    elif self.data0 > self.data1:
                          if self.state == UNDER:
                                 self.line.crossover = 1
                          self.state = OVER        #keep state
          
          class stateless_crossover(bt.Indicator):
             def next(self):
                   #always caculate result from new data, not keeping any state
                   if self.data0 < self.data1 and self.data0[-1] > self.data1[-1]: 
                          self.line.crossover = -1
                   elif self.data0 > self.data1 and self.data0[-1] <self.data1[-1]:
                          self.line.crossover = 1
          

          So I'm wondering if Is backtrader predefined indicator (for example crossover etc) stateless. And if it's not stateless, is there any general way to reset the indicator to initial state?

          Thanks

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

            @yacc-don

            My guess would be that you are trying to differentiate recursive and non-recursive indicators. Both of these types are in bt indicator database and they are mainly defined by the nature of the required calculations. I believe the best way for you would be to look into the source code on the github, and define by yourself, if the indicators are good for you or they need to be re-written.

            Honestly, I don't see any reasons of why any of the examples stated above (first need to have self.state variable to be defined at the beginning) will not work with bt.

            • 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
            Yacc Don 1 Reply Last reply Reply Quote 2
            • Yacc Don
              Yacc Don @ab_trader last edited by

              @ab_trader Thanks

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