Every block of a real indicator is something this series already taught. Inputs with a group heading and minval (EP 3, EP 16); a single-line user function (EP 9); the daily average on an hourly chart with request.security, using avg[1] together with lookahead = barmerge.lookahead_on so it only gives completed values and does not repaint (EP 10, EP 11); a crossover that only counts once the bar has closed, with barstate.isconfirmed (EP 7, EP 11); an alert() whose message is built at run time, firing once per bar close (EP 17); and a dashboard table declared with var and filled only on the last bar (EP 8, EP 12).
The code:
//@version=6
indicator(“MAB · Season 1”, overlay = true)
// inputs (EP 03 · EP 16)
len = input.int(20, “Length”, minval = 1, group = “Average”)
tf = input.timeframe(“D”, “Higher timeframe”, group = “Average”)
// function (EP 09)
avgOf(src, n) => ta.sma(src, n)
avg = avgOf(close, len)
// higher timeframe, no repaint (EP 10 · EP 11)
htf = request.security(syminfo.tickerid, tf, avg[1], lookahead = barmerge.lookahead_on)
plot(avg, “Average”, color.orange)
plot(htf, “HTF average”, color.gray)
// confirmed cross + alert (EP 07 · EP 11 · EP 17)
crossed = ta.crossover(close, avg) and barstate.isconfirmed
bgcolor(crossed ? color.new(color.orange, 85) : na)
if crossed
alert(syminfo.ticker + ” crossed its average · ” + timeframe.period, alert.freq_once_per_bar_close)
// dashboard (EP 08 · EP 12)
var table t = table.new(position.top_right, 2, 1, bgcolor = color.new(color.black, 20))
if barstate.islast
table.cell(t, 0, 0, “TF”, text_color = color.white)
table.cell(t, 1, 0, tf, text_color = color.orange)
Pine Script EP 18 — Season 1 finale. Season 2 coming soon.
Educational example.
Your rules. Coded. · marketalgobot.com
https://marketalgobot.com
#pinescript #tradingview #coding #learntocode #marketalgobot