strangeness using prev_calculated vs prev_calculated-1

 

Here there are the differences between Prev_calculated and Prev_calculated-1 in bars loop

using prev_calculated-1

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
  ArraySetAsSeries(time,false);
  ArraySetAsSeries(open,false); 
  ArraySetAsSeries(low,false); 
  ArraySetAsSeries(close,false); 
  ArraySetAsSeries(high,false); 
  ArraySetAsSeries(Segnale,false);
  .........
 for (int i=(int)MathMax(prev_calculated-1,1000); i<=rates_total-1; i++) {
 Print("NEW BAR, total bars=",Bars(NULL,0));
 Print("i=",i);
 ......
 }
return(ratestotal)


and the output:

(EURUSD,M1) NEW BAR, total bars=250129
(EURUSD,M1) i=250127
(EURUSD,M1) i=250128
(EURUSD,M1) i=250128
(EURUSD,M1) i=250128
(EURUSD,M1) i=250128
(EURUSD,M1) i=250128
(EURUSD,M1) i=250128
(EURUSD,M1) i=250128
(EURUSD,M1) i=250128
(EURUSD,M1) i=250128
(EURUSD,M1) i=250128
(EURUSD,M1) i=250128
(EURUSD,M1) i=250128
(EURUSD,M1) i=250128
(EURUSD,M1) i=250128
(EURUSD,M1) i=250128
(EURUSD,M1) i=250128
......
(EURUSD,M1) i=250128
(EURUSD,M1) NEW BAR, total bars=250130
(EURUSD,M1) i=250128
(EURUSD,M1) i=250129
(EURUSD,M1) i=250129
(EURUSD,M1) i=250129
(EURUSD,M1) i=250129
(EURUSD,M1) i=250129
(EURUSD,M1) i=250129
(EURUSD,M1) i=250129

It seems in this case it calculate value for last bar-1 and then the last bar for every new tick


An here using prev_calculated

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
  ArraySetAsSeries(time,false);
  ArraySetAsSeries(open,false); 
  ArraySetAsSeries(low,false); 
  ArraySetAsSeries(close,false); 
  ArraySetAsSeries(high,false); 
  ArraySetAsSeries(Segnale,false);
  .........
 for (int i=(int)MathMax(prev_calculated,1000); i<=rates_total-1; i++) {
 Print("NEW BAR, total bars=",Bars(NULL,0));
 Print("i=",i);
 ......
 }
return(ratestotal)


output:
(EURUSD,M1) NEW BAR, total bars=250126
(EURUSD,M1) i=250125
(EURUSD,M1) NEW BAR, total bars=250127
(EURUSD,M1) i=250126
(EURUSD,M1) NEW BAR, total bars=250128
(EURUSD,M1) i=250127

It seems in this second case it calculate value for the last bar and Doesn't refresh for every new tick

Why in this second case It doesn't update index for every new tick like in the first case?

 
 {
  ArraySetAsSeries(time,false);
  ArraySetAsSeries(open,false); 
  ArraySetAsSeries(low,false); 
  ArraySetAsSeries(close,false); 
  ArraySetAsSeries(high,false); 
  ArraySetAsSeries(Segnale,false);
  .........
 printf("the for loop won't run when prev_calculated %d > rates_total-1 %d",prev_calculated,rates_total-1); 
 for (int i=(int)MathMax(prev_calculated,1000); i<=rates_total-1; i++) {
 Print("NEW BAR, total bars=",Bars(NULL,0));
 Print("i=",i);
 ......
 }
 
Giovanni:

Here there are the differences between Prev_calculated and Prev_calculated-1 in bars loop

...

Why in this second case It doesn't update index for every new tick like in the first case?

Per your code... your  FOR loop is evaluating always everything starting at the index 1000, or until a new bar is created, when prev_calculated gets into action...

I think your problem is with your evaluation of the previous bars needed to be calculated. Do you really need to start a the index 1000 on every tick?


;)

 
Ernst Van Der Merwe:

Thank you very much Ernst Van Der Merwe, now I undestand.



case 1)
for (int i=(int)MathMax(prev_calculated,1000); i<=rates_total-1; i++) {
16:36:58.408 prevcalculated (EURUSD,M1)  i=373510 prev_calculated=373510
16:38:02.209 prevcalculated (EURUSD,M1)  NEW BAR, total bars=373512
16:38:02.209 prevcalculated (EURUSD,M1)  i=373511 prev_calculated=373511
16:39:00.611 prevcalculated (EURUSD,M1)  NEW BAR, total bars=373513
16:39:00.611 prevcalculated (EURUSD,M1)  i=373512 prev_calculated=373512
the for loop execute only in the first tick for the new bar, othervise when ratest_total incrments itself
............
 


case 2)

for (int i=(int)MathMax(prev_calculated-1,1000); i<=rates_total-1; i++) {
16:39:59.465 prevcalculated_meno1 (EURUSD,M1) i=373512 prev_calculated=373513
16:39:59.465 prevcalculated_meno1 (EURUSD,M1) i=373513 prev_calculated=373513
16:39:59.962 prevcalculated_meno1 (EURUSD,M1) i=373513 prev_calculated=373514
16:40:01.627 prevcalculated_meno1 (EURUSD,M1) NEW BAR, total bars=373514
16:40:01.627 prevcalculated_meno1 (EURUSD,M1) i=373513 prev_calculated=373514
16:40:02.123 prevcalculated_meno1 (EURUSD,M1) i=373513 prev_calculated=373514
16:40:02.628 prevcalculated_meno1 (EURUSD,M1) i=373513 prev_calculated=373514
16:40:03.120 prevcalculated_meno1 (EURUSD,M1) i=373513 prev_calculated=373514
16:40:03.605 prevcalculated_meno1 (EURUSD,M1) i=373513 prev_calculated=373514
............
16:40:55.819 prevcalculated_meno1 (EURUSD,M1) i=373513 prev_calculated=373514
16:41:04.447 prevcalculated_meno1 (EURUSD,M1) NEW BAR, total bars=373515
16:41:04.447 prevcalculated_meno1 (EURUSD,M1) i=373513 prev_calculated=373514   
16:41:04.447 prevcalculated_meno1 (EURUSD,M1) i=373514 prev_calculated=373514
16:41:04.967 prevcalculated_meno1 (EURUSD,M1) i=373514 prev_calculated=373515
16:41:05.452 prevcalculated_meno1 (EURUSD,M1) i=373514 prev_calculated=373515
16:41:05.946 prevcalculated_meno1 (EURUSD,M1) i=373514 prev_calculated=373515
It executes the for loop in each tick of the new bar
Furthermore it explains why when a new bar arrives the for loop is executed two times
(because prev_calculated in this case is far two from rates_total)