Strategy · Pine Script v6 · On the chart

EMA / SMA Crossover + ADX + EMA100 Trend Filter

by FlayerDrift

This strategy automatically adapts its moving average lengths and types based on the chart's timeframe, covering scalping, intraday, and swing styles.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code192
Settings5inputs
LicenceMIT
Last update10 months ago25 Dec 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

192 lines of Pine Script v6 from ema_crossover.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

ema_crossover.pine
//@version=6
strategy(
    "EMA / SMA Crossover + ADX + EMA100 Trend Filter",
    overlay=true,
    process_orders_on_close=true
)

//==================================================
// TIMEFRAME CLASSIFICATION
//==================================================
is_scalping = timeframe.isminutes and timeframe.multiplier >= 1 and timeframe.multiplier <= 15
is_intraday = timeframe.isminutes and timeframe.multiplier >= 30 and timeframe.multiplier <= 240
is_swing    = timeframe.isdaily or timeframe.isweekly or timeframe.ismonthly


//==================================================
// MA LENGTH SETTINGS
//==================================================
short_length = 9
long_length  = 21

if is_scalping
    short_length := 5
    long_length  := 9
else if is_intraday
    short_length := 9
    long_length  := 21
else if is_swing
    short_length := 50
    long_length  := 200


//==================================================
// MOVING AVERAGES
//==================================================
short_ma = ta.ema(close, short_length)
long_ma  = ta.ema(close, long_length)

// Use SMA for swing
if is_swing
    short_ma := ta.sma(close, short_length)
    long_ma  := ta.sma(close, long_length)


//==================================================
// EMA 100 TREND FILTER (HIGHER TIMEFRAME BIAS)
//==================================================
ema100 = ta.ema(close, 50)
plot(ema100, color=color.blue, linewidth=2, title="EMA 100 (Trend Filter)")

priceAboveEMA100 = close > ema100
priceBelowEMA100 = close < ema100


//==================================================
// PLOTS
//==================================================
plot(short_ma, color=color.green, title="Short MA")
plot(long_ma,  color=color.red,   title="Long MA")


//==================================================
// CROSS CONDITIONS
//==================================================
golden_cross = ta.crossover(short_ma, long_ma)
death_cross  = ta.crossunder(short_ma, long_ma)


//==================================================
// MANUAL ADX CALCULATION
//==================================================
adxlen = input.int(14, title="ADX Smoothing")
dilen  = input.int(14, title="DI Length")

dirmov(len) =>
    up   = ta.change(high)
    down = -ta.change(low)
    plusDM  = na(up)   ? na : (up > down and up > 0   ? up   : 0)
    minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
    tr = ta.rma(ta.tr, len)
    plus  = fixnan(100 * ta.rma(plusDM, len)  / tr)
    minus = fixnan(100 * ta.rma(minusDM, len) / tr)
    [plus, minus]

adx_calc(dilen, adxlen) =>
    [plus, minus] = dirmov(dilen)
    sum = plus + minus
    100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)

adx_val = adx_calc(dilen, adxlen)


//==================================================
// ADX REGIMES
//==================================================
is_choppy      = adx_val < 20
is_early_trend = adx_val >= 20 and adx_val < 25
is_trending    = adx_val >= 25 and adx_val < 50
is_strong      = adx_val >= 50

allow_trade = is_trending or is_strong


//==================================================
// CLEAN UI TABLE
//==================================================
var table adxTable = table.new(position.top_right, 1, 1)

if barstate.isconfirmed
    table.cell(
        adxTable, 0, 0,
        is_choppy      ? "ADX: CHOPPY" :
        is_early_trend ? "ADX: EARLY" :
        is_trending    ? "ADX: TREND" :
        is_strong      ? "ADX: STRONG" :
        "ADX: N/A",
        text_color=color.white,
        bgcolor=
            is_choppy      ? color.gray :
            is_early_trend ? color.orange :
            is_trending    ? color.green :
            is_strong      ? color.red :
            color.black
    )


//==================================================
// ATR-BASED STOP LOSS + TRAILING
//==================================================
atrLen = input.int(14, "ATR Length")
atrVal = ta.atr(atrLen)
rr = input.float(3.0, "Risk Reward (Target)", step=0.1)
trailATRMult = input.float(1.0, "Trailing ATR Multiplier", step=0.1)

// ATR multiplier by timeframe
atrMult = 1.5
if is_scalping
    atrMult := 1.0
else if is_intraday
    atrMult := 1.5
else if is_swing
    atrMult := 2.0

// ---- LONG EXIT ----
if strategy.position_size > 0
    longSL   = math.max(
        strategy.position_avg_price - (atrVal * atrMult),
        strategy.position_avg_price - (atrVal * trailATRMult)
    )
    longRisk = strategy.position_avg_price - longSL
    longTP   = strategy.position_avg_price + (longRisk * rr)

    strategy.exit(
        "Buy Exit",
        from_entry = "Buy",
        stop  = longSL,
        limit = longTP,
        trail_points = atrVal * trailATRMult,
        trail_offset = atrVal * trailATRMult
    )

// ---- SHORT EXIT ----
if strategy.position_size < 0
    shortSL   = math.min(
        strategy.position_avg_price + (atrVal * atrMult),
        strategy.position_avg_price + (atrVal * trailATRMult)
    )
    shortRisk = shortSL - strategy.position_avg_price
    shortTP   = strategy.position_avg_price - (shortRisk * rr)

    strategy.exit(
        "Sell Exit",
        from_entry = "Sell",
        stop  = shortSL,
        limit = shortTP,
        trail_points = atrVal * trailATRMult,
        trail_offset = atrVal * trailATRMult
    )


//==================================================
// ENTRY CONDITIONS (EMA100 FILTERED)
//==================================================
longCondition = golden_cross and allow_trade and priceAboveEMA100 and barstate.isconfirmed

shortCondition = death_cross and allow_trade and priceBelowEMA100 and barstate.isconfirmed

if longCondition
    strategy.entry("Buy", strategy.long)

if shortCondition
    strategy.entry("Sell", strategy.short)

Overview

EMA / SMA Crossover + ADX + EMA100 Trend Filter generates entry signals when a fast moving average crosses a slow moving average, provided price is on the correct side of a baseline trend EMA and ADX indicates a trending market above 25. Position management includes an ATR-driven stop loss, target take-profit based on a risk-reward ratio, and a trailing stop. It draws moving averages directly on the price chart and displays an ADX regime table in the top right.

Details

Published by

FlayerDrift on GitHub

Type

Strategy, Pine Script v6

Draws

On the price chart

Category

Trend, Moving averages, Oscillators & momentum, Screeners & dashboards

Markets

Any symbol

Timeframes

M1-M5, M15-H1, H4 and above

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
ADX Smoothingint14adxlen
DI Lengthint14dilen
ATR Lengthint14atrLen
Risk Reward (Target)float3.0rr
Trailing ATR Multiplierfloat1.0trailATRMult
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 long-only trend-following strategy combines Ichimoku Kinko Hyo components with higher timeframe moving average filters. It triggers long entries when price trades above a higher timeframe EMA and a bullish Tenkan-Sen cross above Kijun-Sen occurs under fav

Strategy v5 TrendOscillators & momentum
Updated 1 year ago · MPL-2.0 GitHub repository of Ichimoku Kinko Hyo Momentum Strategy

This strategy calculates trend direction across three user-defined higher timeframes using dual Simple Moving Averages, filtered by On-Balance Volume and Commodity Channel Index conditions alongside fractal pivot swings. Entries trigger when multi-timeframe tr

Strategy v5 TrendMoving averages Repaints
Updated 5 months ago · None stated GitHub repository of Fractals with Pivot Trendlines Strategy
1 stars

This strategy executes trades based on MACD line crossovers filtered by trend direction and histogram slope. Long positions open on bullish MACD crossovers when price is above the trend EMA and histogram momentum is rising. Short positions open on bearish cros

Strategy v6 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of Tn Sovereign MACD v6
330 stars

2 EMA/SMA + RSI Strategy [Alorse] combines three configurable moving averages with a short-period Relative Strength Index filter. It plots fast, slow, and trend moving averages on the price chart. Long entries trigger when the fast moving average is above the

Strategy v4 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of 2 EMA/SMA + RSI Strategy [Alorse]

Acrypto - Weighted Strategy

by AlbertoCuadra
318 stars

This strategy plots on the price chart and opens long or short positions when a weighted score from MACD, Stochastic RSI, RSI, Supertrend and a moving-average crossover exceeds a user-set trigger. Each component has its own weight and candle delay. Entries can

Strategy v5 TrendOscillators & momentum
Updated 3 years ago · Custom licence GitHub repository of Acrypto - Weighted 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