-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
-
Hi there,
I know self.I() has overlay
parameter to make overlap current indicator on price chart but I want to overlay two different (both of them are oscillators with range -100,100). As you can see from the picture I want to overlay EMA and SMA indicators. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 2 replies
-
To join multiple indicator lines on the same indicator plot, just return them together in a tuple. E.g.:
class MyStrategy(Strategy): def init(self): @self.I def my_indicator(): ema = EMA(self.data.High, 21) return ema, SMA(ema, 4)
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi @kernc,
Thanks a lot for helping out. I am kinda python dummy. And trying to find where to import @self.I
. Here is the very basic code I am running on. And I have two questions;
- How to import @self.I?
- Should I use
my_indicator
def in computing side or this is only for plotting?
from backtesting import Strategy
from backtesting.lib import crossover
from talib import EMA, SMA
class WTCross(Strategy):
n1 = 10
n2 = 21
def init(self):
close = self.data.Close
high = self.data.High
low = self.data.Low
ap = (high + low + close) / 3
esa = EMA(ap, self.n1)
d = EMA(abs(ap - esa), self.n1)
self.ci = (ap - esa) / (0.015 * d)
self.wt1 = self.I(EMA, self.ci, self.n2)
self.wt2 = self.I(SMA, self.wt1, 4)
def next(self):
if crossover(self.wt1, self.wt2):
self.buy()
elif crossover(self.wt2, self.wt1):
self.sell()
@self.I
def my_indicator(self):
wt1 = self.I(EMA, self.ci, self.n2)
return wt1, self.I(SMA, self.wt1, 4)
Thanks
Beta Was this translation helpful? Give feedback.
All reactions
-
See my answer above revised. Using it as a decorator is equivalent to a normal self.I()
function call.
Beta Was this translation helpful? Give feedback.
All reactions
-
gotcha. thanks @kernc
Beta Was this translation helpful? Give feedback.