How to read a single cell from a CSV file?

 

Hello, there is not a lot of documentation on this issue, I need to get just one value from a cell from a csv file. My code is below and it doesn't seem to work. I've checked every thread and I do not seem to understand how reading specific columns/rows/cells works in MQL4. Any clarity on this case will be much appreciated. Thank you. 

int OpenRead(){
string Signal;
int handle = FileOpen(filename,FILE_READ|FILE_CSV,',');
if(FileSeek(handle,36,SEEK_SET)==TRUE){
Signal = FileReadString(handle,1);
FileClose(handle);
Alert(Signal);
return(Signal);
}

My data looks like below, the value I need is the 'Predicted Value' column, I only need the first entry below the header. 

DemoDatasetReadCSV

 
Rahul Shaji Parmeshwar:

Hello, there is not a lot of documentation on this issue, I need to get just one value from a cell from a csv file. My code is below and it doesn't seem to work. I've checked every thread and I do not seem to understand how reading specific columns/rows/cells works in MQL4. Any clarity on this case will be much appreciated. Thank you. 

My data looks like below, the value I need is the 'Predicted Value' column, I only need the first entry below the header. 

you mean your code does not even compile...

try structuring it properly and you will see that your brackets are not aligned.

get the code to at least compile before you ask why it does not work

 

@PaulAnscombe, I have edited my code now and the updated one is below, it compiles file now, I was just missing a bracket.

int OpenRead(){
string Signal;
int handle = FileOpen(filename,FILE_READ|FILE_CSV,',');
if(FileSeek(handle,36,SEEK_SET)==TRUE){
Signal = FileReadString(handle,1);
FileClose(handle);
Alert(Signal);
return(Signal);
}
}
 
Rahul Shaji Parmeshwar #:

@PaulAnscombe, I have edited my code now and the updated one is below, it compiles file now, I was just missing a bracket.

so what happens when you run it then, what is the problem?

You have a CSV file  so the fields in that can be of variable size.

Fileseek will move the file pointer a number of bytes, in your case 36 bytes, so it is not going to return the 36th CSV value it is going to return the 36th byte value of the file.

You will need to read through the CSV values until you get to the one you want. 

Depending on your data depends which command you use  from the choices FileReadString, FilerReadInteger etc..

The code styler will help you to structure your code properly and make it readable


int OpenRead()
  {
   string Signal;
   int handle = FileOpen(filename,FILE_READ|FILE_CSV,',');
   if(FileSeek(handle,36,SEEK_SET)==TRUE)
     {
      Signal = FileReadString(handle,1);
      FileClose(handle);
      Alert(Signal);
      return(Signal);
     }
  }
 

@PaulAnscombe It returns 0 as my value but my value should either be 1 or -1. I've attached my dataset here for you to view. I need the column 'Predicted_Signal' specifically cell P2.

I ran it like this.

void OnTick()   {           if(TimeCurrent() > (Switch+30)){       OpenRead();       Alert(OpenRead());       Switch = TimeCurrent();        }   }

int OpenRead(){
string Signal;
int handle = FileOpen(filename,FILE_READ|FILE_CSV,',');
if(FileSeek(handle,36,SEEK_SET)==TRUE){
Signal = FileReadString(handle,1);
FileClose(handle);
Alert(Signal);
return(Signal);
}
}

Files:
Dataset.csv  4 kb
 

@PaulAnscombe

So is this what you're trying to say I should do? I ran this but it's giving me empty values or values with 0. I really need help on this please.

int OpenRead()
{
string Signal;
int handle = FileOpen(filename,FILE_READ|FILE_CSV,',');
  if(FileIsEnding(handle)!= True)
  {
   FileSeek(handle,1,SEEK_SET)==TRUE;
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   Signal = FileReadInteger(handle,1);
   FileClose(handle);
  }
Alert(Signal);
return(Signal);

}
 
mql4 question on mql4 section please.
 
Rahul Shaji Parmeshwar #:

@PaulAnscombe

So is this what you're trying to say I should do? I ran this but it's giving me empty values or values with 0. I really need help on this please.

you need to understand more about data types, your data is not all integers.

Change the file name in this code, run it and it will show you the data read in the Experts tab - it is a script

#property strict


//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   string DataItem;
   int FileHandle = FileOpen("dataset.csv",FILE_READ|FILE_CSV,',');
   if(FileHandle < 0)
   {
      Print("File Error " + GetLastError());
      return;
   }
   
   for(int Count=0; Count<99; Count++)
   {
      if(FileIsEnding(FileHandle))
         break;
      DataItem = FileReadString(FileHandle,0);
      Print("Item: " + Count + "  = " + DataItem);  
   }
   FileClose(FileHandle);
}
 
Do not try to seek on CSV files, the fields are variable length. Just read all the fields.
struct signal{
   datetime date;
   int      time; // MS
   double   open, high, low, close;
   double   s_10, corr, stoch, momentum, macdSig, macdMain, rsi;
   double   o2c, o2o;
   double   predicted;
   double   ndx,start;
   void read(int h){
      date = (datetime)FileReadInteger(h);
      time = FileReadInteger(h);
      open = FileReadDouble(h);
      ⋮
   }
}
/////////////////////////
signal s; s.read(handle); Print(s.predicted);

Don't double post! You already had this thread open.
          General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)

 

@WilliamRoeder So I have implemented your code and 'Print(s.predicted)' still seems to return a value of 0. However if I change the value to 'Print(handle)' it's showing up as 1, however, if I change the value in my data sheet to -1, it doesn't update it in mql4. If you run the code below, it should replicate the problem I'm having. I also want to thank you for your steadfast responses @PaulAnscombe.

//Global Variables
datetime Switch;


void OnTick()
  {
     if(TimeCurrent() > (Switch+39)){
     int handle=FileOpen(filename,FILE_CSV|FILE_READ,",");
     
     if(handle != INVALID_HANDLE){
       
struct signal{
   datetime date;
   int      time; // MS
   double   open, high, low, close;
   double   s_10, corr, stoch, momentum, macdSig, macdMain, rsi;
   double   o2c, o2o;
   double   predicted;
   double   ndx,strategy;
   
   void read(int h){
      date = (datetime)FileReadInteger(h);
      time = FileReadInteger(h);
      open = FileReadDouble(h);
      high = FileReadDouble(h);
      low = FileReadDouble(h);
      close = FileReadDouble(h);
      s_10 = FileReadDouble(h);
      corr = FileReadDouble(h);
      stoch = FileReadDouble(h);
      momentum = FileReadDouble(h);
      macdSig = FileReadDouble(h);
      macdMain = FileReadDouble(h);
      rsi = FileReadDouble(h);
      o2c = FileReadDouble(h);
      o2o = FileReadDouble(h);
      predicted = FileReadDouble(h);
      ndx = FileReadDouble(h);
      strategy = FileReadDouble(h);
   }
};
   signal s;
   s.read(handle);
   Alert(handle);
   Print(s.predicted);
   
   Switch = TimeCurrent();
   FileClose(handle);
   }
   else{Alert("Error Opening File",GetLastError());}
   
   }
 
  1. You haven't shown the actual file, only a spreadsheet. Is the actual file separated by commas?
  2. Predicted is shown as an int not a double.
  3. NDX and startegy are missing on line 14. Therefor your code must skip reading them (if line is ending)
  4. You haven't shown the actual file, only a spreadsheet. Is the 14/15 actually in the file, you need to read them.