Run FactorLab's 200-Day SMA Leverage Shield Live on Your TradingView Charts
No $29 paywalls, no invite-only lock-in. Copy our strictly causal 1-day lagged Pine Script v6 templates below, paste into TradingView's Pine Editor, and activate automated phone & email alerts across TQQQ, SOXL, UPRO, NVDA & BTC in 30 seconds.
1X-Gated 200-Day SMA Leverage Shield (v6)
Computes the 200-day SMA on the unleveraged 1X underlying (request.security(..., lookahead_off), lagged 1 day) so 3X volatility decay never distorts your buy/sell signals.
//@version=6
// FactorLab Research — 1X-Gated 200-Day SMA Leverage Shield (Zero Lookahead)
// Free open-source template from https://factorlab-research.web.app/scripts/
// Tracks unleveraged 1X underlying index (1-day lagged) to avoid 3X volatility decay distortion.
strategy(
"FactorLab 1X-Gated 200MA Leverage Shield",
shorttitle = "FL 200MA Shield",
overlay = true,
initial_capital = 10000,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 100,
commission_type = strategy.commission.percent,
commission_value = 0.05,
slippage = 1
)
// ── Inputs ──────────────────────────────────────────────────────────────────
sigSymbol = input.symbol("NASDAQ:QQQ", "1X Underlying Signal Symbol (e.g. QQQ for TQQQ)")
smaLen = input.int(200, "200-Day SMA Length", minval = 20, maxval = 400)
bandPct = input.float(0.0, "Optional Hysteresis Buffer (%)", minval = 0.0, maxval = 5.0, step = 0.25)
showBg = input.bool(true, "Highlight BULL / DEFENSIVE Regime Background")
showHud = input.bool(true, "Show Live Telemetry HUD Table")
// ── Strictly Causal 1-Day Lagged 1X Signal (No Repaint / No Lookahead) ──────
[uClosePrev, uSmaPrev] = request.security(
sigSymbol,
"D",
[close[1], ta.sma(close, smaLen)[1]],
barmerge.gaps_off,
barmerge.lookahead_off
)
distPct = na(uSmaPrev) or uSmaPrev == 0 ? 0.0 : ((uClosePrev / uSmaPrev) - 1.0) * 100.0
var bool isBull = true
if not na(uSmaPrev)
if isBull and distPct < -bandPct
isBull := false
else if (not isBull) and distPct > bandPct
isBull := true
bullFlip = isBull and not isBull[1]
defFlip = (not isBull) and isBull[1]
// ── Strategy Execution (Long in BULL, Cash / T-Bills in DEFENSIVE) ──────────
if bullFlip
strategy.entry("BULL_LONG", strategy.long, comment = "200MA BULL")
if defFlip
strategy.close("BULL_LONG", comment = "T-BILL SHIELD")
// ── Visual Overlays ─────────────────────────────────────────────────────────
chartSma = ta.sma(close, smaLen)
plot(chartSma, "Chart 200MA Reference", color = isBull ? color.new(#10B981, 20) : color.new(#F43F5E, 20), linewidth = 2)
bgcolor(showBg ? (isBull ? color.new(#10B981, 93) : color.new(#F43F5E, 91)) : na)
plotshape(bullFlip, "BULL Flip", shape.triangleup, location.belowbar, #10B981, size = size.small, text = "BULL", textcolor = #10B981)
plotshape(defFlip, "DEFENSIVE Flip", shape.triangledown, location.abovebar, #F43F5E, size = size.small, text = "CASH", textcolor = #F43F5E)
// ── Native TradingView Alert Triggers ───────────────────────────────────────
alertcondition(bullFlip, title = "FactorLab: BULL Regime Entry", message = "{{ticker}}: 1X underlying crossed ABOVE 200-Day SMA -> RISK-ON LONG")
alertcondition(defFlip, title = "FactorLab: DEFENSIVE T-Bill Exit", message = "{{ticker}}: 1X underlying crossed BELOW 200-Day SMA -> DEFENSIVE T-BILL SHIELD")
// ── Institutional Live HUD ──────────────────────────────────────────────────
if showHud and barstate.islast
var table hud = table.new(position.top_right, 2, 4, bgcolor = color.new(#0B0D11, 10), border_color = #222938, border_width = 1)
table.cell(hud, 0, 0, "FACTORLAB 200MA", text_color = #60A5FA, text_size = size.tiny, text_halign = text.align_left)
table.cell(hud, 1, 0, isBull ? "BULL (LONG)" : "DEFENSIVE (BIL)", text_color = isBull ? #34D399 : #FB7185, text_size = size.tiny, text_halign = text.align_right)
table.cell(hud, 0, 1, "1X Signal Ref", text_color = #94A3B8, text_size = size.tiny, text_halign = text.align_left)
table.cell(hud, 1, 1, sigSymbol, text_color = #F8FAFC, text_size = size.tiny, text_halign = text.align_right)
table.cell(hud, 0, 2, "Gap vs 200MA", text_color = #94A3B8, text_size = size.tiny, text_halign = text.align_left)
table.cell(hud, 1, 2, str.tostring(distPct, "+#.##;-#.##") + "%", text_color = distPct >= 0 ? #34D399 : #FB7185, text_size = size.tiny, text_halign = text.align_right)
table.cell(hud, 0, 3, "Buffer Band", text_color = #94A3B8, text_size = size.tiny, text_halign = text.align_left)
table.cell(hud, 1, 3, str.tostring(bandPct, "#.##") + "%", text_color = #CBD5E1, text_size = size.tiny, text_halign = text.align_right)
Volatility Decay & ER20 Choppy-Market HUD
Tracks 20-day annualized volatility, Kaufman ER20 (flags choppy whipsaw regimes below 0.20), rolling peak drawdown, and estimated annual 3X variance drag.
//@version=6
// FactorLab Research — Volatility Decay, Drawdown & ER20 Choppy-Market HUD
// Free open-source template from https://factorlab-research.web.app/scripts/
indicator("FactorLab Volatility Decay & ER20 Danger HUD", shorttitle = "FL Vol HUD", overlay = true)
volLen = input.int(20, "Realized Volatility Window (Days)", minval = 10, maxval = 60)
erLen = input.int(20, "Kaufman Efficiency Ratio (ER20) Window", minval = 10, maxval = 60)
ddLookbk = input.int(252, "Rolling Peak Drawdown Window (252 = 1Y)", minval = 60, maxval = 2520)
levMult = input.float(3.0, "Target Leverage Multiple (For Decay Math)", minval = 1.0, maxval = 5.0, step = 0.5)
erThresh = input.float(0.20, "Choppy Market Throttle Threshold", minval = 0.05, maxval = 0.50, step = 0.05)
// ── Realized Volatility & Theoretical Leverage Drag ─────────────────────────
logRet = math.log(close / close[1])
annVol = ta.stdev(logRet, volLen) * math.sqrt(252.0) // decimal
annVolPct = annVol * 100.0
// Approximate annualized variance drain for k-times daily leverage: -0.5 * k * (k - 1) * sigma^2
decayDragPct = -0.5 * levMult * (levMult - 1.0) * math.pow(annVol / levMult, 2) * 100.0
// ── Kaufman Efficiency Ratio (ER20) ─────────────────────────────────────────
netChange = math.abs(close - close[erLen])
sumMoves = math.sum(math.abs(close - close[1]), erLen)
er20 = sumMoves == 0 ? 0.0 : netChange / sumMoves
isChoppy = er20 < erThresh
// ── Peak-to-Current Drawdown ────────────────────────────────────────────────
rollHigh = ta.highest(high, ddLookbk)
curDD = rollHigh == 0 ? 0.0 : ((close / rollHigh) - 1.0) * 100.0
// ── Danger Alerts ───────────────────────────────────────────────────────────
alertcondition(ta.crossunder(er20, erThresh), title = "FactorLab: Choppy Market Throttle (ER20)", message = "{{ticker}}: ER20 dropped below threshold — High whipsaw risk for 3X ETFs")
alertcondition(ta.crossunder(curDD, -25.0), title = "FactorLab: -25% Drawdown Warning", message = "{{ticker}}: Drawdown crossed -25% from rolling peak")
// ── HUD Display ─────────────────────────────────────────────────────────────
if barstate.islast
var table hud = table.new(position.bottom_right, 2, 5, bgcolor = color.new(#0B0D11, 10), border_color = #222938, border_width = 1)
table.cell(hud, 0, 0, "FACTORLAB RISK HUD", text_color = #60A5FA, text_size = size.tiny, text_halign = text.align_left)
table.cell(hud, 1, 0, isChoppy ? "CHOPPY (CAP 55%)" : "TRENDING", text_color = isChoppy ? #FBBF24 : #34D399, text_size = size.tiny, text_halign = text.align_right)
table.cell(hud, 0, 1, "20D Ann. Volatility", text_color = #94A3B8, text_size = size.tiny, text_halign = text.align_left)
table.cell(hud, 1, 1, str.tostring(annVolPct, "#.1") + "%", text_color = annVolPct > 45 ? #FB7185 : #F8FAFC, text_size = size.tiny, text_halign = text.align_right)
table.cell(hud, 0, 2, "Kaufman ER20", text_color = #94A3B8, text_size = size.tiny, text_halign = text.align_left)
table.cell(hud, 1, 2, str.tostring(er20, "#.##"), text_color = isChoppy ? #FBBF24 : #34D399, text_size = size.tiny, text_halign = text.align_right)
table.cell(hud, 0, 3, "1Y Peak Drawdown", text_color = #94A3B8, text_size = size.tiny, text_halign = text.align_left)
table.cell(hud, 1, 3, str.tostring(curDD, "#.1") + "%", text_color = curDD <= -25 ? #FB7185 : #CBD5E1, text_size = size.tiny, text_halign = text.align_right)
table.cell(hud, 0, 4, "Est. Vol Drag / Yr", text_color = #94A3B8, text_size = size.tiny, text_halign = text.align_left)
table.cell(hud, 1, 4, str.tostring(decayDragPct, "#.1") + "%", text_color = #F59E0B, text_size = size.tiny, text_halign = text.align_right)
7-Asset Live 200MA Regime Scanner
Watches SPY, QQQ, SMH, NVDA, TSLA, BTC & GLD simultaneously inside one corner table with zero repaint + instant macro flip alerts.
//@version=6
// FactorLab Research — 7-Asset Live 200-Day SMA Regime Matrix
// Free open-source template from https://factorlab-research.web.app/scripts/
// Monitors SPY, QQQ, SMH, NVDA, TSLA, BTC & GLD simultaneously on a single chart.
indicator("FactorLab 7-Asset 200MA Regime Scanner", shorttitle = "FL 7-Asset 200MA", overlay = true)
smaLen = input.int(200, "SMA Period", minval = 20, maxval = 400)
s1 = input.symbol("AMEX:SPY", "Asset 1 (S&P 500)")
s2 = input.symbol("NASDAQ:QQQ", "Asset 2 (Nasdaq-100)")
s3 = input.symbol("NASDAQ:SMH", "Asset 3 (Semiconductors)")
s4 = input.symbol("NASDAQ:NVDA", "Asset 4 (NVIDIA)")
s5 = input.symbol("NASDAQ:TSLA", "Asset 5 (Tesla)")
s6 = input.symbol("BITSTAMP:BTCUSD", "Asset 6 (Bitcoin)")
s7 = input.symbol("AMEX:GLD", "Asset 7 (Gold)")
f_dist(sym) =>
[c, m] = request.security(sym, "D", [close[1], ta.sma(close, smaLen)[1]], barmerge.gaps_off, barmerge.lookahead_off)
na(m) or m == 0 ? 0.0 : ((c / m) - 1.0) * 100.0
d1 = f_dist(s1)
d2 = f_dist(s2)
d3 = f_dist(s3)
d4 = f_dist(s4)
d5 = f_dist(s5)
d6 = f_dist(s6)
d7 = f_dist(s7)
alertcondition(ta.cross(d1, 0.0) or ta.cross(d2, 0.0), title = "FactorLab: SPY or QQQ 200MA Regime Flip", message = "Macro 200-Day SMA Regime Flip detected on SPY or QQQ!")
f_row(tbl, r, lbl, distVal) =>
bull = distVal >= 0.0
table.cell(tbl, 0, r, lbl, text_color = #F8FAFC, text_size = size.tiny, text_halign = text.align_left)
table.cell(tbl, 1, r, bull ? "BULL" : "DEF", text_color = bull ? #34D399 : #FB7185, text_size = size.tiny, text_halign = text.align_center)
table.cell(tbl, 2, r, str.tostring(distVal, "+#.1;-#.1") + "%", text_color = bull ? #34D399 : #FB7185, text_size = size.tiny, text_halign = text.align_right)
if barstate.islast
var table m = table.new(position.top_right, 3, 8, bgcolor = color.new(#0B0D11, 10), border_color = #222938, border_width = 1)
table.cell(m, 0, 0, "ASSET", text_color = #60A5FA, text_size = size.tiny, text_halign = text.align_left)
table.cell(m, 1, 0, "200MA", text_color = #60A5FA, text_size = size.tiny, text_halign = text.align_center)
table.cell(m, 2, 0, "GAP", text_color = #60A5FA, text_size = size.tiny, text_halign = text.align_right)
f_row(m, 1, "SPY", d1)
f_row(m, 2, "QQQ", d2)
f_row(m, 3, "SMH", d3)
f_row(m, 4, "NVDA", d4)
f_row(m, 5, "TSLA", d5)
f_row(m, 6, "BTC", d6)
f_row(m, 7, "GLD", d7)
Get Our Free Saturday Morning 200-Day SMA Regime Digest
All 20 leveraged ETFs & major assets — BULL vs. DEFENSIVE flips delivered straight to your inbox every weekend.
Zero spam. Unsubscribe in 1 click.