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.

 

Hello people.

I just realized that while EA is running on the chart, change in the Input variable (I mean manually) is not effective on the same chart. Actually we need to completely remove the EA from chart and reattach the EA with changed variable to make the variable change effective.

Is there anyway that to make change in input variable  without removing from the chart?

I feel that EA is less responsive like this. Isn't it better to see if the change in input variable is immediately effective on the same chart ? 

Is there any way to get around with this?

Kind regards.

 

check this !

 

https://www.mql5.com/en/forum/30860 

 
YouTrade:

check this !

 

https://www.mql5.com/en/forum/30860 

This is wonderful body. Thanks so much. I also personally use chart event handler to change some parameters on some indicators. I know they work beautifully. 

Just open up the discussion little bit wider, if it is possible to force to run void OnInit () function programmatically when user change input variables and when expert advisor detect the change in input parameters?

When I change input variable on the EA, Expert tab prints the new set of input variable in single line but all the printf(".....") stuff inside OnInit () function never print anything.

So I guess change in input variable does not reinitialize OnInit() function.

Can we reinitialize OnInit() function without restarting EA on fresh chart ? 

Kind regards.  

 

No you cannot !

What I did to bypass this situation (I have had the same request) was mirroring the OnInit() function and call it when necessary.

Just make sure that, just in case of your EA restarts somehow, to not overlap variables.

Regards

MRC 

 
YouTrade:

No you cannot !

What I did to bypass this situation (I have had the same request) was mirroring the OnInit() function and call it when necessary.

Just make sure that, just in case of your EA restarts somehow, to not overlap variables.

Regards

MRC 

wow, thanks so much for the helpful answer.

Just curious how did you detect the change in the input variable programmatically ?

One thing I tried was set some dummy static variable to store the initial input variable,

then later inside OnTick () function or OnTimer() function I called this special function comparing the dummy static variable to new input variable to check if they have changed it.

However, actually when user change the input variable and when I called this special function to compare, this idea never worked.

 

I will give you an example here. CopyStopLoss and CopyTakeProfit is input variable and CopyStopLoss_ and CopyTakeProfit_ is the dummy variable storing the original input variable value. 

When I called this inside OnTick() function or OnTimer() function, the input variable are always the same as original variable even though user changed the value.

When I call the function, its never gete inside if statement as the CopyStopLoss  and CopyTakeProfit variable are the same even though user have changed.  

 

void CheckChangeInInputVariable()
{
    
    if(CopyStopLoss_ != CopyStopLoss || CopyTakeProfit_ != CopyTakeProfit)
    {
        cnt = 0;
        CopyStopLoss_ = CopyStopLoss;
        CopyTakeProfit_ = CopyTakeProfit;
    }
}
 
FinanceEngineer:

wow, thanks so much for the helpful answer.

Just curious how did you detect the change in the input variable programmatically ?

One thing I tried was set some dummy static variable to store the initial input variable,

then later inside OnTick () function or OnTimer() function I called this special function comparing the dummy static variable to new input variable to check if they have changed it.

However, actually when user change the input variable and when I called this special function to compare, this idea never worked.

 

I will give you an example here. CopyStopLoss and CopyTakeProfit is input variable and CopyStopLoss_ and CopyTakeProfit_ is the dummy variable storing the original input variable value. 

When I called this inside OnTick() function or OnTimer() function, the input variable are always the same as original variable even though user changed the value.

When I call the function, its never gete inside if statement as the CopyStopLoss  and CopyTakeProfit variable are the same even though user have changed.  

 

I see no problem here (see below). Please provide more info about your code so I can check.

What happens when you debug it ? 

input double TakeProfit=0;
double TakeProfit_=0;

if(TakeProfit!=TakeProfit_)
  {
   do something;
  }

Regards 

 
YouTrade:

I see no problem here (see below). Please provide more info about your code so I can check.

What happens when you debug it ? 

Regards 

Sure no problem. Debugging is fine. There is no error.

I try to place the  void CheckChangeInInputVariable() inside void OnTimer() or void OnTick() but both of them fails to get new change input value on fly when user change the input value on the chart.

I place them inside while loop. I am not sure if the while loop may cause the problem.  

Kind regards.

 

input bool StopLoss=false;
input bool TakeProfit=false;


bool TakeProfit_=false;
bool StopLoss_=false;


void OnTimer()//tried both OnTimer and OnTick
{   
   while(!IsStopped())
   {
       CheckChangeInInputVariable(); // this function never get the changed input value

       if(cnt != lastCnt)
       {
           break; //break the while loop
       }

       do something;

       Sleep(100);

   }

}


void CheckChangeInInputVariable()
{
    
    if(CopyStopLoss_ != CopyStopLoss || CopyTakeProfit_ != CopyTakeProfit)
    {
        cnt = 0;
        CopyStopLoss_ = CopyStopLoss;
        CopyTakeProfit_ = CopyTakeProfit;
    }
}
 
FinanceEngineer:

Sure no problem. Debugging is fine. There is no error.

I try to place the  void CheckChangeInInputVariable() inside void OnTimer() or void OnTick() but both of them fails to get new change input value on fly when user change the input value on the chart.

I place them inside while loop. I am not sure if the while loop may cause the problem.  

Kind regards.

 

Just to make sure we are sync.

Are you changing the variable manually double clicking on the EA and changing?

If yes, that is the problem because when you do this, you execute OnDeinit()

I thought you were chancing passing a new parameter

 

Add this to your EA and you will get it !

 

void OnDeinit(const int reason)
  {
//--- The first way to get the uninitialization reason code
   Print(__FUNCTION__,"_Uninitalization reason code = ",reason);
//--- The second way to get the uninitialization reason code
   Print(__FUNCTION__,"_UninitReason = ",getUninitReasonText(_UninitReason));

  }
//+------------------------------------------------------------------+
//| get text description                                             |
//+------------------------------------------------------------------+

string getUninitReasonText(int reasonCode)
  {
   string text="";
   switch(reasonCode)
     {
      case REASON_ACCOUNT:
         text="Account was changed";break;
      case REASON_CHARTCHANGE:
         text="Symbol or timeframe was changed";break;
      case REASON_CHARTCLOSE:
         text="Chart was closed";break;
      case REASON_PARAMETERS:
         text="Input-parameter was changed";break;
      case REASON_RECOMPILE:
         text="Program "+__FILE__+" was recompiled";break;
      case REASON_REMOVE:
         text="Program "+__FILE__+" was removed from chart";break;
      case REASON_TEMPLATE:
         text="New template was applied to chart";break;
      default:text="Another reason";
     }
   return text;
  }
//+------------------------------------------------------------------+

 

 
YouTrade:

Add this to your EA and you will get it !

 

 

Yes, you are right.

I would like to detect the change in input variable when user double click EA and change the input variable.

Thanks for the wonderful peace of code.

It does work for MT5 code. Thanks so much.

I have the almost same piece of code on Meta Trader 4 too. But it does not work on the Meta Trader 4 code.

I probably think while loop or unfinished OnTimer() function or OnTick() function due to while loop may be the problem.

 

    Print(__FUNCTION__,"_Uninitalization reason code = ",reason);
//--- The second way to get the uninitialization reason code
   Print(__FUNCTION__,"_UninitReason = ",getUninitReasonText(_UninitReason));

 

Anyway thanks so much, at least half of the success. 

 

Kind regards.

 

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).

Reason: