0
votes

I am working on a moving average indicator which shows the MA line of a given time frame. For some reason is the MA line only displaced till the last ticker.id period close. So when for example I have set the indicator to show a daily MA the line is only updated when the day closes.

(Link to image https://i.stack.imgur.com/QjkvO.jpg)

Does anyone know how my indicator will be able include data between daily closes, so the line is being updated continuously?

I think this line not being updated continuously also causes the label which is supposed to be plotted right at the MA line being plotted at the 1 point / dollar height on the chart.

I have only recently started writing code, so excuse me if this is a dumb question. I have written this code looking at other indicators and trying to fit parts into my own

This is the code of the entire indicator.

//@version=4

study(title="Custom Timeframe SMA", shorttitle="Custom TF MA", overlay=true)

res = input(title="MA Timeframe", type=input.resolution, defval="D",options=["60", "240", "D", "W"])

length1 = input(title="SMA Length", type=input.integer, defval=50)
Label=input(title="show Labels",defval=true)

sma1 = sma(close, length1)
sourceEmaSmooth1 = security(syminfo.tickerid, res, sma1, barmerge.gaps_on, barmerge.lookahead_on)

plot(sourceEmaSmooth1, style=plot.style_line, linewidth=2, title="25 period", color=#a21e7b)
plotchar((sourceEmaSmooth1 ? Label : barstate.islast and not barstate.isconfirmed) ? sourceEmaSmooth1 : na, location=location.absolute, text=" 50 SMA", textcolor=#a21e7b, offset=10, editable=false)

1

1 Answers

0
votes

Using barmerge.gaps_on with security() creates holes which appear as na values at the chart's resolution, which is why your ma wasn't always showing. It wasn't apparent on historical bars because the plot() function fills the space from non-gap to non-gap (you could see it if you plotted circles instead of a line).

Using barmerge.lookahead_on with security() produces lookahead bias on historical bars. Very nasty if you don't index the value you're fetching, as is explained in this publication on how to use security() correctly: How to avoid repainting when using security().

I added show_last = 1 to you label-plotting call and fixed the conditional. Because it now only plots the last occurrence of the label, we no longer need to worry about barstates:

//@version=4

study(title="Custom Timeframe SMA", shorttitle="Custom TF MA", overlay=true)

res = input(title="MA Timeframe", type=input.resolution, defval="D",options=["60", "240", "D", "W"])

length1 = input(title="SMA Length", type=input.integer, defval=50)
Label=input(title="show Labels",defval=true)

sma1 = sma(close, length1)
sourceEmaSmooth1 = security(syminfo.tickerid, res, sma1)

plot(sourceEmaSmooth1, linewidth=2, title="25 period", color=#a21e7b)
plotchar(Label ? sourceEmaSmooth1 : na, location=location.absolute, text=" 50 SMA", textcolor=#a21e7b, offset=10, show_last = 1, editable=false)

enter image description here