EA freezes every time i try to have it wait for the rest of the day

 

I have some logic that needs to pause until the next day or week starts.

The code and logic works great when I'm running MQL5, but recently i tried to port it into MQL4 and every time i run it in the strategy tester, it would freeze as soon as either of the fuctions are called


These are the functions in question:

void WaitTillNextWeek()
  {
   
   static datetime starttime = iTime(Symbol(), PERIOD_W1, 0);
   while(starttime == iTime(Symbol(), PERIOD_W1, 0))
     {
      Sleep(60000);
     }

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void WaitTillNextDay()
  {
   
   static datetime starttime = iTime(Symbol(), PERIOD_D1, 0);
   while(starttime == iTime(Symbol(), PERIOD_D1, 0))
     {
      Sleep(60000);
     }

  }
 
I strongly advise against using the `Sleep()` function in your trading algorithm. For a more robust solution, consider coding a function that continuously checks whether a day or week has elapsed, rather than halting the EA entirely. This approach avoids many pitfalls associated with the `Sleep()` function.

For more insights, I recommend reading this article: To Sleep, or Not to Sleep?
To Sleep, or Not to Sleep?
To Sleep, or Not to Sleep?
  • www.mql5.com
An alternative use of the Sleep() function in the realization of pauses between EA's actions is proposed. The approach under consideration allows a smart use of machine time.
 
akbaobaid:

I have some logic that needs to pause until the next day or week starts.

The code and logic works great when I'm running MQL5, but recently i tried to port it into MQL4 and every time i run it in the strategy tester, it would freeze as soon as either of the fuctions are called


These are the functions in question:

Your logic is wrong.

With that logic, until whenever EA will Sleep.

 
akbaobaid: I have some logic that needs to pause until the next day or week starts.

The code and logic works great when I'm running MQL5, but recently i tried to port it into MQL4 and every time i run it in the strategy tester, it would freeze as soon as either of the fuctions are called

  1. Stop Sleeping. If it is not time, return and wait for a new tick.
  2.    static datetime starttime = iTime(Symbol(), PERIOD_W1, 0);

    That is not an assignment; it's initialization of a common (globally declared), or static variable(s) with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)

  3. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

    Please help with one trade per day code - Day Trading - Expert Advisors and Automated Trading - MQL5 programming forum #3.2 (2020)

Reason: