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

Forex RobotIn Part 8 we briefly discussed functions and curly braces for code grouping and started the first part of our actual pseudo-code. Today, we’ll continue with the pseudo-code which may end up looking more like actual code.

So far, this is what we’ve got:

start()
    {
    if(IsThereAnOpenTrade())
        {
        }
    }

Well, it’s a start, right? And starting is half the battle, or so I’m told. At this point, I’m going to make a little change. Our function IsThereAnOpenTrade() returns a true if there is an open trade and a false if there is not. In the logic of our program, we will be doing nothing if there is already an open trade. In a more elaborate strategy, we would most likely be doing some sort of trade management (watching for hidden stops or places where we would add to the trade or move the stop to break even or some other such thing.) In that case, we would put the trade management code between the braces after our if statement, then use an else statement to execute a block of code that would happen if there was no open trade. For example:

if(IsThereAnOpenTrade())
    {
    //Enter trade management code here
    }
else
    {
    //Enter code that executes if there is no open trade here.
    }

(Notice our use of the double slash for comment code.) The code after the else statement would be executed if our IsThereAnOpenTrade() function returned a false. Since we won’t be executing an trade management code in our EA, here’s a little change I’m going to make. I’m going to use the Logical Negation operator. That’s just a fancy name for the exclamation point (!). What that does is it negates or reverses a boolean variable or function. In our case, this is what it looks like:

if(!IsThereAnOpenTrade())
    {
    //Enter code that executes if there is no open trade here.
    }

What we’ve done is reverse the returned value from our function so we don’t have to use the else statement and our code is simplified.

Once we’ve determined that there is not already an open trade, we need to see if the conditions are right for a trade. Here’s where we’re going to get a little tricky. We’ll create a function (in other words, we’re putting off some coding for a later time) to determine if a moving average crossover has actually taken place. Not only do we need to know if there’s a crossover, but we need to know which way the crossover occurred. So, the function will have to return an integer code with that information. Typically the code I use is -1 = crossover to the downside, 0 = no crossover, and 1 = crossover to the upside. We could test the return of the function in an if statement, like we did with the prior function, but we’ll need to know the direction of the crossover when we want to open a new trade, so now we have to declare a crossover variable to store that value, set it equal to the current crossover situation and then test to see if a crossover has occurred.  You must declare a variable before you can use it. That’s just programmer-speak for telling the computer that you are going to be using a variable of a certain type and name. It’s not necessary to declare your variables at the top of the function, but, because of prior languages that I’ve used, I do it out of habit and convenience.

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

Notice that lines that are “complete thoughts” must be terminated with a semicolon (;). The reason there is no semicolon after the if statements is because the thoughts are not completed until after the section of code that is bound by the curly braces. There is no need for a semicolon after a right brace. Ah, just another example of the “finickiness” of the computer. Also, I usually use shorter function names, but I’m trying to make this code as self-explanatory as possible.

Just to be sure it’s clear what we’ve done, here’s a brief synopsis: The start() function only executes when a tick comes in on the current pair. The first thing we do is see if there’s an open trade. If there is no open trade, we call the function to see if we have a crossover of our moving averages and set the result to our variable. Then we test our variable to see if indeed we have a crossover. Our variable will equal 0 if no crossover has occurred and either 1 or -1 if there is a crossover. If there is no crossover, we do nothing.

Well, way over again today. I guess I’m just too wordy. 🙂 Thanks for your attention. Please don’t hesitate to question or comment. And please follow me on Twitter.

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