Skip to content

Instantly share code, notes, and snippets.

@bouroo
Last active June 15, 2025 13:55
Show Gist options
  • Select an option

  • Save bouroo/4b65982cc85f76a4fbd6cf189519e896 to your computer and use it in GitHub Desktop.

Select an option

Save bouroo/4b65982cc85f76a4fbd6cf189519e896 to your computer and use it in GitHub Desktop.
Turtle Trading Strategy with Supertrend Confirmation
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bouroo
//@version=5
strategy("Turtle Trading Strategy with Supertrend Confirmation", overlay=true, pyramiding=0) // Pyramiding set to 0 to prevent multiple entries for the same direction
// --- Strategy Parameters ---
// Turtle Trading Parameters
length1 = input.int(20, title="Donchian Channel Length (Breakout)", minval=1)
length2 = input.int(10, title="Donchian Channel Length (Exit)", minval=1)
risk_pct = input.float(2.0, title="Risk Percentage per Trade", minval=0.1, maxval=100.0)
atr_length = input.int(20, title="ATR Length", minval=1)
capital = input.float(1000.0, title="Initial Capital", minval=1.0) // Ensure capital is float and positive
// Supertrend Parameters
supertrend_length = input.int(10, title="Supertrend Length", minval=1)
supertrend_multiplier = input.float(3.0, title="Supertrend Multiplier", minval=0.1)
// --- Indicator Calculations ---
// Calculate the Donchian Channels
// Using `[1]` to get the value from the previous bar, as is standard for breakout systems.
float upperBreakout = ta.highest(high, length1)[1]
float lowerBreakout = ta.lowest(low, length1)[1]
float upperExit = ta.highest(high, length2)[1]
float lowerExit = ta.lowest(low, length2)[1]
// Calculate ATR
float atr = ta.atr(atr_length)
// Position sizing based on ATR and risk percentage
// Ensure ATR is positive to avoid division by zero or NaN results.
float calculated_unit_size = (capital * (risk_pct / 100)) / atr
// Ensure unit_size is a positive integer, minimum 1.
int unit_size = atr > 0 ? math.max(1, math.round(calculated_unit_size)) : 0
// Supertrend calculation (original logic preserved)
float supertrendFactor = supertrend_multiplier * ta.atr(supertrend_length)
float supertrendUpper = (high + low) / 2 + supertrendFactor
float supertrendLower = (high + low) / 2 - supertrendFactor
var float supertrend = 0.0 // `var` keyword ensures the variable retains its value across bars
if (close > supertrend[1])
supertrend := supertrendLower
else
supertrend := supertrendUpper
// --- Entry and Exit Conditions ---
// Long Entry: Close crosses above upper Donchian breakout channel AND close is above Supertrend
bool longEntry = ta.crossover(close, upperBreakout) and close > supertrend
// Long Exit: Close crosses below lower Donchian exit channel OR close is below Supertrend
bool longExit = ta.crossunder(close, lowerExit) or close < supertrend
// Note: Short entry/exit conditions and their corresponding strategy calls were commented out in the original code.
// They are removed here to reflect the active functionality and reduce unnecessary computation.
// If bidirectional trading is desired, uncomment and re-enable the short logic.
// --- Trade Execution ---
// Execute Long trades
if (longEntry)
// Only enter if not already in a long position to prevent pyramiding beyond 1 unit
if (strategy.position_size == 0)
strategy.entry("Long", strategy.long, qty=unit_size)
// Execute Long exit
if (longExit)
// Only close if currently in a long position
if (strategy.position_size > 0)
strategy.close("Long")
// --- Plotting ---
// Plotting the Donchian Channels
plot(upperBreakout, color=color.new(color.green, 0), title="Upper Breakout Channel")
plot(lowerBreakout, color=color.new(color.red, 0), title="Lower Breakout Channel")
plot(upperExit, color=color.new(color.blue, 0), title="Upper Exit Channel")
plot(lowerExit, color=color.new(color.orange, 0), title="Lower Exit Channel")
// Plot Supertrend
plot(supertrend, color=color.new(color.purple, 0), linewidth=2, title="Supertrend")
// Plot buy/sell signals (commented out in original, kept commented for consistency)
// plotshape(series=longEntry, location=location.belowbar, color=color.new(color.green, 0), style=shape.labelup, text="Buy")
// plotshape(series=longExit, location=location.abovebar, color=color.new(color.blue, 0), style=shape.labeldown, text="Sell Exit")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment