Strategy · Pine Script v6 · On the chart

Donchian Pullback Strategy

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

This strategy identifies trend direction from Donchian Channel breakouts and executes trades on pullbacks toward the channel midpoint.

View on GitHub

GitHub stars53on the repository
Pine versionv6current
Lines of code51
Settings1inputs
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

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

Donchian Pullback.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 Pullback Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)

length = input.int(20, title="Donchian Channel Period")

// Donchian Channel
donchianHigh = ta.highest(high, length)
donchianLow = ta.lowest(low, length)
donchianMid = (donchianHigh + donchianLow) / 2

// Trend State Tracking
var bool inBearishTrend = false
var bool inBullishTrend = false

// Detect channel breakouts to set overall trend bias
if (low < donchianLow[1])
    inBearishTrend := true
    inBullishTrend := false

if (high > donchianHigh[1])
    inBullishTrend := true
    inBearishTrend := false

// Variables to prevent multiple re-entries
var bool shortTaken = false
var bool longTaken = false

// Entry Conditions
if (inBearishTrend and not shortTaken and close >= donchianMid)
    strategy.entry("Short", strategy.short)
    strategy.exit("TP/SL Short", from_entry="Short", limit=donchianLow, stop=donchianHigh)
    shortTaken := true
    longTaken := false

if (inBullishTrend and not longTaken and close <= donchianMid)
    strategy.entry("Long", strategy.long)
    strategy.exit("TP/SL Long", from_entry="Long", limit=donchianHigh, stop=donchianLow)
    longTaken := true
    shortTaken := false

// Reset signals when position is flat
if (strategy.position_size == 0)
    shortTaken := false
    longTaken := false

// Visualization
plot(donchianHigh, title="Donchian High", color=color.red)
plot(donchianLow, title="Donchian Low", color=color.green)
plot(donchianMid, title="Donchian Mid", color=color.gray)

Overview

A new breakout beyond the previous channel bands sets a bullish or bearish trend state. When price retraces to or across the midline, the strategy enters in the direction of the active trend. Each position uses a bracket exit where the opposite channel band serves as the stop loss and the trending channel band serves as the take-profit target. It plots channel bands directly on the price chart.

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

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

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