Problem Description
Solution
To save indicator values from the last 1000 bars to a CSV file for Excel analysis, the most efficient method is to use an MQL4 Script. A script runs once, performs the task, and then stops, which is perfect for exporting data.
Below is a complete, ready-to-use script. By default, it exports RSI values, but I have included a specific section where you can easily swap this for your specific indicator (built-in or custom).
MQL4 Script: Export Indicator Data to CSV
Save this code as a .mq4 file in your MQL4/Scripts folder (e.g., ExportData.mq4).
//+------------------------------------------------------------------+
//| ExportData.mq4 |
//| Copyright 2023, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Quantitative AI Assistant"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property script_show_inputs
//--- Input Parameters
input string InpFileName = "IndicatorData.csv"; // Name of the file to save
input int InpBarsToExport = 1000; // Number of bars to export
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
// 1. Validate Bar Count
int barsLimit = InpBarsToExport;
if(barsLimit > Bars) barsLimit = Bars; // Prevent out of range errors
// 2. Open File
// We use FILE_CSV to format for Excel, and FILE_ANSI for standard text encoding
int fileHandle = FileOpen(InpFileName, FILE_WRITE | FILE_CSV | FILE_ANSI, ",");
if(fileHandle == INVALID_HANDLE)
{
Print("Error opening file: ", GetLastError());
return;
}
// 3. Write CSV Header
FileWrite(fileHandle, "Date", "Time", "Open", "High", "Low", "Close", "IndicatorValue");
// 4. Loop through bars
// We iterate backwards (from old to new) so the CSV is chronological in Excel
for(int i = barsLimit - 1; i >= 0; i--)
{
string date = TimeToString(Time[i], TIME_DATE);
string time = TimeToString(Time[i], TIME_MINUTES);
// --- GET INDICATOR VALUE HERE ---
// REPLACE the line below with your specific indicator function.
// Example: iRSI(NULL, 0, 14, PRICE_CLOSE, i);
// Example Custom: iCustom(NULL, 0, "MyIndicator", param1, param2, buffer_index, i);
double indicatorValue = iRSI(NULL, 0, 14, PRICE_CLOSE, i);
// --------------------------------
// 5. Write row to file
FileWrite(fileHandle,
date,
time,
DoubleToString(Open[i], _Digits),
DoubleToString(High[i], _Digits),
DoubleToString(Low[i], _Digits),
DoubleToString(Close[i], _Digits),
DoubleToString(indicatorValue, 5) // 5 digits of precision for indicator
);
}
// 6. Close File
FileClose(fileHandle);
Print("Successfully exported ", barsLimit, " bars to ", InpFileName);
Print("File location: MQL4/Files folder");
}
//+------------------------------------------------------------------+
How to Customize for Your Indicator
You need to modify the line inside the loop labeled // --- GET INDICATOR VALUE HERE ---.
Case A: It is a Built-in Indicator (e.g., Moving Average, MACD)
Replace iRSI(...) with the specific function.
// Example for Moving Average
double indicatorValue = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, i);
Case B: It is a Custom Indicator (ex4 file)
If you downloaded an indicator or bought one, use iCustom. You need to know the Buffer Index (usually 0 for the main line) and the Input Parameters.
// Syntax: iCustom(Symbol, Timeframe, "IndicatorName", Input1, Input2, ..., BufferIndex, Shift);
// Example: "SuperTrend" indicator with inputs (10, 3.0) reading Buffer 0
double indicatorValue = iCustom(NULL, 0, "SuperTrend", 10, 3.0, 0, i);
How to Access the File
- Compile the script in MetaEditor (F7).
- Go to the MT4 Terminal.
- Open the chart of the symbol you want to analyze.
- Drag and drop the ExportData script from the Navigator onto the chart.
- Once it finishes (check the "Experts" tab for the success message), go to:
- File -> Open Data Folder.
- Navigate to MQL4 -> Files.
- You will find
IndicatorData.csv. Open this file in Excel.
Q&A: Common Issues
Q: The file opens in Excel but everything is in one column.
A: This depends on your Windows Regional Settings. If your computer uses a comma (,) as a decimal separator, Excel might get confused by the CSV comma delimiter.
- Fix: In the code, change
FileOpen(..., ",")toFileOpen(..., ";"). This uses a semicolon as the column separator, which is safer for European regions.
Q: I see "Empty Value" or huge numbers (e.g., 2147483647) in the CSV.
A: MT4 uses a specific value (EMPTY_VALUE) to indicate that nothing is drawn on the chart at that specific bar (common in arrows or zigzag indicators).
- Fix: You can add a check in the code:
if(indicatorValue == EMPTY_VALUE) indicatorValue = 0;
Q: How do I find the "Buffer Index" for my custom indicator?
A: Open the "Data Window" in MT4 (Ctrl+D). Hover your mouse over a bar where the indicator is visible. Look at the values listed on the left.
- The first value listed under the indicator name is Buffer 0.
- The second value is Buffer 1, and so on. Use that index number in the
iCustomfunction.