Download MetaTrader 5

I need help using File Read function

To add comments, please log in or register
Alberto Tortella
526
Alberto Tortella  

Hi all, I made the attached .txt file using the following code

 int start() 
   {

         double Take_Profit_Long_1   = 1.06475 ;
         double Take_Profit_Long_2   = 1.06579 ; 
         double Take_Profit_Long_3   = 1.07432 ;
         double Take_Profit_Long_4   = 1.08436 ;
           
         string Size_1_Long  = 0.06 ;
         string Size_2_Long  = 0.04 ;  
         string Size_3_Long  = 0.02 ;         
         string Size_4_Long  = 0.01 ;
                                   
                        
         int handle_1=FileOpen("Prova.txt", FILE_TXT|FILE_READ|FILE_WRITE, ';');
         
         if(handle_1>0)
           {
           FileSeek(handle_1, 0, SEEK_END);
           //---- add data to the end of file
           FileWrite(handle_1,"EURUSD","BUY",123456,TimeToStr(OrderOpenTime()),Bars,Size_1_Long,Size_2_Long,Size_3_Long,Size_4_Long,Take_Profit_Long_1,Take_Profit_Long_2,Take_Profit_Long_3,Take_Profit_Long_4);
           FileClose(handle_1);
           handle_1=0;
           }
           
           
   }

Now, I need to read these file at the start of a new week for the opened orders, when the Global Variables are resetted (because I shut down my VPS during the weekend).

First of all I recall the opened orders.

Then I need to assign the fields of the corresponding raw to the Global Variables: here I need an help.

Thank you very much!

int start() 
   {

         double Take_Profit_Long_1    ;
         double Take_Profit_Long_2    ; 
         double Take_Profit_Long_3    ;
         double Take_Profit_Long_4    ;
           
         string Size_1_Long   ;
         string Size_2_Long   ;  
         string Size_3_Long   ;         
         string Size_4_Long   ;
         
  for(int cnt=0;cnt<=OrdersTotal();cnt++)
     {
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);     
        {  
        int handle_1=FileOpen("Prova.txt", FILE_TXT|FILE_READ|FILE_WRITE, ';');       
       
        if(handle_1>0)
           {
           if (OrderSymbol()=="1st field" && OrderType()=="2nd field" && OrderOpenTime()=="4th field") // searching the row that contains the informations related to the selected order
           
           Take_Profit_Long_1 = FileRead (.... ) ; // assign the value to the Global Variable
           Take_Profit_Long_1 = FileRead (.... ) ; // assign the value to the Global Variable
           Take_Profit_Long_1 = FileRead (.... ) ; // assign the value to the Global Variable
           Take_Profit_Long_1 = FileRead (.... ) ; // assign the value to the Global Variable
           
           Size_1_Long = FileRead (.... ) ; // assign the value to the Global Variable
           Size_2_Long = FileRead (.... ) ; // assign the value to the Global Variable
           Size_3_Long = FileRead (.... ) ; // assign the value to the Global Variable
           Size_4_Long = FileRead (.... ) ; // assign the value to the Global Variable
           
           FileClose(handle_1);
           handle_1=0;
           }   
        }  
     }              
                                           
           
   }
Files:
Prova.txt 5 kb
whroeder1
15726
whroeder1  
  1.   for(int cnt=0;cnt<=OrdersTotal();cnt++)
         {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);     
    Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  2. There are OrdersTotal tickets, numbered zero to total - 1. The last OrderSelect will always fail.
  3. You must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
  4. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
  5.         int handle_1=FileOpen("Prova.txt", FILE_TXT|FILE_READ|FILE_WRITE, ';');       
    
    Why are you opening the file for writing in your recovery code?
  6.         if(handle_1>0)
               {
               if (OrderSymbol()=="1st field" && OrderType()=="2nd field" && OrderOpenTime()=="4th field")
    
    You haven't read the line out of the file. Read the columns into variables, then test if the symbol, etc matches.
Carl Schreiber
7238
Carl Schreiber  

I don't use the mt4/5 csv-reading functions - too heavy to handle:

   int hdl = FileOpen(fName,FILE_READ|FILE_WRITE|FILE_BIN|FILE_COMMON);
   ...
   int nL,nC,c,i,sz = (int)FileSize(hdl);
   string lines[],cell[],all = FileReadString(hdl,sz);
   nL = i = StringSplit(all,StringGetCharacter("\n",0);,lines); 
   while(i-->0) {
      nI = c = StringSplit(lines[i],StringGetCharacter(";",0),cell); 
      int x = (int)StringToInteger(cell[0]);
      if (..) {
         lines[i] = ".."; //re-write (only) this line
      ...
   }
   FileSeek(hdl,0,SEEK_SET); // re-write all lines incl. the re-written one
   for(i=0;i<nL;i++) {
      sz = StringLen(lines[i]+"\n");
      if (sz<10) continue; // skip empty lines
      FileWriteString(hdl,lines[i]+"\n",sz); //add the \n which was eliminated by the split
   }
   FileClose(hdl);

(Without checking errors!)
To add comments, please log in or register