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

Forex RobotI hope you guys missed me. I’m still on the road. I left Latrobe PA (home of Winner’s Edge) yesterday morning and traveled to my nephew’s farm in Indiana. I’ll be here a few weeks, so I should be more regular with my articles.

If you’re new to this series and want to check it out from the beginning, you can find that here.

In Part 14, we finished writing the IsThereACrossover() function. We also covered a few concepts that should help us get through the rest of our functions a little more quickly. In this part, we’ll get started handling the boolean NewBar() function which will return true if we have a new bar since our last trade and false if not. We do this to be sure that we don’t start a new trade on the same signal as a prior trade that already hit SL or TP.   I may have mentioned that I learned this lesson by stacking about a hundred trades on the same signal one time. We’ll have to start by covering an error I made in the start() function. Sorry if I’ve disappointed anyone, I’m not infallible. That makes for interesting debugging sessions on my “completed” programs. Ideally, as a reader kindly mentioned in a comment, it doesn’t hurt to test these functions as you write them.

Just to be sure everyone’s on the same page, here is the complete program so far:

int IsThereACrossover()
    {
    int CrossOver = 0;
    double currMA1, currMA2;
    double priorMA1, priorMA2;
    int currLoc, priorLoc;

    //Assign the prices for the moving averages
    currMA1 = iMA(NULL, 0, MA1, 0, MODE_SMA, PRICE_CLOSE, 1);
    currMA2 = iMA(NULL, 0, MA2, 0, MODE_SMA, PRICE_CLOSE, 1);
    priorMA1 = iMA(NULL, 0, MA1, 0, MODE_SMA, PRICE_CLOSE, 2);
    priorMA2 = iMA(NULL, 0, MA2, 0, MODE_SMA, PRICE_CLOSE, 2);

    //Determine relative locations of the prior closed candle moving averages
    if (priorMA1 > priorMA2)
        priorLoc = 1;
    if(priorMA1 < priorMA2)
        priorLoc = -1;
    if(priorMA1 == priorMA2)
        priorLoc = 0;

    //Determine relative locations of the current closed candle moving averages
    if(currMA1 > currMA2)
        currLoc = 1;
    if(currMA1 < currMA2)
        currLoc = -1;
    if(currMA1 == currMA2)
        currLoc = 0;

    //Determine if a crossover has taken place
    if(priorLoc == -1 && currLoc == 1)
        CrossOver = 1;
    if(priorLoc == 1 && currLoc == -1)
        CrossOver = -1;

    return(CrossOver);
    }
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);
    }
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);
               }
            }
        }
     }

Note that MQL4 doesn’t care about the order in which the functions appear. Order them in any way that makes sense to you and allows you to find them easily when you have to edit. There are programming languages that require you to order the function in a certain way, which is where I get my function ordering conventions. I typically put the init() and deinit() functions at the very top, followed by a group of functions that I use in all my EAs (except the one we’re writing ;).) I wrap the code with the start() function at the bottom. If I recall correctly, that comes from Pascal or some other language I used back in the 80s.

OK. Let’s begin to tackle the NewBar() function. We use the bar start time to determine if we’ve got the same bar as the last trade or not.  I mentioned an error in the above code. Here it is:

LastTradeTime = TimeCurrent();

I set the static datetime variable LastTradeTime equal to TimeCurrent(), a function that returns the current time of the MT4 platform you’re using. Instead, I meant to set LastTradeTime equal to the current bar (candle) time in the selected time frame. So, the line should be:

LastTradeTime = iTime(NULL,0,0);

The iTime() function returns the start time of the designated bar. NULL means use the symbol of the chart on which we’re running the EA. The first 0 means use the time frame of the chart on which we’re running the EA and the second 0 means to use the current bar.

We’re out of space. I guess I just talk too much. We’ll finish this NewBar() function in the next installment. It’s a fairly short function, so it won’t take too long. Next week we’ll talk about the OpenTrade() function. That’s a little more complex discussion, so it may take a few articles to cover.

Thanks for your attention. Follow me on Twitter.

Tim

here_for_you_001

1359774937_facebook 1359774984_linkedin 1359774949_twitter 1359774960_google 1359774973_rss 1360027635_youtube
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