I wanted a quick visual for stacked moving averages with the ability to look at either exponential or simple and modify timeframes. I only went out as far as an hour for options because I usually only scalp or intraday. Added in the ability to select another symbol so I can see QQQ and SPY at the same time. You can select 3 moving averages
mod note:
mod note:
| Use Case | Purpose | Operational Behavior |
|---|---|---|
| Higher‑Timeframe Trend Confirmation | Ensure 15m setups align with 30m and 1h structure | Trade only when 4 of 6 or more timeframes show the same MA stack direction |
| Trade Filtering | Avoid low‑probability or countertrend trades | Mixed colors or fewer than 4 aligned timeframes signal no‑trade conditions |
| Trend Continuation Timing | Enter pullbacks or continuation setups with structural support | Use new bull or bear alignment alerts as confirmation of synchronized trend |
| Reversal Trap Avoidance | Prevent premature fading of trends | A reversal is valid only when multiple timeframes flip together, not when a single TF changes |
| Multi‑Timeframe Compression | See 1m through 1h structure without opening multiple charts | TF1–TF6 grid provides a full trend map in one lower pane |
| Novice Trader Guidance | Provide a simple go or no‑go system | Green alignment for long bias, red alignment for short bias, yellow or mixed means stand down |
Code:
# =========================================================
# 6-TF MA STACK ALIGNMENT
# =========================================================
declare lower;
input customSymbol = "";
input averageType = {default Exponential, Simple};
input fastMA = 5;
input midMA = 13;
input slowMA = 21;
input tf1 = {default M1, M2, M3, M4, M5, M10, M15, M20, M30, H1};
input tf2 = {default M2, M1, M3, M4, M5, M10, M15, M20, M30, H1};
input tf3 = {default M3, M1, M2, M4, M5, M10, M15, M20, M30, H1};
input tf4 = {default M4, M1, M2, M3, M5, M10, M15, M20, M30, H1};
input tf5 = {default M5, M1, M2, M3, M4, M10, M15, M20, M30, H1};
input tf6 = {default M10, M1, M2, M3, M4, M5, M15, M20, M30, H1};
input minAlignment = 4;
input showAlert = yes;
input showLabels = yes;
input labelSize = FontSize.SMALL;
input labelLocation = Location.TOP_LEFT;
def useChart = customSymbol == "";
script stackState {
input c = close;
input f = 5;
input m = 13;
input s = 21;
input avgType = {default Exponential, Simple};
def fastLine;
def midLine;
def slowLine;
if avgType == avgType.Exponential then {
fastLine = ExpAverage(c, f);
midLine = ExpAverage(c, m);
slowLine = ExpAverage(c, s);
} else {
fastLine = Average(c, f);
midLine = Average(c, m);
slowLine = Average(c, s);
}
plot state =
if c > fastLine and fastLine > midLine and midLine > slowLine then 1
else if c < fastLine and fastLine < midLine and midLine < slowLine then -1
else 0;
}
# =============================
# TIMEFRAME CLOSE MAPPING
# =============================
def c1 =
if tf1 == tf1.M1 then (if useChart then close(period = AggregationPeriod.MIN) else close(symbol = customSymbol, period = AggregationPeriod.MIN))
else if tf1 == tf1.M2 then (if useChart then close(period = AggregationPeriod.TWO_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TWO_MIN))
else if tf1 == tf1.M3 then (if useChart then close(period = AggregationPeriod.THREE_MIN) else close(symbol = customSymbol, period = AggregationPeriod.THREE_MIN))
else if tf1 == tf1.M4 then (if useChart then close(period = AggregationPeriod.FOUR_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FOUR_MIN))
else if tf1 == tf1.M5 then (if useChart then close(period = AggregationPeriod.FIVE_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FIVE_MIN))
else if tf1 == tf1.M10 then (if useChart then close(period = AggregationPeriod.TEN_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TEN_MIN))
else if tf1 == tf1.M15 then (if useChart then close(period = AggregationPeriod.FIFTEEN_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FIFTEEN_MIN))
else if tf1 == tf1.M20 then (if useChart then close(period = AggregationPeriod.TWENTY_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TWENTY_MIN))
else if tf1 == tf1.M30 then (if useChart then close(period = AggregationPeriod.THIRTY_MIN) else close(symbol = customSymbol, period = AggregationPeriod.THIRTY_MIN))
else (if useChart then close(period = AggregationPeriod.HOUR) else close(symbol = customSymbol, period = AggregationPeriod.HOUR));
def c2 =
if tf2 == tf2.M1 then (if useChart then close(period = AggregationPeriod.MIN) else close(symbol = customSymbol, period = AggregationPeriod.MIN))
else if tf2 == tf2.M2 then (if useChart then close(period = AggregationPeriod.TWO_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TWO_MIN))
else if tf2 == tf2.M3 then (if useChart then close(period = AggregationPeriod.THREE_MIN) else close(symbol = customSymbol, period = AggregationPeriod.THREE_MIN))
else if tf2 == tf2.M4 then (if useChart then close(period = AggregationPeriod.FOUR_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FOUR_MIN))
else if tf2 == tf2.M5 then (if useChart then close(period = AggregationPeriod.FIVE_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FIVE_MIN))
else if tf2 == tf2.M10 then (if useChart then close(period = AggregationPeriod.TEN_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TEN_MIN))
else if tf2 == tf2.M15 then (if useChart then close(period = AggregationPeriod.FIFTEEN_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FIFTEEN_MIN))
else if tf2 == tf2.M20 then (if useChart then close(period = AggregationPeriod.TWENTY_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TWENTY_MIN))
else if tf2 == tf2.M30 then (if useChart then close(period = AggregationPeriod.THIRTY_MIN) else close(symbol = customSymbol, period = AggregationPeriod.THIRTY_MIN))
else (if useChart then close(period = AggregationPeriod.HOUR) else close(symbol = customSymbol, period = AggregationPeriod.HOUR));
def c3 =
if tf3 == tf3.M1 then (if useChart then close(period = AggregationPeriod.MIN) else close(symbol = customSymbol, period = AggregationPeriod.MIN))
else if tf3 == tf3.M2 then (if useChart then close(period = AggregationPeriod.TWO_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TWO_MIN))
else if tf3 == tf3.M3 then (if useChart then close(period = AggregationPeriod.THREE_MIN) else close(symbol = customSymbol, period = AggregationPeriod.THREE_MIN))
else if tf3 == tf3.M4 then (if useChart then close(period = AggregationPeriod.FOUR_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FOUR_MIN))
else if tf3 == tf3.M5 then (if useChart then close(period = AggregationPeriod.FIVE_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FIVE_MIN))
else if tf3 == tf3.M10 then (if useChart then close(period = AggregationPeriod.TEN_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TEN_MIN))
else if tf3 == tf3.M15 then (if useChart then close(period = AggregationPeriod.FIFTEEN_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FIFTEEN_MIN))
else if tf3 == tf3.M20 then (if useChart then close(period = AggregationPeriod.TWENTY_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TWENTY_MIN))
else if tf3 == tf3.M30 then (if useChart then close(period = AggregationPeriod.THIRTY_MIN) else close(symbol = customSymbol, period = AggregationPeriod.THIRTY_MIN))
else (if useChart then close(period = AggregationPeriod.HOUR) else close(symbol = customSymbol, period = AggregationPeriod.HOUR));
def c4 =
if tf4 == tf4.M1 then (if useChart then close(period = AggregationPeriod.MIN) else close(symbol = customSymbol, period = AggregationPeriod.MIN))
else if tf4 == tf4.M2 then (if useChart then close(period = AggregationPeriod.TWO_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TWO_MIN))
else if tf4 == tf4.M3 then (if useChart then close(period = AggregationPeriod.THREE_MIN) else close(symbol = customSymbol, period = AggregationPeriod.THREE_MIN))
else if tf4 == tf4.M4 then (if useChart then close(period = AggregationPeriod.FOUR_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FOUR_MIN))
else if tf4 == tf4.M5 then (if useChart then close(period = AggregationPeriod.FIVE_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FIVE_MIN))
else if tf4 == tf4.M10 then (if useChart then close(period = AggregationPeriod.TEN_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TEN_MIN))
else if tf4 == tf4.M15 then (if useChart then close(period = AggregationPeriod.FIFTEEN_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FIFTEEN_MIN))
else if tf4 == tf4.M20 then (if useChart then close(period = AggregationPeriod.TWENTY_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TWENTY_MIN))
else if tf4 == tf4.M30 then (if useChart then close(period = AggregationPeriod.THIRTY_MIN) else close(symbol = customSymbol, period = AggregationPeriod.THIRTY_MIN))
else (if useChart then close(period = AggregationPeriod.HOUR) else close(symbol = customSymbol, period = AggregationPeriod.HOUR));
def c5 =
if tf5 == tf5.M1 then (if useChart then close(period = AggregationPeriod.MIN) else close(symbol = customSymbol, period = AggregationPeriod.MIN))
else if tf5 == tf5.M2 then (if useChart then close(period = AggregationPeriod.TWO_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TWO_MIN))
else if tf5 == tf5.M3 then (if useChart then close(period = AggregationPeriod.THREE_MIN) else close(symbol = customSymbol, period = AggregationPeriod.THREE_MIN))
else if tf5 == tf5.M4 then (if useChart then close(period = AggregationPeriod.FOUR_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FOUR_MIN))
else if tf5 == tf5.M5 then (if useChart then close(period = AggregationPeriod.FIVE_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FIVE_MIN))
else if tf5 == tf5.M10 then (if useChart then close(period = AggregationPeriod.TEN_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TEN_MIN))
else if tf5 == tf5.M15 then (if useChart then close(period = AggregationPeriod.FIFTEEN_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FIFTEEN_MIN))
else if tf5 == tf5.M20 then (if useChart then close(period = AggregationPeriod.TWENTY_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TWENTY_MIN))
else if tf5 == tf5.M30 then (if useChart then close(period = AggregationPeriod.THIRTY_MIN) else close(symbol = customSymbol, period = AggregationPeriod.THIRTY_MIN))
else (if useChart then close(period = AggregationPeriod.HOUR) else close(symbol = customSymbol, period = AggregationPeriod.HOUR));
def c6 =
if tf6 == tf6.M1 then (if useChart then close(period = AggregationPeriod.MIN) else close(symbol = customSymbol, period = AggregationPeriod.MIN))
else if tf6 == tf6.M2 then (if useChart then close(period = AggregationPeriod.TWO_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TWO_MIN))
else if tf6 == tf6.M3 then (if useChart then close(period = AggregationPeriod.THREE_MIN) else close(symbol = customSymbol, period = AggregationPeriod.THREE_MIN))
else if tf6 == tf6.M4 then (if useChart then close(period = AggregationPeriod.FOUR_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FOUR_MIN))
else if tf6 == tf6.M5 then (if useChart then close(period = AggregationPeriod.FIVE_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FIVE_MIN))
else if tf6 == tf6.M10 then (if useChart then close(period = AggregationPeriod.TEN_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TEN_MIN))
else if tf6 == tf6.M15 then (if useChart then close(period = AggregationPeriod.FIFTEEN_MIN) else close(symbol = customSymbol, period = AggregationPeriod.FIFTEEN_MIN))
else if tf6 == tf6.M20 then (if useChart then close(period = AggregationPeriod.TWENTY_MIN) else close(symbol = customSymbol, period = AggregationPeriod.TWENTY_MIN))
else if tf6 == tf6.M30 then (if useChart then close(period = AggregationPeriod.THIRTY_MIN) else close(symbol = customSymbol, period = AggregationPeriod.THIRTY_MIN))
else (if useChart then close(period = AggregationPeriod.HOUR) else close(symbol = customSymbol, period = AggregationPeriod.HOUR));
# =============================
# STATES
# =============================
def s1 = stackState(c1, fastMA, midMA, slowMA, averageType);
def s2 = stackState(c2, fastMA, midMA, slowMA, averageType);
def s3 = stackState(c3, fastMA, midMA, slowMA, averageType);
def s4 = stackState(c4, fastMA, midMA, slowMA, averageType);
def s5 = stackState(c5, fastMA, midMA, slowMA, averageType);
def s6 = stackState(c6, fastMA, midMA, slowMA, averageType);
# =============================
# ALIGNMENT
# =============================
def bullCount = (s1 == 1) + (s2 == 1) + (s3 == 1) + (s4 == 1) + (s5 == 1) + (s6 == 1);
def bearCount = (s1 == -1) + (s2 == -1) + (s3 == -1) + (s4 == -1) + (s5 == -1) + (s6 == -1);
def bullAlign = bullCount >= minAlignment;
def bearAlign = bearCount >= minAlignment;
def newBull = bullAlign and !bullAlign[1];
def newBear = bearAlign and !bearAlign[1];
# =============================
# ALERTS
# =============================
Alert(showAlert and newBull, "BULL ALIGNMENT (" + bullCount + "/6)", Alert.BAR, Sound.Ding);
Alert(showAlert and newBear, "BEAR ALIGNMENT (" + bearCount + "/6)", Alert.BAR, Sound.Ding);
# =============================
# GRID
# =============================
plot r6 = 6;
plot r5 = 5;
plot r4 = 4;
plot r3 = 3;
plot r2 = 2;
plot r1 = 1;
r6.SetPaintingStrategy(PaintingStrategy.POINTS);
r5.SetPaintingStrategy(PaintingStrategy.POINTS);
r4.SetPaintingStrategy(PaintingStrategy.POINTS);
r3.SetPaintingStrategy(PaintingStrategy.POINTS);
r2.SetPaintingStrategy(PaintingStrategy.POINTS);
r1.SetPaintingStrategy(PaintingStrategy.POINTS);
r6.SetLineWeight(5);
r5.SetLineWeight(5);
r4.SetLineWeight(5);
r3.SetLineWeight(5);
r2.SetLineWeight(5);
r1.SetLineWeight(5);
r6.AssignValueColor(if s6 == 1 then Color.GREEN else if s6 == -1 then Color.RED else Color.YELLOW);
r5.AssignValueColor(if s5 == 1 then Color.GREEN else if s5 == -1 then Color.RED else Color.YELLOW);
r4.AssignValueColor(if s4 == 1 then Color.GREEN else if s4 == -1 then Color.RED else Color.YELLOW);
r3.AssignValueColor(if s3 == 1 then Color.GREEN else if s3 == -1 then Color.RED else Color.YELLOW);
r2.AssignValueColor(if s2 == 1 then Color.GREEN else if s2 == -1 then Color.RED else Color.YELLOW);
r1.AssignValueColor(if s1 == 1 then Color.GREEN else if s1 == -1 then Color.RED else Color.YELLOW);
# =============================
# ANCHORS
# =============================
plot upperAnchor = 7;
plot lowerAnchor = 0;
upperAnchor.Hide();
lowerAnchor.Hide();
# =============================
# LABELS
# =============================
AddLabel(showLabels,
"SYM " + (if useChart then GetSymbol() else customSymbol),
Color.WHITE,
labelLocation, labelSize);
AddLabel(showLabels,
if averageType == averageType.Exponential then "EXP" else "SMA",
Color.WHITE,
labelLocation, labelSize);
AddLabel(showLabels,
"TF6 " +
(if tf6 == tf6.M1 then "1M"
else if tf6 == tf6.M2 then "2M"
else if tf6 == tf6.M3 then "3M"
else if tf6 == tf6.M4 then "4M"
else if tf6 == tf6.M5 then "5M"
else if tf6 == tf6.M10 then "10M"
else if tf6 == tf6.M15 then "15M"
else if tf6 == tf6.M20 then "20M"
else if tf6 == tf6.M30 then "30M"
else "1H"),
if s6 == 1 then Color.GREEN else if s6 == -1 then Color.RED else Color.YELLOW,
labelLocation, labelSize);
AddLabel(showLabels,
"TF5 " +
(if tf5 == tf5.M1 then "1M"
else if tf5 == tf5.M2 then "2M"
else if tf5 == tf5.M3 then "3M"
else if tf5 == tf5.M4 then "4M"
else if tf5 == tf5.M5 then "5M"
else if tf5 == tf5.M10 then "10M"
else if tf5 == tf5.M15 then "15M"
else if tf5 == tf5.M20 then "20M"
else if tf5 == tf5.M30 then "30M"
else "1H"),
if s5 == 1 then Color.GREEN else if s5 == -1 then Color.RED else Color.YELLOW,
labelLocation, labelSize);
AddLabel(showLabels,
"TF4 " +
(if tf4 == tf4.M1 then "1M"
else if tf4 == tf4.M2 then "2M"
else if tf4 == tf4.M3 then "3M"
else if tf4 == tf4.M4 then "4M"
else if tf4 == tf4.M5 then "5M"
else if tf4 == tf4.M10 then "10M"
else if tf4 == tf4.M15 then "15M"
else if tf4 == tf4.M20 then "20M"
else if tf4 == tf4.M30 then "30M"
else "1H"),
if s4 == 1 then Color.GREEN else if s4 == -1 then Color.RED else Color.YELLOW,
labelLocation, labelSize);
AddLabel(showLabels,
"TF3 " +
(if tf3 == tf3.M1 then "1M"
else if tf3 == tf3.M2 then "2M"
else if tf3 == tf3.M3 then "3M"
else if tf3 == tf3.M4 then "4M"
else if tf3 == tf3.M5 then "5M"
else if tf3 == tf3.M10 then "10M"
else if tf3 == tf3.M15 then "15M"
else if tf3 == tf3.M20 then "20M"
else if tf3 == tf3.M30 then "30M"
else "1H"),
if s3 == 1 then Color.GREEN else if s3 == -1 then Color.RED else Color.YELLOW,
labelLocation, labelSize);
AddLabel(showLabels,
"TF2 " +
(if tf2 == tf2.M1 then "1M"
else if tf2 == tf2.M2 then "2M"
else if tf2 == tf2.M3 then "3M"
else if tf2 == tf2.M4 then "4M"
else if tf2 == tf2.M5 then "5M"
else if tf2 == tf2.M10 then "10M"
else if tf2 == tf2.M15 then "15M"
else if tf2 == tf2.M20 then "20M"
else if tf2 == tf2.M30 then "30M"
else "1H"),
if s2 == 1 then Color.GREEN else if s2 == -1 then Color.RED else Color.YELLOW,
labelLocation, labelSize);
AddLabel(showLabels,
"TF1 " +
(if tf1 == tf1.M1 then "1M"
else if tf1 == tf1.M2 then "2M"
else if tf1 == tf1.M3 then "3M"
else if tf1 == tf1.M4 then "4M"
else if tf1 == tf1.M5 then "5M"
else if tf1 == tf1.M10 then "10M"
else if tf1 == tf1.M15 then "15M"
else if tf1 == tf1.M20 then "20M"
else if tf1 == tf1.M30 then "30M"
else "1H"),
if s1 == 1 then Color.GREEN else if s1 == -1 then Color.RED else Color.YELLOW,
labelLocation, labelSize);
AddLabel(showLabels,
if bullAlign then "BULL " + bullCount + "/6"
else if bearAlign then "BEAR " + bearCount + "/6"
else "MIXED",
if bullAlign then Color.GREEN else if bearAlign then Color.RED else Color.GRAY,
labelLocation, labelSize);
Last edited by a moderator: