Strategy · Pine Script v5 · On the chart

Turtle Trading Strategy

by GreyssonEnterprises

This strategy overlays four smoothed moving averages and a trend fill between a two-period EMA and the longest average on the price chart.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code170
Settings14inputs
LicenceMIT
Last update3 months ago1 Aug 2026

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

170 lines of Pine Script v5 from strategies/turtles/pinescript/strategy.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

strategy.pine
//@version=5
strategy(title="Turtle Trading Strategy", shorttitle="TurtleStrategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.1)

// === INPUT PARAMETERS ===
// Donchian Channel Settings
channelLength = input.int(10, minval=1, title="Donchian Channel Length", group="Channel Settings")

// SMMA Trend Filter Settings
len1 = input.int(21, minval=1, title="SMMA 1 Length", group="Trend Filter")
len2 = input.int(50, minval=1, title="SMMA 2 Length", group="Trend Filter")
len3 = input.int(100, minval=1, title="SMMA 3 Length", group="Trend Filter")
len4 = input.int(200, minval=1, title="SMMA 4 Length", group="Trend Filter")
showSMMA = input.bool(true, title="Show SMMA Lines", group="Trend Filter")

// Risk Management Settings
riskPerTrade = input.float(50.0, minval=1.0, title="Risk Per Trade ($)", group="Risk Management")
riskRewardRatio = input.float(3.0, minval=1.0, title="Risk:Reward Ratio", group="Risk Management")
usePercentRisk = input.bool(false, title="Use Percent Risk", group="Risk Management")
percentRisk = input.float(2.0, minval=0.1, maxval=10.0, title="Risk Percentage", group="Risk Management")

// Pattern Recognition Settings
use3LineStrike = input.bool(true, title="Use 3-Line Strike", group="Pattern Recognition")
useEngulfing = input.bool(true, title="Use Engulfing Patterns", group="Pattern Recognition")

// Display Settings
showTradeInfo = input.bool(true, title="Show Trade Information", group="Display")
showPerformance = input.bool(true, title="Show Performance", group="Display")

// === SMOOTHED MOVING AVERAGES ===
smma(src, length) =>
    var float smma = na
    smma := na(smma[1]) ? ta.sma(src, length) : (smma[1] * (length - 1) + src) / length
    smma

smma1 = smma(close, len1)
smma2 = smma(close, len2)
smma3 = smma(close, len3)
smma4 = smma(close, len4)

// Plot SMAs
plot(showSMMA ? smma1 : na, color=color.yellow, linewidth=2, title="SMMA 21")
plot(showSMMA ? smma2 : na, color=color.green, linewidth=2, title="SMMA 50")
plot(showSMMA ? smma3 : na, color=color.blue, linewidth=1, title="SMMA 100")
plot(showSMMA ? smma4 : na, color=color.red, linewidth=2, title="SMMA 200")

// Trend Fill
ema2 = ta.ema(close, 2)
trendFill = ema2 > smma4 ? color.new(color.green, 85) : color.new(color.red, 85)
fill(plot(ema2, display=display.none), plot(smma4, display=display.none), color=trendFill, title="Trend Fill")

// === PATTERN RECOGNITION ===
// 3-Line Strike Patterns
bearStrike = use3LineStrike and close[3] > open[3] and close[2] > open[2] and close[1] > open[1] and close < open[1]
bullStrike = use3LineStrike and close[3] < open[3] and close[2] < open[2] and close[1] < open[1] and close > open[1]

plotshape(bearStrike, style=shape.triangledown, color=color.red, location=location.abovebar, size=size.tiny, text="3S")
plotshape(bullStrike, style=shape.triangleup, color=color.green, location=location.belowbar, size=size.tiny, text="3S")

// Engulfing Patterns
bullEngulf = useEngulfing and open <= close[1] and open < open[1] and close > open[1]
bearEngulf = useEngulfing and open >= close[1] and open > open[1] and close < open[1]

plotshape(bullEngulf, style=shape.triangleup, color=color.lime, location=location.belowbar, size=size.tiny, text="ENG")
plotshape(bearEngulf, style=shape.triangledown, color=color.red, location=location.abovebar, size=size.tiny, text="ENG")

// === DONCHIAN CHANNELS ===
upper = ta.highest(high, channelLength)
lower = ta.lowest(low, channelLength)
basis = (upper + lower) / 2

plot(upper, title="Upper Channel", color=color.white, linewidth=1)
plot(lower, title="Lower Channel", color=color.white, linewidth=1)

// Breakout Signals
bullBreakout = upper > upper[1]
bearBreakout = lower < lower[1]

bullSignal = bullBreakout and not bullBreakout[1]
bearSignal = bearBreakout and not bearBreakout[1]

// === TREND ALIGNMENT ===
trendBullish = smma4 < smma2 and smma2 < smma1
trendBearish = smma4 > smma2 and smma2 > smma1

// === TRADE CONDITIONS ===
longCondition = trendBullish and bullSignal
shortCondition = trendBearish and bearSignal

// === POSITION SIZING ===
var float stopDistance = na
var float positionSize = na
var float riskAmount = na

if longCondition or shortCondition
    stopDistance := 2.0  // Fixed stop distance
    riskAmount := usePercentRisk ? (strategy.equity * percentRisk / 100) : riskPerTrade
    positionSize := riskAmount / stopDistance

// === STRATEGY EXECUTION ===
if longCondition and strategy.position_size == 0
    strategy.entry("Long", strategy.long, qty=positionSize)
    strategy.exit("Long Exit", "Long", stop=close - stopDistance, limit=close + (stopDistance * riskRewardRatio))

if shortCondition and strategy.position_size == 0
    strategy.entry("Short", strategy.short, qty=positionSize)
    strategy.exit("Short Exit", "Short", stop=close + stopDistance, limit=close - (stopDistance * riskRewardRatio))

// === VISUAL SIGNALS ===
plotshape(longCondition, title="Long Signal", style=shape.triangleup, location=location.belowbar, color=color.lime, size=size.normal, text="LONG")
plotshape(shortCondition, title="Short Signal", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.normal, text="SHORT")

// === TRADE LEVEL VISUALIZATION ===
var float entryPrice = na
var float stopPrice = na
var float targetPrice = na

if longCondition
    entryPrice := close
    stopPrice := close - stopDistance
    targetPrice := close + (stopDistance * riskRewardRatio)

if shortCondition
    entryPrice := close
    stopPrice := close + stopDistance
    targetPrice := close - (stopDistance * riskRewardRatio)

// Plot trade levels
plot(strategy.position_size != 0 ? entryPrice : na, title="Entry", color=color.white, linewidth=2, style=plot.style_line)
plot(strategy.position_size != 0 ? stopPrice : na, title="Stop", color=color.red, linewidth=2, style=plot.style_line)
plot(strategy.position_size != 0 ? targetPrice : na, title="Target", color=color.green, linewidth=2, style=plot.style_line)

// === INFORMATION TABLE ===
if barstate.islast and showTradeInfo
    var table infoTable = table.new(position.top_right, 2, 6, bgcolor=color.new(color.black, 80), border_width=1)
    table.cell(infoTable, 0, 0, "Turtle Strategy", text_color=color.white, text_size=size.small)
    table.cell(infoTable, 1, 0, "Status", text_color=color.white, text_size=size.small)
    table.cell(infoTable, 0, 1, "Position", text_color=color.white, text_size=size.tiny)
    table.cell(infoTable, 1, 1, strategy.position_size > 0 ? "LONG" : strategy.position_size < 0 ? "SHORT" : "FLAT", 
               text_color=strategy.position_size > 0 ? color.lime : strategy.position_size < 0 ? color.red : color.gray, text_size=size.tiny)
    table.cell(infoTable, 0, 2, "Risk/Trade", text_color=color.white, text_size=size.tiny)
    table.cell(infoTable, 1, 2, "$" + str.tostring(riskAmount, "#.##"), text_color=color.yellow, text_size=size.tiny)
    table.cell(infoTable, 0, 3, "Upper", text_color=color.white, text_size=size.tiny)
    table.cell(infoTable, 1, 3, str.tostring(upper, "#.##"), text_color=color.lime, text_size=size.tiny)
    table.cell(infoTable, 0, 4, "Lower", text_color=color.white, text_size=size.tiny)
    table.cell(infoTable, 1, 4, str.tostring(lower, "#.##"), text_color=color.red, text_size=size.tiny)
    table.cell(infoTable, 0, 5, "Trend", text_color=color.white, text_size=size.tiny)
    table.cell(infoTable, 1, 5, trendBullish ? "BULL" : trendBearish ? "BEAR" : "NEUTRAL", 
               text_color=trendBullish ? color.lime : trendBearish ? color.red : color.gray, text_size=size.tiny)

// === PERFORMANCE METRICS ===
if barstate.islast and showPerformance
    var table perfTable = table.new(position.bottom_right, 2, 4, bgcolor=color.new(color.black, 80), border_width=1)
    table.cell(perfTable, 0, 0, "Performance", text_color=color.white, text_size=size.small)
    table.cell(perfTable, 1, 0, "Value", text_color=color.white, text_size=size.small)
    table.cell(perfTable, 0, 1, "Net Profit", text_color=color.white, text_size=size.tiny)
    table.cell(perfTable, 1, 1, "$" + str.tostring(strategy.netprofit, "#.##"), 
               text_color=strategy.netprofit > 0 ? color.lime : color.red, text_size=size.tiny)
    table.cell(perfTable, 0, 2, "Win Rate", text_color=color.white, text_size=size.tiny)
    table.cell(perfTable, 1, 2, str.tostring(strategy.wintrades / (strategy.wintrades + strategy.losstrades) * 100, "#.#") + "%", 
               text_color=color.yellow, text_size=size.tiny)
    table.cell(perfTable, 0, 3, "Profit Factor", text_color=color.white, text_size=size.tiny)
    table.cell(perfTable, 1, 3, str.tostring(strategy.grossprofit / strategy.grossloss, "#.##"), 
               text_color=color.cyan, text_size=size.tiny)

// === ALERT CONDITIONS ===
alertcondition(longCondition, title="Long Entry", message="{{ticker}} LONG - Turtle Breakout")
alertcondition(shortCondition, title="Short Entry", message="{{ticker}} SHORT - Turtle Breakdown")

// Bar coloring based on trend
barcolor(trendBullish and bullBreakout ? color.lime : trendBearish and bearBreakout ? color.red : na, title="Turtle Bar Color")

Overview

Turtle Trading Strategy marks optional three-line strike and engulfing candle patterns. Inputs set a Donchian channel length, the four average lengths, a dollar or percentage risk amount, and a risk-to-reward ratio, plus toggles for trade and performance displays. The excerpt ends before the channel and order logic, so entries, exits, and stops are not shown.

Details

Published by

GreyssonEnterprises on GitHub

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

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

SettingTypeDefaultVariable
Donchian Channel Lengthint10channelLength
SMMA 1 Lengthint21len1
SMMA 2 Lengthint50len2
SMMA 3 Lengthint100len3
SMMA 4 Lengthint200len4
Show SMMA LinesbooltrueshowSMMA
Risk Per Trade ($)float50.0riskPerTrade
Risk:Reward Ratiofloat3.0riskRewardRatio
Use Percent RiskboolfalseusePercentRisk
Risk Percentagefloat2.0percentRisk
Use 3-Line Strikebooltrueuse3LineStrike
Use Engulfing PatternsbooltrueuseEngulfing
Show all 14 settings
SettingTypeDefaultVariable
Show Trade InformationbooltrueshowTradeInfo
Show PerformancebooltrueshowPerformance
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

Pin Bar Magic v1

by Alorse
330 stars

This strategy trades price action pin bar patterns aligned with moving average trend alignment on the main price chart. It defines uptrends or downtrends using a fan arrangement of three moving averages (6 EMA, 18 EMA, and 50 SMA). Entry conditions require a b

Strategy v4 Candlestick patternsTrend
Updated 2 months ago · None stated GitHub repository of Pin Bar Magic v1
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 7 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 5 months ago · None stated GitHub repository of 均线突破交易系统
43 stars

This strategy executes trades on short-term cryptocurrency charts using a combination of pivot support and resistance levels and a multi-pole smoothed trend filter. Entries trigger when price crosses the smoothed trend line and confirms above or below a short-

Strategy v5 TrendBreakout Repaints
Updated 2 years ago · None stated GitHub repository of SOL 1m - Strategy Candle Bar

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

More Trend scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers