Blog / Code & convert EAs · Pine Script · Backtesting · MQL5
Why Your Pine Backtest and MT5 Backtest Don't Match
Understand why Pine Script and MT5 backtests produce different results and how to identify the cause.
· 6 min read
You convert a TradingView strategy into an MQL5 Expert Advisor, run both backtests, and get different results.
TradingView shows 150 trades. MT5 shows 132. Entry prices, profit, and drawdown are also different.
Does this mean the conversion is wrong?
Not necessarily. TradingView and MT5 can use different historical data, execution models, and trading assumptions. Even identical strategy logic can produce different backtest results.
The first step is to find where the two versions begin to behave differently.
1. Different historical price data
TradingView and MT5 may obtain prices from different providers.
TradingView uses the selected chart's data feed, while MT5 generally uses the broker's historical data. Even when both platforms display EURUSD, their candle prices may not match exactly.
Small differences in highs, lows, and closing prices can change breakout signals, indicator values, and stop-loss or take-profit execution.
What to check:
- Confirm that both platforms use the same instrument and timeframe.
- Compare the historical dates being tested.
- Inspect OHLC values on candles where signals differ.
- Check whether the feeds use comparable price bases, such as Bid or Last.
Do not assume that identical symbol names mean identical price data.
2. Different backtesting models
TradingView's broker emulator uses chart data to simulate historical order execution. Its Bar Magnifier can use lower-timeframe data to provide more detailed intrabar assumptions.
MT5 offers several testing modes:
| MT5 mode | How it works |
|---|---|
| Open prices only | Tests using bar-opening prices |
| 1 Minute OHLC | Uses four price points from each M1 candle |
| Every tick | Generates intermediate ticks from available minute data |
| Every tick based on real ticks | Uses available historical broker tick data |
These differences can affect whether an entry signal is detected or whether a stop-loss or take-profit is reached first.
What to check:
For a detailed comparison, use MT5's Every tick based on real ticks mode when suitable historical tick data is available. Also check whether TradingView's Bar Magnifier is enabled.
Using the most detailed setting on both platforms does not guarantee identical tick sequences or fills.
3. Different signal and execution timing
Both platforms can identify the same signal but execute the trade at different times.
By default, Pine Script strategies calculate at the closing update of a historical bar. A market order generated then is normally filled at the next available tick, typically the next bar's open.
Settings such as process_orders_on_close, calc_on_every_tick, and calc_on_order_fills can change this behaviour. Pine Script vs MQL5: How the Execution Models Differ explains each one.
An MQL5 EA may evaluate its conditions inside OnTick() and send an order when its conditions are met.
What to check:
- Does the Pine strategy use completed candles or intrabar calculations?
- Does the EA evaluate the current candle or the previous closed candle?
- Does it enter immediately or wait for a new candle?
- Are exit conditions evaluated at the same point?
Compare signal timestamps separately from actual execution timestamps. This helps distinguish a signal-calculation problem from an order-execution difference.
4. Different trading costs
Even when both versions generate identical trades, their profits can differ because of trading costs.
Compare these settings:
| Cost | What to check |
|---|---|
| Spread | Are both tests using comparable spreads? |
| Commission | Are entry and exit commissions included? |
| Slippage | Are differences between requested and executed prices accounted for? |
| Swap | Are overnight financing costs included? |
TradingView allows commission and slippage to be configured in its strategy properties. MT5 uses broker-specific trading conditions, including historical spread data and symbol specifications.
This matters especially for scalping strategies, where small costs across many trades can significantly affect results.
Use realistic costs for the instrument and broker. Do not add arbitrary adjustments just to make the reports match.
5. Different position sizing and account settings
TradingView and MT5 use different position-sizing systems.
TradingView supports fixed quantities, cash-based sizing, and percentage-of-equity sizing. MT5 uses lot sizes governed by the broker's contract specifications and volume limits.
For example, a TradingView strategy using 10% of equity does not automatically translate into an MQL5 EA trading 0.10 lots.
Compare:
- Initial balance and account currency.
- Position-sizing method and actual trade volume.
- Leverage and margin requirements.
- Maximum simultaneous entries.
- Whether profits are compounded.
- Contract size and volume limits.
Compare actual position exposure, not just the displayed lot or quantity.
6. Different position-management rules
Pine Script and MQL5 do not automatically manage positions in the same way.
For example, Pine's strategy.entry() can reverse an existing position when an opposite-direction entry is generated. Its pyramiding setting controls successive entries in the same direction.
In MT5, position handling also depends on whether the account uses netting or hedging. The EA must implement its own reversal, entry, and exit rules.
What to check:
- Reversal behaviour.
- Pyramiding limits.
- Maximum open positions.
- Partial exits.
- Stop-loss and take-profit management.
- Duplicate-signal handling.
If trade counts differ, inspect position management before assuming the indicator calculations are wrong.
7. Synthetic chart prices
Check whether the TradingView strategy uses Heikin Ashi, Renko, or another non-standard chart.
These chart types can use transformed prices rather than ordinary market OHLC values. An EA tested on standard MT5 candlesticks may therefore generate different signals.
TradingView also provides fill_orders_on_standard_ohlc for strategies running on Heikin Ashi charts. This allows simulated fills to use standard OHLC prices instead of synthetic chart prices.
What to check:
Confirm the original chart type and price source. If the strategy intentionally uses synthetic prices, reproduce those calculations in MQL5. Otherwise, compare both versions using standard market prices.
8. Find the first trade that differs
Comparing total profit and drawdown will not tell you what went wrong. You need to locate the first point where the two versions behave differently.
Use this process:
- Compare signals: Record the timestamps of buy and sell signals from both versions.
- Compare entries: Check whether both generated the signal on the same candle and attempted execution at the same time.
- Compare prices: If the signals match but entry prices differ, investigate data, spread, and execution timing.
- Compare exits: Check whether the trade closed because of an opposite signal, stop-loss, take-profit, or another condition.
- Compare volume and costs: Confirm that position exposure and trading costs are equivalent.
- Repeat: Move to the next trade until you find the first unexplained difference.
A simple trade comparison is enough to get started:
| Trade | TradingView | MT5 | What to investigate |
|---|---|---|---|
| 1 | Buy at 10:15 | Buy at 10:15 | Entry prices |
| 2 | Sell at 11:30 | Sell at 11:45 | Signal timing |
| 3 | Buy at 13:00 | No trade | Candle data and conditions |
These examples are illustrative. Use actual trade history from both backtests when debugging.
Final checklist
Before concluding that your Pine-to-MQL5 conversion is incorrect, confirm that:
- Both platforms use comparable historical data.
- Symbol and timeframe match.
- Signal and execution timing are equivalent.
- Tick modelling assumptions are understood.
- Spread, commission, slippage, and swap are accounted for.
- Position sizing and account settings are comparable.
- Reversal and pyramiding rules match.
- Both strategies use equivalent price sources.
The goal is not to force both platforms to produce identical profit figures. It is to confirm that the strategy logic is equivalent and understand which differences come from the testing environment.
PositionAlpha's Pine Script to MQL5 converter converts TradingView indicators (Pine Script v4, v5 and v6) into MT5 indicators and reports what it changed or left out. From a strategy() script it converts the calculations and plots, not the order logic. For the trading rules, describe them in the Bot Builder to get an MT5 Expert Advisor. Compile and test either result in MT5 before you rely on it.
Build a bot from your idea
Describe your trading idea and the AI writes MT4 or MT5 code for you to review and test, with a plain-English explanation and setup steps. Your first bot is free.