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()); } } }

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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.
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.
ss