Custom Indicators and Back-Testing Speed in MT4: How to Make Your Custom Indis Fast

The MT4/5 trading platforms come with a lot of “classic” indicators which allow us to build a wide variety of trading systems. However the scope of these indicators is limited and we often need to implement new code to satisfy our need for some specific type of calculation. For example if you want to use the Laguerre RSI instead of the “classic” implementation you will need to code this yourself or download custom code made by someone else. After you do this you will then load the code into the trading platform and even use it for back-testing within your systems. When you do this you’ll face the nasty surprise of very slow performance caused by your indicator implementation. Why are most custom indicators so slow when used within experts? Why don’t they have the coding speed of regular internal MT4 indicators? How could we make them faster?

From the beginning of my journey with MT4 I have been frustrated by the fact that custom indicators are super slow in back-testing. This is just a minor hassle when running a few backtests of a strategy but when using a genetic framework which needs to perform thousands of runs it becomes an absolute and hard impossibility to have to deal with this type of trading limitation. Since this was simply unacceptable I decided to look into the main reason why these indicators were so sluggish, something which led me to understand what was wrong and allowed me to fix it, achieving fast simulations in MT4.

Certainly many of you – especially those of you who program – might be thinking that dealing with indicators in MT4 might not be the best idea. Why do this if you can simply build a DLL and execute code 100x times faster ? Well the main issue here is that the MT4 online code base is absolutely huge and therefore if we can execute custom indicators in simulations fast it opens up the way for the usage of a lot of custom implementations which would simply be hard to port to a single DLL. Although using DLLs is something we want to do in the future (reason why we have built and are evaluating a TA-lib based solution) in the meantime it is very interesting to solve the custom indicator problem to give us “the chance” to use the whole MT4 knowledge-base.

The first thing I did to evaluate this was to review the time it was taking the custom indicators to go through their loops. Generally custom indicators are called from the last to the first bar of a chart, meaning that an indicator loaded on a daily chart would have to loop through more than 3500+ bars over a ten year period. What I noticed is that the custom indicators were no only doing this slowly but every time they used an internal indicator it implied an internal loop for the calculation of that other expression. So in terms of efficiency this was perhaps the worst thing ever, the indicators are doing 3500+ loops times X loops for their internal calculations so in the end they are cycling through millions of unnecessary calculations on each simulation tick.

The solution to the custom indicator problem is actually tremendously simple. In order to make the indicator calculate things faster you need to change the code so that the indicator only calculates whichever indicator values YOU need instead of the whole set of value for the chart. For example if I am using an indicator’s last two values to trade then it becomes idiotic to calculate values for more than 3500+ bars when all the information I need comes just from 2. By limiting the indicator calculation to whichever is needed to achieve a gargantuan increase in speed which makes the calculation of the indicators sometimes even FASTER than the MT4 internal implementations.

Certainly this requires some coding modification but it can be easily spotted and done by someone with just a little bit of coding experience. Generally indicators will calculate their buffers in terms of a “while” or “for” statement (the “while” is usually more popular), doing a certain loop until a certain value of a counter is reached (usually zero). What we do then is assign a value to that counter before the loop starts such as to make the loop start on the bar we are interested in so if the calculation says “while(i > 0)” we would introduce a line before saying “i = 3 ;” which will make the indicator calculate only the last 4 bars.

This this “trick” in mind I was able to speed up simulations using custom indicators to the point where they become usable entities in our genetic framework (Coatl) allowing us to greatly increase the type of systems and available logic for strategy development. Certainly it will be very interesting to include things as the Laguerre RSI, Absolute Power Indicator (this is a personal development!) and the RSI Stochastic. If you would like to learn more about my work in automated trading and how you too can learn to develop and analyze your own systems please consider joining Asirikuy.com, a website filled with educational videos, trading systems, development and a sound, honest and transparent approach towards automated trading in general . I hope you enjoyed this article ! :o)

You can skip to the end and leave a response. Pinging is currently not allowed.

4 Responses to “Custom Indicators and Back-Testing Speed in MT4: How to Make Your Custom Indis Fast”

  1. Sampson says:

    I appreciate your putting this together, it is exactly what i have being suffering.
    I got this wonderful Wave indicator, it is 100% when back testing with MT4 visual on, but when it gets to the live market-it is like 15 or 16 bars behind.
    What do you think?

    • admin says:

      Hi Sampson,

      Thank you for your post :o) The only thing I can think about is that the indicator is “looking into the future” in backtesting and, since this information is not available when trading live, it simply doesn’t work in live trading (it just shows you data up to the point were it has data to calculate). I would advice you to never use indicators whose code you do not fully understand, always make sure indicators are coded using only data from past bars and always ensure they are robust against repainting and other such issues. Thanks again for posting!

      Best Regards,

      Daniel

      • Sampson says:

        Hello Daniel,
        Thank you for the quick response.
        I do not have a knowledge of MQL4 coding but you can look at the indicator yourself from:
        http://codebase.mql4.com/source/3037
        Somehow i believe something somewhere is missing.
        Thanks a bunch!

        • admin says:

          Hi Sampson,

          Thank you for your reply :o) Sadly I don’t have the time to look into this code myself but you could defintiely post a request there (at mql4.com) or at another programming forum where you might get an answer. If you’re an Asirikuy member posting on the community forum might also yield some help from other members. Thanks again for visiting the website,

          Best Regards,

          Daniel

Leave a Reply

Subscribe to RSS Feed Follow me on Twitter!
Show Buttons
Hide Buttons