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) */
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.
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!
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(); } }
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
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 wrote an Expert Advisor and everything works well except for one function:
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!