Converting Pine Script to MT5: what changes and how to check the signals match
"Converted from Pine Script" should not mean "different signals".
Yet that is the most common complaint after a conversion. The MT5 version compiles, the lines look similar, and then the arrows land on different bars. Sometimes the converter made a mistake. Often the original script and MT5 simply run on different rules, and nobody wrote those rules down.
This article covers what actually changes between Pine Script and MQL5, and a practical method to check that the converted version gives the same signals as the original, bar by bar.
Why this matters from 1 November 2026
TradingView announced on 30 September 2026 that from 1 November 2026, selling access to an invite-only script is only allowed through the TradingView Marketplace (TradingView blog, also reported by FXStreet on 1 October 2026). Vendors may not take new payments outside the Marketplace to renew, extend or maintain access. Buyers who already paid keep their access for the period they purchased, including lifetime access. TradingView's Vendor Requirements page sets out the rules vendors must follow. Read the current wording there before you plan anything, because it is TradingView's rule, not ours, and it can change.
Some sellers are responding by offering MT5 or NinjaTrader versions for buyers who trade on those platforms. That only works if the port behaves like the original. Your buyers already know where the TradingView arrows appear. If the MT5 arrows appear somewhere else, the port costs you trust instead of protecting it.
What changes: the execution model
Pine Script runs your script once per historical bar, at the bar's close. On the realtime bar, an indicator runs again on every price update, and Pine rolls its variables back before each run (except varip variables), then commits the final values when the bar closes.
MQL5 has no such rollback. A custom indicator's OnCalculate() is called on new ticks and you decide which bars to recalculate using prev_calculated. An Expert Advisor's OnTick() runs on each tick, and you decide whether it acts on the forming bar or the last closed one.
So the first decision in any conversion is: does the original act on closed bars or on the forming bar? That single answer changes how every condition is written in MQL5.
Repainting: decide whether to copy it or fix it
Repainting means a script shows different values on historical bars than it showed when those bars were live. In Pine, the usual sources are:
- Reading the forming bar. On the realtime bar,
closeis the latest price, not the final close. A condition can be true mid-bar and false at the close. request.security()on a higher timeframe without the safe pattern. On historical bars the script gets the finished higher-timeframe value; on the realtime bar it gets the developing one.lookahead = barmerge.lookahead_onwithout an offset. This leaks future data into history: the backtest sees the higher-timeframe close before that bar finished. TradingView's own vendor requirements forbid exploitingrequest.*()functions to leak future data.- Plotting into the past. Pivots and swing points that are confirmed a few bars later and then drawn back on the bar where they started.
A faithful conversion of a repainting script will repaint on MT5 as well. That is sometimes what the seller wants (identical behaviour), and sometimes not (they want the MT5 version fixed). It is a decision, and it belongs in the spec before work starts.
The non-repainting higher-timeframe pattern, in both languages
In Pine Script v6, the widely used non-repainting form requests the previous higher-timeframe value with lookahead on:
//@version=6
indicator("Daily close, non-repainting")
dailyClose = request.security(syminfo.tickerid, "D", close[1], lookahead = barmerge.lookahead_on)
plot(dailyClose)
The MQL5 equivalent finds the daily bar that contains the chart bar's time and reads the one before it, which is the last finished daily bar:
// inside OnCalculate, for chart bar i
int shift = iBarShift(_Symbol, PERIOD_D1, time[i], false);
double dailyClose = iClose(_Symbol, PERIOD_D1, shift + 1);
Both versions only change value when a daily bar has finished. Both wait for that confirmation on live bars, which is the honest trade-off of not repainting.
Bar states and calc_on_every_tick
Pine gives you bar-state flags such as barstate.isconfirmed (the last update of a bar), barstate.isrealtime and barstate.isnew. A script that only signals when barstate.isconfirmed is true is a closed-bar script. In MQL5 that maps to detecting a new bar and reading index 1, the last closed bar.
Strategies add another switch. In strategy(), calc_on_every_tick defaults to false: on the realtime bar the strategy only calculates at the bar's close, matching how it ran on history. Set to true, it calculates on every tick live, while history is still calculated on closed bars. That means the live behaviour is not the behaviour you backtested.
Fill timing matters too. By default, a Pine strategy fills market orders at the next bar's open; with process_orders_on_close = true, it fills at the close of the signal bar. An MT5 EA that enters on the first tick of the new bar matches the default. Find out which one the original used before comparing results.
The same idea exists in NinjaTrader 8, where the Calculate property is set to OnBarClose, OnEachTick or OnPriceChange. Whatever the target platform, the per-bar or per-tick choice has to match the original.
Built-in functions that are not identical
Function names line up, but definitions do not always:
- ATR. Pine's
ta.atrsmooths the true range with RMA (Wilder's method). MT5's built-in ATR indicator uses a simple moving average of the true range. Same name, same period, different numbers. If your script uses ATR for stops or bands, the MQL5 version must compute Wilder's ATR itself. - Crossovers.
ta.crossover(a, b)is true whena > bon this bar anda <= bon the previous bar. A hand-written MQL5 check that uses<instead of<=misses the cases where the lines touched exactly. - Warm-up. Recursive averages (EMA, RMA, ATR) depend on where the calculation started. Two platforms with different amounts of history give slightly different values for the first few hundred bars. Compare after a warm-up of several times the longest period.
- Missing values. Pine's
naandnz()have no direct twin in MQL5, where empty indicator values are usuallyEMPTY_VALUEor zero. Eachnapath needs an explicit decision.
Data feed differences you cannot code away
TradingView and your MT5 broker usually do not use the same price feed. Different liquidity providers mean different ticks, so highs, lows and sometimes closes differ by a few points. Volume on forex symbols is tick volume and differs by source.
Daily bars are the biggest trap. Many MT5 brokers set their server clock to GMT+2 or GMT+3 so the daily bar closes at 17:00 New York time. A broker on GMT+0 produces a different daily bar and a small Sunday bar. On TradingView, the daily bar follows the session of the symbol and data source you chose. If the original uses daily or weekly levels, check that both platforms build those bars over the same hours.
Where possible, compare on TradingView using your MT5 broker's own symbol feed, if the broker provides one there. Otherwise agree a tolerance up front.
How to check the signals match
This is the method we use. You can follow it yourself.
- Freeze the test window. One symbol, one timeframe, a fixed period (for example three months), with the same inputs on both platforms. Write it down.
- Export the original signals. From TradingView, export the chart data with the signal plotted as a series, or the strategy's list of trades (export options depend on your plan). Use UTC timestamps.
- Export the MT5 signals. Have the MT5 version write every signal to a CSV file: bar open time (with the server's UTC offset noted), direction, and the price level if there is one.
- Align the clocks. Convert both files to UTC. A common first-pass "mismatch" is nothing more than a two- or three-hour time-zone shift.
- Match bar by bar. For each signal, label it: matched on the same bar, shifted by one bar, missing in MT5, or extra in MT5.
- Explain every mismatch. Look at the bar. If the OHLC values differ between the feeds enough to flip the condition, it is a data difference. If the data is the same and the signal differs, it is a code difference and gets fixed.
- Watch it live. Run both side by side on live bars for a few sessions and record the screen. Any signal that appears and later disappears is repainting, on whichever side it happens.
- Report it. A match rate within a stated tolerance, the list of every mismatch with its reason, and the test settings. Never P&L: two platforms filling at different prices will always differ in profit even when every signal matches.
When a quote includes this comparison, the report states the signal match within the agreed tolerance and explains every mismatch. It never reports profit.
A conversion checklist
- Closed-bar or forming-bar logic decided and written in the spec
- Repainting: copy it or fix it, decided in writing
- Every
request.security()call reviewed for lookahead and offset calc_on_every_tickandprocess_orders_on_closenoted for strategies- ATR and other built-ins checked against their Pine definitions
- Daily and weekly bar alignment checked against the broker's server time
- Warm-up period defined before the comparison window
- Signal comparison on a frozen window, every mismatch explained
- Live side-by-side recording to check for repainting
Convert it once, and prove it matches
Mudasir, the engineer behind AlgoTailor, has finished 680 jobs on MQL5, rated 4.9 from 620 client reviews (public MQL5 profile), and works in Pine Script, MQL4/MQL5 and NinjaScript. A conversion starts with a written spec of how the original behaves, including its repainting decisions, and ends with a recording of each rule working before final payment, plus the signal comparison report if your quote includes it. You get the full source code on final payment.
If you sell a TradingView script and want MT5 or NinjaTrader versions for your buyers, start before your deadline, not after it.
Send your Pine Script. We reply within 24 hours, Monday to Saturday (PKT), with questions and a fixed quote. Send it on algotailor.com
Trading involves substantial risk. AlgoTailor builds software to the client's specification and does not provide trading or investment advice or guarantee trading results. AlgoTailor is not affiliated with TradingView, MetaQuotes or NinjaTrader; platform names are used descriptively.
Related: TradingView Pine Script development · MT4 & MT5 development
Send your rules. Rough notes are fine. We reply within 24 hours, Monday to Saturday (PKT), with questions, a numbered spec and a fixed quote.
Send your rules