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
-
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?
-
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/
-
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)
-
@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.