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

Forex RobotGood morning (afternoon, evening, night – depending on your location) trader-programmers. I’m enjoying the peacefulness of my nephew’s farm, except that rooster that insists on greeting the sun from right outside my bedroom window.

Can you believe that we’re on the 16th installment? It seems like only yesterday…. 😉

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

In Part 15 we covered my apparent imperfection in the form of an error in the start() function. Now that we’ve got that covered, we’ll proceed on to the NewBar() function.

As I’ve said lots of times, the NewBar() function is supposed to tell us if we’re on a new bar (candle) since our last trade. Whenever a trade is opened, we set the LastTradeTime to the time of the current bar (not the current time, which was the mistake I had made.)

Let’s take a look at the NewBar() function:

bool NewBar()
    {
    datetime CurrBarTime = iTime(NULL,0,0);
    bool NewBar = false;

    if(CurrBarTime > LastTradeTime)
        {
        NewBar = true;
        }

    return(NewBar);
    }

We start by creating a datetime variable called CurrBarTime and setting it equal to the time of the current bar.  It’s actually not necessary to use a variable for this, you could just plug the iTime() function directly into the if statement and test it. I just did that for clarity. Plus, for possible future modifications, it’s nice to have a variable with the value set to it.

I created a boolean variable called NewBar to keep track of whether there was a new bar. There’s nothing magical about the name NewBar, you could have named it McGillicuddy if you wanted to. I just like to keep things consistent and understandable. You’ll notice the logic of this function. We set NewBar to false in the very beginning. It is only changed to true if the current bar is greater than the LastTradeTime bar which indicates we have a new bar since the last trade. Then the function returns the value of NewBar() to the caller (that’s programmer-speak for “the place from which the function was called.”)

In case I haven’t mentioned it before, if your if clause only has one line following it, the braces are not necessary. I included them here for clarity.

Well, for once I didn’t overtalk and we got this part done in less-than-normal words. Since it’s Friday and our next topic will take several articles, I’ll give it a rest for now. Have a wonderful weekend and we’ll take this back up on Monday.

Thanks for your attention and 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.

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news