Strategy · Pine Script v5 · On the chart

Turtle Trend Follow Strategy

by imzeeshan

This strategy implements a trend-following system based on Donchian breakout channels and volatility-adjusted position sizing.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code67
Settings9inputs
LicenceMIT
Last update2 years ago8 Jul 2024

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

67 lines of Pine Script v5 from strategies/trend-follow/TurtleTrendFollow/scripts/TurtleTrendFollowStrategy.pinescript, published under MIT. Keep the author's credit and licence lines when you reuse it.

TurtleTrendFollowStrategy.pinescript
//@version=5
//@Author: @GeorgeFreelanceDeveloper, @LucyFreelanceDeveloper
strategy("Turtle Trend Follow Strategy", initial_capital=1000000, overlay=true, pyramiding = 0)

// ********************************
// User defined inputs
// ********************************

var G_BASIC_SETTINGS = "Basic settings"
var G_FILTER_SETTINGS = "Filter settings"
var G_TIME = "Time settings"

entryLength = input.int(defval = 20, title = "Entry length", group = G_BASIC_SETTINGS)
exitLength = input.int(defval = 10, title = "Exit length", group = G_BASIC_SETTINGS)
riskPercentage = input.float(defval = 1, title = "Risk per trade", group = G_BASIC_SETTINGS, tooltip = "Values from 0.00 to 100")
atrMultiplier = input.int(defval = 2, title = "ATR multiplier", group = G_BASIC_SETTINGS)
atrLength = input.int(defval = 20, title = "ATR length", group = G_BASIC_SETTINGS)

enableFilter = input.bool(defval = false, title = "Enable filter", tooltip = "Filter is close price is above 200 day simple moving average of benchmark",  group = G_FILTER_SETTINGS)
benchmarkSymbol = input.symbol(defval = "SPX", title = "Benchmark", group = G_FILTER_SETTINGS)

fromDateTime = input.time(defval = timestamp("2012-01-01T01:01+0000"), title = "From Date and time", group = G_TIME) 
toDateTime = input.time(defval = timestamp("9999-01-01T01:01+0000"), title = "To Date and time", group = G_TIME)


// ********************************
// Functions
// ********************************
tradeDateIsAllowed() => time >= fromDateTime and time <= toDateTime

// **********************************
// Perform calculations and analysis
// **********************************

// Basic 
upperChannel = ta.highest(high[1], entryLength)
lowerChannel = ta.lowest(low[1], entryLength)
exitLong = ta.lowest(low[1], exitLength)

// Trade amount
qty = ((riskPercentage/100)*strategy.equity) / (atrMultiplier * ta.atr(atrLength))

// Filter
benchmarkSymbolClose = request.security(benchmarkSymbol,"1D", close)
filter = enableFilter ? benchmarkSymbolClose[1] >= ta.sma(source = benchmarkSymbolClose, length = 200) : true

buyCondition = ta.crossover(close, upperChannel) and filter and tradeDateIsAllowed()
sellCondition = ta.crossunder(close, exitLong) and tradeDateIsAllowed()

// ********************************
// Draw outputs
// ********************************
plot(upperChannel, color=color.green, title="Upper Channel")
plot(lowerChannel, color=color.red, title="Lower Channel")
plot(exitLong, color=color.gray, title="Exit Long", style=plot.style_cross)

// ********************************
// Manage trade
// ********************************

// Entry
if (buyCondition)
    strategy.entry("Long", strategy.long, qty = qty)

// Exit
if (sellCondition)
    strategy.close("Long")

Overview

Turtle Trend Follow Strategy generates long entry signals when the close price breaks above the multi-bar highest high, provided an optional benchmark trend filter (such as a 200-day simple moving average of an index) is satisfied. Position size is calculated using equity risk percentage and ATR. The strategy exits positions when the close crosses below a specified multi-bar exit channel. It plots channels directly on the main price chart and lacks hard stop-loss exit orders.

Details

Published by

imzeeshan on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Trend, Breakout

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

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

SettingTypeDefaultVariable
Entry lengthint20entryLength
Exit lengthint10exitLength
Risk per tradefloat1riskPercentage
ATR multiplierint2atrMultiplier
ATR lengthint20atrLength
Enable filterboolfalseenableFilter
BenchmarksymbolSPXbenchmarkSymbol
From Date and timetimetimestamp("2012-01-01T01:01+0000")fromDateTime
To Date and timetimetimestamp("9999-01-01T01:01+0000")toDateTime
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

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
257 stars

This strategy plots running pivot highs as red R circles and pivot lows as blue S circles directly on the price chart. It computes an EMA 3 and a Cto trend line from close prices, then generates long signals on upward EMA-Cto crossovers combined with price pos

Strategy v6 TrendMoving averages Repaints
Updated 6 weeks ago · None stated GitHub repository of Best Chart 1m Strategy Candle Bar

12/26-IT Strategy

by hasnocool
106 stars

This strategy executes trades based on crossovers between a 12-period and a 26-period exponential moving average (EMA). Long positions can be filtered by pivot resistance breakouts and RSI boundary ranges. For trade management, it provides two configurable par

Strategy v5 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of 12/26-IT Strategy

均线突破交易系统

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

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