Created
June 4, 2024 06:03
-
-
Save bouroo/4b65982cc85f76a4fbd6cf189519e896 to your computer and use it in GitHub Desktop.
Turtle Trading Strategy with Supertrend Confirmation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
// Turtle Trading Parameters | |
length1 = input.int(20, title="Donchian Channel Length (Breakout)") | |
length2 = input.int(10, title="Donchian Channel Length (Exit)") | |
risk_pct = input.float(2.0, title="Risk Percentage per Trade") | |
atr_length = input.int(20, title="ATR Length") | |
capital = input.float(1000, title="Initial Capital") | |
// Supertrend Parameters | |
supertrend_length = input.int(10, title="Supertrend Length") | |
supertrend_multiplier = input.float(3.0, title="Supertrend Multiplier") | |
// Calculate the Donchian Channels | |
upperBreakout = ta.highest(high, length1)[1] | |
lowerBreakout = ta.lowest(low, length1)[1] | |
upperExit = ta.highest(high, length2)[1] | |
lowerExit = ta.lowest(low, length2)[1] | |
// Calculate ATR | |
atr = ta.atr(atr_length) | |
// Position sizing based on ATR and risk percentage | |
unit_size = (capital * (risk_pct / 100)) / atr | |
// Supertrend calculation | |
supertrendFactor = supertrend_multiplier * ta.atr(supertrend_length) | |
supertrendUpper = (high + low) / 2 + supertrendFactor | |
supertrendLower = (high + low) / 2 - supertrendFactor | |
supertrend = 0.0 | |
if (close > supertrend[1]) | |
supertrend := supertrendLower | |
else | |
supertrend := supertrendUpper | |
// Entry conditions | |
longEntry = ta.crossover(close, upperBreakout) and close > supertrend | |
shortEntry = ta.crossunder(close, lowerBreakout) and close < supertrend | |
// Exit conditions | |
longExit = ta.crossunder(close, lowerExit) or close < supertrend | |
shortExit = ta.crossover(close, upperExit) or close > supertrend | |
// Execute trades | |
if (longEntry) | |
strategy.entry("Long", strategy.long, qty=unit_size) | |
// if (shortEntry) | |
// strategy.entry("Short", strategy.short, qty=unit_size) | |
if (longExit) | |
strategy.close("Long") | |
// if (shortExit) | |
// strategy.close("Short") | |
// Plotting the Donchian Channels | |
plot(upperBreakout, color=color.green, title="Upper Breakout Channel") | |
plot(lowerBreakout, color=color.red, title="Lower Breakout Channel") | |
plot(upperExit, color=color.blue, title="Upper Exit Channel") | |
plot(lowerExit, color=color.orange, title="Lower Exit Channel") | |
// Plot Supertrend | |
plot(supertrend, color=color.purple, linewidth = 2, title="Supertrend") | |
// Plot buy/sell signals | |
// plotshape(series=longEntry, location=location.belowbar, color=color.green, style=shape.labelup, text="Buy") | |
// plotshape(series=shortEntry, location=location.abovebar, color=color.red, style=shape.labeldown, text="Sell") | |
// plotshape(series=longExit, location=location.abovebar, color=color.blue, style=shape.labeldown, text="Sell Exit") | |
// plotshape(series=shortExit, location=location.belowbar, color=color.orange, style=shape.labelup, text="Buy Exit") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment