Download MetaTrader 5
To add comments, please log in or register
Find out how social trading can improve your profits
Route206
211
Route206 2015.07.07 07:51 

consider a certain value being used by an EA (e.g. a profit target)

int h=3;

void OnTick()
  {Comment("Value H = ",h);}

 Now let's say: if i want to change the value of 'h' during EA running, i would ideally simply want to do a little script like this:

#property show_inputs 

input int h=5;

void OnStart()
  { Alert("New value H =",h); }

 Clearly that didn't work... Alert was fine with the new value, but the 'h' in the comment section of the EA remained unchanged.

Is my only option now to use GlobalVariables? (uuughh...)

I'd like to avoid that as i may have the same EA running on multiple charts, where on others i'd like things te remain unchanged. 

Keith Watford
Moderator
7960
Keith Watford 2015.07.07 10:07  

Looks to me that you trying to change the value of a variable in an EA by using a variable with the same name in a script.

It won't work that way. Once the codes are compiled the variable name "h" is not recognised.

Imagine the mayhem if changing the value of a variable in an expert also changed the value of a variable in another expert that happened to have the same name. 

whroeder1
13627
whroeder1 2015.07.07 11:30  
Route206:  Clearly that didn't work...
#property strict
input int h=5;
void OnTick()
  {
Print(h);
  }

2015.07.07 07:26:24.270    Expert test GBPUSD,H1: removed
2015.07.07 07:26:24.266    test GBPUSD,H1: uninit reason 1
2015.07.07 07:26:24.218    test GBPUSD,H1: 0
2015.07.07 07:26:22.331    test GBPUSD,H1: 0
2015.07.07 07:26:19.433    test GBPUSD,H1: 0
2015.07.07 07:26:19.367    test GBPUSD,H1: 0
2015.07.07 07:26:17.606    test GBPUSD,H1: 0
2015.07.07 07:26:16.934    test GBPUSD,H1 inputs: h=0;
2015.07.07 07:26:16.918    test GBPUSD,H1: initialized
2015.07.07 07:26:16.916    test GBPUSD,H1: uninit reason 5
2015.07.07 07:26:15.202    test GBPUSD,H1: 5
2015.07.07 07:26:15.102    test GBPUSD,H1: 5
2015.07.07 07:26:14.361    test GBPUSD,H1: 5
2015.07.07 07:25:55.210    test GBPUSD,H1: 5
2015.07.07 07:25:55.199    test GBPUSD,H1: initialized
2015.07.07 07:25:55.195    test GBPUSD,H1 inputs: h=5;
2015.07.07 07:25:52.263    Expert test GBPUSD,H1: loaded successfully
Clearly it works just fine.
honest_knave
969
honest_knave 2015.07.07 12:00  
Route206
211
Route206 2015.07.07 12:46  
WHRoeder:

Clearly it works just fine.

Thanks, but your log shows an UnInit. I'm assuming you're using one EA, edit it while running and then compile and reload?

that i don't want. I can't go through DeInit() and OnInit() :-( 

That was why i wanted to use a small script to change the variable and leave the EA running as is. 

whroeder1
13627
whroeder1 2015.07.08 12:04  
Route206: I can't go through DeInit() and OnInit() :-(

You have no choice. It has to deinit, reload the new compiled image and init it. All global/static variables are reset.

EA's must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a orderSelect loop to recovery, or persistent storage (GV/file) of ticket numbers required.

/
To add comments, please log in or register