1

I'm trying to plotshape() with multiple conditions from 2 or more timeframe and it's not working IF there are 2 or more conditions from the other timeframe which data pulled with request.security().

Please see code with comments on what works and what doesn't, and advise if my code is wrong or this is plotshape() bug. Pine script v5

Thanks, Om

enter image description here

//////// Long and Short Signals by MACD Histogram, 
//////// 2 minutes(indicator) AND 5 minutes(through request.security()

indicator(title="Moving Average Convergence Divergence Cross w/ Fill & Signal", shorttitle="MACD Cross w/ Fill & Signal")

// Calculate 2 Min MACD
fast_ma = fast_ma_type == "SMA" ? ta.sma(src, fast_len) : ta.ema(src, fast_len)
slow_ma = fast_ma_type == "SMA" ? ta.sma(src, slow_len) : ta.ema(src, slow_len)
macd = fast_ma - slow_ma
signal = slow_ma_type == "SMA" ? ta.sma(macd, signal_len) : ta.ema(macd, signal_len)
hist = macd - signal

// Get 5 Min Histogram Data
[_, _, hist5] = ta.macd(src, fast_len, slow_len, signal_len)
hist5Min = request.security(syminfo.tickerid, "5", hist5, gaps = barmerge.gaps_off)

// Go Long conditions
go_long2 = hist > hist[1]
go_long5A = hist5Min > hist5Min[1]
go_long5B = hist5Min[1] < hist5Min[2]

// Go Short conditions
go_short2 = hist < hist[1]
go_short5A = hist5Min < hist5Min[1]
go_short5B = hist5Min[1] > hist5Min[2]

// Plots

// This works
plot_shape_condition = timeframe.period == "2"
plotshape((plot_shape_condition and hist < 0 and go_long2 and go_long5A) ? hist:na, style=shape.labelup, color=color.new(color.green, 90), text="⬆", textcolor=color.new(color.green, 0), size=size.tiny, location=location.bottom)

// This DOESN'T work, doesn't plot any shape at all
plotshape((plot_shape_condition and hist < 0 and go_long2 and go_long5A and go_long5B) ? hist:na, style=shape.labelup, color=color.new(color.green, 90), text="⬆", textcolor=color.new(color.green, 0), size=size.tiny, location=location.bottom)

// These works, so there's data for Hist5Min[1] and [2]
plot(hist5Min, color=color.red)
plot(hist5Min[1], color=color.green)
plot(hist5Min[2], color=color.blue)
Om Nom
  • 169
  • 1
  • 2
  • 11

1 Answers1

1

Since you are accessing 5M MACD in 2M Interval, the value of 5M MACD is updating after 4 to 6 minutes. So if you access it at 2:00 you get MACD of 2:00, For 2:02 you again get MACD of 2:00, for 2:04 you get MACD of 2:05, for 2:06 again MACD of 2:05 and then for 2:08, 2:10 and 2:12 you get MACD of 2:10. So there can be no 2 min candle where Hist>Hist[1] and Hist[1]<Hist[2]. Two of these three will be equal at any point of 2 min candle.

Rohit Agarwal
  • 1,258
  • 1
  • 3
  • 9
  • Even in the screenshot you can see that in one candle two of values are changing and other one is constant, and in next candle other two are changing and one is constant – Rohit Agarwal Jul 16 '22 at 04:44
  • Hi Rohit, I did some further test and you're right. Thank you! – Om Nom Jul 16 '22 at 11:54