Custom Indicators and BackTesting Speed in MT4 How to Make Your Custom Indis Fast
Post on: 16 Март, 2015 No Comment
Trading in the FX market using mechanical trading strategies
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 youll 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 dont 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 indicators 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)