Learning Center

Converting strategy.entry and strategy.exit stops and targets

Pine uses ticks and percentages; MetaTrader wants absolute prices that respect your broker's stop level.

Pine Script exits are relative

strategy.exit with loss= and profit= measures in ticks from the entry. MetaTrader needs absolute stop-loss and take-profit prices, rounded to the symbol's digits, and far enough from price to satisfy SYMBOL_TRADE_STOPS_LEVEL.

Pine Script

strategy.entry("L", strategy.long)
strategy.exit("X", "L", loss=200, profit=400)  // ticks

MQL5

double tick = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double ask  = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double sl   = NormalizeDouble(ask - 200 * tick, _Digits);
double tp   = NormalizeDouble(ask + 400 * tick, _Digits);
long   minPts = SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);
if((ask - sl) / _Point < minPts) sl = NormalizeDouble(ask - minPts * _Point, _Digits);
trade.Buy(Lots, _Symbol, ask, sl, tp);

Position size

Pine's default_qty_type=strategy.percent_of_equity has no direct MetaTrader equivalent. Calculate lots from the money you are willing to lose and the stop distance, then clamp to SYMBOL_VOLUME_MIN, SYMBOL_VOLUME_MAX and SYMBOL_VOLUME_STEP. See the lot size calculator snippets linked below.

Related code snippets