I'm trying to add a 'Trade' / 'NoTrade' element to my EA. My plan was to have a list of dates on which I don't wan't my EA to run, so UK Bank Holidays and US Holidays that kind of thing. My plan was to have a list in an external file that would be referenced by my EA to compare the results of a Date check. So using
string Date = TimeToStr(TimeCurrent(),TIME_DATE);//"yyyy.mm.dd"
I could establish the days date then check it does/doesn't occur in an external txt file with the dates expressed in the same format,
if(Date=="Something in an external list file")TradeToday="Yes";
so 2014.01.01 no trading on New Years Day or
2014.12/31 no trading on New Years Eve etc
Please can anyone help on this or point me in the right direction.
Here's one idea:
- create text file with one date in the line. Dates should be in format same as TimeToStr() creates
- in the init function read all the dates in the array (to avoid file operations each time you need dates)
- when bar changes, check if the date of the current bar is in the array
Here is example how to read dates from the file. Note that I put fixed size for the dates array (30-no special reason, just thought it would be enough for one year). You can make it dynamic by resizing array, but you'll need to add some code to do that:
int filehandle=FileOpen("Dates.txt", FILE_READ | FILE_TXT);
int i = 0;
dates[i] = FileReadString(filehandle);
Now you have string array where each element is date in string format. Loop through the array and check if current date of the bar is in the list.
thanks drazen64, thats a superb answer, array's are somewhat of a new thing for me, but I recognize I need it for this bit of the code.
Once the array is built how do I loop through the array? I was thinking that I would use the first candle of the day (Midnight GMT) to produce the days date, then an 'if' statement to say 'Trading=On' or else 'Trading=Off' as the result, but I guess using this method I'm not sure what would go in the brackets if( 'Date' is in the Array) Trading="On" else Trading="Off";
Am I going in the wrong direction with this?
If you are not familiar with arrays, then you should get a quick introduction to basic data types. Take a look at the MQL Book, specifically introduction section on Arrays.
You should check time on the start of each bar because you don't know when your EA started or restarted.
Something like this should work:
static datetime last_time = Time;
bool newbar = false;
if(Time != last_time)
newbar = true;
last_time = Time;
string currDate = TimeToStr(TimeCurrent(),TIME_DATE);//"yyyy.mm.dd"
if(newbar == true)
// This continues example from previous post where array is declared with 30 elements
for(int i=0; i<30; i++)
if(currDate == dates[i])
// current date is in list of dates we don't trade
Trading = false;
This code should go in OnTick() function of the EA.
Thanks again drazen64, really appreciate you taking the time to reply. I'm going to swat up on arrays and then hopefully I should be able to interpret what you've given me a little better.
I'm building an EA that supports a trading strategy that I've been both back testing and forward testing over the past year or so. From the testing I've done it appears not trading on the bank holidays and the Christmas period is beneficial, plus it does give some trading down time as well, so that's what I plan to be using the code to do. Just thought I'd give some background.
I will manually add the exclude dates to a txt file as the input to the EA.
I've finally reached the point where my EA is stable and doing what I want it to, I now plan to use the above code, but my requirements have slightly changed. The data stored in my external txt file will contain two columns a date/time as discussed before in the format yyyyy.mm.dd hh:mm and a "double" ie 12345
yyyyy.mm.dd hh:mm "12345"
yyyyy.mm.dd hh:mm "23456"
yyyyy.mm.dd hh:mm "34567"
When I exported my two columns from excel, it added the "speech marks", when this gets read into my EA will it create an array of [rows] and [columns]? - dates[i] = FileReadString(filehandle);
When I loop through it I'm looking for it to find the currDate in the first column and output the value in the second column, which will be assigned to the double "TargetPrice"
I have looked at the array section of the MQL book and I 'get' what an array is and how it can be used, but it is stretching the grey cells beyond that at the moment. Any help or advice would be appreciated.
Since you are exporting from Excel, you should do that in "CSV" format. It is also text file, but with delimiters between columns.
Reason for this is that MQL has mechanism for reading CSV files which can separate values in a row.
If you want to read file as you have it now, you have to write code that will separate column values.
Read documentation on FileOpen() and on functions FileReadDateTime(), FileReadInteger() and FileReadString().
Thanks again for responding, I appreciate there was quite a gap during my previous posts.
So I've saved my csv file so my input is no tab delimited, I have two columns one with a date, the other a double, therefore FileReadDateTime(), FileReadDouble() will be required.
FileOpen() will need to be used with FILE_CSV so can this bit of code:- int filehandle=FileOpen("Dates.txt", FILE_READ | FILE_CSV); still be used?
and presumably this bit needs expanding on to introduce the second column?
Once the data has been read in and the array created within the EA, what is the best way to check that it's been created correctly???
The code above is about reading in the file into an array, which commands should I be looking at to lookup the date and out put the second column to a global 'double'
Thanks again Drazen for your time and help.
To add comments, please log in or register