Issue with indicator, plotting when loaded but not when a new bar is generated.

 

Hello!

I have an issue with my indicator and I'm almost sure it's due to the way I am performing the look back. My problem is basically that when the indicator is loaded, it plots on the chart without issues, but when new bars forms, it doesn't update. Here is the code I am using to perform the loop:

   int counted_bars = IndicatorCounted(), limit;
     
   if(counted_bars > 0)
      counted_bars--;
   
   limit = Bars - counted_bars;
   
   if (limit > max_bars) // Where max bar is an input
      limit = max_bars;
   
   int pos = limit - period; // EDIT: Period is the actual indicator period, so if I need 20 bars to calculate I'll check from limit - 20
   
   for (int i = pos; i > 0; i--)
   { 
	...
   } 

The above image is from the strategy tester, when it's loaded it performs it's required calculations, but as new bars are formed, it doesn't update.


Any advice is appreciated!

 
int pos = limit - period;

What is period?

 
Keith Watford:

What is period?

Sorry, forgot to state that, period is the indicator period, I did that with this logic, if I have 100 bars in history and I need 20 bars at minimum to calculate the indicator value, I need to plot from bar 80 (100 - 20, or limit - period)

 
Fernando Jose Velasco Borea:

Sorry, forgot to state that, period is the indicator period, I did that with this logic, if I have 100 bars in history and I need 20 bars at minimum to calculate the indicator value, I need to plot from bar 80 (100 - 20, or limit - period)

That makes no sense at all.

Apart from that, what do you think happens when limit == 1 ?

 
Keith Watford:

That makes no sense at all.

Apart from that, what do you think happens when limit == 1 ?

Oh shut... Well, that explains a lot, what would be the proper way of having this look back set properly? If I set it to loop to the very first (oldest) bar I will have no data for the indicator to calculate so I still need to start the loop from the 21st bar to bar 1 (I don't want to have calculations on the open bar as the system I'm looking to build will work on bar close) 
 
  1. Fernando Jose Velasco Borea:

    Sorry, forgot to state that, period is the indicator period, I did that with this logic, if I have 100 bars in history and I need 20 bars at minimum to calculate the indicator value, I need to plot from bar 80 (100 - 20, or limit - period)

    int pos = limit - period;
    

    If you are on the H1 you are computing limit - 60. If you are on the D1 you compute limit-1440. Just compute your look back in bars:

       limit = Bars - MathMax(20, counted_bars);
              How to do your lookbacks correctly #9 - #14 & #19

  2. You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 2016.05.11
 
William Roeder:
  1. If you are on the H1 you are computing limit - 60. If you are on the D1 you compute limit-1440. Just compute your look back in bars:

              How to do your lookbacks correctly #9 - #14 & #19

  2. You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 2016.05.11

Thanks a lot! It worked without the -1 from the post you linked on your reply. I'm not completely sure why that is, but when I add it, I face the same issue, I suspect that is because I'm doing i > 0 instead of i >= 1 but I'm not sure:

for (int i = Bars - MathMax(period, prev_calculated); i > 0; i--) // Where Period is an input
   {...}

Also, if I don't want to calculate for all available bars, but for the last X bars, would this call be correct?

iBar = Bars - max_bars - MathMax(period, prev_calculated); // Where max_bars is an input
 
period is not X. What part of "limit - 60" and "limit-1440" was unclear?
 
William Roeder:
period is not X. What part of "limit - 60" and "limit-1440" was unclear?

My bad, when I mean period I refer to the actual indicator period, not the time-frame. Just noticed that my approach for last X bars is not correct, it'd be as follows, still, when max_bars is smaller, for some reason I still seem to have the issue:

iBar = MathMin(Bars, max_bars) - MathMax(period, prev_calculated);

// EDIT: Just noticed the issue, this is the fixed one which seems to work properly:

iBar = MathMin(Bars - MathMax(period, prev_calculated), max_bars - period)

Obviously It'd require something like the code below on top.

if (Bars < period)
        return(-1);
Reason: