Older RSI Divergence - Extremely CPU Intensive

 
I found an older RSI divergence indicator named 00-RsiDiv_v104.mq4 (revised 4/26/09) that appears to work fine when you place it on a chart...

However when I try to use this indicator with iCustom in an EA...it's extremely CPU intensive (60%+) and it is taking days to run the EA in the Strategy Tester (H4 time frame for only a 6mo test)...

In looking at the indicator code...it seems to be calling itself with an iCustom directly in the indicator itself.

I don't know if I've ever seen an indicator calling itself?

Here is one line for an example for the buffer 0 (00-RsiDiv_v104 = sIndSelf  --> calling iCustom for Buffer 0 starts on line 412).

BufferMain[i] = iCustom(NULL, timeFrame, sIndSelf, 0, bCheckDiv, bShowAllDiv, bCheckCross, bCheckLineMan,
                      false, false, false, bDrawOnChart, div_mode, nCheckBars, rsi_period, t3_period,
                      level_lo, level_hi, div_oscErr, div_priceErr, window, nMaxBars, 0, x);

This iCustom calls itself for all 8 buffers during each cycle...and it's very very slow...  The indicator is attached.

Is there anyway to speed this indicator up...?

Or see if there were any recent MT4 improvements (after 2009) that can be added to make this indicator much faster...?

Thanks in advance for any help.

Robert

Files:
 
Is there anyway to speed this indicator up...? Yeah fix/optimize the code. You either have to learn to code in-order to fix this stuff or you'll have to pay someone.
 
ubzen:
Is there anyway to speed this indicator up...? Yeah fix/optimize the code. You either have to learn to code in-order to fix this stuff or you'll have to pay someone.

Hi Ubzen,

I didn't ask anyone to do it for me. I'm looking for suggestions on how to do that...

I'm willing to try...just need a few clues where to start.

Thanks the same...

Robert

 
    int limit = Bars;
    if (nMaxBars > 0) {
    limit = MathMin(limit, nMaxBars);
    }
    
    if (timeFrame != Period()) {
    // MTF
    limit = MathMax(limit, timeFrame / Period());
    for (int i = limit - 1; i >= 0; i--) {
You are recalculating ALL bars EVERY tick. Just do it right:
    int limit = Bars - IndicatorCounted();
    if (nMaxBars > 0) {
    limit = MathMin(limit, nMaxBars);
    }
    
    if (timeFrame != Period()) {
    // MTF
    //limit = MathMax(limit, timeFrame / Period());
    
    for (int i = limit - 1; i >= 0; i--) {
 
WHRoeder:
You are recalculating ALL bars EVERY tick. Just do it right:
Hi WHRoeder...

Thanks for your kind help.

I made the suggested changes and it seems to work fine. 

Afterwards I searched to learn more about an indicator calling itself with an iCustom in the indicator...but I couldn't find anything in my searches.

However I did find one of your indicators (IDWma.mq4) that had this same code in it...so I could directly compare them to see how they work.

So double thanks for all your help...

Still lots to learn...and I appreciate the great tips...

Take care,

Robert
Reason: