MT5: Limit number of bars for calculation of an indicator, a more elegant way ?

Paul Edunyu Carissimo
205
Paul Edunyu Carissimo  

Hello, is there a more elegant way to limit the number of bars for calculation , for both counting up and counting down  ? 

i.e

//---- main indicator calculation loop
   for(bar=limit; bar>=0 && !IsStopped(); bar--)
     {
    if (bar >= MathMin(3-bar, rates_total-1-50)) continue; 


Please suggest, thanks in advance.

Amir Yacoby
1488
Amir Yacoby  
Paul Carissimo:

Hello, is there a more elegant way to limit the number of bars for calculation , for both counting up and counting down  ? 

i.e


Please suggest, thanks in advance.

Not quite sure what you are asking for, but only looking technically to your loop, it can be improved.
//---- main indicator calculation loop
   for(bar=MathMin(3-bar,rates_total-1-50); bar>=0 && !IsStopped(); bar--)
     {
    // no need for if, it starts with the min value

Paul Edunyu Carissimo
205
Paul Edunyu Carissimo  
Amir Yacoby:
Not quite sure what you are asking for, but only looking technically to your loop, it can be improved.

Thanks for the code snippet, what I'm basiclly trying to achieve is faster execution of an indicator by calculating only the latest 3 bars instead of looping through, say 50,000 bars returned by rates_total,I dont know if that makes sense.  I'm calling  13 indicators using Icustom  using 26 CopyBuffers, cpu usage is through the roof and so is memory usage.

What MT5 tells me:


What I'm trying to do:


Amir Yacoby
1488
Amir Yacoby  
Paul Carissimo:

Thanks for the code snippet, what I'm basiclly trying to achieve is faster execution of an indicator by calculating only the latest 3 bars instead of looping through, say 50,000 bars returned by rates_total,I dont know if that makes sense.  I'm calling  13 indicator using Icustom  using 26 CopyBuffers, cpu usage is through the roof and so is memory usage.

What MT5 tells me:


What I'm trying to do:


Then you need something much simpler like
For (int bar=3;bar>0;bar--)

And make the ArraySetAsSeries true for the price arrays in OnCalculate
Paul Edunyu Carissimo
205
Paul Edunyu Carissimo  
Amir Yacoby:
Then you need something much simpler like
For (int bar=3;bar>0;bar--)

And make the ArraySetAsSeries true for the price arrays in OnCalculate

Thanks alot, I'll try it out and give feedback. 

Paul Edunyu Carissimo
205
Paul Edunyu Carissimo  
Amir Yacoby:
Then you need something much simpler like
For (int bar=3;bar>0;bar--)

And make the ArraySetAsSeries true for the price arrays in OnCalculate

Hi, Amir, thanks, this works beautifully for counting up: 

bar--

However, for counting down: 

bar++


No joy, I tried reversing the values; 

   for ( bar=0;bar<3;bar++)

Thanks.

Ali Zarebidaki
9
Ali Zarebidaki  
Tnx . It worked for me after to many search and try and failure.