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

Forex RobotIn case you missed the beginning of this series, you’ll find Part 1 right here.

In Part 11 of our series we discussed static variables, the datetime variable type, variable scope and MQL4’s iTime() function. We also added the code that ensured that we only take a single trade per trade signal. This time around we’ll talk about adding the trade entry code.

Before we get into that, I want to clarify something I wrote in Part 11. When an MQL4 function asks for a time frame, you enter the time frame in minutes. But it can’t be any arbitrary number of minutes, it has to be a valid MT4 time frame expressed in minutes (1, 5, 15, 30, 60, 240 = H4, 1440 = D1, 10080 = W1, 43200 = MT1.)

I would also like to digress and cover something that we sort of glossed over in Part 11. That is the variable type known as string. In the iTime() function, the symbol is passed to the function in the form of a string variable. A string variable is a variable that contains a series of characters. When you set a variable equal to the series of characters, you enclose the characters in double quotes (). So, if I want to designate the string variable called symbol equal to a chart symbol it would look like this:

symbol = "EURUSD";

MQL4 functions usually allow you to use the keyword NULL to represent the symbol of the chart under which the EA is running. In some cases, you are not allowed to do that (we’ll talk more about that later.) In those cases you can use the MQL4 function Symbol() which returns the symbol of the current chart.

Any time you need to pass information to a function, you can pass it using the literal (“EURUSD”) or the variable (symbol). The same is true of all other variable types. Of course, the tricky one is passing a datetime. Again, we’ll discuss that further down the line.

Now, on to the code.

We need only add our pseudo-code (which, as I said is pretty much actually code – I just find it easier to think in code, but you don’t have to do that. Just get the structure down on “paper” then do the actual code) to trigger the trade now. As you may recall, we used a variable CrossOver to hold the crossover direction information. We could have simply tested the direction with our IsThereACrossOver() function, but we wanted to store the direction of the crossover so we could send that to the OpenTrade() function. Here is where we’ll use it:

 

start()
    {
    int CrossOver;
    if(!IsThereAnOpenTrade())
        {
        //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
               OpenTrade(CrossOver);
               }
            }
        }
     }

We’ve added the function OpenTrade() to our code. Again, we’ve put off the actual details of opening a trade by simply using a function name. Typically functions are used so the programmer doesn’t have to put duplicate code in the program. In addition, using functions simplifies the main structure of the program and makes it more obvious when you go back and look at the program in six months.  You’ll notice that we are sending the value CrossOver to our OpenTrade() function so we know which direction the trade should be opened.

That should complete our basic EA structure. Next time we’ll dive into creating the functions that we’ve named here. I find this a fairly simple way of writing software; by breaking down a program into its component parts and dealing with the parts on an individual basis. That way, I can get the program structure out of my head and down on “paper” (I almost never use paper anymore – at least not since the 80s) and can fill in the trivial details (actually, no detail is too trivial for a computer) later.

Thanks for your attention. Please follow me on Twitter and have a great weekend!

Tim

 

here_for_you_001


Connect_With_Winners_Edge

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.

Latest posts by admin (see all)

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news