I got the following code which run as script and I have a problem with blank rows in between like this :
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
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 ofClose.Delete that
\nand I think you'll be fine.