Strategy example

EMA Crossover

Readonly source from tests/EasyLanguage/ceccode-multicharts, generated Pine Script v6, and an expiry-free example EX5.

Original EasyLanguage

  1. { ============================================
  2. Strategy: EMA Crossover with Trend & Volatility Filter
  3. Description:
  4. Trend-following strategy based on a fast/slow EMA crossover.
  5. Entries are allowed only when market strength is confirmed
  6. by ADX, reducing trades during sideways conditions.
  7. Risk management uses ATR-based stop loss and profit target
  8. to adapt position sizing to market volatility.
  9. ============================================ }
  10. Inputs:
  11. FastLength(9),
  12. SlowLength(21),
  13. ADXLen(14),
  14. MinADX(20),
  15. ATRLen(14),
  16. StopATR(2.0),
  17. TargetATR(3.0);
  18. Vars:
  19. FastMA(0),
  20. SlowMA(0),
  21. adxVal(0),
  22. atrVal(0);
  23. // MAs (EMA)
  24. FastMA = XAverage(Close, FastLength);
  25. SlowMA = XAverage(Close, SlowLength);
  26. // Filters
  27. adxVal = ADX(ADXLen);
  28. atrVal = AvgTrueRange(ATRLen);
  29. // --- Entry (only in trend) ---
  30. If MarketPosition = 0 and adxVal >= MinADX then begin
  31. If FastMA crosses over SlowMA then
  32. Buy ("EMA Cross L") next bar at market;
  33. If FastMA crosses under SlowMA then
  34. SellShort ("EMA Cross S") next bar at market;
  35. end;
  36. // --- Exit on opposite signal (optional; keep it for now) ---
  37. If MarketPosition = 1 and FastMA crosses under SlowMA then
  38. Sell ("Exit L") next bar at market;
  39. If MarketPosition = -1 and FastMA crosses over SlowMA then
  40. BuyToCover ("Exit S") next bar at market;
  41. // --- ATR-based SL/TP ---
  42. If MarketPosition = 1 then begin
  43. Sell ("L-SL") next bar at (EntryPrice - StopATR * atrVal) stop;
  44. Sell ("L-TP") next bar at (EntryPrice + TargetATR * atrVal) limit;
  45. end;
  46. If MarketPosition = -1 then begin
  47. BuyToCover ("S-SL") next bar at (EntryPrice + StopATR * atrVal) stop;
  48. BuyToCover ("S-TP") next bar at (EntryPrice - TargetATR * atrVal) limit;
  49. end;

Generated Pine Script v6

  1. //@version=6
  2. strategy("SIG_EMACrossover", overlay=true)
  3. // dependencies:
  4. __el_adx(int length) =>
  5. [_, _, adx] = ta.dmi(length, length)
  6. adx
  7. fastlength = input.float(9.0, "fastlength")
  8. slowlength = input.float(21.0, "slowlength")
  9. adxlen = input.float(14.0, "adxlen")
  10. minadx = input.float(20.0, "minadx")
  11. atrlen = input.float(14.0, "atrlen")
  12. stopatr = input.float(2.0, "stopatr")
  13. targetatr = input.float(3.0, "targetatr")
  14. var float fastma = 0
  15. var float slowma = 0
  16. var float adxval = 0
  17. var float atrval = 0
  18. __el_call1 = ta.ema(close, int(fastlength))
  19. __el_call2 = ta.ema(close, int(slowlength))
  20. __el_call3 = __el_adx(int(adxlen))
  21. __el_call4 = ta.atr(int(atrlen))
  22. if bar_index >= 20
  23. fastma := __el_call1
  24. slowma := __el_call2
  25. adxval := __el_call3
  26. atrval := __el_call4
  27. if (strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0) == 0 and adxval >= minadx
  28. if ta.crossover(fastma, slowma)
  29. strategy.entry("EMA Cross L", strategy.long)
  30. if ta.crossunder(fastma, slowma)
  31. strategy.entry("EMA Cross S", strategy.short)
  32. if (strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0) == 1 and ta.crossunder(fastma, slowma)
  33. strategy.close_all(comment="Exit L")
  34. if (strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0) == -1 and ta.crossover(fastma, slowma)
  35. strategy.close_all(comment="Exit S")
  36. if (strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0) == 1
  37. strategy.exit("L-SL", stop=strategy.position_avg_price - stopatr * atrval)
  38. strategy.exit("L-TP", limit=strategy.position_avg_price + targetatr * atrval)
  39. if (strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0) == -1
  40. strategy.exit("S-SL", stop=strategy.position_avg_price + stopatr * atrval)
  41. 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.

Download SIG_EMACrossover.ex5
An unexpected error occurred. Reload×

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.