Strategy example
Moving Average Cross
Readonly source from tests/EasyLanguage/dorian-draper-learning, generated Pine Script v6, and an expiry-free example EX5.
Original EasyLanguage
{-------------------------------------------------------------------Mov Avg Cross v1Description:- Moving Average Crossover strategy using simple moving averages- Enters long when the fast MA crosses above the slow MA, and short when it crosses below- Exits positions if they have been held for a minimum time and haven't reached a minimum profit-------------------------------------------------------------------}Input:ShortPrice(Close),LongPrice(Close),ShortLen(9),LongLen(18),MinHold(8),MinProfit(100);Vars:ShortMA(0),LongMA(0);ShortMA = Average(ShortPrice, ShortLen);LongMA = Average(LongPrice, LongLen);{Setup entrada}If ShortMA Crosses Over LongMA ThenBuy Next Bar at Market;If ShortMA Crosses Under LongMA ThenSellShort Next Bar at Market;{Setup salida}If BarsSinceEntry > MinHold and OpenPositionProfit < MinProfit ThenBeginSell Next Bar at Market;BuyToCover Next Bar at Market;End;
Generated Pine Script v6
//@version=6strategy("MovingAverageCross_v1", overlay=true)shortprice = input.source(close, "shortprice")longprice = input.source(close, "longprice")shortlen = input.float(9.0, "shortlen")longlen = input.float(18.0, "longlen")minhold = input.float(8.0, "minhold")minprofit = input.float(100.0, "minprofit")var float shortma = 0var float longma = 0__el_call1 = ta.sma(shortprice, int(shortlen))__el_call2 = ta.sma(longprice, int(longlen))if bar_index >= 17shortma := __el_call1longma := __el_call2if ta.crossover(shortma, longma)strategy.entry("Buy", strategy.long)if ta.crossunder(shortma, longma)strategy.entry("SellShort", strategy.short)if (strategy.opentrades > 0 ? bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades - 1) : 0) > minhold and strategy.openprofit < minprofitstrategy.close_all(comment="Sell")strategy.close_all(comment="BuyToCover")
Compiled MetaTrader 5 example
The EX5 uses the original script filename stem and has no PineAnywhere expiration guard.