Indicator · Pine Script v6 · On the chart

EMA Crossover

by rubbermetal· code credited to claygrace74

EMA Crossover plots a fast and a slow exponential moving average, fills the space between them green or red by direction, and marks each cross with a Long or Short label.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code75
Settings10inputs
LicenceMPL-2.0
Last update4 months ago10 Jun 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 indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

75 lines of Pine Script v6 from EMA Crossover.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

EMA Crossover.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © claygrace74

//@version=6
indicator("EMA Crossover", shorttitle="EMA Cross", overlay=true)

// ==================== Settings ====================
var ema_group = "EMA Settings"
ema_src   = input.source(close, "Source", group=ema_group)
ema_f_len = input.int(10, "Fast EMA Length", minval=1, group=ema_group)
ema_s_len = input.int(20, "Slow EMA Length", minval=1, group=ema_group)

var disp_group = "Display Settings"
i_showFill    = input.bool(true, "Show EMA Fill", group=disp_group)
i_showSignals = input.bool(true, "Show Cross Signals", group=disp_group)
i_showTP_SL   = input.bool(false, "Show TP/SL Reference Lines", group=disp_group)
tpPct         = input.float(3.0, "Take Profit %", minval=0.1, step=0.5, group=disp_group)
slPct         = input.float(1.0, "Stop Loss %", minval=0.1, step=0.5, group=disp_group)
fastColor     = input.color(color.green, "Fast EMA Color", group=disp_group)
slowColor     = input.color(color.red, "Slow EMA Color", group=disp_group)

// ==================================================================================
// Core Calculations
// ==================================================================================
ema_f = ta.ema(ema_src, ema_f_len)
ema_s = ta.ema(ema_src, ema_s_len)

bullish   = ema_f > ema_s
crossUp   = ta.crossover(ema_f, ema_s)
crossDown = ta.crossunder(ema_f, ema_s)

// Trend strength — distance between EMAs relative to ATR
atr     = ta.atr(14)
spread  = math.abs(ema_f - ema_s)
strong  = spread > atr * 1.5

// TP/SL reference levels from last cross
var float lastCrossPrice = na
if crossUp or crossDown
    lastCrossPrice := close

tp_long  = lastCrossPrice * (1 + tpPct / 100)
sl_long  = lastCrossPrice * (1 - slPct / 100)
tp_short = lastCrossPrice * (1 - tpPct / 100)
sl_short = lastCrossPrice * (1 + slPct / 100)

// ==================================================================================
// Plots
// ==================================================================================
pFast = plot(ema_f, "Fast EMA", color=fastColor, linewidth=2)
pSlow = plot(ema_s, "Slow EMA", color=slowColor, linewidth=2)

fill_clr = i_showFill ? (bullish ? color.new(color.green, 80) : color.new(color.red, 80)) : color(na)
fill(pFast, pSlow, color=fill_clr, title="EMA Fill")

// Cross signals
plotshape(i_showSignals and crossUp,   style=shape.triangleup,   location=location.belowbar, color=color.green, text="Long",  textcolor=color.green, size=size.small, title="Bull Cross")
plotshape(i_showSignals and crossDown, style=shape.triangledown, location=location.abovebar, color=color.red,   text="Short", textcolor=color.red,   size=size.small, title="Bear Cross")

// TP/SL reference lines
plot(i_showTP_SL and bullish ? tp_long : na,  "TP Long",  color=color.new(color.lime, 50), style=plot.style_linebr, linewidth=1)
plot(i_showTP_SL and bullish ? sl_long : na,  "SL Long",  color=color.new(color.orange, 50), style=plot.style_linebr, linewidth=1)
plot(i_showTP_SL and not bullish ? tp_short : na, "TP Short", color=color.new(color.lime, 50), style=plot.style_linebr, linewidth=1)
plot(i_showTP_SL and not bullish ? sl_short : na, "SL Short", color=color.new(color.orange, 50), style=plot.style_linebr, linewidth=1)

// Trend direction background
bgcolor(strong and bullish ? color.new(color.green, 95) : strong and not bullish ? color.new(color.red, 95) : na, title="Strong Trend BG")

// ==================================================================================
// Alert Conditions
// ==================================================================================
alertcondition(crossUp,                "Bullish EMA Cross",     "Fast EMA crossed above slow EMA")
alertcondition(crossDown,              "Bearish EMA Cross",     "Fast EMA crossed below slow EMA")
alertcondition(crossUp and strong,     "Strong Bullish Cross",  "Bullish EMA cross with strong trend spread")
alertcondition(crossDown and strong,   "Strong Bearish Cross",  "Bearish EMA cross with strong trend spread")

Overview

A background tint appears when the gap between the averages exceeds a multiple of ATR. Optional reference lines show take profit and stop loss levels as percentages from the last cross. These are visual references only. It draws on the price chart.

Details

Published by

rubbermetal on GitHub

Code credit

claygrace74, as written in the script's header

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.

Settings

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

SettingTypeDefaultVariable
Sourcesourcecloseema_src
Fast EMA Lengthint10ema_f_len
Slow EMA Lengthint20ema_s_len
Show EMA Fillbooltruei_showFill
Show Cross Signalsbooltruei_showSignals
Show TP/SL Reference Linesboolfalsei_showTP_SL
Take Profit %float3.0tpPct
Stop Loss %float1.0slPct
Fast EMA Colorcolorcolor.greenfastColor
Slow EMA Colorcolorcolor.redslowColor
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

947 stars

The Adaptive Laguerre Filter calculates a smoothed, dynamically adjusted trendline plotted directly on the main price chart. It dynamically adjusts its damping factor (alpha) based on recent price range variations and median percentile calculations over a spec

Indicator v4 TrendMoving averages
Updated 12 days ago · GPL-3.0 GitHub repository of Adaptive Laguerre Filter
330 stars

This script plots three moving averages on the price chart: a fast EMA or SMA with 10 periods, a slow EMA or SMA with 20 periods, and a trend EMA or SMA with 100 periods. It signals trend changes by drawing a white cross when the fast moving average crosses ab

Indicator v4 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of 3 EMA/SMA + Cross [Alorse]
318 stars

This indicator plots four customizable moving averages directly on the main price chart using default lengths of 3, 8, 21, and 50 periods. It fills the space between adjacent averages in green or maroon depending on trend alignment. Additionally, it computes B

Indicator v5 Moving averagesTrend
Updated 2 months ago · MIT GitHub repository of Multiple Moving Averages

NVDA Trend Ribbon

by longsurf-ai
280 stars

This indicator plots three exponential moving averages and ATR volatility envelopes on the price chart. It compares the fast and slow EMAs to shade a trend ribbon and marks confirmed upward and downward crossovers with triangle labels. Settings change the thre

Indicator v6 TrendMoving averages
Updated 3 days ago · MIT GitHub repository of NVDA Trend Ribbon
257 stars

Donald SPX - Options Trading is an overlay indicator designed for intraday SPX analysis. It plots a 200-period Weighted Moving Average (WMA) on the price chart while evaluating multi-timeframe market structure across Daily, 15-minute, 5-minute, and 2-minute ba

Indicator v5 Moving averagesTrend Repaints
Updated 7 weeks ago · None stated GitHub repository of Donald SPX - Options Trading
228 stars

This indicator computes moving average and RSI bias signals across the current timeframe and four higher timeframes. It sums biases from the MA and RSI to derive an overall bullish or bearish signal. The results appear in a configurable table that displays eac

Indicator v6 TrendMoving averages
Updated 3 months ago · None stated GitHub repository of Multi-Timeframe Analysis Template

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