Pine Script and MQL5 conversion guides
Practical guides for traders and developers moving scripts between TradingView (Pine Script) and MetaTrader (MQL4 and MQL5). Each one shows the Pine and the MQL side by side, explains where the two platforms really differ, and lists the mistakes that make a converted script compile but compute something else. Every complete MQL program in these guides compiles in MetaEditor.
Want the conversion done for you? The converter handles both directions, and every result is checked against the original on the same price history (see how conversions are measured). For finished examples, browse the verified example conversions.
Getting started
- Pine Script vs MQL5: Differences That Matter for Conversion
Pine Script and MQL5 differ in execution model, bar indexing, missing values and trading. What each difference means when you convert an indicator or strategy. - Convert a Pine Script Indicator to MQL5, Step by Step
A worked conversion of a TradingView indicator to MQL5: properties, inputs, the bar loop, ta.ema, plotshape, and fixes for common MetaEditor compile errors. - How to Convert an MQL4 or MQL5 Indicator to Pine Script
Move a MetaTrader indicator or EA to TradingView: buffers to plots, handles to ta.* calls, bar loops to series, and the MQL features Pine cannot express. - MQL4 or MQL5: Which Target for a TradingView Script?
Converting a Pine Script for MetaTrader? Compare MQL4 and MQL5 on indexing, drawing styles, order handling and testing, and see which features survive in each. - Pine Script v4, v5 and v6: What Changes When You Convert
Pine Script v4 to v5 renames and the v5 to v6 changes that alter results, such as lazy and/or, bool na and integer division, and what they mean for conversion.
Series, values and time
- Pine close[1] vs MQL5 arrays: indexing and ArraySetAsSeries
How Pine Script history references such as close[1] map to MQL5 arrays: oldest-first OnCalculate indexing, ArraySetAsSeries, iClose shifts and bar 0. - ta.sma, ta.ema and ta.rma in MQL5: seeding and warm-up
Match Pine Script ta.sma, ta.ema and ta.rma in MQL5: the exact formulas, how each average starts, which iMA modes correspond and why early values differ. - Pine na, nz() and MQL5 EMPTY_VALUE: missing values
How Pine Script na and nz() map to MQL5: EMPTY_VALUE, NaN and PLOT_EMPTY_VALUE, how missing values propagate, and the checks that keep plots and signals right. - ta.* inside if in Pine Script: per-call history explained
Why ta.sma, ta.ema and other ta.* calls inside an if, a ternary or a v6 and/or give different values in Pine Script, and how to fix or reproduce them in MQL5. - Pine time and timestamp() in MQL5: time zones and units
Convert Pine Script time, timestamp(), hour and session filters to MQL5: milliseconds vs seconds, exchange time vs broker server time, and DST pitfalls.
Plots, drawings, inputs and alerts
- Pine plot() to MQL5 indicator buffers and colours
How Pine Script plot(), plotshape(), fill() and per-bar colours map to MQL5 indicator buffers, DRAW_COLOR_LINE, DRAW_ARROW and EMPTY_VALUE, with code. - Pine barcolor and bgcolor in MetaTrader 5
MetaTrader 5 has no barcolor() or bgcolor(). How to rebuild Pine bar and background colours with DRAW_COLOR_CANDLES and chart rectangles in MQL5. - Pine line.new, label.new, box.new as MQL5 objects
Convert Pine Script line.new, label.new and box.new to MQL5 chart objects: OBJ_TREND, OBJ_TEXT, OBJ_RECTANGLE, coordinates, drawing limits and clean-up. - Pine inputs in MQL5: options, source, timeframe
Map Pine Script input.int, input.string options, input.source, input.timeframe and minval/maxval to MQL5 input variables, enums and OnInit checks. - Pine alertcondition() and alert() in MetaTrader 5
Reproduce Pine Script alert() frequencies and alertcondition() in MetaTrader 5 with Alert, SendNotification and per-bar guards, without popups on load.
Strategies and advanced topics
- Convert a Pine Script Strategy to an MQL5 Expert Advisor
How a TradingView strategy becomes a MetaTrader 5 Expert Advisor: bar-close evaluation, orders and fills, stops, netting vs hedging, and testing it. - Pine percent_of_equity and cash Sizing as MT5 Lots
Turn Pine default_qty_type percent_of_equity, cash and fixed quantities into MetaTrader 5 lot sizes, with tick value, lot step and minimum lot handled. - request.security and Multi-Timeframe Logic in MQL5
Convert Pine request.security to MQL5: higher-timeframe values per bar with iBarShift, indicator handles, lookahead and repainting, other symbols. - Pine User-Defined Types, Methods and Libraries in MQL5
Convert Pine type, method and library code to MQL5 structs, classes and include files, keeping Pine's reference semantics and bar-by-bar state. - How to Check a Converted Indicator Matches TradingView
A repeatable way to prove an MQL5 conversion computes the same values as the Pine original: same bars, exported series, timestamp alignment, tolerances.
Troubleshooting converted scripts
- Array out of range in a converted MQL5 indicator: the fix
The MT5 journal shows array out of range and the indicator stops. Why close[i - 1] at bar 0 does it after a Pine port, and the start guard that fixes it. - Cannot use plot in local scope: porting MQL5 to Pine Script
TradingView rejects plot() inside if, for or a function. Why MQL5 code that fills buffers conditionally hits it, and the fix: a global plot(cond ? x : na). - Converted EA opens no trades in the Strategy Tester: checklist
A Pine strategy ported to an MT5 EA runs but never trades. Check AutoTrading, new-bar logic, lot size, stop distance, account mode and the trade retcode. - Converted MQL5 indicator shows nothing on the chart: checklist
A ported indicator compiles and attaches but draws no line. Check the journal, buffer and plot counts, SetIndexBuffer, EMPTY_VALUE, DRAW_BEGIN and the window. - Implicit conversion from 'double' to 'string' in MQL5
MetaEditor warning 94 when Pine string concatenation is ported to MQL5. Why + on a number warns, what it prints, and the DoubleToString and StringFormat fix. - Pine Script mismatched input error: causes and fixes
Mismatched input expecting end of line in Pine Script usually means MQL habits: braces, semicolons, C-style loops or wrong indentation. Each cause and fix. - Possible loss of data due to type conversion in MQL5: the fix
MetaEditor warning 43 after porting Pine Script: a double lands in an int. Where it comes from, when it is harmless, and the casts that silence it. - Undeclared identifier in MQL5 after a Pine Script conversion
MetaEditor says undeclared identifier 'close', 'ta', 'na' or 'hl2' in code ported from Pine. Each cause, the exact error text, and the MQL5 you write instead.
Lookups and worked indicators
For a single function rather than a topic, the Pine Script ↔ MQL5 reference has one page per built-in (ta.rsi, iMA, strategy.entry, OnCalculate…) and two one-page cheat sheets. For a complete indicator on both platforms, see the TradingView indicators in MetaTrader 5.
Convert your script
Paste a Pine Script or MQL4/MQL5 indicator or strategy and get the other platform's source, with every construct that could not be carried over reported.
Open the converter