When is init() called

 

I'm trying to write a custom indicator

usually i press F5 and it compiles and I see the changes immediately on the live chart

except for any changes in init()

I'm using arrows which are sized in init() like so

int init()

int size = 4;
SetIndexBuffer(2, signalUP); SetIndexStyle(2,DRAW_ARROW,EMPTY,size); SetIndexArrow(2,241);
SetIndexBuffer(3, signalDN); SetIndexStyle(3,DRAW_ARROW,EMPTY,size); SetIndexArrow(3,242);

If I change the code and press F5 and get an error the arrows revert to the smallest size = 1

not the size = 4 which I want

If I correct the error and press F5 again with no errors the arrow remains the wrong size - so init isn't getting run.

Now I'm changing the chart period twice to get it looking the way i want

how else can I get init to run while creating my program?

 
sabtrader:

I'm trying to write a custom indicator

usually i press F5 and it compiles and I see the changes immediately on the live chart

except for any changes in init()

I'm using arrows which are sized in init() like so

int init()

int size = 4;
SetIndexBuffer(2, signalUP); SetIndexStyle(2,DRAW_ARROW,EMPTY,size); SetIndexArrow(2,241);
SetIndexBuffer(3, signalDN); SetIndexStyle(3,DRAW_ARROW,EMPTY,size); SetIndexArrow(3,242);

If I change the code and press F5 and get an error the arrows revert to the smallest size = 1

not the size = 4 which I want

If I correct the error and press F5 again with no errors the arrow remains the wrong size - so init isn't getting run.

Now I'm changing the chart period twice to get it looking the way i want

how else can I get init to run while creating my program?


init() only gets run when the indicator / EA is initialized (ie. loaded onto the chart). If you make changes and re-compile, init() doesn't get run. You'll have to delete the indicator and then re-load it to see changes made to the init routine
 
bryans1620:
[...] If you make changes and re-compile, init() doesn't get run. [...]
Not so. See here -> https://docs.mql4.com/runtime/start. sabtrader, Add a Print() statement in init() to verify that it does get called.
 
gordon:
Not so. See here -> https://docs.mql4.com/runtime/start. sabtrader, Add a Print() statement in init() to verify that it does get called.
OK, I was wrong, thanks for pointing that out. Sorry for giving the wrong information.
 
bryans1620:

init() only gets run when the indicator / EA is initialized (ie. loaded onto the chart). If you make changes and re-compile, init() doesn't get run. You'll have to delete the indicator and then re-load it to see changes made to the init routine

gordon:
Not so. See here -> https://docs.mql4.com/runtime/start. sabtrader, Add a Print() statement in init() to verify that it does get called.

Thanks for that useful post gordon - much better than the F1 help: init() is a function to be called during the module initialization.

Your link says:
Immediately after the program has been attached to the chart, it starts working with the init() function. The init() function of an expert advisor or a custom indicator attached to the chart will run just after client terminal has started and history data (this concerns only experts, but not indicators) have been loaded additionally, after the symbol and/or chart period have been changed, after the program has been recompiled in MetaEditor, after inputs have been changed from the window of expert or custom indicator settings. An expert will also be initialized after the account has been changed.

but my experience shows that it doesn't run after being recompiled in MetaEditor???

the quickest way i have found to force an init is to drag a new symbol onto the chart from market watch ctrl-m

or right click the indic > properties and OK. (it thinks the inputs have changed)

p.s. my original program had this line at the start as well which i had overlooked before with size = 1

#property indicator_width3 4 // up arrow size
#property indicator_width4 4 // down arrow size

 

Put a Print() or Comment() inside init() and see when it is executed.

 
phy:

Put a Print() or Comment() inside init() and see when it is executed.


While I'm working on or modifying my code I like to put :

Alert("New Test");

in init and

Alert("End of Test");

in deinit so each time i recompile i can be sure the init was run and everything is reset

 

hi

My problem is:

Suppose during an EA execution ,connection fails. When it reconnects, would this EA Initialize or not?

 

EAs that return from OnTick, will never see a connection fail/reconnect. They just see the next tick. The EA was initialized when it was first loaded.

Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
 

In my OnInit I have some variables that are needed in Ontick. Fore example "the first Order Type". If it is BUY, this variable will be 0, and if is SELL this varible will be 1.

If I don not use OnInit for that purpose, what shall I do?

 
  1. What part of "don't use them in OnInit, wait until the first tick." was unclear? (#8.7)
  2. You have to select in OnTick anyway, as the order could close between ticks.
Reason: