call OnInit() from the chart

 

So I have an indicator and I print some stuff to the chart in OnInit().  It doesn't need to occur regularly.  Is there a way to call OnInit() from the chart?  To reload the indicator I guess.  I was thinking Refresh would do it, but no. 

 

Thanks.

 
You could put a button on the chart and then when it is pressed have your code do whatever you want.
 
Keith Watford:
You could put a button on the chart and then when it is pressed have your code do whatever you want.

OK, yes that would work.  Or just exit and reload MT5.  

But what does the "refresh" button do?  

And shouldn't there be a way to reload the indicator?  

Thanks.

 
Charles Stangor:

OK, yes that would work.  Or just exit and reload MT5.  

But what does the "refresh" button do?  

And shouldn't there be a way to reload the indicator?  

Thanks.

Better to either have a keystroke or button do it like Keith says, or run it on a fixed timer.

Calling OnInit() at random times can have some unintended consequences elsewhere, depending on how your code is written.

 
Charles Stangor: So I have an indicator and I print some stuff to the chart in OnInit().  It doesn't need to occur regularly.  Is there a way to call OnInit() from the chart?  To reload the indicator I guess. 
Fix your broken indicator. Print in OnCalculate()  Not in OnInit(). No reload, no button, done.
 
whroeder1:
Fix your broken indicator. Print in OnCalculate()  Not in OnInit(). No reload, no button, done.

OK, despite all the helpful comments I'm still unsure how to reload an indicator so that OnInit() fires again.  

I need to read a pretty big file from disk, but I only need to read it occasionally.

I‌t's not practical to read it on every call of OnCalculate().

I‌f I change the value of a parameter in an indicator in MT5, the indicator is deinitialized and then reinitialized.

I‌ want to create that process-- I can figure out how to create a button but what should the button call to create the reinitialization?

T‌hanks!

 
Charles Stangor: OK, despite all the helpful comments I'm still unsure how to reload an indicator so that OnInit() fires again.  

I need to read a pretty big file from disk, but I only need to read it occasionally.

I‌t's not practical to read it on every call of OnCalculate().

I‌f I change the value of a parameter in an indicator in MT5, the indicator is deinitialized and then reinitialized.

I‌ want to create that process-- I can figure out how to create a button but what should the button call to create the reinitialization?

As @whroeder1 stated, it is an incorrect practice to call OnInit() or reload the Indicator, when the correct way is to do it in the OnCalculate(). No one said, that is has to occur on every call to OnCalculate(), only that the processing be done there.

So, in the OnCalculate() have a decision be made based on what ever conditions you expect - if NO then continue as normal, but if YES, then reread you external data file and then print whatever you need, and rerun the calculations on all the buffers if required.

Simple as that. No mess, no fuss!‌

 
Fernando Carreiro:

As @whroeder1 stated, it is an incorrect practice to call OnInit() or reload the Indicator, when the correct way is to do it in the OnCalculate(). No one said, that is has to occur on every call to OnCalculate(), only that the processing be done there.

So, in the OnCalculate() have a decision be made based on what ever conditions you expect - if NO then continue as normal, but if YES, then reread you external data file and then print whatever you need, and rerun the calculations on all the buffers if required.

Simple as that. No mess, no fuss!‌


OK, right, so use a button or script to write a global variable and use that to trigger within OnCalculate().  Guess that's simple as pie.

S‌till amazed there's no way to just refresh the indicator.


T‌hanks everyone!

 
Charles Stangor: OK, right, so use a button or script to write a global variable and use that to trigger within OnCalculate(). Guess that's simple as pie.

S‌till amazed there's no way to just refresh the indicator.

And the reason there is no "refresh", is because you are supposed to code the indicator properly and not to code a "hack-job" that needs to be refreshed! Please note that it is not in anyway an attack on you, but just a comedic observation to illustrate the point!
 
Fernando Carreiro:
And the reason there is no "refresh", is because you are supposed to code the indicator properly and not to code a "hack-job" that needs to be refreshed! Please note that it is not in anyway an attack on you, but just a comedic observation to illustrate the point!


I'm definitely a trader not a coder (obviously I guess).  Spent years with NinjaTrader but finding mql5 a hundred times more useful.  Despite the excellent documentation however it does come with a learning curve.

Thanks for the tips Fernando!