3

I am trying to plot multiple timeframe EMA on 15 min chart, this is my code.


//@version=5
indicator(title='higher tf', overlay=true)


// 4 hr

show_4h = input(title='Show 4 hour', defval=true, group = "4 HOUR")

show_ema20_4 = input(title='Show 20 EMA of 4 hour', defval=true, group = "4 HOUR")
ema20_4_period = input(title='20 EMA of 4 hour Period', defval=20, group = "4 HOUR")

show_ema50_4 = input(title='Show 50 EMA of 4 hour', defval=true, group = "4 HOUR")
ema50_4_period = input(title='50 EMA of 4 hour Period', defval=50, group = "4 HOUR")

show_ema100_4 = input(title='Show 100 EMA of 4 hour', defval=true, group = "4 HOUR")
ema100_4_period = input(title='100 EMA of 4 hour Period', defval=100, group = "4 HOUR")

show_ema200_4 = input(title='Show 200 EMA of 4 hour', defval=true, group = "4 HOUR")
ema200_4_period = input(title='200 EMA of 4 hour Period', defval=200, group = "4 HOUR")

ema20_4 = request.security(syminfo.tickerid, '360', ta.ema(close, ema20_4_period),lookahead = barmerge.lookahead_on, gaps=barmerge.gaps_on)
ema50_4 = request.security(syminfo.tickerid, '360', ta.ema(close, ema50_4_period),lookahead = barmerge.lookahead_on, gaps=barmerge.gaps_on)
ema100_4 = request.security(syminfo.tickerid, '360', ta.ema(close, ema100_4_period), lookahead = barmerge.lookahead_on, gaps=barmerge.gaps_on)
ema200_4 = request.security(syminfo.tickerid, '360', ta.ema(close, ema200_4_period), lookahead = barmerge.lookahead_on, gaps=barmerge.gaps_on)


plot(show_4h ? show_ema20_4 ? ema20_4 : na : na, title='20 EMA 4 hour')
plot(show_4h ?show_ema50_4?ema50_4: na : na, title="50 EMA 4 hour")
plot(show_4h ?show_ema100_4?ema100_4: na : na, title="100 EMA 4 hour")
plot(show_4h ?show_ema200_4?ema200_4: na : na, title="200 EMA 4 hour")

on plotting, there are steps on the graph.

However, when I use the in-built indicator and change the timeframe to 4 hour that line is completely smooth.

This is how it looks like.

enter image description here

the red line is the in-built indicator and the blue line is my indicator.

How can I make the line smooth?

alex deralu
  • 569
  • 1
  • 3
  • 18
  • Did you ever find a solution? I tried smoothen the points with `ta.sma(ema20_4, 3)` but without success – Florian May 04 '22 at 15:51

2 Answers2

1

You can just add "timeframe_gaps=true" to your indicator().

indicator(title='higher tf', overlay=true, timeframe_gaps=true)

If u want to add timeframe gaps to a strategy() you have to add "barmerge.gaps_on" to your variable.

avariant
  • 2,234
  • 5
  • 25
  • 33
BadSnakes
  • 26
  • 1
0

You face this problem, because you make the values of timeframe for exponential moving average "EMA" is 360 which mean 6 hours so once you open any chart the EMA will be for 6 hours, you should to change it to timeframe.period to be the EMA similar to timeframe timeframe.period

//@version=5
indicator(title='higher tf', overlay=true)


// 4 hr

show_4h = input(title='Show 4 hour', defval=true, group = "4 HOUR")

show_ema20_4 = input(title='Show 20 EMA of 4 hour', defval=true, group = "4 HOUR")
ema20_4_period = input(title='20 EMA of 4 hour Period', defval=20, group = "4 HOUR")

show_ema50_4 = input(title='Show 50 EMA of 4 hour', defval=true, group = "4 HOUR")
ema50_4_period = input(title='50 EMA of 4 hour Period', defval=50, group = "4 HOUR")

show_ema100_4 = input(title='Show 100 EMA of 4 hour', defval=true, group = "4 HOUR")
ema100_4_period = input(title='100 EMA of 4 hour Period', defval=100, group = "4 HOUR")

show_ema200_4 = input(title='Show 200 EMA of 4 hour', defval=true, group = "4 HOUR")
ema200_4_period = input(title='200 EMA of 4 hour Period', defval=200, group = "4 HOUR")

ema20_4 = request.security(syminfo.tickerid, timeframe.period, ta.ema(close, ema20_4_period),lookahead = barmerge.lookahead_on, gaps=barmerge.gaps_on)
ema50_4 = request.security(syminfo.tickerid, timeframe.period, ta.ema(close, ema50_4_period),lookahead = barmerge.lookahead_on, gaps=barmerge.gaps_on)
ema100_4 = request.security(syminfo.tickerid, timeframe.period, ta.ema(close, ema100_4_period), lookahead = barmerge.lookahead_on, gaps=barmerge.gaps_on)
ema200_4 = request.security(syminfo.tickerid, timeframe.period, ta.ema(close, ema200_4_period), lookahead = barmerge.lookahead_on, gaps=barmerge.gaps_on)


plot(show_4h ? show_ema20_4 ? ema20_4 : na : na, title='20 EMA 4 hour')
plot(show_4h ?show_ema50_4?ema50_4: na : na, title="50 EMA 4 hour")
plot(show_4h ?show_ema100_4?ema100_4: na : na, title="100 EMA 4 hour")
plot(show_4h ?show_ema200_4?ema200_4: na : na, title="200 EMA 4 hour")
Mazen Abduallah
  • 124
  • 1
  • 4
  • Hi, thanks for your answer but I think you got the question wrong. I want to see indicators of different timeframes on a different timeframe chart, for example - 1 hour moving average on a 15 minutes chart. – alex deralu Feb 26 '22 at 13:18