Export OHLC To CSV
Getting bar data out of the terminal for use in a spreadsheet, a Python notebook or another platform is a constant, low-level need, and the built-in "Export to CSV" button in the terminal's own History Center only ever gives you whatever range happens to be open in that dialog. This script gives you a symbol, a timeframe and either a bar count or a precise date range, and drops one clean file into the sandbox. The trap in a script like this is the request that cannot be fully met. Ask for 5000 daily bars on a symbol whose broker only carries 1200, and a script that exports 1200 rows without comment produces a file that looks complete but silently is not - anyone loading it later has no way to tell the difference between "no more history exists" and "something went wrong". This script checks the shortfall directly: for a bar-count request it compares the copied count against what was asked, and for a date-range request it compares the earliest bar actually returned against the requested start. Either way, the log states plainly how far back the data really goes, and the file itself never contains a padded, duplicated or invented row to make the count match. Rows are written oldest first, which matches how a CSV bar file is normally read and matches the order the terminal's own History Center export uses. Notes and limitations - History the terminal has not downloaded cannot be exported. If the file comes up short, scroll the chart for that symbol and timeframe back further first, or open History Center and download the range you need. - A symbol not currently in Market Watch is added automatically where the platform allows it; if that fails, the script still attempts the export, since some brokers permit reading history for symbols outside Market Watch. - Prices are written with the exported symbol's own digit count, not the chart symbol's, so a JPY pair and a 5-digit majors export correctly side by side if you run the script twice. - The file always contains a header row, even when nothing else was written, which only happens if the export failed outright and no file is left open.
Script · MT4 · v1.0 · Free
About Export OHLC To CSV
Export OHLC To CSV is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 7 settings, such as Symbol, Timeframe and Use date range. MQL source code included.
Files in the download: ExportOhlc.ex4, ExportOhlc.mq4, ExportOhlc.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Symbol | Symbol ("" = this chart's symbol) | |
| Timeframe | PERIOD_CURRENT | Timeframe |
| Use date range | false | Use the date range below instead of a bar count |
| Date from | 0 | Range start (only used if the above is true) |
| Date to | 0 | Range end, 0 = now (only used if the above is true) |
| Bar count | 500 | Bars to export, most recent first (only used if the range is off) |
| Delimiter | DelimiterComma | CSV column separator |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Alerts · MT4 Script · MQL4 Source Code