Strategy example
Momentum Cross
Readonly source from tests/EasyLanguage/dorian-draper-learning, generated Pine Script v6, and an expiry-free example EX5.
Original EasyLanguage
{-------------------------------------------------------------------Momentum Cross v1Description:- Buy when Momentum crosses above 0, Sell when it crosses below 0- Exit when Momentum changes direction (3 consecutive bars)- No filters, no limit orders, no stops- Very basic, not realistic, but good for learning- Next version will add filters and limit orders-------------------------------------------------------------------}Input:Price(Close),Length(10),BullLen(4),BearLen(4);Vars:Mom(0),BullCx(false),BearCx(false);Mom = Momentum(Price, Length);BullCx = Mom Crosses Over 0;BearCx = Mom Crosses Under 0;{Setup de entrada}If BullCx and MRO(BearCx, BearLen, 1) = -1 ThenBuy Next Bar at Close of This Bar Limit;If BearCx and MRO(BullCx, BullLen, 1) = -1 ThenSellShort Next Bar at Close of This Bar Limit;{Setup de salida}If Mom < Mom[1] and Mom[1] < Mom[2] ThenSell Next Bar at Market;If Mom > Mom[1] and Mom[1] > Mom[2] ThenBuyToCover Next Bar at Market;
Generated Pine Script v6
//@version=6strategy("MomentumCross_v1", overlay=true)// dependencies:__el_mro(bool test, int length, int instance) =>int found = 0int result = -1for offset = 0 to length - 1if test[offset]found += 1if found == instance and result == -1result := offsetresultprice = input.source(close, "price")length = input.float(10.0, "length")bulllen = input.float(4.0, "bulllen")bearlen = input.float(4.0, "bearlen")var float mom = 0var bool bullcx = falsevar bool bearcx = false__el_call1 = ta.mom(price, int(length))__el_call2 = __el_mro(bearcx, int(bearlen), 1)__el_call3 = __el_mro(bullcx, int(bulllen), 1)if bar_index >= 9mom := __el_call1bullcx := ta.crossover(mom, 0)bearcx := ta.crossunder(mom, 0)if bullcx and __el_call2 == -1strategy.entry("Buy", strategy.long, limit=close)if bearcx and __el_call3 == -1strategy.entry("SellShort", strategy.short, limit=close)if mom < mom[1] and mom[1] < mom[2]strategy.close_all(comment="Sell")if mom > mom[1] and mom[1] > mom[2]strategy.close_all(comment="BuyToCover")
Compiled MetaTrader 5 example
The EX5 uses the original script filename stem and has no PineAnywhere expiration guard.