Event on changing server?

 

Hi,

all important things in the OnInit-routine not be called, if I change the server. Is there another event handler to react on server-change?



Regards

Burkhard

 
BurkhardWille:

Hi,

all important things in the OnInit-routine not be called, if I change the server. Is there another event handler to react on server-change?



Regards

Burkhard

No. There's no event handler for server changing, and what for anyway.

To solve your problem, write OnInit codes in UDF (User Defined Function), if terminal server has change, call this UDF again.

Forget to add : ... and you have to null all value of globally declared variables.


 
onewithzachy:


if terminal server has change, call this UDF again.


but we can I check the server change?
 
BurkhardWille:

but we can I check the server change?

Use AccountServer(), click that, please. If the server name is not the same like the previous one - perhaps after there's no connection - then the server has change.


 
cool, thx a lot for the help. Here is my code for other user, so I give some help back...


//Indicator-Code

string ACCOUNT_SERVER_1;
void OnInit() {
  OnInit_UDF();
}


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[]) {


  if (ACCOUNT_SERVER_1 == NULL || ACCOUNT_SERVER_1 != AccountInfoString(ACCOUNT_SERVER)) {
    OnInit_UDF();
  }

  ...

}



void OnInit_UDF() {
  ACCOUNT_SERVER_1 = AccountInfoString(ACCOUNT_SERVER);

  ...
}


void OnDeinit(const int reason) {
  ACCOUNT_SERVER_1 == NULL;
}
 
void OnDeinit(const int reason) {
  ACCOUNT_SERVER_1 == NULL;
}
Do we really need to NULL it ?
 
I dont know, I got the info "expression has no effect", probably we could delete it, I just thought to myself, safe is safe. I guess the server change does not executes all the deinit lines comletly and does the variables in the garbage.
 

I'll do this, it's pretty much the same though ...

string ACCOUNT_SERVER_1 = ""; // always initialize string variable, otherwise we will get "uninitialize string" error.

void OnInit() 
  {
  OnInit_UDF();
  }


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[]) 
  {
  if (ACCOUNT_SERVER_1 != AccountInfoString(ACCOUNT_SERVER))  OnInit_UDF(); // no need to check string = NULL, just check if string has different value than before
  
  }

void OnInit_UDF() 
  {
  ACCOUNT_SERVER_1 = AccountInfoString(ACCOUNT_SERVER);
  }

I rarely use OnDeinit, except if there are something need/have to/must to be done before indicator is closed, such as deleting some object on chart, or deleting some object variable that was created using operator new and so need/have to/must be deleted using operator delete.


 
onewithzachy:

I'll do this, it's pretty much the same though ...

I rarely use OnDeinit, except if there are something need/have to/must to be done before indicator is closed, such as deleting some object on chart, or deleting some object variable that was created using operator new and so need/have to/must be deleted using operator delete.




I often check UninitializeReason(), how do you set it to REASON_ACCOUNT? When I check UninitializeReason() when the accoun changed there is still whatever but REASON_ACCOUNT. I need it correct.

 
DeepThought:


I often check UninitializeReason(), how do you set it to REASON_ACCOUNT? When I check UninitializeReason() when the accoun changed there is still whatever but REASON_ACCOUNT. I need it correct.

Please check UninitializeReason() in OnInit().


 
onewithzachy:

Please check UninitializeReason() in OnInit().

You can check it anywhere, but I am sure you will never see REASON_ACCOUNT in indicators. This bug has been sitting there since I remember the MT4. I am surprised it is discussed for the first time in years, probably not many indicators depend on the server data directly.