Download MetaTrader 5
To add comments, please log in or register
Do you want to learn MQL5? 300 articles will help you!
Route206
211
Route206 2015.08.20 13:08 

I have use FileWriteArray() succesfully in multiple EAs, but this time it just writes "0"s  - or it doesn't write at all, despite the msg: File Write =OK. As i'm using the exact syntax i used in other EAs i'm really puzzled and after a couple of hours i give up.
Can someone take a look and tell what i'm overlooking?

This is the fist time i'm using a variable filename, ie a filename that is based on an input parameter, given at EA start.

Sole purpose of the Array is to write variables that ensure a proper recovery after a crash (power outage etc.)

// some key variables (not all)
          double  tickets[][3]      ; //Array 
  input   double  swap;
          string  file_name;
//+------------------------------------------------------------------+                                                                           

void OnInit()
   {                                                                           
   EventSetTimer(60);
   swap_mgc=NormalizeDouble(swap,0);
   file_name=IntegerToString(swap_mgc,6,'x');
   file_name=StringConcatenate(file_name,".bin");
   if(!FileIsExist(file_name))
      {
        // normal start of EA ie. not a recovery
      }
   else // a recovery
      {
      int bla=FileOpen(file_name,FILE_READ|FILE_BIN);
      if(bla!=INVALID_HANDLE)
         {
         FileReadArray(bla,tickets);
         FileClose(bla);
         open_price=tickets[0][2]; 
         profit_closed=tickets[1][2];
         longshort=tickets[2][2];  
         swap_count=tickets[3][2];
         swap=tickets[4][2];         
         }
      else
         {
         error_read=true;  //leave file in tact in case of read error for troubleshooting
         ExpertRemove();
         }
      }   
   return;
   } 
//+------------------------------------------------------------------+   
   
void OnDeinit(const int reason)
  {
   CloseOrders();
   if(error_read==false)
      {
      if(FileDelete(file_name))
         {
         Alert("file ",file_name,"deleted"); 
         }
      }  
   return;  
  }
//+------------------------------------------------------------------+ 
  
void OnTick()
  {
   //Main Body including defenition of 'tickets' array.
   }
//+------------------------------------------------------------------+
   

void OnTimer()
   {

// Additional values, necessary for the Recovery File:
   tickets[0][2]=open_price; 
   tickets[1][2]=profit_closed; 
   tickets[2][2]=longshort;   
   tickets[3][2]=swap_count; 
   tickets[4][2]=swap;       

//just some testing that 'tickets' array has actual values other than '0' and this gives the proper values i want to store..   
Alert("some random values:",tickets[0][0],tickets[1][1],tickets[2][2]);    

//Actual writing of Array
   int bla=FileOpen(file_name,FILE_WRITE|FILE_BIN);
   if(bla!=INVALID_HANDLE)
      {
      if(FileWriteArray(bla,tickets)==0)   
         {
         Alert("error writing file file ",file_name,", error ",GetLastError());
         }
      FileClose(bla);
      Print("FileWrite ",file_name," = OK");
      }
   else Alert("error writing recovery file ",file_name,", error ",GetLastError());  
   return;
   }
Route206
211
Route206 2015.08.21 05:01  
Just as further clarification : the file IS being written every time as per timer. Just with only zeros...
whroeder1
13610
whroeder1 2015.08.21 11:49  
  1.           double  tickets[][3]      ; //Array 
    Tickets has no size. If the file doesn't exist in init, do you resize the array? Otherwise you can't assign to it and nothing should be written. Are you using the strict property, why not?
  2.    tickets[0][2]=open_price; 
       tickets[1][2]=profit_closed; 
       tickets[2][2]=longshort;   
       tickets[3][2]=swap_count; 
       tickets[4][2]=swap;  
    
    I also suggest you change from a 2D array to a 1D structure, so the code is self documenting. See the example in FileWriteArray - MQL4 Documentation
Route206
211
Route206 2015.08.26 14:47  

Thanks!

Yes - i do resize the array, but didn't do so until after OnInit()!  Since my file recovery was done in OnInit() obviousely, i think that issue was that while the data was being writting, it wasn't read. The read instruction was prior to the Array being defined. I have corrected that and now it works. So i guess you were right, ...sort of ;-)

As for the 1D array with self documenting coding: That is indeed a great idea. Hadn't thought of that so maybe for a next time. Come to think of it: as i'm using counters in both dimensions on this one, i guess it will not work for this one.

/
To add comments, please log in or register