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/

    Help on customized indicator Thx!

    Indicators/Strategies/Analyzers
    2
    2
    137
    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.
    • S
      sky_aka41 last edited by

      Hello,

      I am totally new to the community and also a fresh user of backtrader. I am trying to develop an indicator which I found in Tradingview. However I found it a bit difficult to do so.

      The logic of the indicator is as the following:

      Momentum Adjusted Moving Average (MaMA)

      The idea is to adjust the prices with the momentum and calculate the moving average using the adjusted prices.

      First, we calculate the momentum and acceleration:

      Mom = Price_now - Price_prev
      
      Acceleration = Mom_now - Mom_prev
      

      Then we calculate a probability value using a few past bars:

      Probability = N_increase / N
      

      where N_increase is the number of bars whose price is increased comparing to the previous one, and N the total number of bars in the calculation window.

      Next we can calculate the adjusted price as:

      Adj = Price_now + Probability * (Mom + 0.5 * Acceleration) 
      

      and finally calculate the moving average of Adj.

      The strategy involving this indicator can be as simple as an MA crossover strategy.

      I was wondering that if anyone would show me an example of how to implement this indicator in Backtrader ? I tried something like this but it somehow goes into an endless run...

      class MaMA(bt.Indicator):
          
          lines = ('MaMA', )
          params = { "momLength": 34, "maLength": 55, "pLength": 13}
          
          def __init__(self):
              max_length = max(self.p.momLength*2, self.p.maLength, self.p.pLength)
              self.addminperiod(max_length)
              
          def next(self):
              
              momentum = bt.ind.Momentum(self.datas[0].close, period=self.params.momLength)
              accelerate = bt.ind.Momentum(momentum, period=self.params.momLength)
              prob_change = bt.ind.Momentum(self.datas[0].close, period=1)
              probability = math.fsum(prob_change.get(size=self.p.pLength) > 0) / self.p.pLength 
              
              adjusted_close = self.datas[0].close + ( momentum + accelerate*0.5 ) * probability
              
              self.lines.MaMA = bt.ind.SMA(adjusted_close, period=self.params.maLength)
      

      Highly appreciate for any advice/suggestions! Thx in adv!

      D 1 Reply Last reply Reply Quote 1
      • D
        davidavr @sky_aka41 last edited by

        @sky_aka41 I think you are pretty close. I tried this all in a strategy (not a custom indicator) and it seems to be working.

        Note I changed the prob_change and probability expressions slightly.

        class St(bt.Strategy):
            params = (("momLength", 34), ("maLength", 55), ("pLength", 13))
        
            def __init__(self) :
                self.momentum = bt.ind.Momentum(self.datas[0].close, period=self.p.momLength)
                self.accelerate = bt.ind.Momentum(self.momentum, period=self.p.momLength)
                self.prob_change = bt.ind.Momentum(self.datas[0].close, period=1) > 0
                self.probability = bt.ind.SumN(self.prob_change) / self.p.pLength 
                
                self.adjusted_close = self.datas[0].close + (self.momentum + self.accelerate*0.5 ) * self.probability
                
                self.lines.MaMA = bt.ind.SMA(self.adjusted_close, period=self.p.maLength)
        
        1 Reply Last reply Reply Quote 1
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors