Strategy · Pine Script v5 · On the chart

EMA Crossover Scott Strategy v2

by g-moe· code credited to goodgains

This strategy executes trades based on price crosses over dynamic swing breakout levels filtered by a baseline moving average.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code125
Settings17inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago27 May 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

125 lines of Pine Script v5 from Tradingview/ema-crossover-scott-strategy-v2-strategy.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

ema-crossover-scott-strategy-v2-strategy.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodgains


//@version=5
strategy(title="EMA Crossover Scott Strategy v2 Strategy", overlay=true)

// @keywords: ema-crossover-scott, sma-ema-stack, swing-trailing-line, strategy-entries, strategy-exits, profit-target, stop-loss, trailing-stop, trading-session, long-short-trades, crossover-signals, crossunder-signals, buy-sell-arrows, bar-background, trend-line, multi-ma, position-management, ny-session, swing-levels, close-cross

//Strategy Inputs
useTradingHours = input.bool(false, title = " ", tooltip = "This is ignored if the chart timeframe is greater than or equal to the daily timeframe.", group = "Additional Trade Settings",inline = '1')
trading_time = input.session("0930-1555", title = "Trading Time", tooltip = "This uses Eastern Time Zone or U.S. NY Market Hours.", group = "Additional Trade Settings",inline = '1')
trading_hours_cond = not useTradingHours or time("",trading_time, "America/New_York")

long_trades = input.bool(true, title = "Long Trades", tooltip = "Do we want to take long trades?" , group = "Additional Trade Settings",inline = '2')
short_trades = input.bool(true, title = "Short Trades", tooltip = "Do we want to take short trades?" , group = "Additional Trade Settings",inline = '3')

use_profit_target = input.bool(true, title = "Profit Target", tooltip = "Do we want to use a profit target?" , group = "Additional Trade Settings",inline = '4')
pt_amount = input.float(5.00, minval = 0.00, maxval = 99.0, step = 0.25,  title = " ", group = "Additional Trade Settings", inline = '4')

use_stop = input.bool(true, title = "Stop", tooltip = "Do we want to use a hard stop loss?" , group = "Additional Trade Settings",inline = '5')
stop_amount = input.float(4.00, minval = 0.00, maxval = 99.0, step = 0.25, title = " ", group = "Additional Trade Settings", inline = '5')

use_trailing_stop = input.bool(true, title = "Trailing Stop", tooltip = "Do we want to use a trailing stop loss?" , group = "Additional Trade Settings",inline = '6')
trailing_stop_amount = input.float(2.50, minval = 0.00, maxval = 99.0, step = 0.25, title = " ", group = "Additional Trade Settings", inline = '6')

//SMA and EMA code
smaInput1 = input(9, title='SMA1')
smaInput2 = input(50, title='SMA2')
smaInput3 = input(180, title='SMA3')
emaInput1 = input(20, title='EMA1')
sma1 = ta.sma(close, smaInput1)
sma2 = ta.sma(close, smaInput2)
sma3 = ta.sma(close, smaInput3)
EMA1 = ta.ema(close, emaInput1)
plot(sma1, color=color.new(color.red, 0), title='SMA1')
plot(sma2, color=color.new(color.blue, 0), title='SMA2')
plot(sma3, color=color.new(color.white, 0), title='SMA3')
plot(EMA1, color=color.new(color.yellow, 0), title='EMA1')
no = input(3, title='BUY/SELL Swing')
Barcolor = input(false, title='BUY/SELL Bar Color')
Bgcolor = input(false, title='BUY/SELL Background Color')
res = ta.highest(high, no)
sup = ta.lowest(low, no)
iff_1 = close < sup[1] ? -1 : 0
avd = close > res[1] ? 1 : iff_1
avn = ta.valuewhen(avd != 0, avd, 0)
tsl = avn == 1 ? sup : res
Buy = ta.crossover(close, tsl) and close > sma3 and trading_hours_cond
Sell = ta.crossunder(close, tsl)  and close < sma3 and trading_hours_cond
plotshape(Buy, title='Buy', color=color.new(color.green, 0), style=shape.arrowup, location=location.belowbar, text='Buy')
plotshape(Sell, title='Sell', color=color.new(color.red, 0), style=shape.arrowdown, text='Sell')
colr = close >= tsl ? color.green : close <= tsl ? color.red : na
plot(tsl, color=colr, linewidth=3, title='BUY/SELL Chart Line')
barcolor(Barcolor ? color.new(colr,80) : na)
bgcolor(Bgcolor ? color.new(colr,80) : na)


//ENTRY CONDITIONS
long_entry = long_trades and Buy
long_exit = not short_trades and Sell

short_entry = short_trades and Sell and trading_hours_cond 
short_exit = not long_trades and Buy and trading_hours_cond

var long_price = 0.00
var short_price = 0.00

var long_mult = 0.00
var short_mult = 0.00

var short_profitTarget = 0.00
var long_profitTarget = 0.00

var short_StopPrice = 0.00
var long_StopPrice = 0.00

var short_trailStopPrice = 0.00
var long_trailStopPrice = 0.00

var short_stop = 0.00
var long_stop = 0.00


long_price := (strategy.position_size > 0) and (strategy.position_size[1] <= 0) ? open : (strategy.position_size > 0) ? math.max(high,long_price[1]) : na
short_price := (strategy.position_size < 0) and (strategy.position_size[1] >= 0) ? open : (strategy.position_size < 0) ? math.min(low,short_price[1]) : na    

long_mult := long_price / 100
short_mult := short_price / 100    

long_profitTarget := use_profit_target and (strategy.position_size > 0) ? strategy.position_avg_price + ((pt_amount/100) * strategy.position_avg_price) : 99999999
short_profitTarget := use_profit_target and (strategy.position_size < 0) ? strategy.position_avg_price - ((pt_amount/100) * strategy.position_avg_price) : 0.00000

short_StopPrice := strategy.position_avg_price + ((stop_amount/100) * strategy.position_avg_price)
long_StopPrice := strategy.position_avg_price - ((stop_amount/100) * strategy.position_avg_price)

short_trailStopPrice := short_price + trailing_stop_amount * short_mult
long_trailStopPrice := long_price - trailing_stop_amount * long_mult


if use_stop and use_trailing_stop
    short_stop := math.min(short_StopPrice, short_trailStopPrice)
    long_stop := math.max(long_StopPrice, long_trailStopPrice)
if use_stop and not use_trailing_stop
    short_stop := short_StopPrice
    long_stop := long_StopPrice 
if not use_stop and use_trailing_stop
    short_stop := short_trailStopPrice
    long_stop := long_trailStopPrice 
if not use_stop and not use_trailing_stop
    short_stop := 999999.99 
    long_stop := 0.00

//ORDERS
strategy.exit(id="LE", limit = long_profitTarget, stop = long_stop, comment = "TP/SL")
strategy.exit(id="SS_E", limit = short_profitTarget, stop = short_stop, comment = "TP/SL")

strategy.close("LE",when = long_exit, comment = "LX")
strategy.close("SS_E",when = short_exit, comment = "SS_X")

strategy.entry("LE", strategy.long,when = long_entry) 
strategy.entry("SS_E", strategy.short, when = short_entry) 

if useTradingHours and not time("",trading_time, "America/New_York")
    strategy.close_all(comment = 'TX')

Overview

EMA Crossover Scott Strategy v2 plots three simple moving averages and an exponential moving average directly on the price chart alongside buy and sell arrows. Long entries occur when the closing price crosses above the swing level while staying above the third SMA within specified session hours. The strategy manages exits through configurable fixed-amount profit targets, stop losses, and trailing stops.

Details

Published by

g-moe on GitHub

Code credit

goodgains, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Moving averages, Trend, Breakout

Markets

Any symbol

Timeframes

M15-H1

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
boolfalseuseTradingHours
Trading Timesession0930-1555trading_time
Long Tradesbooltruelong_trades
Short Tradesbooltrueshort_trades
Profit Targetbooltrueuse_profit_target
float5.00pt_amount
Stopbooltrueuse_stop
float4.00stop_amount
Trailing Stopbooltrueuse_trailing_stop
float2.50trailing_stop_amount
SMA1int9smaInput1
SMA2int50smaInput2
Show all 17 settings
SettingTypeDefaultVariable
SMA3int180smaInput3
EMA1int20emaInput1
BUY/SELL Swingint3no
BUY/SELL Bar ColorboolfalseBarcolor
BUY/SELL Background ColorboolfalseBgcolor
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

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

ADA4H Optuna Strategy

by skywalker0803r
42 stars

This strategy trades longs and shorts on the price chart. Long entries require a close above the prior Donchian high, a close above a long-term SMA, and RSI above a threshold, with size set as a percentage of equity. Long exits use a stop at the prior Donchian

Strategy v5 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of ADA4H Optuna Strategy
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

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