Strategy · Pine Script v6 · On the chart

Donchian Strategy - Dynamic TP, Fixed SL, 1% Risk, Daily Target

by Ahmed-GoCode· code credited to Ahmed - Quant-Edge Indicators

This trend breakout strategy plots Donchian Channels consisting of upper, lower, and middle bands directly on the main price chart.

View on GitHub

GitHub stars53on the repository
Pine versionv6current
Lines of code83
Settings3inputs
LicenceMPL-2.0
Last update2 months ago17 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

83 lines of Pine Script v6 from Quant-Edge-Indicators/Trend & Breakout/Turtle Strategy.pinescript, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

Turtle Strategy.pinescript
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Ahmed - Quant-Edge Indicators
//@version=6
strategy("Donchian Strategy - Dynamic TP, Fixed SL, 1% Risk, Daily Target", overlay=true, default_qty_type=strategy.cash, default_qty_value=100000)

// === INPUTS ===
length = input.int(20, title="Donchian Channel Length")
riskPercent = input.float(1.0, title="Risk per Trade (%)", step=0.1)
daily_target = input.float(500, title="Daily Profit Target ($)", step=50)

// === DONCHIAN CHANNELS ===
donchian_high = ta.highest(high, length)
donchian_low = ta.lowest(low, length)
donchian_mid = (donchian_high + donchian_low) / 2
plot(donchian_high, color=color.green, title="Upper Channel")
plot(donchian_low, color=color.red, title="Lower Channel")
plot(donchian_mid, color=color.gray, title="Middle Channel")

// === CHANNEL TOUCH DETECTION ===
var bool touched_upper = false
var bool touched_lower = false

if high >= donchian_high
    touched_upper := true

if low <= donchian_low
    touched_lower := true

// === RISK CALCULATION ===
capital = strategy.equity
risk_amount = capital * (riskPercent / 100)

// === FIXED SL ===
var float entry_price = na
var float stop_price = na

// === NEW DAY DETECTION ===
current_session = year(time) * 10000 + month(time) * 100 + dayofmonth(time)
var int saved_session = na
var float daily_profit = 0.0

if na(saved_session) or current_session != saved_session
    saved_session := current_session
    daily_profit := 0.0

// === UPDATE ACCUMULATED PROFIT ===
daily_profit += strategy.netprofit - strategy.netprofit[1]

// === CAN TRADE TODAY? ===
can_trade_today = daily_profit < daily_target

// === BUY ===
long_condition = touched_upper and close <= donchian_mid
long_sl = donchian_low
long_entry = close
long_risk_per_unit = math.abs(long_entry - long_sl)
long_qty = long_risk_per_unit > 0 ? risk_amount / long_risk_per_unit : 0

if long_condition and strategy.position_size == 0 and can_trade_today
    entry_price := long_entry
    stop_price := long_sl
    strategy.entry("Buy", strategy.long, qty=long_qty)
    touched_upper := false

// === SELL ===
short_condition = touched_lower and close >= donchian_mid
short_sl = donchian_high
short_entry = close
short_risk_per_unit = math.abs(short_entry - short_sl)
short_qty = short_risk_per_unit > 0 ? risk_amount / short_risk_per_unit : 0

if short_condition and strategy.position_size == 0 and can_trade_today
    entry_price := short_entry
    stop_price := short_sl
    strategy.entry("Sell", strategy.short, qty=short_qty)
    touched_lower := false

// === Dynamic TP, Fixed SL ===
if strategy.position_size > 0
    strategy.exit("TP Buy", from_entry="Buy", stop=stop_price, limit=donchian_high)

if strategy.position_size < 0
    strategy.exit("TP Sell", from_entry="Sell", stop=stop_price, limit=donchian_low)

Overview

Long positions enter when price touches the upper channel and pulls back to or below the middle band, placing a stop loss at the lower channel. Short positions enter when price touches the lower channel and retraces to or above the middle band, placing a stop loss at the upper channel. Position sizes dynamically calculate using a custom equity risk percentage. Trades are restricted when accumulated daily net profit reaches the specified daily target threshold.

Details

Published by

Ahmed-GoCode on GitHub

Code credit

Ahmed - Quant-Edge Indicators, as written in the script's header

Type

Strategy, Pine Script v6

Draws

On the price chart

Category

Trend, Breakout, Support & resistance

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.

Topics

fair-value-gapfibonaccimarket-structurepine-scriptrsi-strategysmart-money-conceptstechnical-analysistradingviewalgorithmic-tradingcrypto-trading

Settings

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

SettingTypeDefaultVariable
Donchian Channel Lengthint20length
Risk per Trade (%)float1.0riskPercent
Daily Profit Target ($)float500daily_target
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
106 stars

Calculates multi-period logarithmic regression trend channels and deviation bands across up to twenty selectable lookback lengths directly on the price chart. It evaluates channel slopes and Pearson's correlation coefficient to detect market trends and generat

Strategy v5 TrendSupport & resistance Repaints
Updated 2 years ago · GPL-3.0 GitHub repository of Advanced Trend Detection Strategy
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
16 stars

This strategy trades Donchian breakouts measured on weekly bars while orders execute on the chart timeframe. It allows long and short entries when price breaks the prior weekly channel and exits against the shorter weekly channel. Position size risks a set per

Strategy v6 TrendBreakout
Updated 11 months ago · None stated GitHub repository of Seykota Alt 4: Weekly Donchian

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