Strategy example
EMA Crossover
Readonly source from tests/EasyLanguage/ceccode-multicharts, generated Pine Script v6, and an expiry-free example EX5.
Original EasyLanguage
{ ============================================Strategy: EMA Crossover with Trend & Volatility FilterDescription:Trend-following strategy based on a fast/slow EMA crossover.Entries are allowed only when market strength is confirmedby ADX, reducing trades during sideways conditions.Risk management uses ATR-based stop loss and profit targetto adapt position sizing to market volatility.============================================ }Inputs:FastLength(9),SlowLength(21),ADXLen(14),MinADX(20),ATRLen(14),StopATR(2.0),TargetATR(3.0);Vars:FastMA(0),SlowMA(0),adxVal(0),atrVal(0);// MAs (EMA)FastMA = XAverage(Close, FastLength);SlowMA = XAverage(Close, SlowLength);// FiltersadxVal = ADX(ADXLen);atrVal = AvgTrueRange(ATRLen);// --- Entry (only in trend) ---If MarketPosition = 0 and adxVal >= MinADX then beginIf FastMA crosses over SlowMA thenBuy ("EMA Cross L") next bar at market;If FastMA crosses under SlowMA thenSellShort ("EMA Cross S") next bar at market;end;// --- Exit on opposite signal (optional; keep it for now) ---If MarketPosition = 1 and FastMA crosses under SlowMA thenSell ("Exit L") next bar at market;If MarketPosition = -1 and FastMA crosses over SlowMA thenBuyToCover ("Exit S") next bar at market;// --- ATR-based SL/TP ---If MarketPosition = 1 then beginSell ("L-SL") next bar at (EntryPrice - StopATR * atrVal) stop;Sell ("L-TP") next bar at (EntryPrice + TargetATR * atrVal) limit;end;If MarketPosition = -1 then beginBuyToCover ("S-SL") next bar at (EntryPrice + StopATR * atrVal) stop;BuyToCover ("S-TP") next bar at (EntryPrice - TargetATR * atrVal) limit;end;
Generated Pine Script v6
//@version=6strategy("SIG_EMACrossover", overlay=true)// dependencies:__el_adx(int length) =>[_, _, adx] = ta.dmi(length, length)adxfastlength = input.float(9.0, "fastlength")slowlength = input.float(21.0, "slowlength")adxlen = input.float(14.0, "adxlen")minadx = input.float(20.0, "minadx")atrlen = input.float(14.0, "atrlen")stopatr = input.float(2.0, "stopatr")targetatr = input.float(3.0, "targetatr")var float fastma = 0var float slowma = 0var float adxval = 0var float atrval = 0__el_call1 = ta.ema(close, int(fastlength))__el_call2 = ta.ema(close, int(slowlength))__el_call3 = __el_adx(int(adxlen))__el_call4 = ta.atr(int(atrlen))if bar_index >= 20fastma := __el_call1slowma := __el_call2adxval := __el_call3atrval := __el_call4if (strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0) == 0 and adxval >= minadxif ta.crossover(fastma, slowma)strategy.entry("EMA Cross L", strategy.long)if ta.crossunder(fastma, slowma)strategy.entry("EMA Cross S", strategy.short)if (strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0) == 1 and ta.crossunder(fastma, slowma)strategy.close_all(comment="Exit L")if (strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0) == -1 and ta.crossover(fastma, slowma)strategy.close_all(comment="Exit S")if (strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0) == 1strategy.exit("L-SL", stop=strategy.position_avg_price - stopatr * atrval)strategy.exit("L-TP", limit=strategy.position_avg_price + targetatr * atrval)if (strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0) == -1strategy.exit("S-SL", stop=strategy.position_avg_price + stopatr * atrval)strategy.exit("S-TP", limit=strategy.position_avg_price - targetatr * atrval)
Compiled MetaTrader 5 example
The EX5 uses the original script filename stem and has no PineAnywhere expiration guard.