call OnInit() from the chart

To add comments, please log in or register
Charles Stangor
215
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.  I was thinking Refresh would do it, but no. 

 

Thanks.

Keith Watford
Moderator
18344
Keith Watford  
You could put a button on the chart and then when it is pressed have your code do whatever you want.
Charles Stangor
215
Charles Stangor  
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.

honest_knave
Moderator
2308
honest_knave  
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.

William Roeder
19826
William Roeder  
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.
Charles Stangor
215
Charles Stangor  
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!

Fernando Carreiro
4945
Fernando Carreiro  
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!‌

Charles Stangor
215
Charles Stangor  
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!

Fernando Carreiro
4945
Fernando Carreiro  
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!
Charles Stangor
215
Charles Stangor  
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!

To add comments, please log in or register