Summary: A practical walkthrough of resolving MT5 historical data export failures during account statement generation, including specific formatting pitfalls and file path permission denials.




I hit a wall last Thursday trying to generate an equity curve for a prop firm audit. The client needed a clean CSV of their MT5 account history from the last three months. I fired up the standard script, hit export, and boom — "History error: Failed to get data."

The first suspect was the obvious one: the date range. I double-checked the datetime input format in the script. The MQL5 documentation on Date and Time specifies that the TimeCurrent() and struct MqlDateTime are your go-to. But my script was using a hardcoded string format that MT5 didn't like. The fix was converting everything to datetime variables explicitly.

But the real headache started after I fixed that. The file would save, but when I opened it in Excel, the Open and Close price columns were swapped for positions that straddled the weekend. This is a classic MT5 quirk: when you request history data, the weekend gap can cause a shift in the indexing if you don't specify a timezone in the HistorySelectByPosition function.

Here's where my unique insight kicks in: most tutorials tell you to use HistorySelect(0, TimeCurrent()) to get the entire history. But this overloads the memory if you have a year of data, and more importantly, it pulls tick-level data if available, which slows down the export significantly for the equity curve. For an equity curve, you only need the daily or trade level closing balance.

I traced the issue to the OrderGetDouble(ORDER_PRICE_OPEN) function. It was returning the correct value, but the HistoryDealGetDouble for the same deal was off by a pip because of the timezone mismatch. The official MetaQuotes Help Center (help.metaquotes.net) mentions that HistorySelect uses the broker's server time, but it doesn't explicitly warn you that exporting that data directly without converting to local time can shift your daily profit calculations.

My solution to bypass this was to restructure the output:
  • <strong>Select only closed positions</strong> using HistorySelect(0, TimeCurrent()) but filter by POSITION_REASON.

  • <strong>Loop through the deals</strong> using HistoryDealGetTicket and HistoryDealGetDouble to pull DEAL_PROFIT.

  • <strong>Recalculate the balance curve manually</strong> inside the script instead of relying on the built-in AccountInfoDouble(ACCOUNT_EQUITY) at specific time slices. This ensures the equity curve matches the <em>closed</em> trade P&L, not floating P&L, which is what the prop firm wanted.


  • Another silent issue: file permission. On Windows Server, the script kept failing silently because it was trying to write to the root C:\ folder. I moved the export path to C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\... and it worked.

    The step-by-step fix for "History Error":
  • <strong>Explicit Date Format:</strong> Convert input dates to datetime using StringToTime or StructToTime to avoid parsing errors.

  • <strong>Timezone Handling:</strong> Add + (broker_offset * 3600) to your timestamps if you compare TimeCurrent() with the server time to avoid weekend shift issues.

  • <strong>Permission Path:</strong> Never export to system-protected folders. Use TerminalInfoString(TERMINAL_DATA_PATH) + "\MQL5\Files" as your base directory.

  • <strong>Reduce Data Load:</strong> Use HistorySelectByPosition to fetch only necessary deals, not the entire server history.


  • Reference: MQL5 Documentation - Date and Time (www.mql5.com), MetaQuotes Help Center - History Data export.

    This article was originally published on FXEAR.com. All rights reserved. Unauthorized reproduction is prohibited.