Download MetaTrader 5

Event on changing server?

To add comments, please log in or register
Log in to MQL5.community using OpenID. It is convenient!
BurkhardWille
170
BurkhardWille 2014.05.01 05:48 

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

onewithzachy
2271
onewithzachy 2014.05.01 07:03  
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.


BurkhardWille
170
BurkhardWille 2014.05.01 09:12  
onewithzachy:


if terminal server has change, call this UDF again.


but we can I check the server change?
onewithzachy
2271
onewithzachy 2014.05.01 09:27  
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.


BurkhardWille
170
BurkhardWille 2014.05.01 15:40  
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;
}
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.05.01 15:53  
void OnDeinit(const int reason) {
  ACCOUNT_SERVER_1 == NULL;
}
Do we really need to NULL it ?
BurkhardWille
170
BurkhardWille 2014.05.01 17:55  
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.
onewithzachy
2271
onewithzachy 2014.05.01 22:39  

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.


Halley
198
Halley 2014.05.02 17:38  
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.

onewithzachy
2271
onewithzachy 2014.05.07 11:21  
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().


Halley
198
Halley 2014.05.07 12:43  
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.

12
To add comments, please log in or register