Strategy · Pine Script v6 · On the chart

Seykota Alt 36: BE + Momentum

by trustdan· code credited to danieltuckerrust

This trend-following strategy initiates long or short trades on Donchian channel breakouts.

View on GitHub

GitHub stars16on the repository
Pine versionv6current
Lines of code119
Settings28inputs
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

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

06_PF-1.338_SPY_seykota_alt36_breakeven_momentum.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 36: Breakeven Lock + Momentum Gating
// THEORY: Combine Alt 21 (breakeven lock: +28.2%) with Alt 30 (momentum gating: +23.6%)
// - Move stop to BE after 2N profit (risk protection)
// - Only add when RSI confirms momentum (quality adds)
// INSIGHT: Best protection + best add quality = optimal risk/reward
// Trade-off: Fewer adds but guaranteed breakeven on all winners
//
// BACKTEST RESULTS (SPY 2022-2025): +28.36% | Profit Factor: 1.338 | Win Rate: 50.00% | Max DD: 16.03%

//@version=6
strategy("Seykota Alt 36: BE + Momentum 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)

allowLong = input.bool(true, "Allow LONGs?"), allowShort = input.bool(true, "Allow SHORTs?")
entryLen = input.int(55, "Donchian ENTRY", minval=10), rsiLen = input.int(14, "RSI length", minval=2)
rsiLongAdd = input.float(60.0, "RSI for LONG adds", minval=50, maxval=80), rsiShortAdd = input.float(40.0, "RSI for SHORT adds", minval=20, maxval=50)
nLen = input.int(20, "N = ATR length", minval=5), stopN = input.float(2.0, "Initial stop (N)", minval=0.5, step=0.25)
breakEvenN = input.float(2.0, "Move to BE after X*N", minval=1.0, step=0.5)
trailLen = input.int(22, "Trail lookback", minval=5), trailN = input.float(3.0, "Trail (N)", minval=0.5, step=0.25)
addStepN = input.float(0.5, "Add every X*N", minval=0.25, step=0.25), maxUnits = input.int(4, "Max units", minval=1, maxval=10)
riskPct = input.float(1.0, "Risk % per unit", minval=0.1, maxval=5, step=0.1)
useTargets = input.bool(true, "Use targets?"), target1N = input.float(3.0, "T1 (N)", minval=1, step=0.5)
target2N = input.float(6.0, "T2 (N)", minval=1, step=0.5), target3N = input.float(9.0, "T3 (N)", minval=1, step=0.5)
useMarket = input.bool(false, "Use regime filter?"), marketSym = input.symbol("SPY", "Market symbol")
marketTF = input.timeframe("D", "Regime TF"), marketLen = input.int(200, "Market MA", minval=50)
minVol = input.int(0, "Min volume", minval=0), showSignals = input.bool(true, "Plot signals?"), plotDon = input.bool(true, "Plot Donchian?")
fromDate = input.time(defval=timestamp("2022-01-01T00:00:00"), title="FROM", confirm=true)
toDate = input.time(defval=timestamp("2099-12-31T23:59:59"), title="TO", confirm=true)
flatAtFrom = input.bool(true, "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")

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

inRange = (time >= fromDate) and (time <= toDate)
N = ta.atr(nLen), volMA = ta.sma(volume, 20), liqOK = volMA >= minVol
rsi = ta.rsi(close, rsiLen), longMomentum = rsi >= rsiLongAdd, shortMomentum = rsi <= rsiShortAdd
donHi = ta.highest(high, entryLen), donLo = ta.lowest(low, entryLen)
donHiPrev = donHi[1], donLoPrev = donLo[1]
trailHighest = ta.highest(high, trailLen), trailLowest = ta.lowest(low, trailLen)
mClose = request.security(marketSym, marketTF, close), mMA = request.security(marketSym, marketTF, ta.sma(close, marketLen))
longRegOK = not useMarket or (mClose > mMA), shortRegOK = not useMarket or (mClose < mMA)
longBreak = allowLong and liqOK and longRegOK and (close > donHiPrev)
shortBreak = allowShort and liqOK and shortRegOK and (close < donLoPrev)

var float N_entry = na, var float entryPrice = na, var float lastAddLong = na, var float lastAddShort = na
var int units = 0, var bool t1 = false, var bool t2 = false, var bool t3 = false, var bool beLocked = false
inPos = strategy.position_size != 0, inLong = strategy.position_size > 0, inShort = strategy.position_size < 0
if not inPos
    units := 0, lastAddLong := na, lastAddShort := na, N_entry := na, entryPrice := na, t1 := false, t2 := false, t3 := false, beLocked := false

if inRange and not inPos and longBreak
    N_entry := N, entryPrice := close, strategy.entry("L", strategy.long, qty=sharesForUnit(strategy.equity, N_entry))
    units := 1, lastAddLong := close
if inRange and not inPos and shortBreak
    N_entry := N, entryPrice := close, strategy.entry("S", strategy.short, qty=sharesForUnit(strategy.equity, N_entry))
    units := 1, lastAddShort := close

if inRange and inLong and units < maxUnits and high >= nz(lastAddLong) + addStepN * N_entry and longMomentum
    strategy.entry("L", strategy.long, qty=sharesForUnit(strategy.equity, N_entry)), units += 1, lastAddLong := close
if inRange and inShort and units < maxUnits and low <= nz(lastAddShort) - addStepN * N_entry and shortMomentum
    strategy.entry("S", strategy.short, qty=sharesForUnit(strategy.equity, N_entry)), units += 1, lastAddShort := close

if useTargets and inLong and not na(entryPrice) and not na(N_entry)
    if not t1 and high >= entryPrice + target1N * N_entry
        strategy.close("L", qty=sharesForUnit(strategy.equity, N_entry), comment="T1"), t1 := true
    if t1 and not t2 and high >= entryPrice + target2N * N_entry
        strategy.close("L", qty=sharesForUnit(strategy.equity, N_entry), comment="T2"), t2 := true
    if t2 and not t3 and high >= entryPrice + target3N * N_entry
        strategy.close("L", qty=sharesForUnit(strategy.equity, N_entry), comment="T3"), t3 := true
if useTargets and inShort and not na(entryPrice) and not na(N_entry)
    if not t1 and low <= entryPrice - target1N * N_entry
        strategy.close("S", qty=sharesForUnit(strategy.equity, N_entry), comment="T1"), t1 := true
    if t1 and not t2 and low <= entryPrice - target2N * N_entry
        strategy.close("S", qty=sharesForUnit(strategy.equity, N_entry), comment="T2"), t2 := true
    if t2 and not t3 and low <= entryPrice - target3N * N_entry
        strategy.close("S", qty=sharesForUnit(strategy.equity, N_entry), comment="T3"), t3 := true

var float stopL = na, var float stopS = na
if inRange and inLong
    initStopL = entryPrice - stopN * N_entry
    chandelierL = trailHighest - trailN * N_entry
    profitL = close - entryPrice
    if not beLocked and profitL >= breakEvenN * N_entry
        beLocked := true
    stopL := beLocked ? math.round_to_mintick(math.max(entryPrice, chandelierL)) : math.round_to_mintick(math.max(initStopL, chandelierL))
    strategy.exit("L-EXIT", from_entry="L", stop=stopL)
else
    stopL := na
if inRange and inShort
    initStopS = entryPrice + stopN * N_entry
    chandelierS = trailLowest + trailN * N_entry
    profitS = entryPrice - close
    if not beLocked and profitS >= breakEvenN * N_entry
        beLocked := true
    stopS := beLocked ? math.round_to_mintick(math.min(entryPrice, chandelierS)) : math.round_to_mintick(math.min(initStopS, chandelierS))
    strategy.exit("S-EXIT", from_entry="S", stop=stopS)
else
    stopS := na

plot(plotDon ? donHi : na, "DonHi", color=color.new(color.blue, 40))
plot(plotDon ? donLo : na, "DonLo", color=color.new(color.blue, 40))
plotshape(showSignals and longBreak, title="L", style=shape.triangleup, location=location.belowbar, size=size.tiny, color=color.new(color.green, 0))
plotshape(showSignals and shortBreak, title="S", style=shape.triangledown, location=location.abovebar, size=size.tiny, color=color.new(color.red, 0))
stopColor = beLocked ? color.green : color.red
plot(showSignals and inLong ? stopL : na, "Stop", color=color.new(stopColor, 0), style=plot.style_linebr, linewidth=2)
plot(showSignals and inShort ? stopS : na, "Stop", color=color.new(stopColor, 0), style=plot.style_linebr, linewidth=2)

Overview

Position sizing is calculated from an Average True Range (ATR) volatility model. The script scales into winning trades up to a user-defined unit limit only when RSI confirms momentum direction. Stop losses dynamically shift to breakeven once price advances by a target ATR multiple, with trailing stops and optional three-stage profit targets securing gains. An optional higher-timeframe moving average serves as an overall market regime filter. Plots overlay directly 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

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 ENTRYint55entryLen
RSI lengthint14rsiLen
RSI for LONG addsfloat60.0rsiLongAdd
RSI for SHORT addsfloat40.0rsiShortAdd
N = ATR lengthint20nLen
Initial stop (N)float2.0stopN
Move to BE after X*Nfloat2.0breakEvenN
Trail lookbackint22trailLen
Trail (N)float3.0trailN
Add every X*Nfloat0.5addStepN
Show all 28 settings
SettingTypeDefaultVariable
Max unitsint4maxUnits
Risk % per unitfloat1.0riskPct
Use targets?booltrueuseTargets
T1 (N)float3.0target1N
T2 (N)float6.0target2N
T3 (N)float9.0target3N
Use regime filter?boolfalseuseMarket
Market symbolsymbolSPYmarketSym
Regime TFtimeframeDmarketTF
Market MAint200marketLen
Min volumeint0minVol
Plot signals?booltrueshowSignals
Plot Donchian?booltrueplotDon
FROMtimetimestamp("2022-01-01T00:00:00")fromDate
TOtimetimestamp("2099-12-31T23:59:59")toDate
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

53 stars

This strategy enters long or short on the price chart when price is on the correct side of a 200-period EMA, MACD agrees, RSI is not at the opposing extreme, ADX exceeds a threshold, volume beats a 20-bar average, and close breaks the recent high or low. A tim

Strategy v6 TrendOscillators & momentum
Updated 2 months ago · MPL-2.0 GitHub repository of BTCUSD 5M Optimized Strategy

ADA4H Optuna Strategy

by skywalker0803r
42 stars

This strategy trades longs and shorts on the price chart. Long entries require a close above the prior Donchian high, a close above a long-term SMA, and RSI above a threshold, with size set as a percentage of equity. Long exits use a stop at the prior Donchian

Strategy v5 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of ADA4H Optuna Strategy
7 stars

This strategy plots dynamic pivot-based support and resistance trendlines on the price chart and trades breakouts beyond those lines. By default it allows longs only when EMA 50 is above EMA 200 and an RSI-SMA momentum filter is met, with an ATR entry buffer.

Strategy v6 TrendBreakout
Updated 12 months ago · None stated GitHub repository of Easy Trendline - XAUUSD 30m

Q-Trend + KVO Strategy

by TradersPost
218 stars

This strategy combines the ATR-banded Q-Trend indicator with the Klinger Volume Oscillator (KVO) to execute confluence trades. It enters long when a Q-Trend buy trigger aligns with a positive KVO value and a bullish candle confirmation, taking short trades und

Strategy v6 TrendVolume
Updated 3 months ago · None stated GitHub repository of Q-Trend + KVO Strategy

This trend-following strategy executes a classic Donchian channel breakout system designed to cross-validate backtest results against external framework benchmarks. It triggers a long entry when the closing price breaks above the highest high of the prior entr

Strategy v6 BreakoutTrend
Updated today · AGPL-3.0 GitHub repository of Forven S01509 - Simple Donchian 20/10 Breakout
330 stars

2 EMA/SMA + RSI Strategy [Alorse] combines three configurable moving averages with a short-period Relative Strength Index filter. It plots fast, slow, and trend moving averages on the price chart. Long entries trigger when the fast moving average is above the

Strategy v4 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of 2 EMA/SMA + RSI Strategy [Alorse]

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