If you've ever tried to run a backtest on a specific date range that isn't neatly covered by the built-in "Every tick" model, you know the frustration. The default export function in MT4 only gives you daily or minute data, but what if you need clean 5-minute bars for a three-year stretch that includes a specific event you're studying?
I ran into this while trying to validate a scalping strategy during the 2020 March volatility. The standard export just wasn't cutting it.
The Default Export Route (and its limits)
Open the History Center via F2 or navigate to Tools > History Center. You can double-click a pair, select a timeframe like M5, and hit "Export." MT4 will dump a CSV file into your
\MQL4\Files folder. The problem? It's clunky. You can only export one timeframe at a time, and the date range is tied to whatever is currently loaded in your chart.The Hidden Folder Trick
Here's something I stumbled across that the official docs gloss over. MT4 stores all its historical data as
.hst files in the \history\[BrokerName]\ subfolder. But the data you see in the History Center isn't always the raw data. If you've ever downloaded data from multiple brokers, you'll notice these folders pile up. MT4 will prioritize the .hst file that matches the current chart's symbol.If you want to export data that isn't corrupted by gaps or broker-specific quirks, you can swap these
.hst files manually. But that's a headache. Instead, use the built-in script Period_Converter found in the Scripts folder of Navigator. This script, mentioned in the MQL4 documentation (docs.mql4.com) under working with files, can generate offline charts, which effectively lets you create custom period data.My Script-Based Workaround for Custom Dates
The
Period_Converter script is fine, but it doesn't let you pick a start and end date easily. So I wrote a simple modification. You set extern int StartYear = 2020; and extern int EndYear = 2023; and the script will only process bars within that range. This saves you from exporting massive files and trimming them in Excel.Here's the core logic: use
FileOpen to write to a CSV, loop through the iTime and iOpen arrays, and use an if statement to filter the dates. When you run it, it generates a clean CSV file with only the bars you need.The "Missing" Data Problem
One more thing. If you export M1 data and notice gaps, it's often because your broker doesn't provide that data for the full period. The fix? Download from a different broker's terminal and copy the
.hst file into your current terminal's history folder. The MQL4 documentation confirms that file operations use the standard FILE_CSV and FILE_WRITE flags, which means as long as the format matches, the terminal will read it.Reference: MetaQuotes MQL4 Documentation. "Working with Files." docs.mql4.com.
本文首发于FXEAR.com,原创内容,未经授权禁止转载。