#include <Trade\Trade.mqh> CTrade trade; input int CloseTimeHour = 23; input int CloseTimeMin = 0; input int CloseTimeSec = 0; input ENUM_DAY_OF_WEEK CloseTimeDayOfTheWeek = -1; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnTick() { if(PositionsTotal() > 0 && CloseTimeDayOfTheWeek > -1) { MqlDateTime structTime; TimeCurrent(structTime); structTime.hour = CloseTimeHour; structTime.min = CloseTimeMin; structTime.sec = CloseTimeSec; datetime timeClose = StructToTime(structTime); if(TimeCurrent() >= timeClose && structTime.day_of_week == CloseTimeDayOfTheWeek) { Print("CLOSE ALL POSITIONS!"); Print("Current time: ",TimeCurrent()," >= ","Closing time: ",timeClose); for(int i = PositionsTotal()-1; i>=0; i--) { ulong posTicket = PositionGetTicket(i); if(PositionSelectByTicket(posTicket)) { Print("Position ticket: ",posTicket); if(trade.PositionClose(posTicket)) { Print(__FUNCTION__," > Position number",posTicket,"was closed!"); } } } Comment("\n Server Time", TimeCurrent(), "\nClose Time", timeClose); } } }
Thank you @amrali for the wisdom!
Forum on trading, automated trading systems and testing trading strategies
Only trading Mondays to Fridays
Colin Kimble, 2025.01.17 21:01
Hi! I coded in my EA that it should only trade on the dates between Monday and Friday, it keeps on returning the value 0 in my Print of the structTime.day, obviously Monday to Sunday ranges from numbers 0 to 6. My syntax is correct because upon compiling there is no error codes. Do you know where I might have gone wrong?
Ctrade trade; input ENUM_DAY_OF_WEEK CloseTimeDayOfTheWeek = -1;
void OnTick() { MqlDateTime structTime; TimeCurrent(structTime); structTime.day = CloseTimeDayOfTheWeek; Print("Day of the Week: ",structTime.day);
// Check overall conditions for placing a trade if(CloseTimeDayOfTheWeek > 0 && CloseTimeDayOfTheWeek < 6) { if(structTime.day_of_week == CloseTimeDayOfTheWeek) { static datetime prevTime=0; datetime lastTime[1]; if(CopyTime(_Symbol,PERIOD_CURRENT,0,1,lastTime)==1 && prevTime!=lastTime[0]) { prevTime=lastTime[0]; // (15/22 or higher) if(OneDayCalculationMA == 1 && OneDayCalculationOS > 0.15 && BuyLevel1 == 1 && BuyLevel2 == 1 && BuyLevel3 == 1 && BuyLevel4 == 1 && BuyLevel5 == 1 && BuyLevel6 == 1 && BuyLevel7 == 1) { double EnterLong = SymbolInfoDouble(_Symbol,SYMBOL_ASK); EnterLong = NormalizeDouble(EnterLong,_Digits); trade.Buy(Lots,NULL,EnterLong,StopLoss,NULL,"Enter Long"); } // Check conditions to enter a short position (-15/22 or higher) if(OneDayCalculationMA == -1 && OneDayCalculationOS < -0.15 && SellLevel1 == -1 && SellLevel2 == -1 && SellLevel3 == -1 && SellLevel4 == -1 && SellLevel5 == -1 && SellLevel6 == -1 && BuyLevel7 == -1) { double EnterShort = SymbolInfoDouble(_Symbol,SYMBOL_BID); EnterShort = NormalizeDouble(EnterShort,_Digits); trade.Sell(Lots,NULL,EnterShort,StopLoss,NULL,"Enter Short"); } } } } }

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
Hi! I added into my EA that my positions should close on a specific day of the week. I understand that the int values for each day of the week is as follows:
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 0
The code I made is as follows
This is my inputs in the global area.
This code is in the OnTick() area. When I run this code the print "Print("Day: ", structTime.day_of_week);" returns the value 7 constantly even though this value is set at 1 to signify that it will close all open positions at 23:00 on Monday. Does anyone know where I might have missed the mark?