FileWrite

 
Hi,

I'm trying to create my own log of my trading actions. I'm using FileWrite which keeps writing the first line over and over again. How do I write to a log keeping each entry on a separate line?

Many thanks,

T
 
topos35196:
Hi,

I'm trying to create my own log of my trading actions. I'm using FileWrite which keeps writing the first line over and over again. How do I write to a log keeping each entry on a separate line?

Many thanks,

T
Do not use FILE_WRITE only, but FILE_READ|FILE_WRITE.
Example: FileOpen("log.csv",FILE_CSV|FILE_READ|FILE_WRITE,';');
 

Also, if you open and close the file, you may need to go to the end of it before writing again:

Example:

FileOpen("log.csv",FILE_CSV|FILE_READ|FILE_WRITE,';');
FileSeek("log.csv", 0 SEEK_END); // go to end of file
FileWrite...
FileClose...

 
phy:

Also, if you open and close the file, you may need to go to the end of it before writing again:

Example:

FileOpen("log.csv",FILE_CSV|FILE_READ|FILE_WRITE,';');
FileSeek("log.csv", 0 SEEK_END); // go to end of file
FileWrite...
FileClose...


i have the same problem & fileseek dosn't help

anyone ?

thanks

 
qjol:


i have the same problem & fileseek dosn't help

anyone ?

Did u implement Zap's answer from this thread? If u did, then please post the related code...
 
gordon:
Did u implement Zap's answer from this thread? If u did, then please post the related code...



thanks for trying to help

  CloseOrdesrs = FileOpen(Symbol() + "_data.csv",FILE_CSV|FILE_READ|FILE_WRITE,',');
      while (FileIsEnding(CloseOrdesrs)==false)
      {   
         string CDateNTime = FileReadString(CloseOrdesrs);
         string CType = FileReadString(CloseOrdesrs);
         string CSize = FileReadString(CloseOrdesrs);
         string CSymbol = FileReadString(CloseOrdesrs);
         string CLastPrice = FileReadString(CloseOrdesrs);
         string CSL = FileReadString(CloseOrdesrs);
         string CTP = FileReadString(CloseOrdesrs);


      if (FileIsEnding(CloseOrdesrs)==true)
         break;

CloseDateNTime = CDateNTime;
CloseType = CType;
CloseSize = CSize;
CloseSymbol = CSymbol;
ClosePrice = CLastPrice;
CloseSL = CSL;
CloseTP = CTP;
}

     FileClose(CloseOrdesrs);

   
//----
double bid   =MarketInfo("EURUSD",MODE_BID);
double ask   =MarketInfo("EURUSD",MODE_ASK);

{
    if(mode == "buy") 

      if(DoubleToStr(bid,5) >= (LTP)) 
       for(cnt = OpenOrders; cnt >= 0; cnt--)
         {
         RefreshRates();
  CloseOrdesrs = FileOpen(Symbol() + "_data.csv",FILE_CSV|FILE_READ|FILE_WRITE,',');
             {
             FileSeek(Symbol() + "_data.csv", 0, SEEK_SET);
             FileWrite(
         CloseOrdesrs,
         TimeToStr(TimeCurrent(),TIME_DATE)+" "+ TimeToStr(TimeCurrent(),TIME_MINUTES),"0","0","0","0","5",DoubleToStr(bid,5));
     FileClose(CloseOrdesrs);
     }
     }

    if(mode == "SELL") 

      if((DoubleToStr(ask,5)) <= (LTP)) 
         {
       RefreshRates();
  CloseOrdesrs = FileOpen(Symbol() + "_data.csv",FILE_CSV|FILE_READ|FILE_WRITE,',');
             {
             FileSeek(Symbol() + "_data.csv", 0, SEEK_SET);
             FileWrite(
         CloseOrdesrs,
         TimeToStr(TimeCurrent(),TIME_DATE)+" "+ TimeToStr(TimeCurrent(),TIME_MINUTES),"0","0","0","0","5",DoubleToStr(ask,5));
     FileClose(CloseOrdesrs);
     
             }
         }

}

here is what is doing

2010.06.01 04:48,0,0,0,0,5,1.22604
100,0,0
2010.02.24 22:52,buy,0.2,EURUSD,1.23100,0,0
2010.02.24 22:52,buy,0.4,EURUSD,1.23100,0,0
2010.04.15 04:59,sell,0.4,EURUSD,1.27996,0,0
2010.05.01 08:21,buy,0.0,EURUSD,1.25225,0,0
2010.05.12 12:44,buy,0.3,EURUSD,1.20485,0,0
2010.05.15 10:41,sell,0.1,EURUSD,1.20490,0,0
2010.05.15 03:37,sell,2.5,EURUSD,1.27158,0,0
2010.05.18 21:51,sell,0.5,EURUSD,1.21118,0,0
2010.05.22 09:12,sell,1.0,EURUSD,1.22950,0,1.2230

 

Your code is quite buggy and most bugs have nothing to do with the question raised on this thread... But I'll give u some comments/pointers:

  • The following won't work:
if(DoubleToStr(bid,5) >= (LTP)) 

You need to convert your string to double (using StrToDouble()) and not vice-versa. See conversion functions here -> https://docs.mql4.com/convert. Also note that checking equality of doubles is a bit tricky, see part #4 in this article -> https://www.mql5.com/en/articles/1561.

  • In the following code:
for(cnt = OpenOrders; cnt >= 0; cnt--)
You are looping on all orders, but you are not selecting them or reading any of their properties. What's the point of that? Please see here for a general explanation -> https://www.mql5.com/en/forum/126165. Also, your 'OpenOrders' value should be equal to OrdersTotal()-1 for this to work properly.
  • FileSeek() function has the file handle as it's first argument and NOT the file name... This is regarding the following line:
FileSeek(Symbol() + "_data.csv", 0, SEEK_SET);
Should be: FileSeek(CloseOrdesrs,...
  • You can't just overwrite the first line and expect it to 'fit' properly:
FileWrite(CloseOrdesrs,TimeToStr(TimeCurrent(),TIME_DATE)+" "+ TimeToStr(TimeCurrent(),TIME_MINUTES),"0","0","0","0","5",DoubleToStr(bid,5));
What happens is that the amount of characters in this line is written to the beginning of the file and a newline character is added at the end of this line automatically. But it is most likely that this line will be slightly shorter or slightly longer than the current first line, hence you get the weird looking output.... In your case the second line in the output '100,0,0' is what's left of the previous line that was there since the new line is much smaller than the previous line...

Anyway, to achieve your goal u need to design a fixed length database, where each cell has a known and fixed number of characters (or if you decide to do it as a binary file - a fixed length of bytes). With this kind of design you'll be able to achieve what u r trying to do now...


p.s You haven't fixed the problem u mentioned in this thread -> https://www.mql5.com/en/forum/126351

 
gordon:

Your code is quite buggy and most bugs have nothing to do with the question raised on this thread... But I'll give u some comments/pointers:

  • The following won't work:

You need to convert your string to double (using StrToDouble()) and not vice-versa. See conversion functions here -> https://docs.mql4.com/convert. Also note that checking equality of doubles is a bit tricky, see part #4 in this article -> https://www.mql5.com/en/articles/1561.

  • In the following code:
You are looping on all orders, but you are not selecting them or reading any of their properties. What's the point of that? Please see here for a general explanation -> https://www.mql5.com/en/forum/126165. Also, your 'OpenOrders' value should be equal to OrdersTotal()-1 for this to work properly.
  • FileSeek() function has the file handle as it's first argument and NOT the file name... This is regarding the following line:
Should be: FileSeek(CloseOrdesrs,...
  • You can't just overwrite the first line and expect it to 'fit' properly:
What happens is that the amount of characters in this line is written to the beginning of the file and a newline character is added at the end of this line automatically. But it is most likely that this line will be slightly shorter or slightly longer than the current first line, hence you get the weird looking output.... In your case the second line in the output '100,0,0' is what's left of the previous line that was there since the new line is much smaller than the previous line...

Anyway, to achieve your goal u need to design a fixed length database, where each cell has a known and fixed number of characters (or if you decide to do it as a binary file - a fixed length of bytes). With this kind of design you'll be able to achieve what u r trying to do now...


p.s You haven't fixed the problem u mentioned in this thread -> https://www.mql5.com/en/forum/126351


thanks for the advice but just one thing i tried a few calculation with double's & it's working just fine without trick's just like i'm doing with int's

 
qjol:

thanks for the advice but just one thing i tried a few calculation with double's & it's working just fine without trick's just like i'm doing with int's

I was talking specifically about equality checks; checking if one double is larger/smaller than another double works normally... But checking for equality might return the wrong result if not done using the method described in that article.
 
how did it do in the end? did you manage to create your own log? im in the process of doing the same and if you have some code i could learn from would make me very happy :)