Strategy · Pine Script v4 · On the chart

Trend #4 - ATR+EMA channel

by wynnforthework· code credited to zxcv55602

Trend #4 - ATR+EMA channel is a strategy that plots ATR bands, a blue Keltner-style channel, and a red EMA on the price chart.

View on GitHub

GitHub stars2on the repository
Pine versionv4older, still runs
Lines of code69
Settings11inputs
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

69 lines of Pine Script v4 from 策略/新建文件夹 (2)/趋势 #4 - ATR+EMA 通道/趋势 #4 - ATR+EMA 通道.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

趋势 #4 - ATR+EMA 通道.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 #4 - ATR+EMA channel", overlay = true,slippage=2,commission_value=0.05)
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"))
stoploss=input(1000, minval=1, title="stoploss")
length = input(10, minval=1, title="Blue EMA length",step=1)
multy = input(1.8, step=0.1, title="Blue EMA multy")
atrlen = input(15, "ATR Period")
mult_atr = input(0.7, "ATR mult", step=0.1)
redema=input(20, "Red EMA length", step=1)
longsignal=input(true,"long",type=input.bool)
shortsignal=input(true,"short",type=input.bool)
Breakout=input(false,"Breakout mode",type=input.bool)
//------------------
upper_atr = ema(tr(true),atrlen) * mult_atr + close
lower_atr = close - ema(tr(true),atrlen) * mult_atr
plot(upper_atr, "+ATR", color=color.new(color.white,80),style=plot.style_stepline)
plot(lower_atr, "-ATR", color=color.new(color.white,80),style=plot.style_stepline)
//------------------
var op_upper_atr=0.0
var op_lower_atr=0.0
var op_close=0.0
//------------------
Keltma =ema(close, length)
range = high - low
rangema = ema(range, length)
upperk =Keltma + rangema * multy
lowerk = Keltma - rangema * multy
plot(upperk, color=color.blue, title="")
plot(lowerk, color=color.blue, title="")
//------------------
red_ema=ema(close,redema)
plot(red_ema, color=color.red, title="")
//------------------
if time >= date1 and time <= date2 and Breakout==false
    if longsignal and strategy.position_size==0 and close>upperk and close>red_ema
        op_close:=close
        op_upper_atr:=upper_atr
        strategy.entry("L",true,qty=(stoploss/(op_close-red_ema)),stop=red_ema)
        strategy.exit("L",limit=op_upper_atr,comment="exit_L",stop=red_ema)
    if shortsignal and strategy.position_size==0 and close<lowerk and close<red_ema
        op_close:=close
        op_lower_atr:=lower_atr
        strategy.entry("S",false,qty=(stoploss/(red_ema-op_close)),stop=red_ema)
        strategy.exit("S",limit=op_lower_atr,comment="exit_S",stop=red_ema)
    if strategy.position_size>0
        strategy.exit("L",limit=op_upper_atr,comment="exit_L",stop=red_ema)
    if strategy.position_size<0
        strategy.exit("S",limit=op_lower_atr,comment="exit_S",stop=red_ema)
//------------------
if time >= date1 and time <= date2 and Breakout
    if longsignal and strategy.position_size==0 and close[0]>high[1] and close>red_ema and close>upperk
        op_upper_atr:=upper_atr
        op_lower_atr:=lower_atr
        op_close:=close
        strategy.entry("long",strategy.long,qty=stoploss/(op_close-red_ema),limit=op_upper_atr,stop=red_ema)
        strategy.exit("long",comment="exit_L1",limit=op_upper_atr,stop=red_ema)
    if shortsignal and strategy.position_size==0 and close[0]<low[1] and close<red_ema and close<lowerk
        op_upper_atr:=upper_atr
        op_lower_atr:=lower_atr
        op_close:=close
        strategy.entry("short",strategy.short,qty=stoploss/(red_ema-op_close),limit=op_lower_atr,stop=red_ema)
        strategy.exit("short",comment="exit_S1",limit=op_lower_atr,stop=red_ema)
    if strategy.position_size>0
        strategy.exit("long",comment="exit_L",limit=op_upper_atr,stop=red_ema)
    if strategy.position_size<0
        strategy.exit("short",comment="exit_S",limit=op_lower_atr,stop=red_ema)

Overview

Trend #4 - ATR+EMA channel generates long entries when price crosses above the blue channel and red EMA with zero position, using position sizing based on stoploss divided by the gap to the stop level. Short entries mirror this logic when price breaks below. Entries use a limit at the opposite ATR band and a stop at the red EMA. The strategy offers non-breakout and breakout modes. Main inputs control channel lengths, ATR multiplier, and entry filters. The script draws directly on the price chart.

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, Moving averages, Volatility & bands

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
stoplossint1000stoploss
Blue EMA lengthint10length
Blue EMA multyfloat1.8multy
ATR Periodint15atrlen
ATR multfloat0.7mult_atr
Red EMA lengthint20redema
longbooltruelongsignal
shortbooltrueshortsignal
Breakout modeboolfalseBreakout
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 mean-reversion strategy plots Bollinger Bands and a long-term moving average directly on the price chart. It enters trades when price candles penetrate the outer Bollinger Bands beyond a user-defined body percentage, anticipating a price reversal. Entries

Strategy v5 Mean reversionVolatility & bands
Updated 2 months ago · None stated GitHub repository of Bollinger Bands Winner PRO [Alorse]
257 stars

This overlay strategy targets one-minute ES futures and plots volume-weighted moving-average bands from the open, scaled by a standard-deviation multiple, plus a slower band from ten-minute opens. A higher-timeframe exponential moving average can filter trades

Strategy v6 Moving averagesVolatility & bands
Updated 6 weeks ago · None stated GitHub repository of ES - Futures 1M No Repaint

STRG-BBForce

by harryguiacorn
219 stars

STRG-BBForce is a Bollinger Bands trend-following strategy. It calculates standard Bollinger Bands and tracks directional alignment across the upper, lower, and basis lines. When all three lines slope upward, it opens a long position and closes short positions

Strategy v5 TrendVolatility & bands
Updated 1 year ago · None stated GitHub repository of STRG-BBForce
106 stars

This trading strategy combines three Exponential Moving Averages (8, 14, and 50 periods by default) with Stochastic RSI and Average True Range (ATR). It enters a long position when EMA 1 is above EMA 2, EMA 2 is above EMA 3, and the Stochastic RSI %K line cros

Strategy v4 TrendOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 3 x EMA + Stochastic RSI + ATR

Bollinger Bands Strategy

by cklose2000
105 stars

This strategy generates buy and sell signals based on Bollinger Band crossovers combined with a simple moving average trend filter. It enters a long position when price crosses above the lower band while trading above the SMA filter line, and enters a short po

Strategy v5 Volatility & bandsMoving averages
Updated 1 year ago · None stated GitHub repository of Bollinger Bands Strategy
104 stars

This overlay strategy combines a Chandelier Exit with a zero-lag least squares moving average. Although the header mentions Heikin Ashi candles, the calculations use ordinary closes. An ATR trailing stop sets long and short levels, and direction flips when pri

Strategy v5 TrendMoving averages
Updated 3 years ago · MIT GitHub repository of Chandelier Exit & ZLSMA 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