if (DayOfWeek() == 6) CloseTrades(); doesn't close my trade (MQL4). Any idea why?

 

I wrote an Expert Advisor and everything works well except for one function:


void OnTick()
{  

//My stuff...
 
if (DayOfWeek() == 6)
   {
      CloseTrades();
   }
}

void CloseTrades()
{
   for (int i = OrdersTotal(); i >= 0; i--)
   {
      if (OrderSelect(i, SELECT_BY_POS) == true)
      if (OrderSymbol() == Symbol())
         {
            OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE);
         }
   }
}


I tested the CloseTrades() part and it works. Apparently, it doesn't recognize the DayOfWeek() part. Any suggestions why it doesn't and what should be done? Thank you for your time!

 

Day 6 is Saturday == market closed.

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- The date is on Sunday
   datetime time=D'2018.06.10 12:00';
   string symbol="GBPUSD";
   ENUM_TIMEFRAMES tf=PERIOD_H1;
   bool exact=false;
//--- there is no bar at the specified time, iBarShift will return the index of the nearest bar
   int bar_index=iBarShift(symbol,tf,time,exact);
   PrintFormat("1. %s %s %s(%s): bar index is %d (exact=%s)",
               symbol,EnumToString(tf),TimeToString(time),DayOfWeek(time),bar_index,string(exact));
   datetime bar_time=iTime(symbol,tf,bar_index);
   PrintFormat("Time of bar #%d is %s (%s)",
               bar_index,TimeToString(bar_time),DayOfWeek(bar_time));
//PrintFormat(iTime(symbol,tf,bar_index));
//--- Request the index of the bar with the specified time; but there is no bar, return -1
   exact=true;
   bar_index=iBarShift(symbol,tf,time,exact);
   PrintFormat("2. %s %s %s (%s):bar index is %d (exact=%s)",
               symbol,EnumToString(tf),TimeToString(time),DayOfWeek(time),bar_index,string(exact));
  }
//+------------------------------------------------------------------+
//| Returns the name of the day of the week                          |
//+------------------------------------------------------------------+
string DayOfWeek(const datetime time)
  {
   MqlDateTime dt;
   string day="";
   TimeToStruct(time,dt);
   switch(dt.day_of_week)
     {
      case 0: day=EnumToString(SUNDAY);
      break;
      case 1: day=EnumToString(MONDAY);
      break;
      case 2: day=EnumToString(TUESDAY);
      break;
      case 3: day=EnumToString(WEDNESDAY);
      break;
      case 4: day=EnumToString(THURSDAY);
      break;
      case 5: day=EnumToString(FRIDAY);
      break;   
      default:day=EnumToString(SATURDAY);
      break;
     }
//---
   return day;
  }
/* The result:
   1. GBPUSD PERIOD_H1 2018.06.10 12:00(SUNDAY): bar index is 64 (exact=false)
   Time of bar #64 is 2018.06.08 23:00 (FRIDAY)
   2. GBPUSD PERIOD_H1 2018.06.10 12:00 (SUNDAY):bar index is -1 (exact=true)
*/  
 
Marco vd Heijden:

Day 6 is Saturday == market closed.

Thank you for the reply! In my country market is closed at 01.00 on Saturday. What I thought was to close trades as soon as DayOfWeek() detects Saturday (at 00.00).

Now I suspect at night price hardly moves, and as the function is implemented per tick, maybe I have to wait some time (5-10 min.) until it actually ticks. (I closed it manually in haste after a minute). I'll wait a bit more the next time and see if that solves the problem.
 
Remember, all times are broker's time, not your local time.
 
William Roeder:
Remember, all times are broker's time, not your local time.
Oh, that's a good piece of info here! So, I will have to write it this way: If the day is Friday and the time is 23.59, close my trades. Thanks!
 
William Roeder:
Remember, all times are broker's time, not your local time.

This is what I ended up writing, haven't tested it yet though.


void OnTick()
{   

if ((DayOfWeek() == 5 && TimeHour(TimeCurrent()) == 23))
        {
        bool trade = false;
        }
else
        {
        trade = true;
        }

if (trade == true)
        {
        //trade
        }

if (trade == false)
        {
        CloseTrades();
        }
}