Strategy · Pine Script v6 · On the chart

Seykota Alt 42: Momentum-Gated Time

by trustdan· code credited to danieltuckerrust

This strategy enters long or short on a Donchian channel break and sizes each unit by risking a percentage of equity against an ATR stop.

View on GitHub

GitHub stars16on the repository
Pine versionv6current
Lines of code242
Settings23inputs
LicenceNone statedfile: MPL-2.0
Last update11 months ago3 Nov 2025

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

242 lines of Pine Script v6 from pine-scripts/seykota_alt42_momentum_gated_time.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

seykota_alt42_momentum_gated_time.pine
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © danieltuckerrust
// ALTERNATIVE 42: Momentum-Gated Time Exit Strategy
// THEORY: Alt9's rigid 40-bar time exit works brilliantly on individual stocks (AMZN +37.7%,
// WMT +30.4%, UNH +35.9%) but CATASTROPHICALLY fails on slow-grinding ETFs (SPY -24.0%!).
// The problem: Some trends need MORE than 40 bars (ETFs), others should exit SOONER (momentum fades).
//
// INNOVATION: Three-tiered exit system that adapts to momentum:
// 1. EARLY EXIT: If momentum fades (RSI crosses 50) → exit immediately (could be bar 5, 10, 20...)
// 2. STANDARD EXIT: At 40 bars (Alt9 baseline) → exit unless momentum is exceptionally strong
// 3. EXTENDED EXIT: Up to 60 bars IF at bar 40, momentum is very strong (RSI >65 longs, <35 shorts)
//
// INSIGHT FROM 210 BACKTESTS: Alt9's time discipline is CORRECT (prevents overstaying), but needs
// flexibility for different asset behaviors. Individual stocks explode quickly (need 40-bar limit),
// ETFs grind slowly (need extended time if momentum intact), choppy assets reverse fast (need early exit).
//
// WHY THIS SOLVES KEY PROBLEMS:
// - SPY/QQQ ETFs: Can hold beyond 40 bars if momentum strong → avoid premature exits on slow grinders
// - Individual stocks (AMZN, MSFT): Still exit at 40 bars → maintain discipline on explosive moves
// - Choppy assets: Exit immediately when RSI crosses 50 → avoid holding through reversals
// - Aging positions: Force exit at 60 bars maximum → prevent capital tie-up
//
// Trade-off: More complex than Alt9's simple 40-bar rule, but theoretically superior - adapts to momentum.
// For options traders: Exits range 5-60 bars (1-12 weeks), still suitable for options expiration cycles.

//@version=6
strategy("Seykota Alt 42: Momentum-Gated Time v1.0",
     overlay=true,
     initial_capital=100000,
     commission_type=strategy.commission.percent, commission_value=0.005,
     slippage=2,
     default_qty_type=strategy.fixed, default_qty_value=0,
     pyramiding=10,
     max_bars_back=5000)

//================ Inputs
allowLong   = input.bool(true,  "Allow LONGs?")
allowShort  = input.bool(true,  "Allow SHORTs?")

// Entry
entryLen    = input.int(55,     "Donchian ENTRY lookback",            minval=10)
nLen        = input.int(20,     "N = ATR length",                     minval=5)
stopN       = input.float(2.0,  "Initial stop (in N)",                minval=0.5, step=0.25)

// Pyramiding
addStepN    = input.float(0.5,  "Add every X * N",                    minval=0.25, step=0.25)
maxUnits    = input.int(4,      "Max units (incl. initial)",          minval=1, maxval=10)
riskPct     = input.float(1.0,  "Risk % of equity PER UNIT",          minval=0.1, maxval=5, step=0.1)

// TIME EXIT PARAMETERS (KEY INNOVATION)
standardExitBars = input.int(40,  "Standard time exit (bars)",        minval=20, maxval=100)
maxExitBars      = input.int(60,  "Maximum hold time (bars)",         minval=40, maxval=120)
rsiLen           = input.int(14,  "RSI length for momentum",          minval=5)
rsiMomentumFade  = input.float(50.0, "RSI fade threshold (early exit)", minval=40, maxval=60, step=1)
rsiStrongMomentum = input.float(65.0, "RSI strong threshold (extend hold)", minval=60, maxval=80, step=1)

// Optional market filter
useMarket   = input.bool(false, "Use market regime filter?")
marketSym   = input.symbol("SPY", "Market symbol for regime")
marketTF    = input.timeframe("D", "Regime timeframe")
marketLen   = input.int(200,    "Market MA length",                   minval=50)

minVol      = input.int(0,      "Min 20-bar avg volume",              minval=0)
showSignals = input.bool(true,  "Plot signals & stops?")
plotDon     = input.bool(true,  "Plot Donchian bands?")

// Backtest range
fromDate = input.time(defval=timestamp("2010-01-01T00:00:00"), title="Backtest FROM")
toDate   = input.time(defval=timestamp("2099-12-31T23:59:59"), title="Backtest TO")
flatAtFrom = input.bool(true, "Force FLAT at range start?")

// Force flat at range start
isRangeStart = (time[1] < fromDate) and (time >= fromDate)
if flatAtFrom and isRangeStart and strategy.position_size != 0
    strategy.close_all(comment="Flat at FROM")

//================ Core Calculations
inRange = (time >= fromDate) and (time <= toDate)
N = ta.atr(nLen)

// Donchian channels
donUp = ta.highest(high, entryLen)
donDn = ta.lowest(low, entryLen)

// RSI for momentum detection
rsiValue = ta.rsi(close, rsiLen)

// Market regime filter
var float mktPrice = na
var float mktMA = na
if useMarket
    mktPrice := request.security(marketSym, marketTF, close)
    mktMA := request.security(marketSym, marketTF, ta.sma(close, marketLen))

mktLongOK = (not useMarket) or (mktPrice > mktMA)
mktShortOK = (not useMarket) or (mktPrice < mktMA)

// Volume filter
avgVol = ta.sma(volume, 20)
volOK = (minVol == 0) or (avgVol >= minVol)

//================ Position Tracking
var int unitCount = 0
var float entryPrice = na
var int barsInPosition = 0  // KEY: Track how long we've been in position

// Track bars in position
if strategy.position_size != 0
    barsInPosition += 1
else
    barsInPosition := 0

//================ Position Sizing
sharesForUnit(_e, _n) =>
    r = _e * (riskPct / 100.0)
    psr = math.max(stopN * _n, syminfo.mintick)
    math.max(1, math.floor(r / psr))

//================ Entry Logic

// Long entry
longEntry = allowLong and inRange and mktLongOK and volOK and (close > donUp[1])
if longEntry and strategy.position_size == 0
    entryPrice := close
    qty = sharesForUnit(strategy.equity, N)
    strategy.entry("L1", strategy.long, qty=qty, comment="L1")
    unitCount := 1
    barsInPosition := 0

// Short entry
shortEntry = allowShort and inRange and mktShortOK and volOK and (close < donDn[1])
if shortEntry and strategy.position_size == 0
    entryPrice := close
    qty = sharesForUnit(strategy.equity, N)
    strategy.entry("S1", strategy.short, qty=qty, comment="S1")
    unitCount := 1
    barsInPosition := 0

//================ Pyramiding
isLong = strategy.position_size > 0
isShort = strategy.position_size < 0

if isLong and unitCount < maxUnits
    addPrice = entryPrice + (unitCount * addStepN * N)
    if close >= addPrice
        unitCount += 1
        qty = sharesForUnit(strategy.equity, N)
        label = "L" + str.tostring(unitCount)
        strategy.entry(label, strategy.long, qty=qty, comment=label)

if isShort and unitCount < maxUnits
    addPrice = entryPrice - (unitCount * addStepN * N)
    if close <= addPrice
        unitCount += 1
        qty = sharesForUnit(strategy.equity, N)
        label = "S" + str.tostring(unitCount)
        strategy.entry(label, strategy.short, qty=qty, comment=label)

//================ THREE-TIERED EXIT LOGIC (KEY INNOVATION)

// Calculate momentum crosses globally (must run on every bar for consistency)
rsiCrossUnder = ta.crossunder(rsiValue, rsiMomentumFade)
rsiCrossOver = ta.crossover(rsiValue, rsiMomentumFade)

// 1. EARLY EXIT: Momentum fades (RSI crosses 50)
momentumFadedLong = isLong and rsiCrossUnder
momentumFadedShort = isShort and rsiCrossOver

if momentumFadedLong
    strategy.close_all(comment="Momentum Fade L")

if momentumFadedShort
    strategy.close_all(comment="Momentum Fade S")

// 2. STANDARD EXIT: 40 bars reached (unless momentum exceptionally strong)
reachedStandardTime = barsInPosition >= standardExitBars

// Check if momentum is strong enough to extend beyond standard time
strongMomentumLong = isLong and rsiValue > rsiStrongMomentum
strongMomentumShort = isShort and rsiValue < (100 - rsiStrongMomentum)

// Exit at standard time UNLESS momentum is exceptionally strong
if reachedStandardTime and isLong and not strongMomentumLong
    strategy.close_all(comment="Time Exit L")

if reachedStandardTime and isShort and not strongMomentumShort
    strategy.close_all(comment="Time Exit S")

// 3. EXTENDED EXIT: Maximum 60 bars (forced exit regardless of momentum)
reachedMaxTime = barsInPosition >= maxExitBars

if reachedMaxTime and isLong
    strategy.close_all(comment="Max Time L")

if reachedMaxTime and isShort
    strategy.close_all(comment="Max Time S")

// Initial stop loss (only triggered if position moves against us before any exit)
if isLong
    stopPrice = entryPrice - (stopN * N)
    for i = 1 to maxUnits
        strategy.exit("Stop-L" + str.tostring(i), from_entry="L" + str.tostring(i), stop=stopPrice, comment="Stop-L")

if isShort
    stopPrice = entryPrice + (stopN * N)
    for i = 1 to maxUnits
        strategy.exit("Stop-S" + str.tostring(i), from_entry="S" + str.tostring(i), stop=stopPrice, comment="Stop-S")

// Reset unit count when flat
if strategy.position_size == 0
    unitCount := 0
    entryPrice := na

//================ Plotting
plot(plotDon ? donUp : na, "Don Upper", color=color.green, linewidth=1)
plot(plotDon ? donDn : na, "Don Lower", color=color.red, linewidth=1)

// Plot RSI momentum zones
hline(rsiMomentumFade, "RSI Fade (50)", color=color.orange, linestyle=hline.style_dotted)
hline(rsiStrongMomentum, "RSI Strong (65)", color=color.green, linestyle=hline.style_dotted)
hline(100 - rsiStrongMomentum, "RSI Strong Short (35)", color=color.red, linestyle=hline.style_dotted)

// Show position age on chart
var label barsLabel = na
if barstate.islast and showSignals and strategy.position_size != 0
    label.delete(barsLabel)
    barsText = "Bars: " + str.tostring(barsInPosition) + "/" + str.tostring(standardExitBars)
    barsColor = barsInPosition >= standardExitBars ? color.orange : color.blue
    barsLabel := label.new(bar_index, high, barsText, color=barsColor, textcolor=color.white, style=label.style_label_down)

// Show momentum regime
var label momentumLabel = na
if barstate.islast and showSignals and strategy.position_size != 0
    label.delete(momentumLabel)
    momentumText = "RSI: " + str.tostring(rsiValue, "#.#")
    momentumColor = isLong ? (rsiValue > rsiStrongMomentum ? color.green : (rsiValue < rsiMomentumFade ? color.red : color.gray)) :
                             (rsiValue < (100 - rsiStrongMomentum) ? color.green : (rsiValue > rsiMomentumFade ? color.red : color.gray))
    momentumLabel := label.new(bar_index, low, momentumText, color=momentumColor, textcolor=color.white, style=label.style_label_up)

// Background color showing exit zones
exitZoneColor = barsInPosition >= maxExitBars ? color.new(color.red, 85) : barsInPosition >= standardExitBars ? color.new(color.orange, 90) : color.new(color.blue, 95)
bgcolor(exitZoneColor, title="Exit Zone: Red=Force Exit, Orange=Standard, Blue=Early")

Overview

Seykota Alt 42: Momentum-Gated Time adds units as price advances by a fraction of ATR, up to a set maximum. A three-tier time exit uses RSI: fading momentum closes early, a standard bar count closes unless RSI is strong, and a maximum hold forces an exit. An optional moving-average regime filter on another symbol can block trades. Signals, stops, and Donchian bands draw on the price chart.

Details

Published by

trustdan on GitHub

Code credit

danieltuckerrust, as written in the script's header

Type

Strategy, Pine Script v6

Draws

On the price chart

Category

Trend, Breakout, Oscillators & momentum, Volatility & bands

Markets

Stocks, Indices

Timeframes

Any timeframe

Risk Notes

  • The strategy can add several entries in the same direction

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Allow LONGs?booltrueallowLong
Allow SHORTs?booltrueallowShort
Donchian ENTRY lookbackint55entryLen
N = ATR lengthint20nLen
Initial stop (in N)float2.0stopN
Add every X * Nfloat0.5addStepN
Max units (incl. initial)int4maxUnits
Risk % of equity PER UNITfloat1.0riskPct
Standard time exit (bars)int40standardExitBars
Maximum hold time (bars)int60maxExitBars
RSI length for momentumint14rsiLen
RSI fade threshold (early exit)float50.0rsiMomentumFade
Show all 23 settings
SettingTypeDefaultVariable
RSI strong threshold (extend hold)float65.0rsiStrongMomentum
Use market regime filter?boolfalseuseMarket
Market symbol for regimesymbolSPYmarketSym
Regime timeframetimeframeDmarketTF
Market MA lengthint200marketLen
Min 20-bar avg volumeint0minVol
Plot signals & stops?booltrueshowSignals
Plot Donchian bands?booltrueplotDon
Backtest FROMtimetimestamp("2010-01-01T00:00:00")fromDate
Backtest TOtimetimestamp("2099-12-31T23:59:59")toDate
Force FLAT at range start?booltrueflatAtFrom
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

257 stars

This strategy plots VWMA-based Bollinger bands on the 1-minute ES futures price chart for volatility analysis and potential breakout identification. It calculates dynamic long and short stop loss and take profit levels using percentage inputs relative to entry

Strategy v6 Volatility & bandsSupport & resistance
Updated 6 weeks ago · None stated GitHub repository of V1.6 ES - Futures 1M No Repaint
106 stars

This trading strategy combines three Exponential Moving Averages (8, 14, and 50 periods by default) with Stochastic RSI and Average True Range (ATR). It enters a long position when EMA 1 is above EMA 2, EMA 2 is above EMA 3, and the Stochastic RSI %K line cros

Strategy v4 TrendOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 3 x EMA + Stochastic RSI + ATR

均线突破交易系统

by tangly1024
76 stars

This strategy trades long breakouts of a fast and a slow moving average on the price chart. It opens a long position when price crosses above either average, using a percentage of equity as size. It reduces the position by a set share when the high reaches the

Strategy v5 TrendMoving averages
Updated 4 months ago · None stated GitHub repository of 均线突破交易系统

Momentum Rider DAILY FINAL

by greenpeas007
58 stars

This strategy combines exponential moving averages (EMA 9, 21, 50), RSI, MACD, and Supertrend indicators to enter long positions. It triggers trades using six dynamic setups based on momentum breakouts, EMA crossovers, and pullbacks. Position sizing is compute

Strategy v6 TrendOscillators & momentum Repaints
Updated 4 months ago · Custom licence GitHub repository of Momentum Rider DAILY FINAL
57 stars

This strategy identifies trend reversals through market structure changes (CHoCH) filtered by MACD momentum. It initiates long positions on bullish structure breaks when MACD is positive and above its signal line, and short positions on bearish breaks when MAC

Strategy v6 SMC / ICT / price actionOscillators & momentum Repaints
Updated 1 year ago · Apache-2.0 GitHub repository of Market Structure MACD 4H + Trailing

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers