Speed up external indicator(s)

 

Does any of you traders/developers know a way of how to speed up the use/backtesting of an external indicator?


I now use two different types of Moving Average Indicators next to the ones build into MT5, but when backtesting these it takes 5-10 times as much time to process crossovers with these indicators versus the ones build into MT5. 

 
Danny Verpoorten: Does any of you traders/developers know a way of how to speed up the use/backtesting of an external indicator? I now use two different types of Moving Average Indicators next to the ones build into MT5, but when backtesting these it takes 5-10 times as much time to process crossovers with these indicators versus the ones build into MT5. 

That means the code for the indicators is not very efficient, and maybe even the EA code itself can be improved. It takes some skill to make truely efficient code, and its not as easy as providing a a step-by-step recipe of things to do do by a newbie coder.

Only by analysing the code and its logic (for both for the Indicator and the EA), can one then start to break it down and improve it.

 
Fernando Carreiro #:

That means the code for the indicators is not very efficient, and maybe even the EA code itself can be improved. It takes some skill to make truely efficient code, and its not as easy as providing a a step-by-step recipe of things to do do by a newbie coder.

Only by analysing the code and its logic (for both for the Indicator and the EA), can one then start to break it down and improve it.

I figured that would be the main issue. So it's just a matter of hiring someone who can make these indicators for me with much less code?


Right now, the Hull Moving Average for example that I use has 200 lines of code in it, whereas the formula is just this:

"HMA[i] = MA( (2*MA(input, period/2) – MA(input, period)), SQRT(period)) where MA is a moving average and SQRT is square root"
 
Danny Verpoorten #: I figured that would be the main issue. So it's just a matter of hiring someone who can make these indicators for me with much less code?

Right now, the Hull Moving Average for example that I use has 200 lines of code in it, whereas the formula is just this:

"HMA[i] = MA( (2*MA(input, period/2) – MA(input, period)), SQRT(period)) where MA is a moving average and SQRT is square root"

It's not about having less code. Less code does not always equal more efficiency (in terms of speed). Sometimes it's the opposite. Each case is different.

Sometimes its about breaking down the math and saving intermediate results and not recalculating them on every iteration. A Hull average is a very good example of where there are several steps that can be broken down to prevent recalculations on every iteration.

Here is an example in pseudocode — The "period/2" or "SQRT(period)" do not need to be calculated every time. Given that the period is an input, they can easily be calculated once when initialised and then reused.

int halfperiod;
double sqrtperiod;

int OnInit()
{
   // some code

   halfperiod = period / 2;
   sqrtperiod = sqrt( period );

   // some code
};

int OnCalculate( ... )
{
   // some code

   HMA[i] = MA( ( 2 * MA(input, halfperiod ) – MA( input, period ) ), sqrtperiod ) 

   // some code
};
EDIT: Please note that there are many more steps to take to improve the efficiency of the Hull average. The above example was only for the most basic of improvements in terms of efficiency.
 
  1. EAs : Don't do per tick what you can do per bar, or on open.
    If you are waiting for a level, don't reevaluate, wait until price reaches it (or a new bar starts, and you recalculate.)
    If you are waiting for an order to close, only look when OrdersTotal (or MT5 equivalent) has changed.
              How to get backtesting faster ? - MT4 - MQL4 programming forum (2017)

  2. Indicators: Code it properly so it only recomputes bar zero (after the initial run.)
              How to do your lookbacks correctly. (2016)
    Or, reduce Tools → Options (control+O) → Charts → Max bars in chart to something reasonable (like 1K.)

  3. Danny Verpoorten: I now use two different types of Moving Average Indicators, but when backtesting these it takes 5-10 times as much time 
    If you want help on those, you will have to post them (or a link).
  4. If you are not looking at bar zero values (cross over), you can modify them to not compute bar zero.
  5. Be aware that using resources is 40x times slower than using CIs directly.
              A custom indicator as a resource - MQL4 programming forum (2019)

 
William Roeder #:
  1. EAs : Don't do per tick what you can do per bar, or on open.
    If you are waiting for a level, don't reevaluate, wait until price reaches it (or a new bar starts, and you recalculate.)
    If you are waiting for an order to close, only look when OrdersTotal (or MT5 equivalent) has changed.
              How to get backtesting faster ? - MT4 - MQL4 programming forum (2017)

  2. Indicators: Code it properly so it only recomputes bar zero (after the initial run.)
              How to do your lookbacks correctly. (2016)
    Or, reduce Tools → Options (control+O) → Charts → Max bars in chart to something reasonable (like 1K.)

  3. If you want help on those, you will have to post them (or a link).
  4. If you are not looking at bar zero values (cross over), you can modify them to not compute bar zero.
  5. Be aware that using resources is 40x times slower than using CIs directly.
              A custom indicator as a resource - MQL4 programming forum (2019)

I'm not a coder (yet) unfortunately so my understanding of what you're saying is limited but I get the main idea of it. I've attached the two indicators. Can you easily see what's wrong with them? 

Files:
 
Danny Verpoorten #:

I'm not a coder (yet) unfortunately so my understanding of what you're saying is limited but I get the main idea of it. I've attached the two indicators. Can you easily see what's wrong with them? 

For some reason the volume weighted moving average cannot be posted here

 
Danny Verpoorten #: I've attached the two indicators. Can you easily see what's wrong with them? 

There is nothing inherently wrong with it. It was just not coded with speed in mind.

 
https://www.mql5.com/en/articles/270

It appeared in search results for "faster calculation" in articles.
Reason: