# Trader’s Tech – Writing Your Own EA Part 31 – Reusable Code

And once again it’s Monday morning. I hope you all had a great weekend. I’m still on the farm and still loving it. Fresh eggs. What else can be said? ðŸ˜‰

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 30, we discussed reusable code and I passed on a FREE, free, free (that was an echo) copy of my MAXover() function and showed you how to make it an include file. This time I’ll show you how to use it and include it in your EAs.

Let’s start by discussing the ins and outs of the function. First of all, the function returns an integer that tells you if a crossover has occurred that meets the criteria and which direction the MAs crossed. Â I use this integer notation frequently in my EAs. If the function returns a 0, no qualifying crossover has occurred. If it returns a 1, MA1 crossed MA2 from below to above (up) potentially signaling a long (buy) trade (depending upon your strategy.) If a -1, MA1 crossed MA2 from above to below (down), potentially signaling a short (sell) trade.

The ins are as follows:

MAType – An integer representing the type (MT4 calls it the Moving Average Method) of Moving Average to use. This integer corresponds to MQL4’s internal pre-defined constants. 0 – Simple Moving Average, 1 – Exponential Moving Average. I’ve never actually used the SMMA (Smoothed moving average) or the LWMA (Linear weighted moving average), but they would also work with this since we just plug the MAType right into MQL4’s iMA() function.

MAPer1 – Moving Average Period (number of bars to calculate the moving average) for the first moving average.

MAPer2 – Moving Average Period for the second moving average. The first moving average must cross from above to below (down) over this moving average to consider it a cross down and vice versa.

LookBack & MultiXMaxX – I added these integers experimentally. What this does is reject any crossover if there has been more than MultiXMaxX (Multiple Crossovers Maximum Cross) over the last LookBack number of bars. For example, if LookBack = 10 and MultiXMax = 2, A crossover on the current bar will be ignored if there have been more than 2 crosses over the last 10 bars. The idea was to trap situations where the market is continually crossing back and forth, as in a consolidation. In practice, we missed a lot of breakouts by doing this. Feel free to experiment with it. Â You can set these to 0 if you’re not worried about prior crosses.

This version of the MAXover() function assumes the Time Frame is the current chart time frame. Anyone want to comment on what changes would need to be made to allow us to pass a time frame to the function?

And that’s all the details you have to know about this function to use it. The nice thing about using a black box like this is, the details of the function are no longer important. The function is tested and works and you don’t have to worry about writing or debugging it ever again. Well, at least until the next time MetaTrader decides to change something. All you have to know is what to send to it and what you get back from it.

The last detail is you must use the #include pre-processor command at the beginning of your EA to include the code:

`#include <MAXover.mqh>`

That will make the compiler pre-processor look up the code in the MAXover.mqh file in the experts/include folder and add it to the EA code before it is compiled. The only downside to using the #include is the included files will not have access to any of your EA’s global variables.

Tim

The following two tabs change content below.