Exporting Metatrader 4 historical data to excel
Exporting Metatrader 4 historical data to excel
Loading saved threads...
Alan Wong · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Alan Wong
Original post: https://stackoverflow.com/questions/78170355
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I got the following code which run as script and I have a problem with blank rows in between like this :
enter image description here
I've been trying to figure out and not sure why there are blank rows. Any help would be appreciated .Here is the code:
#property script_show_inputs
input datetime StartDateTime = D'2023.11.21 00:00';
input datetime EndDateTime = D'2023.11.22 12:00';
// ------ OnInit() function ------
int OnInit()
{
string filename = "MyMT4Data.csv";
int file_handle = FileOpen(filename, FILE_WRITE | FILE_CSV | FILE_ANSI);
if (file_handle == INVALID_HANDLE) {
Print("Failed to open file: ", filename);
return(INIT_FAILED);
}
// Loop through bars in reverse order, and filter by StartDateTime and EndDateTime
for (int i = Bars - 1; i >= 0; i--) {
datetime barTime = Time[i];
if (barTime >= StartDateTime && barTime <= EndDateTime) {
// Build CSV line
// Explicit Line Initialization (Safeguard)
string line ="";
line = TimeToString(barTime, TIME_DATE | TIME_SECONDS) + "," +
DoubleToString(Open[i], Digits) + "," +
DoubleToString(High[i], Digits) + "," +
DoubleToString(Low[i], Digits) + "," +
DoubleToString(Close[i], Digits) + "\n";
// Modify line to get four-digit year
line = StringSubstr(line, 0, 8) + StringSubstr(line, 11);
// Remove any leading spaces (if needed)
line = StringTrimLeft(line);
// Write to file
FileWrite(file_handle, line);
line ="";
}
}
FileClose(file_handle);
return(INIT_SUCCEEDED);
}
I have tried to print to debug so many times and still blank rows happen
Quote
Report
Mark SdS · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Mark SdS
Original post: https://stackoverflow.com/a/78170858
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I assume, since you're declaring each line individually and add it to your CSV, those lines don't need the
\n (new line)
character after the addition of
Close
.
Delete that
\n
and I think you'll be fine.
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Alan Wong Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/questions/78170355 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I got the following code which run as script and I have a problem with blank rows in between like this : enter image description here I've been trying to figure out and not sure why there are blank rows. Any help would be appreciated .Here is the code: #property script_show_inputs input datetime StartDateTime = D'2023.11.21 00:00'; input datetime EndDateTime = D'2023.11.22 12:00'; // ------ OnInit() function ------ int OnInit() { string filename = "MyMT4Data.csv"; int file_handle = FileOpen(filename, FILE_WRITE | FILE_CSV | FILE_ANSI); if (file_handle == INVALID_HANDLE) { Print("Failed to open file: ", filename); return(INIT_FAILED); } // Loop through bars in reverse order, and filter by StartDateTime and EndDateTime for (int i = Bars - 1; i >= 0; i--) { datetime barTime = Time[i]; if (barTime >= StartDateTime && barTime <= EndDateTime) { // Build CSV line // Explicit Line Initialization (Safeguard) string line =""; line = TimeToString(barTime, TIME_DATE | TIME_SECONDS) + "," + DoubleToString(Open[i], Digits) + "," + DoubleToString(High[i], Digits) + "," + DoubleToString(Low[i], Digits) + "," + DoubleToString(Close[i], Digits) + "\n"; // Modify line to get four-digit year line = StringSubstr(line, 0, 8) + StringSubstr(line, 11); // Remove any leading spaces (if needed) line = StringTrimLeft(line); // Write to file FileWrite(file_handle, line); line =""; } } FileClose(file_handle); return(INIT_SUCCEEDED); } I have tried to print to debug so many times and still blank rows happen
Checking account access…