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/

    Building and strategy around two different indicators

    Indicators/Strategies/Analyzers
    1
    1
    162
    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
      AALLI last edited by

      I'm new to backtrader and coding in general, I managed to add indicator to a dataset and would like to build my strategy around this and more indicators and parameters in the future this is my code rn:

      class _StochasticBase(bt.Indicator):
      lines = ('percK', 'percD',)
      params = (('period', 14), ('period_dfast', 3), ('movav', bt.indicators.SMA),
                ('upperband', 80.0), ('lowerband', 20.0),
                ('safediv', False), ('safezero', 0.0))
      
      plotlines = dict(percD=dict(_name='%D', ls='--'),
                       percK=dict(_name='%K'))
      
      def _plotlabel(self):
          plabels = [self.p.period, self.p.period_dfast]
          plabels += [self.p.movav] * self.p.notdefault('movav')
          return plabels
      
      def _plotinit(self):
          self.plotinfo.plotyhlines = [self.p.upperband, self.p.lowerband]
      
      def init(self):
          highesthigh = Highest(self.data.high, period=self.p.period)
          lowestlow = Lowest(self.data.low, period=self.p.period)
          knum = self.data.close - lowestlow
          kden = highesthigh - lowestlow
          if self.p.safediv:
              self.k = 100.0 * bt.DivByZero(knum, kden, zero=self.p.safezero)
          else:
              self.k = 100.0 * (knum / kden)
          self.d = self.p.movav(self.k, period=self.p.period_dfast)
      
          super(_StochasticBase, self).init()
      

      class StochasticFast(_StochasticBase):
      def init(self):
      super(StochasticFast, self).init()
      self.lines.percK = self.k
      self.lines.percD = self.d

      I want to make an strategy based on the two lines generated from the indicator and backtest from there but when I create my strategy class I don't know how to import the indicators to it (like I said I'm new to coding)
      Also I have a RSI indicator and another separate CI that I've added but I don't know how to bring those two in my strategy as well and mix em together, thanks in advance

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