Trader’s Tech – Writing Your Own EA Part 11 – Program Structure

Forex RobotIn Part 10 of our series, we talked about how MQL4 identifies “candle lookback” and that we need to be sure we only take one trade per signal. This time we’ll get into the mechanics of that one trade thing.

The first thing we have to do to be sure we only take a single trade per signal is to keep track of when the last trade was executed. The way I like to do it is to store the candle time of the last trade in a variable. As I’ve mentioned briefly before, EAs re-initialize when you change time frames on your chart. To be sure our last trade time is not lost when you change time frames, we have to use a special designation of variable known as a static variable. Any variable type (int, string, double, whatever) can be declared as a static variable. A static variable is one that maintains its value even when the EA is de-initialized. So the value of the last trade time will survive a time frame change.

Dates and times in MQL4 are stored in a variable type known as datetime. A datetime variable is simply an integer variable that stores the seconds since 1 January 1970.  There are MQL4 functions that allow you to manipulate dates and times and return different elements of the date/time (day, month, year, hour, minute, sec, day of the week, day of the year and so on.) So a datetime variable is pretty handy.

We will be declaring the last trade time variable as a global variable which means it will be available to any function in the EA. Which brings me to a short discussion on variable scope. Variable scope is the term that describes the places within the program that you can actually use a certain variable. If a variable is declared in a function, it can only be used within that function. If the variable declared in the function has the same name as a global variable, the variable declared in the function is a different variable and takes precedence in that function over the global variable. Also, remember that variables with the same name, but different cases (eg. Widget and widget) are different variables. So, if your program doesn’t work for some reason, go through your code and be sure you have the correct case for all your variable names (yes, once again speaking from experience.)

Since our last trade time variable will be declared as a global, we’ll add the declaration to our code outside of any function.  For the sake of brevity, I’ll show it outside our start() function, but when we actually put this code in the MetaEditor, it will be near the top of the program. This is what our declaration will look like

static datetime LastTradeTime;
 Now we need to add code the check and set our LastTradeTime:

    int CrossOver;
        //Enter code that executes if there is no open trade here.
        CrossOver = IsThereACrossOver();
        if(CrossOver != 0)
            //Enter code that executes if there is a crossover
            if(iTime(NULL,0,0) > LastTradeTime)
               //Set LastTradeTime to iTime(NULL,0,0)
               LastTradeTime = TimeCurrent();
               //Enter code to trigger a trade

The MQL4 function iTime() returns the time value for the specified candle. In this case, the first parameter (NULL) is the symbol of the pair to be tested. NULL means the current chart symbol. You could replace it with an actual symbol name surrounded with quotes (“). The second parameter is the time frame in minutes, 0 (zero) means the current chart time frame. And the third parameter is the candle look back or shift. 0 (zero) means the currently active candle. What we are doing is determining if the current candle time is the same as the Last Trade Time. If not, a new trade will be entered and we will reset the Last Trade Time to the current candle time.

That’s it for this episode. Next time we’ll add the function to trigger the trade and hopefully we’ll soon start looking at the functions that will actually perform these feats. Thanks for your attention and follow me on Twitter.


In case you missed this series, it starts right here.



Get trade set ups everyday!

The following two tabs change content below.
Winners Edge Trading was founded in 2009 and is working to create the most current and useful Forex information and training available on the internet.

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news