Strategy · Pine Script v4 · On the chart

Trend #1 - ATR+Breakout high/low

by wynnforthework· code credited to zxcv55602

This strategy plots period high and low breakout bands on the price chart and trades breaks of those bands.

View on GitHub

GitHub stars2on the repository
Pine versionv4older, still runs
Lines of code80
Settings14inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago7 Oct 2025

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

80 lines of Pine Script v4 from 策略/新建文件夹 (2)/趋势突破高低 #1/趋势突破高低 #1.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

趋势突破高低 #1.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zxcv55602
//@version=4
strategy("Trend #1 - ATR+Breakout high/low", overlay = true,slippage=0,commission_value=0,initial_capital=100000)
date1 = input(title="Start Date", type=input.time, defval=timestamp("2020-01-01T00:00:00"))
date2 = input(title="Stop Date", type=input.time, defval=timestamp("2030-01-01T00:00:00"))
t1= input(title ="Trading period",type=input.session,defval="3D",options=["1M","3W","2W","1W","5D","4D","3D","2D","1D","720","480","360","240","180","120","60","30","15","10","5","3","1"] )
stop_loss=input(1000,"Stop loss", minval=0,step =1)
Trange=input(100,"Trading range", step =1)
range=input(100,"Stop range", step =1)
atrlen = input(1, "ATR Period")
mult_atr = input(1, "ATR mult", step=0.1)
smalong=input(title="SMA switch", type=input.bool, defval=false)
smainput=input(300,"SMA switch", minval=2,step =1)
longsignal=input(title="Long", type=input.bool, defval=true)
shortsignal=input(title="Short", type=input.bool, defval=true)
fixedmode=input(title="Fixed mode", type=input.bool, defval=false)
fixed=input(1,"Fixed mode",step =0.1)
//-----------
var float Y_Low = na
var float Y_High = na
if change(time(t1),length=1)!= 0
    Y_High:=high
    Y_Low:= low
else
    Y_High:=high>nz(Y_High)?high:Y_High
    Y_Low:=low<nz(Y_Low)?low:Y_Low
con1= change(time(t1),length = 1 )  != 0?1:0
//plot(Y_High[0],"LOW",color.yellow,linewidth = 2)
//plot(Y_Low[0],"LOW",color.white,linewidth = 2)
//-----------
var upper=0.0
var lower=0.0
var con_op=0.0
var central_line=0.0
if con1==1
    upper:=Y_High[1]-(((Y_High[1]-Y_Low[1])/100)*(100-Trange)/2)
    lower:=Y_Low[1]+(((Y_High[1]-Y_Low[1])/100)*(100-Trange)/2)
    con_op:=0
    central_line:=(Y_High[1]+Y_Low[1])/2
//----------- 
//plotarrow(con1==1?con1:na,colorup=color.white,colordown=color.white)
//-----------
var sma1=0.0
sma=sma(close[0],smainput)
sma1:=security(syminfo.tickerid,t1,sma)
if smalong==false
    sma1:=sma(close[0],smainput)
plot(sma1,color=color.new(color.blue,0),linewidth=1)
//------------------
upper_atr = ema(tr(true),atrlen) * mult_atr + close
lower_atr = close - ema(tr(true),atrlen) * mult_atr
plot(upper_atr, color=color.new(color.white,80),style=plot.style_stepline)
plot(lower_atr, color=color.new(color.white,80),style=plot.style_stepline)
//------------------
plot(upper,"HIGH",color.green,linewidth = 1,style=plot.style_stepline)
plot(lower,"LOW",color.red,linewidth = 1,style=plot.style_stepline)
plot(upper-(((upper-lower)/100)*range),color=color.new(color.green,50), linewidth=1)
plot(lower+(((upper-lower)/100)*range),color=color.new(color.red,50), linewidth=1)
//-----------
var OP_L_stop=0.0
var OP_S_stop=0.0
//-----------
if time >= date1 and time <= date2 and con_op==0
    if longsignal
        if strategy.position_size==0 and upper>sma1 and close>upper
            OP_L_stop:=upper-(((upper-lower)/100)*range)
            con_op:=1
            strategy.entry("OP_L",strategy.long,when=close>upper,qty=fixedmode==true?fixed:(stop_loss/(close-OP_L_stop)),limit=upper_atr)
            strategy.exit("OP_L",stop=OP_L_stop,comment="sell_L",limit=upper_atr)
        if strategy.position_size>0
            strategy.exit("OP_L",stop=OP_L_stop,comment="stop_L",limit=upper_atr)
    if shortsignal
        if strategy.position_size==0 and lower<sma1 and close<lower
            OP_S_stop:=lower+(((upper-lower)/100)*range)
            con_op:=1
            strategy.entry("OP_S",strategy.short,when=close<lower,qty=fixedmode==true?fixed:(stop_loss/(OP_S_stop-close)),limit=lower_atr)
            strategy.exit("OP_S",stop=OP_S_stop,comment="sell_S",limit=lower_atr)
        if strategy.position_size<0
            strategy.exit("OP_S",stop=OP_S_stop,comment="stop_S",limit=lower_atr)

Overview

Trend #1 - ATR+Breakout high/low goes long when price closes above the upper band while that band is above a simple moving average, and short on the opposite setup. Position size is either a fixed quantity or sized from the stop distance. Exits use a stop inside the band and a limit at an ATR envelope around price. Users set the period, band width, ATR length and multiplier, moving-average length, trade direction, and date window.

Details

Published by

wynnforthework on GitHub

Code credit

zxcv55602, as written in the script's header

Type

Strategy, Pine Script v4

Draws

On the price chart

Category

Trend, Breakout, Volatility & bands, Moving averages

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
Start Datetimetimestamp("2020-01-01T00:00:00")date1
Stop Datetimetimestamp("2030-01-01T00:00:00")date2
Trading periodsession3Dt1
Stop lossint1000stop_loss
Trading rangeint100Trange
Stop rangeint100range
ATR Periodint1atrlen
ATR multint1mult_atr
SMA switchboolfalsesmalong
SMA switchint300smainput
Longbooltruelongsignal
Shortbooltrueshortsignal
Show all 14 settings
SettingTypeDefaultVariable
Fixed modeboolfalsefixedmode
Fixed modeint1fixed
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

106 stars

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

Strategy v5 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of 9:22 5 MIN 15 MIN BANKNIFTY

均线突破交易系统

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

This strategy plots Donchian channels, trailing stops, and profit targets directly on the price chart. It enters breakout trades using a 55-bar Donchian channel and sizes positions according to an ATR-based risk percentage per unit. The system adds units as pr

Strategy v6 TrendBreakout
Updated 11 months ago · None stated GitHub repository of Seykota Alt 10: Profit Targets
4 stars

This strategy executes trend-following trades based on Bollinger Band breakouts confirmed by a long-term moving average filter. A long position opens when the price crosses above the upper Bollinger Band, provided the band spread meets a minimum threshold and

Strategy v5 Volatility & bandsTrend
Updated 2 years ago · None stated GitHub repository of Bollinger Trend - Strategy V1
4 stars

The Keltner Channel Strategy is a breakout system plotted directly on the main price chart. It calculates upper and lower channel bands around an exponential or simple moving average using ATR, True Range, or bar range multiplied by a specified factor. A long

Strategy v5 TrendVolatility & bands
Updated 5 months ago · None stated GitHub repository of Keltner Channel 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