EA to write boolean values to a file (txt or bin) and retrieve values from file

 

Need help in writing a code to write 8 boolean values to a text file every new bar after my conditions for buy/sell have been processed. If EA is turned off and restarted it would initialize the boolean values from the same file. 

I did something like below (my functions to obtain and write values from the file) but get lots of error messages. Any guidance would be appreciated.

___________________________________________ 

int [] readbParameters(string filename)

{    int handle = FileOpen(filename,FILE_BIN|FILE_READ);

     int bParametersR []= {0,0,0,0,0,0,0,0,0};

     if(handle < 1) { 

                      writebParameters(filename, bParametersR);

                      return bParametersR;

                    }

     handle = FileOpen(filename,FILE_BIN|FILE_READ);               

     if(handle > 0)

     { FileReadArray(handle, bParametersR, 0, 9); // reading parameter values

       FileClose(handle);

     }

     else {Print("readbParameter - Parameters file not found."); return(false);}

     return bParametersR;

}


bool writebParameters(string filename, int bParametersW[])

{    int handle = FileOpen(filename,FILE_BIN|FILE_WRITE);

     

     if(handle > 0)

     { FileWriteArray(handle, bParametersW, 0, 9); // writing parameter boolean  - 1 = true; 0 = false

       FileClose(handle);

     }

     else {Print("writebParameter - parameter file not found."); return(false);}

     return(true);

}

 

kanash 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags
  • www.mql5.com
Standard Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags - Documentation on MQL5
 
kanash264:

Need help in writing a code to write 8 boolean values to a text file every new bar after my conditions for buy/sell have been processed. If EA is turned off and restarted it would initialize the boolean values from the same file. 

I did something like below (my functions to obtain and write values from the file) but get lots of error messages. Any guidance would be appreciated.

___________________________________________ 

int [] readbParameters(string filename)

{    int handle = FileOpen(filename,FILE_BIN|FILE_READ);

     int bParametersR []= {0,0,0,0,0,0,0,0,0};

     if(handle < 1) { 

                      writebParameters(filename, bParametersR);

                      return bParametersR;

                    }

     handle = FileOpen(filename,FILE_BIN|FILE_READ);               

     if(handle > 0)

     { FileReadArray(handle, bParametersR, 0, 9); // reading parameter values

       FileClose(handle);

     }

     else {Print("readbParameter - Parameters file not found."); return(false);}

     return bParametersR;

}


bool writebParameters(string filename, int bParametersW[])

{    int handle = FileOpen(filename,FILE_BIN|FILE_WRITE);

     

     if(handle > 0)

     { FileWriteArray(handle, bParametersW, 0, 9); // writing parameter boolean  - 1 = true; 0 = false

       FileClose(handle);

     }

     else {Print("writebParameter - parameter file not found."); return(false);}

     return(true);

}

 

kanash 


#define SIZE 10

static int Buffer[SIZE];
m_file = " some name for the file ";

//+------------------------------------------------------------------+
//| Constructor for signal.                                          |
//| INPUT:  no.                                                      |
//| OUTPUT: no.                                                      |
//| REMARK: no.                                                      |
//+------------------------------------------------------------------+
void CSignal::CSignal()
  {
      
      //--- setting default values...
      
      //--- initializing of weights buffer
      int handle = FileOpen(m_file+".dat",FILE_READ|FILE_BIN|FILE_ANSI);
      if(handle!=INVALID_HANDLE)
      {
         FileReadArray(handle,Buffer,0,WHOLE_ARRAY);
         FileClose(handle);
      }

      //--- You can omit this if you do not need it...
      if(FileSize(handle)<=0||handle==INVALID_HANDLE)
      {  
            for(int j=0; j<SIZE; j++)
            {
               Buffer[j] = MathRand()%2;
            }
         
         
      }
  }


//+------------------------------------------------------------------+
//| Destructor for signal.                                    |
//| INPUT:  no.                                                      |
//| OUTPUT: no.                                                      |
//| REMARK: no.                                                      |
//+------------------------------------------------------------------+
void CSignal::~CSignal()
  {

   //--- any deinitiallizing source you may have

   int handle = FileOpen(m_file+".dat",FILE_WRITE|FILE_BIN|FILE_ANSI);
   if(handle!=INVALID_HANDLE)
   {
      FileWriteArray(handle,Buffer,0,WHOLE_ARRAY);
      FileClose(handle);
   }
   if(handle==INVALID_HANDLE)
   {
      printf(__FUNCTION__+": error writing to file");
   }
  }
 
ssn:


Thanks ssn. 
Reason: