ReInit indicator when account changes

 

Can MT4 deinit and reinit an indicator when one changes account or server?


I have read around that there should an option for that, but I cannot find it.

 
There is no "option for that" because it's automatic. Uninitialization Reason Codes - MQL4 Documentation REASON_ACCOUNT
 
WHRoeder:
There is no "option for that" because it's automatic. Uninitialization Reason Codes - MQL4 Documentation REASON_ACCOUNT




This is what I thought, but I did many tests and it seems to me that MT4 (build 765) does not reinitialize the indicators.


This is a minimal indicator to test:


//+------------------------------------------------------------------+
//|                                                ChangeAccount.mq4 |
//+------------------------------------------------------------------+
#property strict
#property indicator_chart_window

int OnInit() {
        SetIndexLabel(0, NULL);
        
        Print("Initializing. Demo? ", IsDemo());

        return INIT_SUCCEEDED;
}

void OnDeinit(const int reason) {
        Print("OnDeinit, reason = ", reason, " (code REASON_ACCOUNT = ", REASON_ACCOUNT, ")");
}

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[]) {
        return 0;
}
OnInit is called only when I add the indicator or I recompile the indicator. If I double-click on another account to log in it, the indicator is not reinitialized. Why?
 
ale:


This is what I thought, but I did many tests and it seems to me that MT4 (build 765) does not reinitialize the indicators.


This is a minimal indicator to test:


OnInit is called only when I add the indicator or I recompile the indicator. If I double-click on another account to log in it, the indicator is not reinitialized. Why?

 

 Unlike EAs, indicators never reload with account or server change. It's been like this since I can remember. Neither there is an easy function to support reloading the indicator. You have to watch the account number yourself and recalculate the indicator if the account changes.

 
  1. Ovo:  Unlike EAs, indicators never reload with account or server change. It's been like this since I can remember. Neither there is an easy function to support reloading the indicator. You have to watch the account number yourself and recalculate the indicator if the account changes.

    Since indicators are based on chart's pair/TF, there is no need for them to recalculate anything. Try changing one of those and you will see a deinit/init cycle.

  2. ale: OnInit is called only when I add the indicator or I recompile the indicator. If I double-click on another account to log in it, the indicator is not reinitialized. Why?
    A deinit/init cycles is NEVER a reinitialization (recompile being the exception.) Globally/static declared variables are set once on load.
 
WHRoeder:
  1. Ovo:  Unlike EAs, indicators never reload with account or server change. It's been like this since I can remember. Neither there is an easy function to support reloading the indicator. You have to watch the account number yourself and recalculate the indicator if the account changes.

    Since indicators are based on chart's pair/TF, there is no need for them to recalculate anything. Try changing one of those and you will see a deinit/init cycle.

  2. ale: OnInit is called only when I add the indicator or I recompile the indicator. If I double-click on another account to log in it, the indicator is not reinitialized. Why?
    A deinit/init cycles is NEVER a reinitialization (recompile being the exception.) Globally/static declared variables are set once on load.

Also, init is no reinit? if not recompile? 

It`s clear:  In Spring I Like to Spring across the Spring Like a Spring. 

 
tara:

Also, init is no reinit? if not recompile? 

It`s clear:  In Spring I Like to Spring across the Spring Like a Spring. 

Lol, actually there is confusing behaviour in EAs (not indicators) that they do not unload variables/objects at global level with timeframe/symbol change. Nevertheless indicators do reset them, so I do not understand his objections either.
 

So there is not way to make MT4 call OnInit (or any other function) when an account is changed.

All I can do is check every time in OnCalculate if the account number is equal to the past account number. This makes the indicator much more slow :(

Thank you

 
ale:

So there is not way to make MT4 call OnInit (or any other function) when an account is changed.

All I can do is check every time in OnCalculate if the account number is equal to the past account number. This makes the indicator much more slow :(

Thank you

Not "much more slow" as it is quite fast operation from my point of view, but rather more complicated.
 
tara: Also, init is no reinit? if not recompile? 
extern int test=0
void OnInit(){
  ++test; Comment(IntegerToString(test));
}
Try that and change timeframes and then recompile. What happens?
 
WHRoeder:
Try that and change timeframes and then recompile. What happens?
The timeframes change and then the source gets recompiled. Beside that nothing unexpected.
Reason: