Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to overlay two indicators? #470

Unanswered
flexelem asked this question in Q&A
Sep 19, 2021 · 2 comments · 2 replies
Discussion options

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!

plot

You must be logged in to vote

Replies: 2 comments 2 replies

Comment options

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)
You must be logged in to vote
2 replies
Comment options

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;

  1. How to import @self.I?
  2. 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

Comment options

See my answer above revised. Using it as a decorator is equivalent to a normal self.I() function call.

Comment options

gotcha. thanks @kernc

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /