EA freezes at close of Parameter window

 
Hello everyone!
My EA freezes annoyingly when I okays (pressing OK) the parameter window after making some changes. I suspect the reason to be that the EA was still busy with the last known income tick (which I want to refer to as control from now). Thus, control kept moving around the blocks of code as at the time the parameter window was Okayed). My question is which command can I place at the OnDeinit() funcion to disable or halt the progress of the already moving control so that it doesn't go out of control.

Something like:
 void OnDeinit(int reason)
  {
    if (reason == REASON_PARAMETER)
    {DIsableControl(); bla bla bla}
  } 

Also, the EA works through 30 open charts and I have the following in two blocks of code within the EA:
while (RefreshRates() == false) { }
It says that the EA should be making efforts to refresh rates untill it is successful.

Note that I have also tried preventing new incoming ticks at the OnTick() function prior to the close of the parameter window but the issue continued.
 
macpee: My EA freezes annoyingly
while (RefreshRates() == false) { }

That loop only exits when a new tick arrives. Delete it.

 
William Roeder #:

That loop only exits when a new tick arrives. Delete it.

I have deleted it and replaced it with RefreshRates() and it works fine. Thanks

 

My EA works only when I open MT$ afresh and the runs once.
It does not close the trade it opens.

When I try to run it again, it does nothing.
When I try to remove it from the chart, it freezes for a while before leaving the chart.

Here is a the full code (82 lines of code).
I will appreciate any help that resolves this challenge.

#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

// Define the input parameters
extern int ADX_Period = 14;
extern int ADX_Level = 20;

// Define the global variables
double adx_plus;
double adx_minus;
double adx_signal;

int chartPeriod = Period();
string chartName = Symbol();

// Define the start function
void OnTick()
{
   // Loop forever
   while (true)
   {
      // Calculate ADX values
      adx_plus = iADX(chartName, chartPeriod, ADX_Period, PRICE_CLOSE, MODE_PLUSDI, 0);
      adx_minus = iADX(chartName, chartPeriod, ADX_Period, PRICE_CLOSE, MODE_MINUSDI, 0);
      adx_signal = iADX(chartName, chartPeriod, ADX_Period, PRICE_CLOSE, MODE_MAIN, 0);

      // Check for buy signal
      if (adx_plus > ADX_Level && adx_signal > ADX_Level && adx_minus < ADX_Level)
      {
         // Check if there is no open buy order
         if (OrderType() != OP_BUY)
         {
            // Place a buy order
            int ticket = OrderSend(Symbol(), OP_BUY, 0.1, Ask, 3, 0, 0, "ADX Buy", 0, 0, Green);
         }
      }

      // Check for sell signal
      if (adx_minus > ADX_Level && adx_signal > ADX_Level && adx_plus < ADX_Level)
      {
         // Check if there is no open sell order
         if (OrderType() != OP_SELL)
         {
            // Place a sell order
            int ticket = OrderSend(Symbol(), OP_SELL, 0.1, Bid, 3, 0, 0, "ADX Sell", 0, 0, Red);
         }
      }

      // Check for open buy order
      if (OrderType() == OP_BUY)
      {
         // Close buy order if adx_plus goes below 20 or adx_minus goes above 20
         if (adx_plus < ADX_Level || adx_minus > ADX_Level)
         {
            OrderClose(OrderTicket(), OrderLots(), Bid, 3, Green);
         }
      }

      // Check for open sell order
      if (OrderType() == OP_SELL)
      {
         // Close sell order if adx_minus goes below 20 or adx_plus goes above 20
         if (adx_minus < ADX_Level || adx_plus > ADX_Level)
         {
            OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
         }
      }

      // Sleep for 1 second
      Sleep(1000);
   }
}
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.04.07
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
@Adenekan Ogungbola #: My EA works only when I open MT$ afresh and the runs once. It does not close the trade it opens. When I try to run it again, it does nothing. When I try to remove it from the chart, it freezes for a while before leaving the chart. Here is a the full code (82 lines of code). I will appreciate any help that resolves this challenge.

You have been warned about this before on other posts.

Please EDIT your post and use the CODE button when you post code.

Code button in editor

 

I also get the error

0 13:37:29.014 ADX2_Advisor EURUSD,M1: uninit reason 1

0 13:37:29.014 Expert ADX2_Advisor EURUSD,M1: removed

 
Adenekan Ogungbola #: I also get the error 0 13:37:29.014 ADX2_Advisor EURUSD,M1: uninit reason 1 0 13:37:29.014 Expert ADX2_Advisor EURUSD,M1: removed

I will repeat ... edit your post.

 
Fernando Carreiro #:

I will repeat ... edit your post.

I apologize for the wrong post and thank you for the correction.
I have made the edit.

 
Adenekan Ogungbola #: I have made the edit.

Now, remove your loop and sleep. You return from OnTick and wait for a new one.

 
@Adenekan Ogungbola #: I apologize for the wrong post and thank you for the correction.I have made the edit.
William Roeder #: Now, remove your loop and sleep. You return from OnTick and wait for a new one.

To make things more clear for you, as William has pointed out, remove these parts of your code ...

   // Loop forever
   while (true)
   {
      // Sleep for 1 second
      Sleep(1000);
   }

OnTick, is an event handler. It should not be blocked. It needs to carry out its task and return as quickly as possible so that it can handle the next tick event.

OnTick

The function is called in EAs when the NewTick event occurs to handle a new quote