Trader’s Tech – Writing Your Own EA Part 13 – Functions

Forex RobotHi faithful readers, sorry I’ve been absent, I was actually on the road and now I’m hanging out with Casey, Nathan and Tony at the Winner’s Edge office in Latrobe PA. This is a great bunch of guys and I really enjoy working with them. We’re discussing new trading strategies and we’ll have some great new stuff coming out soon.

If you’ve missed this series, you can catch up starting with Part 1 here.

In Part 12 we wrapped up our basic EA structure and now we’re going to get started on the individual functions. As I’ve mentioned, you can use pseudo-code instead of actual code just to get your idea down on paper. I usually just write in code because I think that way. If you’re new to programming, it may take you some time to get used to the syntax and you don’t want your great idea to escape just because you were focusing on trivial (but necessary) details.

Here’s our code thus far:

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);
               }
            }
        }
     }

The first function we’ve got to flesh out is IsThereAnOpenTrade(). We’ve used it in the context of a boolean (true/false), so the function needs to return a boolean value. So the start is fairly straightforward:

bool IsThereAnOpenTrade()
    {
    bool OpenTrade = false;

    return(OpenTrade);
    }

We precede the function name with the return type (bool), as I mentioned there is no semi-colon after lines that precede code surrounded by braces. I’ve created a boolean variable called OpenTrade and initialized (set it equal) to false. If we don’t include any more code, this function will always return false. All we have to do now is look through all the trades in the platform and see if any of them are opened in the pair on which our EA is currently running and have the magic number we’ve assigned to this EA. If we find a trade that meets those requirements, we’ll set the value of OpenTrade to true and end the function.

To search through the trades, we use a few functions provided by MQL4 and we use the for operator. The for operator allows us to cycle through values for each iteration (cycle) of the function. We will assign an integer variable called i to count our cycles. To be sure we stop at the end of the list of trades, we’ll use an MQL4 function called OrdersTotal() which returns the total number of open trades and orders on the platform.

int i;
for (i = 0; i < OrdersTotal(); i++)
    {
    }

What this says is: repeat the code between the braces starting with i = 0 and increasing i by 1 (i++ = add 1 to i) each cycle (we call them iterations) as long as i < OrdersTotal(). When i = OrdersTotal(), the code will not execute again and the code immediately following the braces will begin to execute.

We now put this code between the braces:

int i;
for (i = 0; i < OrdersTotal(); i++)
    {
    OrderSelect(i,SELECT_BY_POS);
    if (OrderPair() == Symbol() && OrderMagicNumber() == MagicNumber)
        {
        OpenTrade = true;
        break;
        }
    }

The OrderSelect() function is an MQL4 function that selects the order that is represented by the i in our function. There are other settings that can be used for other searches of the orders (historical searches, searches via ticket number, etc.) but for now, we just need to look at trade #0, trade #1, trade #2 and so on (most everything in a computer is numbered starting with 0 rather than 1 – it’s something you get used to after a while.) After we have selected the appropriate order, we can look at the values that are important to us. In this case, the pair and the magic number. For future reference, you can look at all sorts of values, current price, current profit, stop loss, take profit, etc. As I’ve mentioned before Symbol() returns the symbol of our current chart. The double equals sign is used in boolean (true/false) comparisons to distinguish from situations where you are setting a variable equal to a value. So, what we’ve done here is said, if the pair of the selected order is equal to the chart pair and the magic number of the selected order is equal to the assigned magic number, then there is an open trade and we set our return value OpenTrade to true. The break statement will cause the for cycles to stop and jump out of the code within the braces – without completing all the cycles. If it cycles through all the platform’s open trades and finds no trade that meets these requirements, the code will continue on to the return statement and OpenTrade will be false. Here is the completed function:

 

bool IsThereAnOpenTrade()
    {
    bool OpenTrade = false;
    int i;
    for (i = 0; i < OrdersTotal(); i++)
        {
        OrderSelect(i,SELECT_BY_POS);
        if (OrderPair() == Symbol() && OrderMagicNumber() == MagicNumber)
            {
            OpenTrade = true;
            break;
            }
        }
    return(OpenTrade);
    }

As usual we’ve exceeded our article length. Oh well, we gotta get through it, right? Thanks for your attention. Please don’t hesitate to ask questions. I apologize if it takes me a little while to answer your questions, though. I will definitely answer them as soon as I catch them. Please follow me on Twitter.

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