0

Struggling to get the Moving average value displayed correctly from the HL candle. Want to see the moving average value @ HL candle

Script output Screenshot-Please see this also

//@version=5
indicator("My script", overlay = true)
Period = 6
smalength = 20
float pivotLow = na
pivotLow := ta.pivotlow(6, 6)
valuewhen_5 = ta.valuewhen(pivotLow, low[Period], 1)
valuewhen_6 = ta.valuewhen(pivotLow, low[Period], 0)
higherlow = na(pivotLow) ? na : valuewhen_5 < valuewhen_6 ? pivotLow : na
plotshape(higherlow, title='HL', style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 50), text='HL', textcolor=color.new(color.green, 50), offset=-Period)
plot(ta.sma(close[0], smalength))
var label bbmbcfmlableHL = na
if (not na(higherlow))
    smavalue = ta.valuewhen(higherlow, ta.sma(close, smalength),0)
    bbmbcfmlableHL := label.new(x=bar_index, y=high, color=color.orange,  textcolor=color.blue, text=str.tostring(smavalue))

1 Answers1

0

Don't ignore warnings. You should call the function ta.sma() and ta.valuewhen() on each bar, and only present the result under the if condition.

If you want to present the label on the same bar where the plotshape is, you'll need to use the same offset:

//@version=5
indicator("My script", overlay = true)
Period = 6
smalength = 20
float pivotLow = na
pivotLow := ta.pivotlow(6, 6)
valuewhen_5 = ta.valuewhen(pivotLow, low[Period], 1)
valuewhen_6 = ta.valuewhen(pivotLow, low[Period], 0)
higherlow = na(pivotLow) ? na : valuewhen_5 < valuewhen_6 ? pivotLow : na
plotshape(higherlow, title='HL', style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 50), text='HL', textcolor=color.new(color.green, 50), offset=-Period)

plot(ta.sma(close[0], smalength))
smavalue = ta.valuewhen(higherlow, ta.sma(close, smalength),0)

var label bbmbcfmlableHL = na
if (not na(higherlow))
    bbmbcfmlableHL := label.new(x=bar_index - Period, y=high, color=color.orange,  textcolor=color.blue, text=str.tostring(smavalue))
mr_statler
  • 1,913
  • 2
  • 3
  • 14
  • Thanks for your advice. It was helpful. Updated my code as below and got the output of what I was expecting. Your help was much appreciated and my gratitude for your advice. Good day. – Dolla Chandra Babu Nov 06 '22 at 07:06