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/

    Use of indicator within a new indicator

    Indicators/Strategies/Analyzers
    2
    2
    125
    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.
    • G
      gomesneto98 last edited by

      I want to use Bollinger Bands as input for a new indicator, but the program apparently goes into a loop.

      New indicator:

      class Bbw(bt.Indicator):
      
         lines = ('bbwline',)
         params = (('period', 20),('devfactor',2.0),)
      
         def __init__(self):
      
             b_band = btind.BollingerBands(self.data, period=self.p.period, devfactor=self.p.devfactor)
             self.lines.bbwline = b_band.lines.top - b_band.lines.bot
      

      Strategy:

      class PST(bt.Strategy):
      
          params = (('period', 20),('devfactor', 2.0),)
      
         def __init__(self)
      
             self.bbwline = Bbw(self.data[0])
      

      The error below occurs when I uncomment the code that calls the new indicator Bbw. I appreciate if anyone can find out where I'm going wrong.

      /usr/local/lib/python3.6/dist-packages/backtrader/strategy.py in _periodset(self)
       171 
       172                 # See if the current clock has higher level clocks 
      --> 173                 clk2 = getattr(clk, '_clock', None)
       174                 if clk2 is None:
       175                     clk2 = getattr(clk._owner, '_clock', None)
      
      KeyboardInterrupt:
      
      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @gomesneto98 last edited by

        @gomesneto98 Perhaps instead of using a class file, this simple indicator could be added directly to init in the strategy file?

        def __init__(self):
            self.b_band = bt.ind.BollingerBands()
            self.bbwline = self.b_band.top - self.b_band.bot
        

        Results in:

        2020-03-16 10:03:00, High: 2434.00, Low: 2425.25, BB_Top: 2567.09, BB_Bot: 2302.46, bbwline: 264.62
        2020-03-16 10:04:00, High: 2428.25, Low: 2416.00, BB_Top: 2480.50, BB_Bot: 2361.80, bbwline: 118.71
        2020-03-16 10:05:00, High: 2421.50, Low: 2405.00, BB_Top: 2463.22, BB_Bot: 2370.06, bbwline:  93.16
        2020-03-16 10:06:00, High: 2429.50, Low: 2410.75, BB_Top: 2460.59, BB_Bot: 2378.88, bbwline:  81.71
        2020-03-16 10:07:00, High: 2434.00, Low: 2423.75, BB_Top: 2457.06, BB_Bot: 2387.62, bbwline:  69.44
        2020-03-16 10:08:00, High: 2433.50, Low: 2423.25, BB_Top: 2455.67, BB_Bot: 2393.33, bbwline:  62.33
        2020-03-16 10:09:00, High: 2438.75, Low: 2430.00, BB_Top: 2456.09, BB_Bot: 2395.64, bbwline:  60.45
        2020-03-16 10:10:00, High: 2441.50, Low: 2430.25, BB_Top: 2456.16, BB_Bot: 2399.79, bbwline:  56.37
        

        RunBacktest.com

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