How MQL5 OnInit() handles + CopyBuffer() become Pine Script v6
Unlike MQL4 (where iMA() returns a price directly inside the loop), MetaTrader 5 creates an integer indicator handle once in OnInit() and copies buffer slices via CopyBuffer(handle, bufferNum, startPos, count, arr). Every row below is generated at build time by pineForMql5().
| MetaTrader 5 MQL5 (.mq5) | Pine Script v6 |
|---|---|
int hFast = iMA(_Symbol, _Period, 21, 0, MODE_EMA, PRICE_CLOSE); | hFast = ta.ema(close, 21) |
int hSmma = iMA(_Symbol, _Period, 50, 0, MODE_SMMA, PRICE_MEDIAN); | hSmma = ta.rma(hl2, 50) |
int hHtf = iMA(_Symbol, PERIOD_H1, 20, 0, MODE_EMA, PRICE_CLOSE); | hHtf = request.security(syminfo.tickerid, "60", (ta.ema(close, 20))[1], lookahead = barmerge.lookahead_on) |
int hRsi = iRSI(_Symbol, _Period, 14, PRICE_CLOSE); | hRsi = ta.rsi(close, 14) |
int hAtr = iATR(_Symbol, _Period, 14); | hAtr = ta.atr(14) |
int hCci = iCCI(_Symbol, _Period, 20, PRICE_TYPICAL); | hCci = ta.cci(hlc3, 20) |
int hBb = iBands(_Symbol, _Period, 20, 0, 2.0, PRICE_CLOSE); | [_hBb_mid, _hBb_upper, _hBb_lower] = ta.bb(close, 20, 2.0) |
int hMacd = iMACD(_Symbol, _Period, 12, 26, 9, PRICE_CLOSE); | [_hMacd_macd, _hMacd_signal, _hMacd_hist] = ta.macd(close, 12, 26, 9) |
int hStoc = iStochastic(_Symbol, _Period, 14, 3, 3, MODE_SMA, STO_LOWHIGH); | hStoc = ta.sma(ta.stoch(close, high, low, 14), 3) |
int hAdx = iADX(_Symbol, _Period, 14); | [_hAdx_diPlus, _hAdx_diMinus, _hAdx_adx] = ta.dmi(14, 14) |
double hi = iHigh(_Symbol, _Period, iHighest(_Symbol, _Period, MODE_HIGH, 20, 1)); | hi = ta.highest(high, 20)[1] |
int shift = iHighest(_Symbol, _Period, MODE_HIGH, 20, 0); | shift = -ta.highestbars(high, 20) |
double pt = SymbolInfoDouble(_Symbol, SYMBOL_POINT); | pt = syminfo.mintick |
Why ArraySetAsSeries(arr, true) vs rates_total - 1 indexing matters
In MQL5's OnCalculate(), default arrays are indexed oldest-first (0 is the oldest candle and rates_total - 1 is the live candle), whereas calling ArraySetAsSeries(arr, true) flips index 0 to the live candle and 1 to the last closed candle—matching Pine Script's series[0] and series[1]. The converter tracks ArraySetAsSeries() and chronological for (int i = start; i < rates_total; i++) loops so arr[i - 1] and arr[1] both map to series[1].
MQL5 inputs and CTrade Expert Advisor orders
input / sinput declarations and trailing comments become TradingView settings; CTrade market calls become Pine v6 strategy.entry() and strategy.close_all().
| MetaTrader 5 MQL5 (.mq5) | Pine Script v6 |
|---|---|
input int FastLen = 21; // Fast EMA Length | FastLen = input.int(21, "Fast EMA Length") |
input double AtrMult = 1.5; // ATR Multiplier | AtrMult = input.float(1.5, "ATR Multiplier") |
input ENUM_APPLIED_PRICE PriceSrc = PRICE_TYPICAL; // Source Price | PriceSrc = input.source(hlc3, "Source Price") |
trade.Buy(0.10, _Symbol, 0, 0, 0, "Long Cross"); | strategy.entry("Long Cross", strategy.long) |
trade.Sell(0.10, _Symbol, 0, 0, 0, "Short Cross"); | strategy.entry("Short Cross", strategy.short) |
trade.PositionClose(_Symbol); | strategy.close_all() |
What cannot convert automatically (and why)
Flagged inline with TODO(MQL5) and 1-based source line numbers rather than silent guesswork.
Compiled .ex5 binaries, iCustom(), MqlTradeRequest structs and OnBookEvent()
- Compiled .ex5 vs editable .mq5 source: MetaEditor compiles
.mq5source into a binary.ex5file. Compiled.ex5programs cannot be read as source code and are never decompiled here—inspect your MT5 file type in our .mq5, .ex5 & .set file opener. - iCustom() handles: Calling
iCustom(_Symbol, _Period, "MyIndi", ...)loads another compiled indicator binary fromMQL5\Indicators. Paste that indicator's own.mq5source or merge multiple Pine scripts with our Pine Script indicator merger. - Raw MqlTradeRequest OrderSend() & PositionModify(): MT5's multi-field
MqlTradeRequeststruct sets broker fill policies (ORDER_FILLING_FOK/IOC), deviation ticks, and ticket SL/TP modifications. Express stop-loss and take-profit rules in Pine v6 withstrategy.exit(), and check multi-timeframe repainting rules in our non-repainting Pine Script guide. - Converting in another direction or platform? Use our MQL4 (.mq4) to Pine v6 converter, NinjaScript (C#) to Pine v6 converter, cTrader (C# & .algo) to Pine v6 converter, thinkScript to Pine v6 converter, or Pine Script to MQL5 port planner (paired with our Pine Script to MQL5 architecture guide).