Having big difficulty "appending" text to a file

 

Hi again, I'm back again pestering you all with another question. I have an EA that runs on about 10 charts. When the EA generates an order for a chart, I also want to write some data to a file (for debugging later). It seems it will always only write to the very first line of the file and keeps overwriting the first line and not appending. Here is my code, thanks!!:


          Ticket=OrderSendReliable(Symbol(),OP_BUYSTOP,Lots,FinalBuyPrice,Slippage,BuyStopPrice,BuyProfTarg,
"MyEA Buy",MagicNumber,0,Green);
          if(Ticket<=0) Print("Error = ",GetLastError());
          else 
          { 
            Print("Pending BUY order ticket = ",Ticket); 
            
            hwnd = Window.getHandle(Symbol());
            Window.activate(hwnd);
            Window.maximize(hwnd);
            filehandle=FileOpen("tracer.txt", FILE_CSV|FILE_WRITE|FILE_READ, ';');
            if(filehandle>0)
            {
             FileWrite(filehandle, Ticket, Symbol(), "BUY", BuyPrice, FinalBuyPrice, BuyProfTarg, BuyStopPrice);
            }
            FileClose(filehandle);
          }

 

You can find my AppendToFile script here:

Append To File

 

appenddinf is 1st move to end position and then write.

FileSeek(handle, 0, SEEK_END);
//---- add data to the end of file
FileWrite(filehandle, Ticket, Symbol(), "BUY", BuyPrice, FinalBuyPrice, BuyProfTarg, BuyStopPrice);

 

Since you're using a common filename, if two EA try to write simultaneously you could have problems (open fails or one write is lost)

You could use a semaphore around the orderSend/FileWrite and those plus context is busy.

 

Thanks to all, but Codersguru - your suggestion was perfect!!


Thanks!

Shawn'

Reason: