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/

    Creating unique identifiers on my indicator.

    General Code/Help
    2
    4
    85
    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.
    • cept0r
      cept0r last edited by

      Hey I've been working on my mean reversion strategy but I'm having a hard time creating a unique identifier for each respective band.
      Here is my code so far:

      import backtrader as bt
      
      
      class MeanReversionLong(bt.Strategy):
      
          lines = ('kama',
                   'top1',
                   'bot1',
                   'top2',
                   'bot2',
                   'top3',
                   'bot3',
                   'top4',
                   'bot4')
      
          params = (
              ('period', 30),
              ('fast', 1.4),
              ('slow', 7.7),
              ('perc1', 0.3),
              ('perc2', 0.6),
              ('perc3', 0.9),
              ('perc4', 1.35)
          )
      
          """Create and define indicator(s)"""
          def __init__(self):
              self.ama1 = bt.indicators.AdaptiveMovingAverageEnvelope(period=self.p.period,
                                                                      fast=self.p.fast,
                                                                      slow=self.p.slow,
                                                                      perc=self.p.perc1,
                                                                      plotname='ama1')
              plotlinelabels =
              #  Plot and give each individual band a unique identifier, 1st step
              #  Formula for the upper band is: Kama * (1 + 0.3%) switch out + with - for the below band
      
              self.ama2 = bt.indicators.AdaptiveMovingAverageEnvelope(period=self.p.period,
                                                                      fast=self.p.fast,
                                                                      slow=self.p.slow,
                                                                      perc=self.p.perc2,
                                                                      plotname='ama2')
              #  Plot and give each individual band a unique identifier, 2nd step
              #  Formula for the upper band is: Kama * (1 + 0.6%) switch out + with - for the below band
      
              self.ama3 = bt.indicators.AdaptiveMovingAverageEnvelope(period=self.p.period,
                                                                      fast=self.p.fast,
                                                                      slow=self.p.slow,
                                                                      perc=self.p.perc3,
                                                                      plotname='ama3')
              #  Plot and give each individual band a unique identifier, 3rd step
              #  Formula for the upper band is: Kama * (1 + 0.9%) switch out + with - for the below band
      
              self.ama4 = bt.indicators.AdaptiveMovingAverageEnvelope(period=self.p.period,
                                                                      fast=self.p.fast,
                                                                      slow=self.p.slow,
                                                                      perc=self.p.perc4,
                                                                      plotname='ama4')
              #  Plot and give each individual band a unique identifier, 4th step
              #  Formula for the upper band is: Kama * (1 + 1.35%) switch out + with - for the below band
      
          """Define trade logic"""
          def __next__(self):
              pass
      
      

      What I want is to be able to use each top and bottom band and make them display values when I plot. So far my output is: Figure_0.png
      As you can see here I can only access the moving average, what is the best way to create unique identifiers for each respective band so I can easily access them in:

      def __next__(self):
      

      I've tried doing this to no effect:

      self.l.top1 = self.ama1(self.p.period * (1 + self.p.perc1)
      bt.LinePlotterIndicator(self.l.top1, name='top1)
      

      What am I doing wrong?

      cept0r

      1 Reply Last reply Reply Quote 0
      • cept0r
        cept0r last edited by

        Solved it!
        After hours of trying different things I managed to solve it using this code:

            def __init__(self):
                self.ama = bt.indicators.AdaptiveMovingAverage(period=self.p.period,
                                                               fast=self.p.fast,
                                                               slow=self.p.slow,
                                                               plotname='AdaptiveMovingAverage',
                                                               color='yellow')
        
                #  Create upper & lower band 1
                self.l.top = self.ama * (1 + self.p.perc / 100)
                top = bt.LinePlotterIndicator(self.l.top, name='top', subplot=False)
                top.plotinfo.plotname = 'top'
        
                self.l.bot = self.ama * (1 - self.p.perc / 100)
                bot = bt.LinePlotterIndicator(self.l.bot, name='bot', subplot=False)
                bot.plotinfo.plotname = 'bot'
        
                #  Create upper & lower band 2
                self.l.top2 = self.ama * (1 + self.p.perc2 / 100)
                top2 = bt.LinePlotterIndicator(self.l.top2, name='top2', color='cyan', subplot=False)
                top2.plotinfo.plotname = 'top2'
        
                self.l.bot2 = self.ama * (1 - self.p.perc2 / 100)
                bot2 = bt.LinePlotterIndicator(self.l.bot2, name='bot2', color='cyan', subplot=False)
                bot2.plotinfo.plotname = 'bot2'
        
                #  Create upper & lower band 3
                self.l.top3 = self.ama * (1 + self.p.perc3 / 100)
                top2 = bt.LinePlotterIndicator(self.l.top3, name='top3', color='red', subplot=False)
                top2.plotinfo.plotname = 'top3'
        
                self.l.bot3 = self.ama * (1 - self.p.perc3 / 100)
                bot2 = bt.LinePlotterIndicator(self.l.bot3, name='bot3', color='red', subplot=False)
                bot2.plotinfo.plotname = 'bot3'
        
                #  Create upper & lower band 4
                self.l.top4 = self.ama * (1 + self.p.perc4 / 100)
                top2 = bt.LinePlotterIndicator(self.l.top4, name='top4', color='orange', subplot=False)
                top2.plotinfo.plotname = 'top4'
        
                self.l.bot4 = self.ama * (1 - self.p.perc4 / 100)
                bot2 = bt.LinePlotterIndicator(self.l.bot4, name='bot4', color='orange', subplot=False)
                bot2.plotinfo.plotname = 'bot4'
        
        1 Reply Last reply Reply Quote 1
        • A
          ab_trader last edited by

          This can be wrapped into single indicator:

          class AmaBands(bt.Indicator):
          
              lines = ('ama', 'top', 'bot', 'top2', 'bot2', 'top3', 'bot3', 'top4', 'bot4')
          
              params = (('period',2), ('fast',2), ('slow',5), 
                        ('perc', 1.0), ('perc2', 2.0), ('perc3', 3.0), ('perc4', 4.0))
              
              plotlines = dict(ama=dict(color='yellow'),
                               top=dict(color='blue'), bot=dict(color='blue'),
                               top2=dict(color='cyan'), bot2=dict(color='cyan'),
                               top3=dict(color='green'), bot3=dict(color='green'),
                               top4=dict(color='orange'), bot4=dict(color='orange'),)
          
              def __init__(self):
          
                  self.lines.ama = bt.indicators.AdaptiveMovingAverage(
                      period=self.p.period, fast=self.p.fast, slow=self.p.slow)
          
                  self.lines.top = self.ama * (1 + self.p.perc/100)
                  self.lines.top2 = self.ama * (1 + self.p.perc2/100)
                  self.lines.top3 = self.ama * (1 + self.p.perc3/100)
                  self.lines.top4 = self.ama * (1 + self.p.perc4/100)
          
                  self.lines.bot = self.ama * (1 - self.p.perc/100)
                  self.lines.bot2 = self.ama * (1 - self.p.perc2/100)
                  self.lines.bot3 = self.ama * (1 - self.p.perc3/100)
                  self.lines.bot4 = self.ama * (1 - self.p.perc4/100)
          

          Then called in the strategy:

              def __init__(self):
                  self.dataclose = self.datas[0].close
                  self.ind = AmaBands(plot=True, subplot=False)
          
              def next(self):
                  self.log('%0.2f, %0.2f, %0.2f' % (
                           self.dataclose[0], self.ind.top[0], self.ind.bot[0]))
          
          • 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
          cept0r 1 Reply Last reply Reply Quote 2
          • cept0r
            cept0r @ab_trader last edited by

            @ab_trader
            Oh sweet!
            Thanks

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