Two engines cannot agree, and here is why
If you build one idea twice — once as MQL5 for MetaTrader 5, once as Pine for TradingView — the two backtests will not match. That is not a porting mistake. The engines model price inside a bar differently, and both vendors document it.
What MetaTrader 5 can do
The MT5 Strategy Tester offers five tick-generation modes [2]:
| Mode [2] | How price inside the bar is produced |
|---|---|
| Every tick based on real ticks | real broker-accumulated ticks — no simulation is performed |
| Every tick | all ticks simulated; the most accurate simulated mode, and the slowest |
| 1 minute OHLC | only the four prices of each minute bar are emulated |
| Open prices only | OHLC modelled, but only the open is used for testing |
| Math calculations | no history, no ticks; only OnInit(), OnTester() and OnDeinit() run |
The first row is the one with no counterpart on the other platform. Given ticks a broker actually recorded, MT5 replays the real path through the bar.
What Pine does instead
All indicators execute once per historical bar. [1]
On the realtime bar the behaviour changes — the script "executes not once, but repeatedly on the realtime bar — once for each new update (tick)" [1] — and before each of those executions the runtime performs a rollback that reverts variables and objects to their last committed states [1]. Historical bar values are final; a realtime bar updates as data arrives [1].
So Pine's history is evaluated at bar boundaries. There is no recorded intrabar path to replay, because the historical pass never had one.
The consequence, stated plainly
For any strategy whose result depends on what happened inside the bar, the two platforms are not measuring the same thing:
- a stop and a target that both sit inside one bar's range — MT5 can know which came first from real ticks; Pine must resolve it by assumption
- an intrabar breakout entry, where the fill price depends on the path
- anything triggered while a bar is still forming
And for strategies that decide only on confirmed closes and exit on later closes, the two will agree closely, because there is no intrabar question to answer.
That distinction is the useful takeaway. Getting the two numbers to match is the wrong goal — no amount of parameter matching will do it. Knowing which engine is guessing about what, for your particular strategy, is the right one.
Which to trust for which claim
| Question | Ask |
|---|---|
| Would this have filled at a real price? | MT5 with real ticks [2] |
| Does the logic do what I intended on closes? | either; Pine is faster to iterate |
| How does it behave across 40,000 bars? | TradingView, if your plan reaches that far |
| What did the spread actually cost? | MT5, against your own broker's data |
The last row is the one people skip. TradingView models commission and slippage from what you declare; MT5 tested on real ticks from the broker you will actually trade with is closer to an answer than a number you typed yourself.
What does not carry over
Indicator names port cleanly. An RSI is an RSI. What does not port is everything around them — and the dangerous cases are the ones that look identical and are measured differently, because those carry over silently.
Units that change meaning
| Setting | On TradingView | On MetaTrader 5 |
|---|---|---|
| Slippage | slippage, counted in ticks [3] | deviation in points, and a point is not a tick |
| Commission | commission_value, read according to commission_type [3] | per-lot from the broker's own contract specification |
| Position size | default_qty_type with strategy.fixed or strategy.percent_of_equity [3] | lots, subject to the symbol's min, max and step |
| Capital | initial_capital [3] | the tester's deposit, plus a leverage setting |
Copying a slippage of 3 across is the classic one. Three ticks and three points are different distances, and nothing warns you.
Timezones and sessions
A Pine session string is interpreted in the exchange's timezone, not the
chart's — which is why our own generator emits hour(time, "UTC") instead. MT5
has a separate problem: the server's time is the broker's, and it is usually
neither UTC nor yours, and it shifts with the broker's own DST rules.
A strategy that trades the London open therefore needs its session re-derived on each platform rather than copied. The session-filter rules cover the Pine side.
History, and how much of it you get
On TradingView the number of historical bars available depends on your account plan [3] — see running and maintaining a strategy for the table. On MT5 it depends on what your broker has published and what you have downloaded, and real tick history is typically far shorter than bar history [2].
The practical result is that you are rarely comparing the same date range. Fix the range explicitly on both sides before comparing anything, or the comparison means nothing.
The execution model itself
This is the one people port without noticing. MQL5 code is organised around
event handlers — OnTick() fires on each new quote. Pine has no such handler:
the script runs once per historical bar [1] and repeatedly on the realtime bar
[1], and you express "when this happens" as a condition evaluated on each
execution rather than as a callback.
So an MQL5 strategy that counts ticks, or acts on the n-th quote within a bar, has no direct Pine translation. It has to be re-expressed in terms of bars, or of a lower-timeframe request — and that re-expression is a change to the strategy, not a translation of it.
A sane porting order
- Fix the instrument, timeframe and date range on both platforms first
- Port the entry and exit conditions only, on confirmed closes
- Confirm the two trade counts are in the same neighbourhood before looking at profit — if the counts differ a lot, the logic differs, and comparing returns is premature
- Only then set costs, separately, in each platform's own units
- Expect the remaining gap and locate it, rather than trying to close it
Step 3 is the one worth insisting on. Two equity curves invite comparison and tell you almost nothing; two trade counts tell you whether you ported the strategy or wrote a new one.
Tactix AI on this workflow
AlfaTactix includes Tactix AI: use Tactix Studio to describe your idea in plain language and draft timeframes, signals, filters, and risk into the same six-step Strategy Builder form — or open Tactix Guide on any step when you only need a field explained. Review every value, then export MQL5 or Pine Script from Code Generator (form-first — not untested prompt-to-code).

