2

I am saving all the trades done by my EA into a CSV file. When a Trade is closed by the EA, I have to add string "Book Profit" to the end of particular line from the file. eg: Below is the line that is saved in the file while trade is open "Buy GBPJPY 146.28 145.15", I would like to add string "Book Profit" to the end of the above line and save it to the file. After saving the line should look like "Buy GBPJPY 146.28 145.15 Book Profit"

int file_handle_dtf=FileOpen("MyTrades.CSV",FILE_READ|FILE_WRITE|FILE_CSV);
   if(file_handle_dtf!=INVALID_HANDLE){
      while(!FileIsEnding(file_handle_dtf)){

         str_size1=FileReadInteger(file_handle_dtf,INT_VALUE);
         //--- read the string

         str1=FileReadString(file_handle_dtf,str_size1);
         strBP=StringConcatenate(str1,",Book Profit");
         FileWriteString(file_handle_dtf,strBP+"\n");
         }

      }

This code just overwrites the file and it is not readable

Michael M.
  • 10,486
  • 9
  • 18
  • 34
  • first you have to find the correct line, dont you? you may have many lines starting with "Buy GBPJPY" so saving the ticket number should help. Second, you can navigate over the file using `FileSeek` and `FileTell` functions but it seems if you write a char, you will lose everything below that. So either rewrite the whole file, or 1. to find the correct line, 2.find the cursor at start of the line and 3. save everything after that line in the memory; 4. write the updated line; 5. write everything saved in step 3. the easiest way is probably just to write IN and OUT as separate lines then merge – Daniel Kniaz May 14 '19 at 21:37
  • Thank you Daniel Kniaz, I shall try and let you know – Karthikeyan V May 15 '19 at 04:38
  • Dear Daniel Kniaz, 1) I copied all the lines, except the line i want to modify or delete to an temp_array using for loop. 2) Now the temp_array have all the lines except the line I donot want. 3) I reset the pointer using FileSeek to the start of the file. 4) I tried to write the temp_array to file using FileWriteString in a for loop. The problem is it writes the first line, and doesn't write the whole temp_array. if(FileSeek(file_handle,0,SEEK_SET)==true){ FileWriteString(file_handle,temp_array[i]+"\n"); } – Karthikeyan V May 24 '19 at 03:14
  • I am not sure you need to find the cursor, probably closing and opening for writing would work faster. in order to write all the elements, write them in loop – Daniel Kniaz May 24 '19 at 08:08
  • cursor is really needed, because after copying all the trades into an new array, we need to go to start of the file, otherwise the new array is written from where the cursor is. – Karthikeyan V May 28 '19 at 19:11
  • There was a error in for loop which is clear now. The total trades is now copied into the file. – Karthikeyan V May 28 '19 at 19:14
  • There was a error in for loop which is clear now. The total trades is now copied into the file. Now the problem is say for eg. There are 10 trades in the file(10 lines.) when we are deleting 1 trade, only 9 lines are copied into the new array. We are also writing the new array using for loop. The problem now is the 10 th line is still available, and so when ever the line is deleted there is an extra set of lines at the end of file. Any suggestion, kindly let me know – Karthikeyan V May 28 '19 at 19:21
  • Do not write a line if its size is 0. e.g. `if(StringLen(line)==0) continue;` – Daniel Kniaz May 29 '19 at 20:03
  • Is there any way to clear all data in a file – Karthikeyan V May 31 '19 at 04:30
  • Now the problem is solved. 1.Copy all the lines to an Temp_array, except the line i want to delete. 2. Delete the content of the file using int file_handle_trn=FileOpen("MyTrades.CSV",FILE_WRITE|FILE_CSV); when we open a file only with FILE_WRITE, it truncates the file. Now we have a empty file, where we can write our content from temp_array to file. Now we have only the trades which are open. – Karthikeyan V May 31 '19 at 06:57

2 Answers2

0

Seek the end of the file first before writing to it:

if (FileSeek(file_handle_dtf, 0, SEEK_END)) 
{
   // put file writing code here
}
kenorb
  • 155,785
  • 88
  • 678
  • 743
John
  • 11
  • 1
  • 3
  • If SEEK_END is used, then the cursor will move to the end of the file and again all the trades will be written into the file. So there will be duplicate Trades, and the size of the file will be increasing. – Karthikeyan V May 28 '19 at 18:52
0

Use the following function with your four parameters (Buy, GBPJPY, 146.28, 145.15):

void func_replaceStringInCSV(string _order,string _symbol,string _SL,string _TP)
 {
  int handle=FileOpen("MyTrades.CSV",FILE_READ|FILE_WRITE|FILE_CSV);
  if(handle!=INVALID_HANDLE)
    {
     while(!FileIsEnding(handle))
       {
        int lineStart=(int)FileTell(handle);
        string order=FileReadString( handle);
        if(FileIsLineEnding(handle))continue;
        string symbol=FileReadString(handle);
        if(FileIsLineEnding(handle))continue;
        string SL=FileReadString(handle);
        if(FileIsLineEnding(handle))continue;
        string TP=FileReadString(handle);
        if(FileIsLineEnding(handle))
          {
            if(StringConcatenate(order,symbol,SL,TP)==
            StringConcatenate(_order,_symbol,_SL,_TP))
             {
              string blankSpace="";
              int lineLen=StringLen(StringConcatenate(order,symbol,SL,TP))+3;
              FileSeek(handle,lineStart,SEEK_SET);
              for(int l=0;l<=lineLen;l++)
                 blankSpace+=" ";
              FileWrite(handle,order,symbol,SL,TP,"Book Profit");
              FileFlush(handle);
             }
          }
       }
    }
 }
Nima Derakhshanjan
  • 1,380
  • 9
  • 24
  • 37