[Coding Issues] Unable to read and select values from .csv file, function returns no error

 

Hello all,

Currently I am trying to make an EA which suppose to make my life a bit easier. In the end it should be able to automatically import an Economic Calendar. At the EA initalisation I specifie the Buy and Sell thresholds. So Manual analysis has been made, and if Indicator XXX comes out <=2.1% it should sell, if the indicator comes out >= 2.4% it should send a buy order. Also I want the data retrieved from the calendar plotted on the chart.

I started out this morning, and at this moment I am really struggling with reading the file. This is what I would like to have working first, before starting with automatically downloading. Also, I must say that I was -until today - unfamiliar with File functions, so don't go to hard on me please :)

A question on which I could not found the answer. Data in the Calendar.csv is presented in this format:

Date,Time,Time Zone,Currency,Event,Importance,Actual,Forecast,Previous
Wed Jul 24,01:30,GMT,AUD,AUD Consumer Prices Index RBA Weighted Median (QoQ),Low,,0.5%,0.5%
Wed Jul 24,01:30,GMT,AUD,AUD Consumer Prices Index RBA Trimmed Mean (QoQ),Low,,0.5%,0.3%
Wed Jul 24,01:30,GMT,AUD,AUD Consumer Prices Index RBA Trimmed Mean (YoY),Low,,2.1%,2.2%

While using the FileReadString() function to read the data, how does this work? I thought that every FileReadString() function reads a variable, so that we need to have 8( Date, Time, TimeZone, Currency, Event, Importance, Actual, Forecast, and Previous) variables to store them in? After copying those 8 values, it goes to the next 8 variables(which is the next line).

At initalisation I must fill in keywords that able the StringFind() function to stop when the correct order is selected.

Am I correct in this thought process or does it not work this way?

Relevant code (Every GetLastError() function returns 0. Also, the Calendar.csv is been put in manually for now, so it really is there and it is read)

//+------------------------------------------------------------------+
//| Download calendar function: If the settings in the GetRight.exe  |
//| program are correct, the program should download the calendar    |
//| into C:/Program~1/Broker/expert/files -specified in Getright.exe |
//+------------------------------------------------------------------+
//| This function reads and converts the data from the saved file    |
//| into information. This information should be converted into this |
//| Format displayed on the chart                                    |
//| Date|Time|TimeZone|Currency|Event|Imp.|Actual|Cons.|Previous|    |
//+------------------------------------------------------------------+
void GrabNews() 
  {
// Download calendar  
 //WinExec(GetrightAdress+" /URL:"+HtmlAdress+" /FILE:Calendar.csv /W /O",0);
 
// Opens the file: from now on we are in the file
   int handle = FileOpen("Calendar.csv",FILE_READ|FILE_CSV,';');
   if (handle==-1||FileSize(handle)==0)
       Print("Error is",GetLastError());
           
// Select the order using StringFind    
   while(StringFind(NewsEvent,Event,0)==false)
   {
     NewsDate = FileReadString(handle);
     NewsTime = FileReadString(handle);
     NewsTimeZone = FileReadString(handle);
     NewsCurrency = FileReadString(handle);
     NewsEvent = FileReadString(handle);
     NewsImportance = FileReadString(handle);
     NewsActual = FileReadString(handle);
     NewsForecast = FileReadString(handle);
     NewsPrevious = FileReadString(handle);   
   }

  Print("Error is",GetLastError());

//Create Objects at initalisation     
  if(first==1)
   {    
//Create Event Label   
    ObjectCreate("Event", OBJ_LABEL, 0, 0, 0);
    ObjectSetText("Event",NewsEvent,12, "Times New Roman", White);
    ObjectSet("Event", OBJPROP_CORNER, 0);
    ObjectSet("Event", OBJPROP_XDISTANCE, 10);
    ObjectSet("Event", OBJPROP_YDISTANCE, 15);    
    
//Create Time Label   
    ObjectCreate("Time", OBJ_LABEL, 0, 0, 0);
    ObjectSetText("Time","Rls: " +NewsDate+" "+NewsTime+" "+NewsTimeZone,12, "Times New Roman", White);
    ObjectSet("Time", OBJPROP_CORNER, 0);
    ObjectSet("Time", OBJPROP_XDISTANCE, 10);
    ObjectSet("Time", OBJPROP_YDISTANCE, 30);
        
//Create Impact label 
    ObjectCreate("Impact", OBJ_LABEL, 0, 0, 0);
    ObjectSetText("Impact","Imp: " +NewsImportance,12, "Times New Roman", White);
    ObjectSet("Impact", OBJPROP_CORNER, 0);
    ObjectSet("Impact", OBJPROP_XDISTANCE, 10);
    ObjectSet("Impact", OBJPROP_YDISTANCE, 45);      
    
//Create Currency Label   
    ObjectCreate("Cur", OBJ_LABEL, 0, 0, 0);
    ObjectSetText("Cur","Cur: " +NewsCurrency,12, "Times New Roman", White);
    ObjectSet("Cur", OBJPROP_CORNER, 0);
    ObjectSet("Cur", OBJPROP_XDISTANCE, 10);
    ObjectSet("Cur", OBJPROP_YDISTANCE, 60);          

//Create Previous Label
    ObjectCreate("Previous", OBJ_LABEL, 0, 0, 0);
    ObjectSetText("Previous","Prv: " +NewsPrevious,12, "Times New Roman", White);
    ObjectSet("Previous", OBJPROP_CORNER, 0);
    ObjectSet("Previous", OBJPROP_XDISTANCE, 10);
    ObjectSet("Previous", OBJPROP_YDISTANCE, 75);  

//Create Consensus Label
    ObjectCreate("Consensus", OBJ_LABEL, 0, 0, 0);
    ObjectSetText("Consensus","Cns: " +NewsForecast,12, "Times New Roman", White);
    ObjectSet("Consensus", OBJPROP_CORNER, 0);
    ObjectSet("Consensus", OBJPROP_XDISTANCE, 10);
    ObjectSet("Consensus", OBJPROP_YDISTANCE, 90);  
         
//Create Actual Label
    ObjectCreate("Actual", OBJ_LABEL, 0, 0, 0);
    ObjectSetText("Actual","Act: " +NewsActual,12, "Times New Roman", White);
    ObjectSet("Actual", OBJPROP_CORNER, 0);
    ObjectSet("Actual", OBJPROP_XDISTANCE, 10);
    ObjectSet("Actual", OBJPROP_YDISTANCE, 105);  
                           
    first=0;
   }

// Update actual in case it has changed:
    ObjectSetText("Actual","Act: " +NewsActual,12, "Times New Roman", White);
   
 Print("Error is",GetLastError());   

 FileClose(handle);

 Print("Error is",GetLastError());               
   }  

Also added is the document as a whole. But, it is not closed to finish yet, but everything to make this part work should be in it!

Any help would be greatly appreciated,

Regards,

Maarten

Files:
news1_2.mq4  14 kb
 
Maarten91 : A question on which I could not found the answer. Data in the Calendar.csv is presented in this format:
Date,Time,Time Zone,Currency,Event,Importance,Actual,Forecast,Previous
And what separator are you using in your code?
 

Really thanks for the hint, I've corrected it.

 int handle = FileOpen("Calendar.csv",FILE_READ|FILE_CSV,',');

Although, at this moment still the same issue occurs, is there anything else I am doing wrong?

For clearance, the 'issue', is that the values that should be read and appear as text on the label object still do not appear. From that I suspect they are not selected correct.

Edit: Now I am rushing a bit too much, I believe my FindString function is wrong, since when I remove the 'while' condition, the correct variables appear on my chart.

 
Maarten91 : I believe my FindString function is wrong, since when I remove the 'while' condition, the correct variables appear on my chart.
while(StringFind(NewsEvent,Event,0)==false)

What does StringFind return?
 
WHRoeder :
What does StringFind return?


-1: Substring not found. But I really do not get why it does not recognises it. Is there a possibility you could provide me with another hint?

Edit: Of course, I've got it! Thanks for the informative and educative course :)

 

Maarten91 :

-1: Substring not found.

But I really do not get why it does not recognises it.

  1. So what does your "While( -1 == false )" mean
  2. Add print statements dumping your 'NewsEvent' and 'Event' and the StringFind() result.
 

Hi, Maarten91

If you haven't solved your problem yet I think I can offer some insight. What I think is wrong with your use of the StringFind function is that NewsEvent and Event don't have any values. StringFind directs the search to look for the second string within the first string and the position in the first string that you want to start the search at. Neither string you're passing has a value yet so there's nothing for it to find and nothing for it to search. Also, all StringFind will tell you is the position in the first string the the second string starts at. That's not at all what you want. Try something like this:

if (FileIsEnding(handle)==false) // Make sure there's data to read
{
int i = 0; // Array indexer
string arrNewsData[9]; // Declare a string array to hold all the News Event items in the current line in the file
while (FileLineIsEnding(handle)==false) // Spin through the current line to get the items, quit when you've reached the end of the line
{
arrNewsData[i] = FileReadString(handle); // Read the item string from the file line(the delimiter you specified earlier makes sure you're only getting one item at a time), 
                                         // set the current array item to the news item
i++;      // Increment the array index so that the next item is placed in the correct position in the array

}

}

If I understand the way MQL does things correctly, this should give you the info you're looking for. You can then use the array items to set the text in your objects. Arrays are 0 based, so the first item in the array would be arrNewsData[0] and would hold the Date string, the second would be arrNewsData[1] and would hold the NewsTime string, etc. When setting the Time label text, for example, you would replace NewsDate with arrNewsData[0], NewsTime with arrNewsData[1] and NewsTimeZone with arrNewsData[2]. You can set all the news item text for the objects using the array - just be sure you use the correct array index for the correct news item. Again, remember that arrays are zero based so for your 9 element array, the indexes would run from 0 - 8 NOT 1 - 9. Hope this makes sense and helps. :-)

 
RaptorUK :
Please edit your post . . . please use the SRC button to post code: How to use the SRC button.


Done, Raptor. Thanks for the heads up. Being as I'm relatively new to the forum, is there a "rules" thread I can go through to make sure that I'm not violating forum etiquette? I do see the directions below the comment box. I didn't notice them when I was posting earlier.
 
ProfessorMetal :

Done, Raptor. Thanks for the heads up. Being as I'm relatively new to the forum, is there a "rules" thread I can go through to make sure that I'm not violating forum etiquette? I do see the directions below the comment box. I didn't notice them when I was posting earlier.

No rules thread as such, there are the Forum rules when you registered . . . https://www.mql5.com/en/users/register

Just read a few pages of past posts and you will get the gist of what is appreciated and what is not . . .

 
Will do, My Friend. I want to be a decent contributing member of the forum. BTW, was the advice I gave the OP on target? You may recall from a discussion that I started that I'm just beginning my MQL programming journey. The response I gave her was based on my experience as a software developer plus a bit of research on MQL.
 
RaptorUK :

No rules thread as such, there are the Forum rules when you registered . . . https://www.mql5.com/en/users/register

Just read a few pages of past posts and you will get the gist of what is appreciated and what is not . . .


Just read the directives on the registration page. Don't be a scammer, spammer or a butthole to other members and pay attention to what mods and admin tell you. Makes perfect sense to me. Glad to be here and I look forward to getting to know everybody. I hope I'll turn out to be a valuable contributing member.
Reason: