The short answer
You cannot reliably convert a Pine Script strategy to MQL5 line by line. The two
languages run on different execution models: Pine runs once per historical bar [1],
while an MQL5 Expert Advisor reacts to each new quote. Fills, bar handling,
request.security and repainting all behave differently, so a line-by-line
translation compiles and then trades a slightly different strategy.
The honest method is to rebuild the rules once, not to translate the code:
- Write the strategy down as rules: entry, exit, filters, sessions and risk, all with numbers.
- Enter those rules in a form that knows both platforms. In the AlfaTactix Strategy Builder that is 7 steps with 47 indicator types, and in Step 1 you choose the platform: MetaTrader 5 (MQL5) or TradingView (Pine Script v6).
- To move a strategy to the other platform, press "Change" next to the platform in Step 1. Before it switches, the form lists the settings the other platform does not offer and how they change. A strategy is built for one platform at a time.
- Test it again on the new platform. Results from one platform are not evidence about the other.
The rest of this guide explains what breaks in a line-by-line port, why the two backtests cannot agree, and which platform suits which job.
What does not carry over line by line
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, compile, run and mean something else.
Bar handling: no event handler in Pine
MQL5 code is organised around event handlers: OnTick() runs when a new quote
arrives. Pine has no such handler. The script runs once per historical bar 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 a strategy that counts ticks, or acts on the n-th quote inside a bar, has no direct translation in either direction. It has to be re-expressed in terms of bars, and that re-expression is a change to the strategy, not a port of it. Anything defined on confirmed bar closes moves between the platforms with little friction.
Fills: when the order is filled
On historical bars, TradingView's broker emulator fills a market order at the
open of the next bar by default, and settings such as process_orders_on_close
change that [3]. An Expert Advisor's order is filled when it is sent, at the price
the trade server gives at that moment. A Pine script that relies on the next-bar-open
assumption, copied into MQL5 statement by statement, enters at a different price and
sometimes on a different bar.
request.security: other timeframes
A Pine strategy that reads a higher timeframe calls request.security. With
lookahead = barmerge.lookahead_on on historical bars, it can return a
higher-timeframe value before that bar has closed, which is information the strategy
could not have had at the time [4]. MQL5 has no request.security: an Expert Advisor
reads the other timeframe's bars itself, and which bar counts as "closed" has to be
decided again. Port the call literally and you either leak the future or shift every
signal by one higher-timeframe bar.
Repainting
A script repaints when it behaves differently on historical bars than on the realtime bar [5]: a signal that appears on a forming bar and vanishes when the bar closes, or a value calculated from data that was not yet available. A repainting Pine strategy looks better in its backtest than it can trade, and an MQL5 copy of it inherits the same flaw in a less visible form. Decide on confirmed closes before you port anything.
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 contract specification |
| Position size | default_qty_type with strategy.fixed or strategy.percent_of_equity [3] | lots, subject to the symbol's minimum, maximum and step |
| Session hours | a session string, read in the exchange's timezone | the broker's server time, which is usually neither UTC nor yours |
Copying a slippage of 3 across is the classic mistake: three ticks and three points are different distances, and nothing warns you. The session row causes the most silent breakage. A strategy that trades the London open needs its session re-derived on each platform rather than copied; the session-filter rules cover the Pine half.
Keep the strategy in a form that is neither language
A written specification (entry condition, exit condition, sizing rule, session, all with numbers) is the thing that ports cleanly, because it is what both platforms are expressing. With a specification you hold separately, switching platforms is a rebuild from a document rather than a translation of code: faster, and it produces a working strategy instead of a subtly different one. Converting a manual strategy is the same exercise from the other direction.
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.
The honest method: rebuild the rules once
Instead of translating Pine statements, put the rules into a form that already knows how each platform handles bars, sessions and orders, and let a generator write the file for the platform you choose.
In the AlfaTactix Strategy Builder
The Strategy Builder has 7 steps and 47 indicator types, up to 3 timeframes and up to 8 filters, for MetaTrader 5 (MQL5) and TradingView (Pine Script v6). You enter the entry rules, filters, exits and risk once, from your written specification.
- Step 1 sets the platform. Choose MetaTrader 5 to get an MQL5 Expert Advisor.
- Changing platform is one button. Press "Change" next to the platform in Step 1. Before it switches, the form lists the settings the other platform does not offer and how they change.
- One platform at a time. A strategy is built for MQL5 or for Pine Script v6, not both at once.
- Readable output. The generated code uses no platform indicator built-ins: every indicator is written out in the code you download, so you can read it.
Results will differ between the platforms
MetaTrader 5 and TradingView read two different price feeds, so no exact match between them is claimed: 82 to 95 % of entries fell at the same hour and side (R1 94 %, R2 82 %, R3 88 %, R5 94 %, R6 95 %). On the same bars each platform equals the engine, so the rest comes from the data: a cross by a point or two, a volume filter on two different tick counts, an exit an hour apart that changes the next position.
The backtest engine page explains how those comparisons were run.
A sane test order after the move
- Fix the instrument, timeframe and date range on the new platform first.
- Check the entry and exit conditions on confirmed closes.
- Compare the trade counts before anything else. If the counts differ a lot, the logic differs, and comparing equity curves is premature.
- Set costs only then, in the new platform's own units.
- Expect the remaining gap and locate it, rather than trying to close it.
You can backtest the rules on the site before you open MetaTrader 5: the backtest runs on the site, and you don't need MetaTrader 5 or TradingView to backtest. Backtests run on data you upload, on any timeframe down to one minute; how many days one run covers depends on your plan. The backtest is open on every plan, the Free plan included.
Next step: see what the MT5 EA builder produces, or create a free account and rebuild your TradingView strategy in Step 1 with MetaTrader 5 selected.
Choose by what has to be true
Feature comparisons between MQL5 and Pine are long and rarely decide anything, because both platforms can express most rule-based strategies. A shorter question decides it: what has to be true for your strategy to be worth running at all?
| If this has to be true | Pick | Because |
|---|---|---|
| "it would have filled at a real price" | MetaTrader 5 | its tester can replay real broker-accumulated ticks with no simulation [2] |
| "I can iterate on the idea quickly" | TradingView | the script runs on the chart with no build step, and a change is one save away |
| "the spread cost is my broker's, not a guess" | MetaTrader 5 | tested against the data of the broker you will actually trade with |
| "it holds up over tens of thousands of bars" | TradingView, if your plan reaches | up to 40,000 bars on the top tier [6] |
| "nothing of mine has to stay switched on" | TradingView | the script runs on the chart, not on a machine you maintain |
| "it must place orders without me" | MetaTrader 5 | a Pine alert is a notification, not an order [7] |
That last row is the one that ends most of these debates, and it is worth being exact about.
The line Pine does not cross
A Pine strategy can raise alerts, and an alert created from a strategy can be set so order fill events also trigger it [7]. But an alert is a notification. Getting from a fired alert to a filled position needs a broker integration, and the delay between them is yours.
An MQL5 Expert Advisor is different in kind: it runs inside a terminal that is connected to your account, and it sends orders itself. That is the actual distinction — not language features, not indicator coverage.
And the detail that catches everyone once:
Alerts only trigger in the realtime bar. [7]
A backtest showing two hundred trades produces zero alerts. Historical fills are a replay; alerts are realtime events.
What each one asks of you in return
Neither is free, and the costs are of different kinds.
MetaTrader 5 needs a terminal running on a machine you control, which means that machine has to stay on and connected — hence the VPS most EA guides assume. It also makes your backtest only as good as the tick history your broker published, which varies. See deploying and maintaining an EA.
TradingView ties how much history you can test to your subscription tier
[6], and bounds each script: up to 40 unique request.* calls (64 on the top
plan), 64 plot counts, and 20 seconds of execution time on basic accounts, 40 on
others [6]. Those are generous for a rule-based strategy and are real ceilings
for anything elaborate.
The honest summary
Use TradingView to find out whether an idea is worth anything, because iterating is fast and the chart tells you immediately. Use MetaTrader 5 to find out whether it survives real fills, because only it can test against ticks that actually happened.
They are not competitors for the same job, and why their backtests cannot agree is a property of the engines rather than a fault in either.

