Strategy example

Momentum Cross

Readonly source from tests/EasyLanguage/dorian-draper-learning, generated Pine Script v6, and an expiry-free example EX5.

Original EasyLanguage

  1. {-------------------------------------------------------------------
  2. Momentum Cross v1
  3. Description:
  4. - Buy when Momentum crosses above 0, Sell when it crosses below 0
  5. - Exit when Momentum changes direction (3 consecutive bars)
  6. - No filters, no limit orders, no stops
  7. - Very basic, not realistic, but good for learning
  8. - Next version will add filters and limit orders
  9. -------------------------------------------------------------------}
  10. Input:
  11. Price(Close),
  12. Length(10),
  13. BullLen(4),
  14. BearLen(4);
  15. Vars:
  16. Mom(0),
  17. BullCx(false),
  18. BearCx(false);
  19. Mom = Momentum(Price, Length);
  20. BullCx = Mom Crosses Over 0;
  21. BearCx = Mom Crosses Under 0;
  22. {Setup de entrada}
  23. If BullCx and MRO(BearCx, BearLen, 1) = -1 Then
  24. Buy Next Bar at Close of This Bar Limit;
  25. If BearCx and MRO(BullCx, BullLen, 1) = -1 Then
  26. SellShort Next Bar at Close of This Bar Limit;
  27. {Setup de salida}
  28. If Mom < Mom[1] and Mom[1] < Mom[2] Then
  29. Sell Next Bar at Market;
  30. If Mom > Mom[1] and Mom[1] > Mom[2] Then
  31. BuyToCover Next Bar at Market;

Generated Pine Script v6

  1. //@version=6
  2. strategy("MomentumCross_v1", overlay=true)
  3. // dependencies:
  4. __el_mro(bool test, int length, int instance) =>
  5. int found = 0
  6. int result = -1
  7. for offset = 0 to length - 1
  8. if test[offset]
  9. found += 1
  10. if found == instance and result == -1
  11. result := offset
  12. result
  13. price = input.source(close, "price")
  14. length = input.float(10.0, "length")
  15. bulllen = input.float(4.0, "bulllen")
  16. bearlen = input.float(4.0, "bearlen")
  17. var float mom = 0
  18. var bool bullcx = false
  19. var bool bearcx = false
  20. __el_call1 = ta.mom(price, int(length))
  21. __el_call2 = __el_mro(bearcx, int(bearlen), 1)
  22. __el_call3 = __el_mro(bullcx, int(bulllen), 1)
  23. if bar_index >= 9
  24. mom := __el_call1
  25. bullcx := ta.crossover(mom, 0)
  26. bearcx := ta.crossunder(mom, 0)
  27. if bullcx and __el_call2 == -1
  28. strategy.entry("Buy", strategy.long, limit=close)
  29. if bearcx and __el_call3 == -1
  30. strategy.entry("SellShort", strategy.short, limit=close)
  31. if mom < mom[1] and mom[1] < mom[2]
  32. strategy.close_all(comment="Sell")
  33. if mom > mom[1] and mom[1] > mom[2]
  34. strategy.close_all(comment="BuyToCover")

Compiled MetaTrader 5 example

The EX5 uses the original script filename stem and has no PineAnywhere expiration guard.

Download MomentumCross_v1.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.