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/

    Ema Slope indicator

    Indicators/Strategies/Analyzers
    3
    4
    903
    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.
    • T
      ThinkWaitFast last edited by

      Hello, I'm new to backtrader and currently trying to build a really simple custom indicator (EMA slope). I'm having trouble accessing past values of the ema, can anyone point me in the right direction?

      1 Reply Last reply Reply Quote 1
      • R
        rho33 last edited by

        Is this along the lines of what you're trying to do?

        class EMASlope(bt.Indicator):
            lines = ('slope',)
            
            def __init__(self):
                self.ema = bt.indicators.ExponentialMovingAverage()
            
            def next(self):
                self.l.slope[0] = self.ema[0] - self.ema[-1]
        

        If it really is this simple you could also just calculate it inside your strategy's next method

        class MyStrat(bt.Strategy):
            
            def __init__(self):
                self.ema = bt.indicators.ExponentialMovingAverage()
        
            def next(self):
                slope = self.ema[0] - self.ema[-1]
        

        I'm relatively new to backtrader too and I looked at this page to get this awnser https://www.backtrader.com/docu/inddev/

        1 Reply Last reply Reply Quote 0
        • run-out
          run-out last edited by

          For a vectorized approach you can use:

          class EMASlope(bt.Indicator):
              lines = ('slope',)
              
              def __init__(self):
                  self.ema = bt.indicators.ExponentialMovingAverage()
                  self.ema2 = self.ema - self.ema(-1)
          

          RunBacktest.com

          R 1 Reply Last reply Reply Quote 0
          • R
            rho33 @run-out last edited by

            @run-out said in Ema Slope indicator:

            self.ema2 = self.ema - self.ema(-1)

            Yeah this approach is probably better because it automatically updates the indicator's minimum period.

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