Changing the values of extern variables after EA activation

 

There is a problem that creeps up now and then, when I run an EA. Sometimes after an EA is activitated, I want to change my initial extern values. So I deactivate my EA and click on properties to change the variables. After entering the new values and hitting ok, often the EA continues as before without any problems. Other times the EA may duplicate an open trade and act unpredictably. Does anyone know what causes this and what I can do to prevent this from happening?

 
forestmyopia:

There is a problem that creeps up now and then, when I run an EA. Sometimes after an EA is activitated, I want to change my initial extern values. So I deactivate my EA and click on properties to change the variables. After entering the new values and hitting ok, often the EA continues as before without any problems. Other times the EA may duplicate an open trade and act unpredictably. Does anyone know what causes this and what I can do to prevent this from happening?

When changing params via properties window, the expert goes through the following -> deinit() is called (when opening the properties window), init() is called (after pressing 'OK'), start() is called (on the next incoming tick).

Some experts don't take this into consideration and do not work properly when this happens. You should examine how your code handles this process...


BTW - there is no reason to 'deactivate' your EA. Just press F7, change extern params and press 'OK'.

 
gordon wrote >>

When changing params via properties window, the expert goes through the following -> deinit() is called (when opening the properties window), init() is called (after pressing 'OK'), start() is called (on the next incoming tick).

Some experts don't take this into consideration and do not work properly when this happens. You should examine how your code handles this process...

BTW - there is no reason to 'deactivate' your EA. Just press F7, change extern params and press 'OK'.

Thanks for the info. I'll check it out.

 

Extern variables are reset when you press enter, the global and static variable are not reset.

Therefor EA's that modify extern's may get confused. EA's that assume global/static variables start at the initializer value, will fail.

I have previously posted this code:

//---- These are adjusted for 5 digit brokers.
double  pips2points     = 1,    //  slippage  3 pips    3=points    30=points
        pips2dbl;   //  = Point //  Stoploss 15 pips    0.0015      0.00150
int     Digits.pips     = 0;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int init()
{
    pips2dbl    = Point;    // Init
    if (Digits == 3 || Digits == 5) {
        pips2points *= 10;
        pips2dbl    *= 10;
        Digits.pips++;
    } //...

On a modify sequence, those three variables will be wrong. They would also be wrong if you just changed the chart period, as that's the same sequence.

Now my code looks like this:

int init() {
  if (Digits == 5 || Digits == 3) {	// Adjust for five (5) digit brokers.
		pips2dbl = Point*10;	pips2points = 10;	Digits.pips = 1;
  } else {	pips2dbl = Point;	pips2points =  1;	Digits.pips = 0;
  }
 
WHRoeder:

Therefor EA's that modify extern's may get confused.

forestmyopia, to further clarify WHRoeder comment, please note that any time the expert goes through deinit() -> init() all extern variables are reset to their last value that was set in the properties window. Hence if an expert changes extern variables via code, an accidental deinit() -> init() would cause all extern variables to reset, and that might break an EA or cause subtle bugs. Hence, using code to change extern variable is dangerous if proper measures are not taken.


If u r not aware of when deinit(), init() are called, please read this carefully -> MQL4 Reference - Program Run.

 
WHRoeder wrote >>

Extern variables are reset when you press enter, the global and static variable are not reset.

Therefor EA's that modify extern's may get confused. EA's that assume global/static variables start at the initializer value, will fail.

I have previously posted this code:

On a modify sequence, those three variables will be wrong. They would also be wrong if you just changed the chart period, as that's the same sequence.

Now my code looks like this:

Thanks for the input. I modified my program accordingly and I have no problems now.

 
gordon wrote >>

forestmyopia, to further clarify WHRoeder comment, please note that any time the expert goes through deinit() -> init() all extern variables are reset to their last value that was set in the properties window. Hence if an expert changes extern variables via code, an accidental deinit() -> init() would cause all extern variables to reset, and that might break an EA or cause subtle bugs. Hence, using code to change extern variable is dangerous if proper measures are not taken.

If u r not aware of when deinit(), init() are called, please read this carefully -> MQL4 Reference - Program Run.

I modified my program by changing which variables are extern variables-- extern variables that would not be affected by manual changes in the property window. Everything is working great now. Thanks!

Reason: