Change input variable while EA is running on chart is not effective on the same chart. It requires to remove the EA completely and reattach it with changed variable. - page 2

 
marketeer:

If you do an infinite loop in your EA, you can't expect it will have an opportunity to get new input values.

There was a long discussion of the matter on Russian forum, I can provide the link, if you wish - you'll then be able to translate the topic by means of autotranslation facilities of the forum.

Here it is anyway, Need help of MT4 developers (in Russian).

Great post. I got part of answer.

Here is the part of answer

 

 

If you use this two code, you will see clearly that the infinite loop is the main problem of not getting the change in the input variable.

 

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright     "Grizzly_v :)"
#property link          "http://www.mql5.com"
//+------------------------------------------------------------------+
input int      A1=      1;
input int      A2=      2;
int      C1,C2;
//+------------------------------------------------------------------+
int OnInit(){
   C1=A1;
   C2=A2;
   
   EventSetTimer(1);
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   Comment("");
}

//+------------------------------------------------------------------+
void OnTimer()
{
   string   t;


   while (!IsStopped ())
   {
   
     
    t=TimeToStr(TimeCurrent(),TIME_MINUTES|TIME_SECONDS);
    Comment(t+"\nA1="+A1+"   A2="+A2+"\nC1="+C1+"   C2="+C2);
    Sleep(50);
   }

   
  
}
//+------------------------------------------------------------------+
 

Compare above code without infinite loop.

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright     "Grizzly_v :)"
#property link          "http://www.mql5.com"
//+------------------------------------------------------------------+
extern int      A1=      1;
extern int      A2=      2;
int      C1,C2;
//+------------------------------------------------------------------+
int OnInit(){
   C1=A1;
   C2=A2;
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason){
   Comment("");
}
//+------------------------------------------------------------------+
void OnTick(){
   string   t;

   t=TimeToStr(TimeCurrent(),TIME_MINUTES|TIME_SECONDS);
   Comment(t+"\nA1="+A1+"   A2="+A2+"\nC1="+C1+"   C2="+C2);
}
//+------------------------------------------------------------------+
 

And Sleep() or RefreshRate() function does not help in this case.

Only solution seems to be the periodic breakout of the infinite loop. I guess.

Even Event Handler won't work inside the infinite loop.

 
FinanceEngineer:

And Sleep() or RefreshRate() function does not help in this case.

Only solution seems to be the periodic breakout of the infinite loop. I guess.

Even Event Handler won't work inside the infinite loop.

You should use OnTimer instead of the loop.
 
marketeer:
You should use OnTimer instead of the loop.
Hello Yes, that could be one answer. Just curious if OnTimer() function can be used in interval less than 1 second. The reason I use Infinite loop is because my EA operates on 100 to 200 ms interval. Kind regards.
 
FinanceEngineer:
Hello Yes, that could be one answer. Just curious if OnTimer() function can be used in interval less than 1 second. The reason I use Infinite loop is because my EA operates on 100 to 200 ms interval. Kind regards.
Just use EventSetMillisecondTimer for this.
 
marketeer:
Just use EventSetMillisecondTimer for this.

Superb and many thanks. This is the solution. I think this post should be archived here.

And here is the solution code.

 

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright     "Grizzly_v :)"
#property link          "http://www.mql5.com"
//+------------------------------------------------------------------+
extern int      A1=      1;
extern int      A2=      2;
int      C1,C2;
//+------------------------------------------------------------------+


int OnInit()
{
   C1=A1;
   C2=A2;
   
   EventSetMillisecondTimer(100);
   //EventSetTimer(1);
   
   return(INIT_SUCCEEDED);
   
}

//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{

   Comment("");
}

//+------------------------------------------------------------------+
void OnTimer()
{
   string   t;

   t=TimeToStr(TimeCurrent(),TIME_MINUTES|TIME_SECONDS);
   Print(t+"\nA1="+A1+"   A2="+A2+"\nC1="+C1+"   C2="+C2);
}
//+------------------------------------------------------------------+
 

I replaced my infinite loop with OnTimer() function with EventSetMillisecondTimer(100);

It works like charm and I can indeed detect change in the input variable when user change input variable immediately. It does work for both MT4 and MT5.  

You don't need to set dummy variable like  int      C1 and C2 because change in your input variable is already effective. So simpler in code too.
 

Really Superb and Great !!!

Thumps up for both marketeer and   YouTrade. :) :)

Meta Trader is superb platform too. 

 
some times i wonder how you guys are able to make this ea that you do. how i wish i have some one to tech me coding
Reason: