Strategy · Pine Script v6 · On the chart

Seykota / Turtle Core

by trustdan· code credited to danieltuckerrust

This strategy trades Donchian channel breakouts on the price chart.

View on GitHub

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

178 lines of Pine Script v6 from sectors/basic-materials-FCX/strategies/turtle-core-v2.2.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

turtle-core-v2.2.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

//@version=6
strategy("Seykota / Turtle Core v2.2 (compiler-safe)",
     overlay=true,
     initial_capital=100000,
     commission_type=strategy.commission.percent, commission_value=0.005,
     slippage=0,                               // <-- Must be CONST. Adjust in Properties UI.
     default_qty_type=strategy.fixed,
     pyramiding=10,
     max_bars_back=5000)

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

entryLen    = input.int(55,     "Donchian ENTRY lookback", minval=10)
exitLen     = input.int(10,     "Donchian EXIT lookback",  minval=5)
nLen        = input.int(20,     "N = ATR length",          minval=5)
stopN       = input.float(2.0,  "Initial stop (in N)",     minval=0.25, step=0.25)
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)

useMarket   = input.bool(false, "Use market regime filter?")
marketSym   = input.symbol("SPY", "Market symbol for regime (if used)")
marketTF    = input.timeframe("D", "Regime timeframe (if used)")
marketLen   = input.int(200,    "Market MA length (if used)", minval=50)
marketMA    = input.string("SMA", "Regime MA type", options=["SMA","EMA","WMA"])

timeExitMode= input.string("None","Time exit", options=["None","Close","Roll"])
timeExitBars= input.int(60,     "Time exit bars (if used)", minval=5)

minVol      = input.int(0,      "Min 20-bar avg volume (chart TF)", minval=0)
useIntrabarAdds = input.bool(false, "Add-ons use intrabar extremes (High/Low)?")
useATRGuard = input.bool(false, "Block add-ons when ATR > SMA(ATR, nLen)?")

showSignals = input.bool(true,  "Plot signals & stops?")
plotDon     = input.bool(true,  "Plot Donchian bands?")

// === Date-range filter (local chart timezone)
fromDate = input.time(defval=timestamp("2022-01-01T00:00:00"), title="Backtest FROM (yyyy-mm-dd)", confirm=true)
toDate   = input.time(defval=timestamp("2099-12-31T23:59:59"), title="Backtest TO (yyyy-mm-dd)",   confirm=true)
flatAtFrom = input.bool(true, "Force FLAT at range start?")
isFirstBar = na(time[1])
isRangeStart = (not isFirstBar) and (time[1] < fromDate) and (time >= fromDate)
if flatAtFrom and isRangeStart and strategy.position_size != 0
    strategy.close_all(comment="Flat at FROM")

//================ Persistent state
var float N_entry     = na
var float lastAddLong = na
var float lastAddShort= na
var int   units       = 0
var int   barsInPos   = 0
if isRangeStart
    N_entry       := na
    lastAddLong   := na
    lastAddShort  := na
    units         := 0
    barsInPos     := 0

//================ Core calcs (chart timeframe)
N        = ta.atr(nLen)
atrSmaN  = ta.sma(N, nLen)                  // <-- precomputed (no conditional calls)
volMA    = ta.sma(volume, 20)
liqOK    = volMA >= minVol

// Donchian levels
donHi    = ta.highest(high, entryLen)
donLo    = ta.lowest(low,  entryLen)
donHiPrev= donHi[1]
donLoPrev= donLo[1]
exitHiPrev = ta.highest(high, exitLen)[1]
exitLoPrev = ta.lowest(low,  exitLen)[1]

// Market regime (compute ALL MAs; pick later to avoid conditional TA calls)
mClose = request.security(marketSym, marketTF, close)
mSMA   = request.security(marketSym, marketTF, ta.sma(close, marketLen))
mEMA   = request.security(marketSym, marketTF, ta.ema(close, marketLen))
mWMA   = request.security(marketSym, marketTF, ta.wma(close, marketLen))
mMA = switch marketMA
    "EMA" => mEMA
    "WMA" => mWMA
    => mSMA

longRegOK  = not useMarket or (mClose > mMA)
shortRegOK = not useMarket or (mClose < mMA)

//================ Signals (no lookahead)
longBreak  = allowLong  and liqOK and longRegOK  and (close > donHiPrev)
shortBreak = allowShort and liqOK and shortRegOK and (close < donLoPrev)

//================ Position state
inPos   = strategy.position_size != 0
inLong  = strategy.position_size > 0
inShort = strategy.position_size < 0
barsInPos := inPos ? (barsInPos + 1) : 0

//================ Helpers
sharesForUnit(_equity, _Nentry) =>
    riskDollars   = _equity * (riskPct/100.0)
    perShareRisk  = math.max(stopN * _Nentry, syminfo.mintick)
    math.max(1, math.floor(riskDollars / perShareRisk))

//================ Entries
inRange = (time >= fromDate) and (time <= toDate)

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

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

//================ Add-on logic (every addStepN * N_entry)
priceForLongAdd  = useIntrabarAdds ? high : close
priceForShortAdd = useIntrabarAdds ? low  : close
atrGuardOK       = not useATRGuard or (N <= atrSmaN)

canAddLong  = inRange and inLong  and atrGuardOK and (units < maxUnits) and (priceForLongAdd  >= nz(lastAddLong)  + addStepN * N_entry)
canAddShort = inRange and inShort and atrGuardOK and (units < maxUnits) and (priceForShortAdd <= nz(lastAddShort) - addStepN * N_entry)

if canAddLong
    sh = sharesForUnit(strategy.equity, N_entry)
    strategy.entry("L", strategy.long, qty=sh)
    units       += 1
    lastAddLong := close

if canAddShort
    sh = sharesForUnit(strategy.equity, N_entry)
    strategy.entry("S", strategy.short, qty=sh)
    units        += 1
    lastAddShort := close

//================ Protective & Donchian exits — tick-rounded
var float stopL = na
var float stopS = na

if inRange and inLong
    initStopL = strategy.position_avg_price - stopN * N_entry
    stopLraw  = math.max(initStopL, exitLoPrev)
    stopL     := math.round_to_mintick(stopLraw)
    strategy.exit("L-EXIT", from_entry="L", stop=stopL)
else
    stopL := na

if inRange and inShort
    initStopS = strategy.position_avg_price + stopN * N_entry
    stopSraw  = math.min(initStopS, exitHiPrev)
    stopS     := math.round_to_mintick(stopSraw)
    strategy.exit("S-EXIT", from_entry="S", stop=stopS)
else
    stopS := na

//================ Optional time exit
timeExitTrig = inRange and inPos and (timeExitMode != "None") and (barsInPos >= timeExitBars)
if timeExitTrig
    if inLong
        strategy.close("L", comment=timeExitMode == "Roll" ? "Time Exit (Roll)" : "Time Exit")
        units := 0
    if inShort
        strategy.close("S", comment=timeExitMode == "Roll" ? "Time Exit (Roll)" : "Time Exit")
        units := 0

//================ Plots
plot(plotDon ? donHi : na, "Donchian High", color=color.new(color.blue, 40))
plot(plotDon ? donLo : na, "Donchian Low",  color=color.new(color.blue, 40))
plot(showSignals and inLong  ? stopL : na, "Long Stop",  color=color.new(color.red,  0), style=plot.style_linebr)
plot(showSignals and inShort ? stopS : na, "Short Stop", color=color.new(color.red,  0), style=plot.style_linebr)

Overview

Seykota / Turtle Core enters long or short when price breaks the prior entry channel, sizes each unit from a percentage of equity and ATR, and adds units at fixed ATR steps up to a set maximum. It exits on the opposite Donchian channel and places an initial ATR stop. Optional filters cover market trend, volume, ATR, and a time exit. By default the time exit is off.

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, Volatility & bands

Markets

Stocks

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
Donchian EXIT lookbackint10exitLen
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
Use market regime filter?boolfalseuseMarket
Market symbol for regime (if used)symbolSPYmarketSym
Regime timeframe (if used)timeframeDmarketTF
Show all 24 settings
SettingTypeDefaultVariable
Market MA length (if used)int200marketLen
Regime MA typestringSMAmarketMA
Time exitstringNonetimeExitMode
Time exit bars (if used)int60timeExitBars
Min 20-bar avg volume (chart TF)int0minVol
Add-ons use intrabar extremes (High/Low)?boolfalseuseIntrabarAdds
Block add-ons when ATR > SMA(ATR, nLen)?boolfalseuseATRGuard
Plot signals & stops?booltrueshowSignals
Plot Donchian bands?booltrueplotDon
Backtest FROM (yyyy-mm-dd)timetimestamp("2022-01-01T00:00:00")fromDate
Backtest TO (yyyy-mm-dd)timetimestamp("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 strategy runs on the price chart and enters long when a fast simple moving average crosses above a slow one, and short on the opposite cross, while closing the other side. Entries also need ATR above a filter, a pullback check, a minimum candle-body chang

Strategy v5 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of 9:22 5 MIN 15 MIN BANKNIFTY

均线突破交易系统

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 均线突破交易系统

This strategy executes breakout trades using two Donchian Channels alongside an optional 200-period EMA trend filter. Entries trigger when the price sets a new highest high or lowest low over the primary Donchian Channel period. Exits are managed via a shorter

Strategy v5 BreakoutTrend
Updated 1 year ago · None stated GitHub repository of Ruckard Walcrypto DoubleDonchian B002

Breakout Strategy

by JoelPasapera
11 stars

This strategy implements a channel breakout system directly on the main price chart. It calculates highest highs and lowest lows over configurable entry and exit periods using historical candles. A long entry triggers when the close breaks above the entry chan

Strategy v6 BreakoutTrend
Updated 5 months ago · Apache-2.0 GitHub repository of Breakout Strategy

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