Discussion about OnCalculate returning 0.

 
No problem, note that I changed the deinitialization function as well (it's "OnDeInit" in MQL5)
 

sorry but some other problems were noticed in this

return 0 in OnCalculate will cause the indicator to deallocate itself prematurely, and the initializer should return INIT_SUCCEEDED success number

Files:
 
Conor Mcnamara #: return 0 in OnCalculate will cause the indicator to deallocate itself prematurely, and the initializer should return INIT_SUCCEEDED success number

You mean in OnInit, not OnCalculate.

 
William Roeder #:

You mean in OnInit, not OnCalculate.

nope in OnCalculate, return 0 can cause the indicator to stop calculating values and deinitialize itself. Try it. 
It can cause a lot of fuss, and it shouldn't be used.

OnInit can return nothing (if the other OnInit with void is used), but one should never use return 0 in OnCalculate, it will crash the indicator and there's never a reason to anyway. Return any number at the end but not 0 or -1
 
Conor Mcnamara #:
n OnCalculate, return 0 can cause the indicator to stop calculating values and deinitialize itself

A very dubious statement.

Conor Mcnamara #:
but one should never use return 0 in OnCalculate, it will crash the indicator and there's never a reason to anyway. Return any number at the end but not 0 or -1

Are you saying that the code example in the documentation is incorrect and will crash the indicator?

https://www.mql5.com/en/docs/event_handlers/oncalculate

int OnCalculate(const int rates_total,     // price[] array size 
                const int prev_calculated, // number of previously handled bars
                const int begin,           // where significant data start from 
                const double &price[])     // value array for handling
  {
//--- initial position for calculations
   int StartCalcPosition=(IntPeriod-1)+begin;
//---- if calculation data is insufficient
   if(rates_total<StartCalcPosition)
      return(0);
 
Conor Mcnamara #:
nope in OnCalculate, return 0 can cause the indicator to stop calculating values and deinitialize itself. Try it. 
It can cause a lot of fuss, and it shouldn't be used.

OnInit can return nothing (if the other OnInit with void is used), but one should never use return 0 in OnCalculate, it will crash the indicator and there's never a reason to anyway. Return any number at the end but not 0 or -1

I always thought that the return value of OnCalculate doesn't affect anything except the value of the prev_calculated variable the next time OnCalculate is called.

Could you show some code that can be used to make sure that the return value affects something other than prev_calculated?

 
Vladislav Boyko #:

I always thought that the return value of OnCalculate doesn't affect anything except the value of the prev_calculated variable the next time OnCalculate is called.

Could you show some code that can be used to make sure that the return value affects something other than prev_calculated?


Well I wouldn't say it unless I experienced the indicator deinitializing with return 0.
Why would I write all this otherwise? Take that file I uploaded in this thread, change the return at the end to return 0 and you'll see.
 
Conor Mcnamara #:
Take that file I uploaded in this thread, change the return at the end to return 0 and you'll see.

I did this, changing the return value does not affect anything. I can't reproduce what you write.

 
Conor Mcnamara #:

Well I wouldn't say it unless I experienced the indicator deinitializing with return 0.
Why would I write all this otherwise? Take that file I uploaded in this thread, change the return at the end to return 0 and you'll see.

That indicator simply doesn't work correctly, regardless of the return value. The variables are not initialized with anything and contain garbage values. If your indicator deinitialized itself, it's possible that a division by 0 occurred somewhere because the variables were not initialized.

My demo account history is empty. The indicator always displays random garbage.

If you can reproduce the situation you described, please do so and attach the log here. Most likely, we will find a division by 0 or some other runtime error in the log.

 
Vladislav Boyko #:

I did this, changing the return value does not affect anything. I can't reproduce what you write.

It will work sometimes, not other times. This is what I experienced


And can you explain why the indicator always seems to work correctly if return rates_total is used instead of return 0?  😏