Files
MT5-PY-AI-Tbot/separate_codes/Export_EURUSD_History.mq5
T

68 lines
2.7 KiB
Plaintext
Raw Normal View History

2025-10-23 23:02:10 +01:00
//+------------------------------------------------------------------+
//| Export_EURUSD_History.mq5 |
//| Copyright 2025, https://github.com/Anaswar-ash |
//| author: Ash |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, https://github.com/Anaswar-ash"
#property link "https://github.com/Anaswar-ash"
#property version "1.00"
#property script_show_inputs
//--- input parameters
input string InpFileName = "raw_price_data.csv"; // File name to save the data to
input int InpDays = 1000; // Number of days of historical data to export
2025-10-23 23:02:10 +01:00
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- Array to store the price data
2025-10-23 23:02:10 +01:00
MqlRates rates[];
//--- Variable to store the number of bars copied
2025-10-23 23:02:10 +01:00
int copied;
//--- Get daily price data for EURUSD for the specified number of days
2025-10-23 23:02:10 +01:00
copied = CopyRates("EURUSD", PERIOD_D1, 0, InpDays, rates);
//--- Check if the data was copied successfully
2025-10-23 23:02:10 +01:00
if(copied > 0)
{
//--- Open the file to write the data to
2025-10-23 23:02:10 +01:00
int file_handle = FileOpen(InpFileName, FILE_WRITE | FILE_CSV | FILE_ANSI, ',');
//--- Check if the file was opened successfully
2025-10-23 23:02:10 +01:00
if(file_handle != INVALID_HANDLE)
{
//--- Write the header row to the CSV file
2025-10-23 23:02:10 +01:00
FileWrite(file_handle, "Time", "Open", "High", "Low", "Close", "Volume");
//--- Loop through the copied data and write each row to the CSV file
2025-10-23 23:02:10 +01:00
for(int i = 0; i < copied; i++)
{
FileWrite(file_handle,
TimeToString(rates[i].time, TIME_DATE),
DoubleToString(rates[i].open, _Digits),
DoubleToString(rates[i].high, _Digits),
DoubleToString(rates[i].low, _Digits),
DoubleToString(rates[i].close, _Digits),
rates[i].tick_volume);
}
//--- Close the file
2025-10-23 23:02:10 +01:00
FileClose(file_handle);
Print("Data successfully exported to ", InpFileName);
}
else
{
//--- Print an error message if the file could not be opened
2025-10-23 23:02:10 +01:00
Print("Error opening file: ", GetLastError());
}
}
else
{
//--- Print an error message if the data could not be copied
2025-10-23 23:02:10 +01:00
Print("Error copying rates: ", GetLastError());
}
}
//+------------------------------------------------------------------+