Strategy · Pine Script v6 · On the chart

EMA Cross with ATR Exits

by theonetrade

This strategy generates long entry signals based on a fast and slow exponential moving average crossover.

View on GitHub

GitHub stars4on the repository
Pine versionv6current
Lines of code44
Settings6inputs
LicenceMIT
Last update9 days ago15 Nov 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

44 lines of Pine Script v6 from skills/pynecore-practice/templates/strategy_template.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

strategy_template.pine
//@version=6
// Pine-first template: edit this file in content/, never the generated scripts/<name>.py.
// Every top-level input becomes a main() parameter of the Pyne script; the strategy()
// arguments become the @script.strategy(...) decorator; `var` becomes Persistent state.
strategy("EMA cross with ATR exits", overlay=true,
     initial_capital=10000,
     default_qty_type=strategy.percent_of_equity, default_qty_value=10,
     commission_type=strategy.commission.percent, commission_value=0.05)

// Inputs: a default here is used until scripts/<name>.toml sets `value =` for it
fastLen   = input.int(12, "Fast EMA", minval=1)
slowLen   = input.int(21, "Slow EMA", minval=2)
atrLen    = input.int(14, "ATR length", minval=1)
stopMult  = input.float(2.0, "Stop ATR multiple", minval=0.1, step=0.1)
tpMult    = input.float(3.0, "Target ATR multiple", minval=0.1, step=0.1)
useFilter = input.bool(true, "Only above 200 EMA")

// Indicators: call ta.* on every bar, at the top level, so their history is complete
fast  = ta.ema(close, fastLen)
slow  = ta.ema(close, slowLen)
trend = ta.ema(close, 200)
atr   = ta.atr(atrLen)

longSignal = ta.crossover(fast, slow) and (not useFilter or close > trend)
exitSignal = ta.crossunder(fast, slow)

// State kept across bars
var float stopPrice   = na
var float targetPrice = na

if longSignal and strategy.position_size == 0
    stopPrice   := close - stopMult * atr
    targetPrice := close + tpMult * atr
    strategy.entry("L", strategy.long)
    strategy.exit("L exit", "L", stop=stopPrice, limit=targetPrice)

if exitSignal and strategy.position_size > 0
    strategy.close("L", comment="cross down")

// Plots: each title becomes a column of output/<name>.csv
plot(fast, "Fast EMA", color=color.teal)
plot(slow, "Slow EMA", color=color.orange)
plot(strategy.position_size > 0 ? stopPrice : na, "Stop", color=color.red, style=plot.style_linebr)
plot(strategy.position_size > 0 ? targetPrice : na, "Target", color=color.green, style=plot.style_linebr)

Overview

An optional 200-period EMA filter restricts trades to broad uptrends. Position management relies on dynamic stop loss and take profit targets calculated from Average True Range multipliers at the time of entry. Trades can also exit early when the fast EMA crosses below the slow EMA. User inputs adjust the EMA lengths, ATR length, stop and target multipliers, and filter toggle.

Details

Published by

theonetrade on GitHub

Draws

On the price chart

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

agent-skillsai-agentsalgorithmic-tradingclaude-codeclaude-skillscodecursorpinescriptpynecorepythontrading-toolkittradingview

Settings

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

SettingTypeDefaultVariable
Fast EMAint12fastLen
Slow EMAint21slowLen
ATR lengthint14atrLen
Stop ATR multiplefloat2.0stopMult
Target ATR multiplefloat3.0tpMult
Only above 200 EMAbooltrueuseFilter
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 3 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 2 months ago · None stated GitHub repository of ES - Futures 1M No Repaint

STRG-BBForce

by harryguiacorn
220 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 Trend scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers