// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Stacked Moving Average
// © dvinales
//@version=5
indicator("Stacked Moving Averages", shorttitle="Stacked MA", overlay=true)
string timeFrame = input.timeframe(defval="")
int len1 = input.int(title="Length Moving Average 1", defval=8, minval=1)
int len2 = input.int(title="Length Moving Average 2", defval=21, minval=1)
int len3 = input.int(title="Length Moving Average 3", defval=34, minval=1)
int len4 = input.int(title="Length Moving Average 4", defval=55, minval=0)
int len5 = input.int(title="Length Moving Average 5", defval=89, minval=0)
float source = input.source(title="Source", defval=close)
string mafn = input.string(title="Moving Average", defval="EMA", options=["SMA", "EMA", "DEMA", "WMA", "HMA", "RMA"])
bool showSignals = input.bool(title="Show Signals?", defval=true)
color uptrendColor = input.color(color.new(color.green, 75), "Uptrend")
color downtrendColor = input.color(color.new(color.red, 75), "Downtrend")
fnMA(series, length) =>
if (length > 0)
switch mafn
"SMA" => ta.sma(series, length)
"EMA" => ta.ema(series, length)
"WMA" => ta.wma(series, length)
"HMA" => ta.hma(series, length)
"RMA" => ta.rma(series, length)
"DEMA" =>
emaValue = ta.ema(series, length)
2 * emaValue - ta.ema(emaValue, length)
=> float(na)
else
float(na)
securityNoRepaint(sym, tf, src) =>
request.security(sym, tf, src[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1]
ma1 = securityNoRepaint(syminfo.tickerid, timeFrame, fnMA(source, len1))
ma2 = securityNoRepaint(syminfo.tickerid, timeFrame, fnMA(source, len2))
ma3 = securityNoRepaint(syminfo.tickerid, timeFrame, fnMA(source, len3))
ma4 = len4 > 0 ? securityNoRepaint(syminfo.tickerid, timeFrame, fnMA(source, len4)) : float(na)
ma5 = len5 > 0 ? securityNoRepaint(syminfo.tickerid, timeFrame, fnMA(source, len5)) : float(na)
plot(showSignals ? ma1 : float(na), "MA 1", color=color.maroon, linewidth=2)
plot(showSignals ? ma2 : float(na), "MA 2", color=color.fuchsia, linewidth=2)
plot(showSignals ? ma3 : float(na), "MA 3", color=color.orange, linewidth=2)
plot(showSignals ? ma4 : float(na), "MA 4", color=color.aqua, linewidth=2)
plot(showSignals ? ma5 : float(na), "MA 5", color=color.navy, linewidth=2)
is_uptrend = ma1 > ma2 and ma2 > ma3 and (ma3 > ma4 or len4 == 0) and (ma4 > ma5 or len4 == 0 or len5 == 0)
is_downtrend = (ma5 > ma4 or len4 == 0 or len5 == 0) and (ma4 > ma3 or len4 == 0) and ma3 > ma2 and ma2 > ma1
bgcolor(is_uptrend ? uptrendColor : (is_downtrend ? downtrendColor : na))