Skip to main content

Fixing “invalid pointer access in ‘project-file-….mq5’” (MT5 Backtest Crash)

MT5 backtest crash “invalid pointer access”? It’s usually a bad SL/TP/ATR/variable value at one tick. Learn how to reproduce in Visual Mode, visualize variables, find the exact value that breaks, and fix it with guards and minimums.

Updated over 2 months ago

Summary

If MetaTrader 5 stops your backtest with “invalid pointer access in ‘project-file-….mq5’”, your exported EA hit a runtime crash. In plain English: at one specific moment in the backtest, a value used in your strategy (usually SL/TP, ATR, or a variable) became invalid, and MT5 terminated the EA.

This is almost never a login/broker issue. It’s typically a strategy math / build-logic issue that surfaces mid-backtest.


What the error means (plain language)

MT5 tried to execute a trade or management action, but a required value was not usable (for example: stop-loss distance = 0, a negative price level, a missing variable, or a division by zero). When that happens, MT5 can crash the running EA and prints invalid pointer access.


Why it can look “random”

The EA may run fine for hours or days and then suddenly crash. That’s because the invalid value happens only on one specific tick/candle, e.g.:

  • ATR briefly returns 0 (no data yet / edge case)

  • your computed SL becomes negative or equals entry

  • a variable wasn’t set in a branch and is used later

  • a candle ID points to data that doesn’t exist yet

So the crash is deterministic — it’s just hiding until the exact moment the bad value occurs.


Most frequent reasons for an invalid pointer access

These are the patterns we see most often:

ATR-based SL/TP logic

  • ATR variable not stored/updated before being used

  • ATR value becomes 0 (or extremely small), causing SL distance to collapse

Invalid SL/TP calculations

  • SL/TP becomes 0, negative, or lands on the wrong side of entry

  • SL/TP distance too small (effectively “invalid stops”)

  • Using “price fraction” vs “price level” incorrectly

Pips/points conversion issues

  • Mixing points and pips causes SL/TP to be off by 10x or 100x

  • Leads to impossible levels or negative distances after math

Variables missing / not initialized

  • Variable used in a formula before it’s set

  • A logic branch skips a “set variable” step, but later blocks assume it exists

Division by zero

  • Any formula where a divisor can become 0 (range width, ATR, spread, etc.)

Negative range / offsets

  • Range width becomes negative due to wrong subtraction order

  • Time/window offsets produce invalid values

Unset or invalid Candle ID

  • Candle ID references data that is not available at that moment (common early in backtest)


Recommended debug workflow (fast, no guessing)

Step 1 — Reproduce it in MT5 Visual Mode

Run the Strategy Tester in Visual Mode and note:

  • Symbol

  • Timeframe

  • Date range

  • The approximate time/candle where it crashes

Step 2 — Visualize the variables that drive SL/TP/ATR/Offsets

In Profectus, visualize the values that feed your trade execution and management logic:

  • ATR value (if used)

  • SL distance / SL price level

  • TP distance / TP price level

  • Any range width / offsets / multipliers

  • Any variables used in Formula blocks that end up in Buy/Sell blocks

Step 3 — Watch the values right before the crash

Re-run the backtest and observe the visualized values as you approach the crash candle.
You’re looking for the moment where a value becomes impossible, e.g.:

  • ATR = 0

  • SL distance ≤ 0

  • SL price equals entry price

  • Range width < 0

  • A multiplier flips to 0 unexpectedly

Step 4 — Fix by adding “guards” (minimums + validity checks)

Typical fixes:

  • Ensure the variable is set every candle before it’s used (especially ATR/range variables)

  • Enforce minimum distances:

    • SL distance must be > 0

    • TP distance must be > 0

  • Prevent negative values:

    • Use correct subtraction order (high − low, not low − high)

  • Avoid division by zero:

    • Add logic to skip calculations when divisor is 0

  • Confirm correct unit:

    • If you mean pips, don’t accidentally compute points (or vice versa)

If you can’t guarantee the value is valid on every tick, don’t let the EA use it.


If you’re still stuck: what to send support

Send the following so we can pinpoint it quickly:

  1. Screenshot(s) of the blocks that compute SL/TP/ATR/offsets (Formula + Variables + Buy/Sell/Modify SLTP)

  2. Screenshot showing your visualized variables right before the crash

  3. Exact symbol + timeframe + date range where it crashes

  4. The exact MT5 message: “invalid pointer access in ‘project-file-….mq5’”

Did this answer your question?