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/

    An indicator that knows if I have a position or not

    Indicators/Strategies/Analyzers
    2
    4
    366
    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.
    • Kristofer Monisit
      Kristofer Monisit last edited by

      I am building an indicator that does one thing if I'm not in the trade, and does another if I'm in.

      Is it possible for a subclass of Indicators to access the position attribute of the Strategy?

      Of course, this indicator cannot be pre-computed, but is emits data as the backtest progresses.

      The following code surely raises AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Indicat' object has no attribute 'position'

      class CustomIndicator(bt.Indicator):
          lines = ('custom_indicator',)
          params = (('period', 14), )
          
          def next(self):
              if self.position:
                  self.lines.custom_indicator[0] = True and is_something_else_true()
                  self.do_something()
              else:
                  self.lines.custom_indicator[0] = False
                  self.do_nothing()
      
      1 Reply Last reply Reply Quote 0
      • M
        momentum last edited by

        The strategy can turn on/off a parameter of the indicator according to whether in trade or not. Have a look at the dynamic indicator example
        https://www.backtrader.com/blog/posts/2018-02-06-dynamic-indicator/dynamic-indicator.html

        Kristofer Monisit 1 Reply Last reply Reply Quote 1
        • Kristofer Monisit
          Kristofer Monisit @momentum last edited by

          @momentum Thanks! I would add that Cerebro will have to be instantiated with Cerebro(runonce=False). Otherwise, if I use the default, i.e. runonce=True, all indicators would be precalculated all the way to the end, and they would not be able to observe if a trade is open or not.

          Reference for the runonce argument:
          https://www.backtrader.com/docu/cerebro.html?highlight=vectorize#backtrader.Cerebro

          1 Reply Last reply Reply Quote 0
          • M
            momentum last edited by

            I even like better the _nextforce = True pointed out to me by @backtrader some time ago.

            class CustomIndicator(bt.Indicator):
                _nextforce = True
            ...
            
            1 Reply Last reply Reply Quote 1
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
            $(document).ready(function () { app.coldLoad(); }); }