Higher Timeframe MA Stack — Trend Drop
# Higher Timeframe MA Stack — Trend Drop
A trend-context tool you can drop on any chart. It pulls three moving averages from a higher timeframe, color-codes them by alignment, and tells you whether the bigger picture is bull-stacked, bear-stacked, or mixed — so your lower-timeframe entries stop fighting the macro trend.
## What it does
- Plots three MAs (default 20 / 50 / 200 EMA) computed on a higher timeframe of your choice
- Colors all three lines green when the stack is bullish (Fast > Mid > Slow)
- Colors them red when the stack is bearish (Fast < Mid < Slow)
- Shows a label with the current stack state and tints the background lightly
- Fires alerts the moment the stack flips bull or bear
## Recommended settings
- Higher timeframe: 60 (use D for swing, 30 for day trades)
- Fast length: 20
- Mid length: 50
- Slow length: 200
- MA type: EMA (try SMA for slower, smoother bias)
## Ideal timeframe
Best on a chart at least 4x faster than your selected higher timeframe. Common combos: 15-min chart with 60 HTF, or 1-hour chart with D HTF.
## How to use
Trade in the direction of the stack. When the script shows a Bull Stack and tints the background green, take long setups from your favorite system and ignore shorts. When it flips Bear, do the opposite. Mixed (gray) means the higher timeframe is in transition — size down or stand aside until the stack reasserts.
## Pine Script v5 code
```
//@version=5
// ─────────────────────────────────────────────
// │ MarketFragments.com | DNA & Market │
// │ info@marketfragments.com │
// │ www.marketfragments.com │
// ─────────────────────────────────────────────
// Time →
// │
// █ █ █│ █
// █ █ █ │ █ █
// █ █ █ │ █ █ █ ╭─╮
// █ █ █ │ █ █ █ █ ╭─╯ ╰─╮
// █ █ █ │ █ █ █ █ █ █ ╭─╯ ╰─╮
// █ █ █ │ █ █ █ █ █ █ █ █ ╭─╯ ╰─╮
// █ █ █ │ █ █ █ █ █ █ █ █ █ █╭─╯ ╰─╮
// █ █ █ │ █ █ █ █ █ █ █ █ ╰─╮ ╭─╯
// █ █ █ │ █ █ █ █ █ █ ╰─╮ ╭─╯
// █ █ █ │ █ █ █ █ ╰─╮ ╭─╯
// █ █ █ │ █ █ ╰─╮ ╭─╯
// █ █ █ │ █ ╰─────╯
// ──────┴──────────────────────────────────────────────────────────────
// T1 T2 T3 T4 T5 T6
//
// Higher Timeframe MA Stack
// Plots 3 MAs from a higher timeframe and flags bull / bear / mixed alignment.
// Free for personal, non-commercial use. Attribution appreciated.
indicator("MarketFragments HTF MA Stack", overlay=true, max_labels_count=20)
// ── Inputs ──────────────────────────────────────────────────────────
htf = input.timeframe("60", "Higher Timeframe")
len1 = input.int(20, "Fast MA Length", minval=1)
len2 = input.int(50, "Mid MA Length", minval=1)
len3 = input.int(200, "Slow MA Length", minval=1)
maType = input.string("EMA", "MA Type", options=["EMA","SMA","WMA","RMA"])
showLabel = input.bool(true, "Show Stack State Label")
tintBg = input.bool(true, "Tint Background by Stack")
// ── MA helper ───────────────────────────────────────────────────────
f_ma(src, len, t) =>
t == "SMA" ? ta.sma(src, len) :
t == "WMA" ? ta.wma(src, len) :
t == "RMA" ? ta.rma(src, len) :
ta.ema(src, len)
// ── Pull MAs from the higher timeframe ──────────────────────────────
ma1 = request.security(syminfo.tickerid, htf, f_ma(close, len1, maType), barmerge.gaps_off, barmerge.lookahead_off)
ma2 = request.security(syminfo.tickerid, htf, f_ma(close, len2, maType), barmerge.gaps_off, barmerge.lookahead_off)
ma3 = request.security(syminfo.tickerid, htf, f_ma(close, len3, maType), barmerge.gaps_off, barmerge.lookahead_off)
// ── Stack state: 1 = bull, -1 = bear, 0 = mixed ─────────────────────
bullStack = ma1 > ma2 and ma2 > ma3
bearStack = ma1 < ma2 and ma2 < ma3
state = bullStack ? 1 : bearStack ? -1 : 0
// ── Colors ──────────────────────────────────────────────────────────
bullCol = color.new(color.green, 0)
bearCol = color.new(color.red, 0)
neutCol = color.new(color.gray, 30)
stateCol = state == 1 ? bullCol : state == -1 ? bearCol : neutCol
// ── Plots ───────────────────────────────────────────────────────────
plot(ma1, "Fast MA", color=stateCol, linewidth=2)
plot(ma2, "Mid MA", color=stateCol, linewidth=2)
plot(ma3, "Slow MA", color=stateCol, linewidth=3)
bgcolor(tintBg and state == 1 ? color.new(color.green, 92) :
tintBg and state == -1 ? color.new(color.red, 92) : na)
// ── State label ─────────────────────────────────────────────────────
var label stackLbl = na
if showLabel
label.delete(stackLbl)
txt = state == 1 ? "Bull Stack" : state == -1 ? "Bear Stack" : "Mixed"
stackLbl := label.new(bar_index, high,
txt + " | HTF: " + htf,
style=label.style_label_left, color=stateCol,
textcolor=color.white, size=size.small)
// ── Alerts on stack flips ───────────────────────────────────────────
flipBull = state == 1 and state[1] != 1
flipBear = state == -1 and state[1] != -1
alertcondition(flipBull, title="Bull Stack Flip", message="MarketFragments HTF MA Stack: Bull stack engaged")
alertcondition(flipBear, title="Bear Stack Flip", message="MarketFragments HTF MA Stack: Bear stack engaged")
```
Drop your tweaks, screenshots, and questions below — that's how the library gets better.

