FileWrite keeps writing over the same line

 

Hi,


I'm trying to create a log file for an expert advisor, but I'm having a problem writing the data to a file. I'm testing it in the strategy tester right now. The problem is that when I open the log file, there's only one line of text in the file - the last line that was written. It appears to be overwriting the same line of data over and over again. Yes, I have added FileSeek, but to no avail.


The complete code is below. Can someone tell me what I'm missing?



int Log = FileOpen(ExpertName,FILE_CSV|FILE_READ|FILE_WRITE);
if (Log == -1)
{
Log = FileOpen(ExpertName,FILE_CSV|FILE_WRITE);
int WriteLog = FileWrite(Log,"Time","Symbol","Period","Ticket","Type","Lots","Price","Profit");
FileSeek(Log,0,SEEK_END);
if(WriteLog < 0)
{
Err = GetLastError();
Alert("Account Log: Error "+Err+" - "+ErrorDescription(Err));
return(0);
}
}
int HistoryCount = 0;
int OpenOrders = 0;
while(OrderTicket() != 0)
{
OrderSelect(HistoryCount,SELECT_BY_POS,MODE_TRADES);
if(OrderMagicNumber() == ChartID && OrderSymbol() == Symbol()) OpenOrders++;
if(OpenOrders > LastOpenCount && OrderTicket() != 0) WriteLog = FileWrite(Log,TimeToStr(OrderOpenTime()),Symbol(),Period(),OrderTicket(),OrderType(),OrderLots(),OrderOpenPrice());
FileSeek(Log,0,SEEK_END);
if(WriteLog < 0)
{
Err = GetLastError();
Alert("Account Log: Error "+Err+" - "+ErrorDescription(Err));
return(0);
}
HistoryCount++;
}
LastOpenCount = OpenOrders;
HistoryCount = 0;
int ClosedOrders = 0;
while(OrderTicket() != 0)
{
OrderSelect(HistoryCount,SELECT_BY_POS,MODE_HISTORY);
if(OrderMagicNumber() == ChartID && OrderSymbol() == Symbol()) ClosedOrders++;
if(ClosedOrders > LastClosedCount && OrderTicket() != 0) WriteLog = FileWrite(Log,TimeToStr(OrderCloseTime()),Symbol(),Period(),OrderTicket(),OrderType(),OrderLots(),OrderClosePrice(),OrderProfit());
FileSeek(Log,0,SEEK_END);
if(WriteLog < 0)
{
Err = GetLastError();
Alert("Account Log: Error "+Err+" - "+ErrorDescription(Err));
return(0);
}
HistoryCount++;
}
LastClosedCount = ClosedOrders;
FileClose(Log);

 

You don't see your line of headers?


How many bytes are in the file?

 

Ok, I figured it out. The FileSeek function had to be before the FileWrite function. Even having it right after it in the loop didn't work for some reason. Here's the updated loop code:



while(OrderTicket() != 0)
{
OrderSelect(HistoryCount,SELECT_BY_POS,MODE_TRADES);
if(OrderMagicNumber() == ChartID && OrderSymbol() == Symbol()) OpenOrders++;
if(OpenOrders > LastOpenCount && OrderTicket() != 0)
{
string PrintPeriod = PeriodToStr(Period());
string PrintOrder = OrderTypeToStr(OrderType());
FileSeek(Log,0,SEEK_END);
WriteLog = FileWrite(Log,TimeToStr(OrderOpenTime()),Symbol(),PrintPeriod,OrderTicket(),PrintOrder,OrderLots(),OrderOpenPrice());
}
if(WriteLog < 0)
{
Err = GetLastError();
Alert("Account Log: Error "+Err+" - "+ErrorDescription(Err));
return(0);
}
HistoryCount++;
}

 
fuzzbomb:

Hi,


I'm trying to create a log file for an expert advisor, but I'm having a problem writing the data to a file. I'm testing it in the strategy tester right now. The problem is that when I open the log file, there's only one line of text in the file - the last line that was written. It appears to be overwriting the same line of data over and over again. Yes, I have added FileSeek, but to no avail.


The complete code is below. Can someone tell me what I'm missing?



int Log = FileOpen(ExpertName,FILE_CSV|FILE_READ|FILE_WRITE);
if (Log == -1)
{
Log = FileOpen(ExpertName,FILE_CSV|FILE_WRITE);
int WriteLog = FileWrite(Log,"Time","Symbol","Period","Ticket","Type","Lots","Price","Profit");
FileSeek(Log,0,SEEK_END);
if(WriteLog < 0)
{
Err = GetLastError();
Alert("Account Log: Error "+Err+" - "+ErrorDescription(Err));
return(0);
}
}
int HistoryCount = 0;
int OpenOrders = 0;
while(OrderTicket() != 0)
{
OrderSelect(HistoryCount,SELECT_BY_POS,MODE_TRADES);
if(OrderMagicNumber() == ChartID && OrderSymbol() == Symbol()) OpenOrders++;
if(OpenOrders > LastOpenCount && OrderTicket() != 0) WriteLog = FileWrite(Log,TimeToStr(OrderOpenTime()),Symbol(),Period(),OrderTicket(),OrderType(),OrderLots(),OrderOpenPrice());
FileSeek(Log,0,SEEK_END);
if(WriteLog < 0)
{
Err = GetLastError();
Alert("Account Log: Error "+Err+" - "+ErrorDescription(Err));
return(0);
}
HistoryCount++;
}
LastOpenCount = OpenOrders;
HistoryCount = 0;
int ClosedOrders = 0;
while(OrderTicket() != 0)
{
OrderSelect(HistoryCount,SELECT_BY_POS,MODE_HISTORY);
if(OrderMagicNumber() == ChartID && OrderSymbol() == Symbol()) ClosedOrders++;
if(ClosedOrders > LastClosedCount && OrderTicket() != 0) WriteLog = FileWrite(Log,TimeToStr(OrderCloseTime()),Symbol(),Period(),OrderTicket(),OrderType(),OrderLots(),OrderClosePrice(),OrderProfit());
FileSeek(Log,0,SEEK_END);
if(WriteLog < 0)
{
Err = GetLastError();
Alert("Account Log: Error "+Err+" - "+ErrorDescription(Err));
return(0);
}
HistoryCount++;
}
LastClosedCount = ClosedOrders;
FileClose(Log);

 
FileWrite ONLY works with a .csv extension! we noticed this running mt4 under ubuntu. we ran into this exact same issue (in linux file extensions have no meaning whatsoever). however if you're porting over from the windoze world better not forget those extensions. and this is NOT mentioned in the docs anywhere...
Reason: