Trader’s Tech – Writing Your Own EA Part 29 – Code Modifications

Forex RobotGood morning on this beautiful Thursday morning. More interesting things are happening here on this northern Indiana farm. It turns out that the strutting peacock I mentioned the other day (yes, they actually strut) is now the proud daddy of four little pea-chicks. New life is a wonder to behold.

If you’re new to this series and want to check it out from the beginning, you can find that here. And look here for a list of all the programming articles.

In Part 28 we finally got our EA running properly and we looked at the ST Journal and how it can be used to diagnose problems. In the absence of an inline debugger, we must use the Journal along with the Print function to debug our program. But that’s a rant for another day. Today we’ll do a couple small modifications to our EA to make it more versatile.

The first is a very simple modification to automatically allow for 4- & 5- digit brokers. This is something I include in all my EAs. I test my EAs on many broker platforms (one of my computers has over 15 MT4 installations on it.) I learned early on that just because an EA works on one broker’s platform doesn’t mean it will work on all brokers’ platforms. My AdjPoint (Adjusted Point) variable makes it much easier to switch platforms without regard for the 4- & 5- digit issue.

I’ve never actually written this into a separate function. Like most programmers, I’m a tad OCD about the organization of my code. I just realized that this little piece of code should actually be an included function (we’ll talk about reusable code, includes and libraries in a future installment.) For now, it’s just a couple of lines in the init() function of the EA.

The AdjPoint modification requires a new global variable,

double AdjPoint;

be declared at the top of the program. This can be placed anywhere, but I put this in a special place right after the external variable declarations. Then the following code goes near the top of the init() function.

int FiveDig;
if(Digits == 5 || Digits == 3)
   FiveDig = 10;
   FiveDig = 1;

AdjPoint = Point * FiveDig;

As you know, the predefined variable Digits returns the number of digits after the decimal point for the current symbol. A five-digit broker will return 3 digits for JPY pairs and 5 digits for all the other pairs. A four-digit broker will return 2 digits for JPY pairs and 4 digits for the other pairs. So, if Digits returns 3 or 5 digits, you know this is a 5-digit broker. The FiveDig multiplier variable is then loaded with 10 (10 points/pip.) If it’s a 4-digit broker, the FiveDig variable will be loaded with 1 (1 point/pip.)  Then our AdjPoint variable is set equal to Point X FiveDig (the predefined variable Point is the value required to convert between Points and Price.) The result is a variable (AdjPoint) that is the conversion factor between Pips and Price. Now we can designate our SL & TP in Pips and the EA will allow for different broker’s price representations.

In case I haven’t mentioned it before, MT4 uses the double equals sign (==) to represent a test of equality, the double pipe (||) is a logical OR and the double ampersand (&&) is a logical AND. With the logical OR, only one of the two sub statements (Digits=5 OR Digits=3) must be true for the whole statement to return a true. With the logical AND, both sub statements must be true for the whole statement to return a true.

To use our new variable AdjPoint, just go through the EA and change each instance of the variable Point to our new variable AdjPoint. Just don’t convert the variable Point in our code above by accident. A quick way to change all the instances is to hit CTRL-H, put Point in the Find what: field and AdjPoint in the Replace with: field. I typically click the Find Next button and then click Replace for each instance. I almost always regret using the Replace All button because I invariably forget the dozen instances where I don’t want something replaced. 😉

I like to keep things properly named and organized – remember, you’re going to have to interpret what you’ve done six months from now – so we should change the names of our variables TPPoints and SLPoints to TPPips and SLPips, respectively. You can use the CTRL-H feature to replace them throughout the EA. This is one of those times that using the Replace All button should work well.

That’s all we have space for today. Next time we’ll make another modification so we can adjust the risk to a percentage of your account size. Thanks for your attention and please follow me on Twitter and LinkedIn.




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