Close All Trades on Fridays - failing if statement, but the code works without it.

 

I apologize if I'm using the forum incorrectly, I'm learning.  I'm referencing this article which has helped a lot.  ( How to close all orders before weekend. - Market Hours - MQL4 and MetaTrader 4 - MQL4 programming forum (mql5.com)

I'm close to having all trades close on Fridays using inputs for day, hour, minute.  The goal is to meet the HFT prop firm requirements of no trades open on weekends and not having to manually close them and forget about them.

It's not evaluating this if statement to true for some reason.  The equality of the Date Functions is not equal to the values being set my input.  Is this a data type issue?  Maybe there's a leading 0 on Hour or Minute that I can't figure out how to see?  Thank you for guidance.

    if( (DayOfWeek()=="usedayOfWeek") && (Hour()=="usetradeHour") && (Minute()=="usetradeMinute") ){

When I comment out the prior "if" statement and one bracket for the if, I get the comment and all the open trades are closed.

//+------------------------------------------------------------------+
//|                                              close_alltrades.mq4 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
#include <stderror.mqh>
enum dayOfWeek 
  {
   S=0,     // Sunday
   M=1,     // Monday
   T=2,     // Tuesday
   W=3,     // Wednesday
   Th=4,    // Thursday
   Fr=5,    // Friday,
   St=6,    // Saturday,
   };
   //--- input parameters
input dayOfWeek swapday=St; //Day of the Week
input int TradeHour=21; //Hour to close
input int TradeMinute=0; //Hour to close
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
//void OnTick() //use this to run each tick vs one time on OnInit
void OnInit()
  {
  int usedayOfWeek=StrToInteger("dayOfWeek");
  int usetradeHour=StrToInteger("TradeHour");
  int usetradeMinute=StrToInteger("TradeMinute");
      
   Comment (
    "Input Day of Week: ",swapday,"\n",
   "Input Trade Hour: ",TradeHour,"\n",
   "Input Trade Hour: ",TradeMinute,"\n"
   );
  } 
  
 void OnTick()
  {
   int usedayOfWeek=StrToInteger("dayOfWeek");
   int usetradeHour=StrToInteger("TradeHour");
   int usetradeMinute=StrToInteger("TradeMinute");
    int EA_magic=0;
    int slippage=100;
    
    if( OrdersTotal() > 0 ){
    //if( (DayOfWeek()=="usedayOfWeek") && (Hour()=="usetradeHour") && (Minute()=="usetradeMinute") ){
    Comment (    
    "WE ARE IN THE RIGHT PLAYCE!","\n"
    );
    int ordticket;
    for( int i = 0 ; i < OrdersTotal() ; i++ ){
    if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ){
    break; // or should I use "continue" here?
    }
  if( OrderMagicNumber() != EA_magic ){
  continue;
    }
  if( OrderSymbol() != Symbol() ){
  continue;
    }
    ordticket = OrderTicket();
    OrderSelect(ordticket, SELECT_BY_TICKET, MODE_TRADES);
    //OrderClose(ordticket,OrderLots(),Bid,slippage+MarketInfo(Symbol(),MODE_SPREAD),White);*/
    CloseAllShorts();
    CloseAllLongs();
    }
   // }
  }
}
 
 
void CloseAllShorts()
{
    int ordticket;
    int EA_magic=0;
    int slippage=100;
    
    for( int i = OrdersTotal()-1; i>=0; i-- ){
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ){
         continue;
      }
      if( OrderMagicNumber() != EA_magic ){
          continue;
      }
      if( OrderSymbol() != Symbol() ){
          continue;
      }
      if( OrderType() != OP_SELL ){
          continue;
      }
      RefreshRates();
      ordticket = OrderTicket();
      if( OrderSelect(ordticket, SELECT_BY_TICKET, MODE_TRADES) == true ){
         OrderClose(ordticket,OrderLots(),Ask,slippage,White);
      } else {
         Print("CloseAllShorts() returned an error: ",GetLastError());
      }
    }

}

void CloseAllLongs()
{
    int ordticket;
    int EA_magic=0;
    int slippage=100;
    
    for( int i = OrdersTotal()-1; i>=0; i-- ){
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ){
         continue;
      }
      if( OrderMagicNumber() != EA_magic ){
          continue;
      }
      if( OrderSymbol() != Symbol() ){
          continue;
      }
      if( OrderType() != OP_BUY ){
          continue;
      }
      RefreshRates();
      ordticket = OrderTicket();
      if( OrderSelect(ordticket, SELECT_BY_TICKET, MODE_TRADES) == true ){
         OrderClose(ordticket,OrderLots(),Bid,slippage,White);
      } else {
         Print("CloseAllLongs() returned an error: ",GetLastError());
      }
    }
}


ss

How to close all orders before weekend. - How to find out when the forex market closes on Friday evening
How to close all orders before weekend. - How to find out when the forex market closes on Friday evening
  • 2010.05.21
  • www.mql5.com
The function dayofweek() returns the current zero-based day of the week (0-sunday,1,2,3,4,5,6) of the last known server time. My broker's (fxopen) server time is gmt. If the forex market officially closes at 5:00 pm on friday in new york, my server time should be 9:00 pm
 

Working code.  I was using the wrong variables, once I compared to the input variables all worked as expected.

Note the minute code I changed to >= so if you set the minute value to 40 then for the next 20 minutes it will keep any sells or buys closed before the market closes.

//+------------------------------------------------------------------+
//|                                              close_alltrades.mq4 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
#include <stderror.mqh>
enum dayOfWeek 
  {
   S=0,     // Sunday
   M=1,     // Monday
   T=2,     // Tuesday
   W=3,     // Wednesday
   Th=4,    // Thursday
   Fr=5,    // Friday,
   St=6,    // Saturday,
   };
   //--- input parameters
input dayOfWeek swapday=St; //Day of the Week
input int TradeHour=21; //Hour to close
input int TradeMinute=0; //Hour to close
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
//void OnTick() //use this to run each tick vs one time on OnInit
void OnInit()
  {
  int usedayOfWeek=StrToInteger("dayOfWeek");
  int usetradeHour=StrToInteger("TradeHour");
  int usetradeMinute=StrToInteger("TradeMinute");
      
   Comment (
    "Input Day of Week: ",swapday,"\n",
   "Input Trade Hour: ",TradeHour,"\n",
   "Input Trade Hour: ",TradeMinute,"\n"
   );
  } 
  
 void OnTick()
  {
   int usedayOfWeek=StrToInteger("dayOfWeek");
   int usetradeHour=StrToInteger("TradeHour");
   int usetradeMinute=StrToInteger("TradeMinute");
    int EA_magic=0;
    int slippage=100;
 
  
    if( OrdersTotal() > 0 ){
    if( (DayOfWeek()==swapday) && (Hour()==TradeHour) && (Minute()>=TradeMinute) ){
    int ordticket;
    for( int i = 0 ; i < OrdersTotal() ; i++ ){
    if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ){
    break; // or should I use "continue" here?
    }
  if( OrderMagicNumber() != EA_magic ){
  continue;
    }
  if( OrderSymbol() != Symbol() ){
  continue;
    }
    ordticket = OrderTicket();
    OrderSelect(ordticket, SELECT_BY_TICKET, MODE_TRADES);
    CloseAllShorts();
    CloseAllLongs();
    Comment (    
   "Closing all Trades based on Input parameters!","\n"
   "Input Day of Week: ",swapday,"\n",
   "Input Trade Hour: ",TradeHour,"\n",
   "Input Trade Hour: ",TradeMinute,"\n"
    );
    }
   }
  }
}
 
void CloseAllShorts()
{
    int ordticket;
    int EA_magic=0;
    int slippage=100;
    
    for( int i = OrdersTotal()-1; i>=0; i-- ){
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ){
         continue;
      }
      if( OrderMagicNumber() != EA_magic ){
          continue;
      }
      if( OrderSymbol() != Symbol() ){
          continue;
      }
      if( OrderType() != OP_SELL ){
          continue;
      }
      RefreshRates();
      ordticket = OrderTicket();
      if( OrderSelect(ordticket, SELECT_BY_TICKET, MODE_TRADES) == true ){
         OrderClose(ordticket,OrderLots(),Ask,slippage,White);
      } else {
         Print("CloseAllShorts() returned an error: ",GetLastError());
      }
    }
}

void CloseAllLongs()
{
    int ordticket;
    int EA_magic=0;
    int slippage=100;
    
    for( int i = OrdersTotal()-1; i>=0; i-- ){
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ){
         continue;
      }
      if( OrderMagicNumber() != EA_magic ){
          continue;
      }
      if( OrderSymbol() != Symbol() ){
          continue;
      }
      if( OrderType() != OP_BUY ){
          continue;
      }
      RefreshRates();
      ordticket = OrderTicket();
      if( OrderSelect(ordticket, SELECT_BY_TICKET, MODE_TRADES) == true ){
         OrderClose(ordticket,OrderLots(),Bid,slippage,White);
      } else {
         Print("CloseAllLongs() returned an error: ",GetLastError());
      }
    }
}