Trend
Three MA Stack
Three moving averages whose type is chosen from a dropdown, coloured by whether they are stacked upwards, downwards or neither, with the bar a full stack forms marked.
indicator
//@version=6
indicator("Three MA Stack", "MA Stack", overlay = true)
maType = input.string("EMA", "Average type", options = ["SMA", "EMA", "WMA"], group = "Settings")
fastLen = input.int(10, "Fast", minval = 1, group = "Settings")
midLen = input.int(30, "Middle", minval = 1, group = "Settings")
slowLen = input.int(60, "Slow", minval = 1, group = "Settings")
bull = input.color(#26A69A, "Stacked up", group = "Style")
bear = input.color(#EF5350, "Stacked down", group = "Style")
flat = input.color(#787B86, "Mixed", group = "Style")
// Every average is computed on every bar, then one is selected. A ta.* call
// reached only sometimes would be fed a subset of bars and return nonsense.
pick(src, len, kind) =>
smaVal = ta.sma(src, len)
emaVal = ta.ema(src, len)
wmaVal = ta.wma(src, len)
switch kind
"SMA" => smaVal
"WMA" => wmaVal
=> emaVal
fast = pick(close, fastLen, maType)
mid = pick(close, midLen, maType)
slow = pick(close, slowLen, maType)
stackedUp = fast > mid and mid > slow
stackedDown = fast < mid and mid < slow
tone = stackedUp ? bull : stackedDown ? bear : flat
plot(fast, "Fast", color = tone, linewidth = 2)
plot(mid, "Middle", color = color.new(tone, 25))
plot(slow, "Slow", color = color.new(tone, 50))
formedUp = stackedUp and not stackedUp[1]
formedDown = stackedDown and not stackedDown[1]
plotshape(formedUp, "Stack up", location = location.belowbar, style = shape.triangleup, color = bull, size = size.tiny)
plotshape(formedDown, "Stack down", location = location.abovebar, style = shape.triangledown, color = bear, size = size.tiny)
alertcondition(formedUp, "Stacked up", "The three moving averages stacked fast above middle above slow")
alertcondition(formedDown, "Stacked down", "The three moving averages stacked fast below middle below slow")
This runs in TradingView, not here
Pine Script only executes inside TradingView. Paste the source into the Pine Editor and add it to a chart to see it plotted.
What it will not do
- Changing the type changes the signals, not how much they lag. Every option is an average of past bars and every option is late.
- Mixed is the ordinary state. Most bars are not stacked either way, and the markers only fire on the transition into a stack.
- A stack describes the bars behind it. It looks cleanest in the middle of a move and most confusing at the turn, which is where it matters.
Written from this description
Plot fast, middle and slow moving averages whose type is picked from a dropdown of SMA, EMA and WMA, colour them by the stack order, and mark and alert the bar where a full stack forms in either direction.
Educational only, not financial advice. The maths is simple arithmetic on the numbers you enter; it knows nothing about your broker, fees, slippage or the market. Something wrong with it? Say so in Site Feedback.