export of indicator data

 

How do you export to a file the data calculated by a specific indicator (indicator figures from a chart for a given historical period)?

MT4

 
  1. Why did you post your MT4 question in the MT5 General section (a miscellaneous catch-all category) instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. I have moved this thread.

  2. Learn to code MT4 or pay someone. You read the indicator and write the file.

 
dzurak:

How do you export to a file the data calculated by a specific indicator (indicator figures from a chart for a given historical period)?

MT4

You need to write a program to extract the data. Youll need to have some input variables for eg.: File name to save as CSV, the indicator you want to use, the settings, and then start coding. 

Here is an example, note, i have not tested this. 

//+------------------------------------------------------------------+
//| Script Exporting Indicator Data to a CSV File ...                |
//+------------------------------------------------------------------+
#property strict

// Input variables
input string fileName = "indicator_data.csv"; // File name
input int maPeriod = 14;                      // MA period
input int maShift = 0;                        // MA shift
input int maMethod = MODE_SMA;                // MA method
input int maPrice = PRICE_CLOSE;              // Price type for MA

//+------------------------------------------------------------------+
//| Script start function ...                                        |
//+------------------------------------------------------------------+
void OnStart()
{
    int handle = FileOpen(fileName, FILE_CSV | FILE_WRITE, ';');
    if (handle == INVALID_HANDLE)
    {
        Print("Error opening file: ", GetLastError());
        return;
    }

    // Write the header row...
    FileWrite(handle, "Date", "Time", "Moving Average");

    // Loop through the historical data...
    for (int i = 0; i < 1000; i++) // Adjust to the desired number of bars
    {
        datetime time = iTime(Symbol(), Period(), i);  // Time of the bar
        double maValue = iMA(Symbol(), Period(), maPeriod, maShift, maMethod, maPrice, i);  // MA value

        if (time == 0)
            break;  // No more data available...

        string dateString = TimeToString(time, TIME_DATE);
        string timeString = TimeToString(time, TIME_MINUTES);

        // Write data to file
        FileWrite(handle, dateString, timeString, DoubleToString(maValue, 5));  // 5 decimal places
    }

    // Close the file
    FileClose(handle);
    Print("Data exported successfully.");
}
//+------------------------------------------------------------------+