Download MetaTrader 5

FileRead csv file getting an error

To add comments, please log in or register
Larry
1268
Larry  

I wrote this code and put it in my EA's init location. Each time I start the EA, I want to read in this file.

   int Handle;
   string FileName = "Callouts1.csv";
   string OP_Type = "";
   string Symb = "";
   double Entry;
   double StopL;
   double FirstNum;
   double SecondNum;
   
   Handle=FileOpen(FileName,FILE_CSV|FILE_READ,",");    // File opening
   if(Handle<0)                                         // File opening fails
   {
      if(GetLastError()==4103)                          // If the file does not exist,..
      {
         Alert("No file named ",FileName);              //.. inform trader
      }
      else
      {
                                                         // If any other error occurs..
         Alert("Error while opening file ",FileName);    //..this message
         PlaySound("Bzrrr.wav");                         // Sound accompaniment
         return;                                         // Exit start()
      }
   }
   
   while(FileIsEnding(Handle)==false)                    // While the file pointer..     
   {                                                     // ..is not at the end of the file
      OP_Type = FileReadString(Handle);                  // Is this a buy or sell
      Symb = FileReadString(Handle);                     // What symbol?
      Entry = FileReadDouble(Handle);                    // Entry price
      StopL = FileReadDouble(Handle);                    // Stop price
      FirstNum = FileReadDouble(Handle);                 // First price
      SecondNum = FileReadDouble(Handle);                   // Second price
      if (StringSubstr(Symbol(),0,6) == Symb)
      {
         if (OP_Type == "Buy")
         {
            BuyEntry = Entry;
            BuyFirst = FirstNum;
            BuySecond = SecondNum;
            BuyStopLoss = StopL;
         }
         else
         {
            SellEntry = Entry;
            SellFirst = FirstNum;
            SellSecond = SecondNum;
            SellStopLoss = StopL;
         }
      }
   }
   
   FileClose( Handle );                // Close file

File data looks like this:

Buy,Eur/Usd,1.423,1.419,1.426,1.429
Buy,Gbp/Usd,1.647,1.642,1.6495,1.653
Buy,Aud/Usd,0.809,0.811,0.814,0.818
Buy,Usd/Cad,1.119,1.115,1.1235,1.125
Buy,Usd/Chf,1.089,1.085,1.092,1.095
Buy,Usd/Jpy,94.5,94.1,94.8,95.1
Sell,Eur/Usd,1.404,1.408,1.402,1.4
Sell,Gbp/Usd,1.635,1.639,1.631,1.628
Sell,Aud/Usd,0.793,0.796,0.791,0.788
Sell,Usd/Cad,1.108,1.1125,1.105,1.103
Sell,Usd/Chf,1.068,1.071,1.065,1.062
Sell,Usd/Jpy,92.7,93.1,92.5,92.2

Error:
2009.07.19 14:04:24 Callout1: attempt to read binary data from a CSV file

File is located in ...\experts\files\

fbj
1047
fbj  

look at FileReadNumber() and FileReadString() which work with CSV files. Your "," delimiter is the terminating token for each call of these functions.

the ones you use are for BINARY files. Different format...

FileReadNumber() reads in valid number characters for types int/double until delimiter is seen.

FileReadString() reads in characters until delimiter is seen and since comma is your delimiter, the file data looks ok for that.

Just replace the ones you are using with above two.

Larry
1268
Larry  

That is not making sense. Are you saying I should not use comma "," or that I am using the wrong methods to get the data? The data type for the number I am pulling in is double, so shouldn't I use FileReaddouble?

Aslo, this is the same code I pulled from the File Operations information here on this site. File Operations.

I am sorry I am confused at what you are saying the problem is.

Larry
1268
Larry  
I got it, I figure d it out. You was referring to the FileRea dNumber instea d of FileRea d double. It works now thanks.
fbj
1047
fbj  

Greeeat Job...

Mahdi E
195
Mahdi E  

Hi dear

What isthe right code for this EA?

and if we have just 3 prices in the FIL3DATA and want to change BuyFirst to TakeProfit and remove BuySecond what shall to do?

and the typ of order is buy limit and sell limit.


Thanks a lot for ane hellping  ;-)

To add comments, please log in or register