1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
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
95 lines of Pine Script v6 from Breakout Strategy/Breakout Strategy.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.
Breakout Strategy.pine
//@version=6
strategy("Breakout Strategy",
overlay=true,
default_qty_type=strategy.percent_of_equity, //
default_qty_value=10, // Invests 10% of capital per trade
pyramiding=0) // Does not allow adding to an existing position
// --- 1. INPUT PARAMETERS (Configurable by the user) ---
// Breakout parameter group
var group_channels = "Breakout channels"
int entryLength = input.int(20, "Entry breakout period", minval=2, group=group_channels)
int exitLength = input.int(10, "Period for outbound breakout", minval=1, group=group_channels)
// Grupo de Parámetros de Gestión de Riesgo
var group_risk = "Risk management and confirmation"
bool useSL = input.bool(true, "Use stop loss", group=group_risk)
float slMultiplier = input.float(2.0, "ATR multiplier for stop loss", minval=0.1, group=group_risk)
bool useTP = input.bool(true, "Use Take Profit", group=group_risk)
float tpMultiplier = input.float(4.0, "ATR multiplier for Take Profit", minval=0.1, group=group_risk)
int atrPeriod = input.int(14, "ATR period", minval=1, group=group_risk)
// Wait for the candle to close
bool useBarConfirm = input.bool(true, "Wait for confirmation of the candle?",
tooltip="Wait for the candle to close before entering/exiting. This helps prevent repainting.", group=group_risk)
// --- 2. CALCULATIONS ---
// Calculate the ATR for risk management
float atr = ta.atr(atrPeriod)
// Calculate the input channels (we use [1] to refer to the *previous* candle)
float upperEntry = ta.highest(high, entryLength)[1]
float lowerEntry = ta.lowest(low, entryLength)[1]
// Calculate the output channels
float upperExit = ta.highest(high, exitLength)[1]
float lowerExit = ta.lowest(low, exitLength)[1]
// Confirmation status
// We use barstate.isconfirmed to prevent the strategy from operating in the middle of the candle.
bool confirmation = useBarConfirm ? barstate.isconfirmed : true
// --- 3. STRATEGY LOGIC ---
// Terms and Conditions of Entry
bool enterLong = (close > upperEntry) and confirmation
bool enterShort = (close < lowerEntry) and confirmation
// Exit Conditions (by opposite signal)
bool exitLong = (close < lowerExit) and confirmation
bool exitShort = (close > upperExit) and confirmation
// --- 4. EXECUTION OF ORDERS ---
// Calculate SL and TP levels
float entryPrice = strategy.position_avg_price
float sl_long = entryPrice - (atr * slMultiplier)
float tp_long = entryPrice + (atr * tpMultiplier)
float sl_short = entryPrice + (atr * slMultiplier)
float tp_short = entryPrice - (atr * tpMultiplier)
// Entry
if (enterLong and strategy.position_size == 0) // Enter only if there is no open position
strategy.entry("Entrada Long", strategy.long)
if (enterShort and strategy.position_size == 0) // Enter only if there is no open position
strategy.entry("Entrada Short", strategy.short)
// Signal outputs
if (exitLong)
strategy.close("Entrada Long", comment="Salida por Señal")
if (exitShort)
strategy.close("Entrada Short", comment="Salida por Señal")
// Stop Loss and Take Profit Exits
if (strategy.position_size > 0) // If we are in a long trade
strategy.exit("Stop/Profit Long",
stop = useSL ? sl_long : na,
limit = useTP ? tp_long : na)
if (strategy.position_size < 0) // If we are in a short trade
strategy.exit("Stop/Profit Short",
stop = useSL ? sl_short : na,
limit = useTP ? tp_short : na)
// --- 5. PLOT (Visualization) ---
plot(upperEntry, "Upper Channel (Entrance)", color=color.new(color.green, 0))
plot(lowerEntry, "Lower Channel (Entrance)", color=color.new(color.red, 0))
plot(upperExit, "Upper Channel (Outlet)", color=color.new(color.green, 70), style=plot.style_circles, linewidth=2)
plot(lowerExit, "Lower Channel (Outlet)", color=color.new(color.red, 70), style=plot.style_circles, linewidth=2)
Overview
Breakout Strategy calculates highest highs and lowest lows over configurable entry and exit periods using historical candles. A long entry triggers when the close breaks above the entry channel, while a short entry triggers when it drops below. Position exits occur via channel reversals or optional ATR-based stop-loss and take-profit targets. User settings control channel lengths, ATR multipliers, and bar confirmation.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
Entry breakout period
int
20
entryLength
Period for outbound breakout
int
10
exitLength
Use stop loss
bool
true
useSL
ATR multiplier for stop loss
float
2.0
slMultiplier
Use Take Profit
bool
true
useTP
ATR multiplier for Take Profit
float
4.0
tpMultiplier
ATR period
int
14
atrPeriod
Wait for confirmation of the candle?
bool
true
useBarConfirm
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.
This strategy plots VWMA-based Bollinger bands on the 1-minute ES futures price chart for volatility analysis and potential breakout identification. It calculates dynamic long and short stop loss and take profit levels using percentage inputs relative to entry
This strategy runs on the price chart and enters long when a fast simple moving average crosses above a slow one, and short on the opposite cross, while closing the other side. Entries also need ATR above a filter, a pullback check, a minimum candle-body chang
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
This trend-following strategy adapts classic Donchian breakout channels according to market volatility regimes. It calculates an ATR percentile rank across a rolling lookback period, shifting dynamically between tighter breakout channels with smaller stops dur
This strategy executes breakout trades using two Donchian Channels alongside an optional 200-period EMA trend filter. Entries trigger when the price sets a new highest high or lowest low over the primary Donchian Channel period. Exits are managed via a shorter
This strategy combines channel breakout concepts with an ATR-ADX volatility trailing framework and multi-timeframe moving averages. It evaluates trend conditions using configurable moving average types, including SMA, EMA, Hull MA, and Tilson T3, paired with d