For your desired close time calculate how many bars must elapse - divide the desired duration (in seconds) by PeriodSeconds() to get this number. That should span weekends
It does not span weekends. That assumes that all bars exist — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
"Free-of-Holes" Charts - MQL4 Articles (2006)
No candle if open = close ? - MQL4 programming forum (2010)
Use iBarShift.
You likely do not want one (1) day. You want one trading day. Use iBarShift
It does not span weekends. That assumes that all bars exist — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
"Free-of-Holes" Charts - MQL4 Articles (2006)
No candle if open = close ? - MQL4 programming forum (2010)
Use iBarShift.
You likely do not want one (1) day. You want one trading day. Use iBarShift
Ah yes that's a better way, I had assumed the bars exist and ticks would be occuring - iBarshift sounds like the best solution
Ah yes that's a better way, I had assumed the bars exist and ticks would be occuring - iBarshift sounds like the best solution
It does not span weekends. That assumes that all bars exist — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
"Free-of-Holes" Charts - MQL4 Articles (2006)
No candle if open = close ? - MQL4 programming forum (2010)
Use iBarShift.
You likely do not want one (1) day. You want one trading day. Use iBarShift
The documentation of the IBarShif states the folliwing:
iBarShift
Search bar by time. The function returns the index of the bar corresponding to the specified time.
In this case I shall specify the time as an input parameter. But this is my original question. How do I determine the time +1d in the active market phase, skipping off days and weekends.
What I mean by that: buying friday 12:00, time elapsed monday 12:00. Here the time span would be 1 day in the active market. Stupid workaround for fridays:
int openTime = PositionGetInteger(POSITION_TIME); // check if open position is open too long => if yes close it int time = TimeCurrent(); // if the opening was on friday, close only on monday MqlDateTime openDate; int weekendFactor = 0; TimeToStruct(openTime, openDate); // opening was on friday, allow +2 days before closing if(openDate.day_of_week == 5) { weekendFactor = 172800; // 2 days } ZeroMemory(openDate); if(openTime > 0 && inpCheckMaxTradeDuration > 0 && ((openTime + (inpCheckMaxTradeDuration * 86400) + weekendFactor) < TimeCurrent()))// close open position
I can't really say about iBarShift without doing some testing, but maybe there is an easier way.
If you simply check if 24hrs has passed, even if the order is placed on a Fri this will be true on Mon, so do you really need to code for weekend/holiday gaps?
If not, then it is simply a matter of then checking hh:mm:ss to see if your desired time window has expired - I attach a quick and dirty (untested) example to illustrate
datetime openTime = (datetime)PositionGetInteger(POSITION_TIME); if((openTime + 86400) < TimeCurrent()) //check if 24 hrs has passed - true even after hols/weekends { MqlDateTime openTimeStruc; MqlDateTime currTimeStruc; TimeToStruct(openTime, openTimeStruc); TimeToStruct(TimeCurrent(), currTimeStruc); if((currTimeStruc.hour >= openTimeStruc.hour) && (currTimeStruc.min >= openTimeStruc.min) && (currTimeStruc.sec >= openTimeStruc.sec)) { //execute closure code } }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Dear all,
I am trying to limit the duration of my trade in my ea. In this example to 1 day.
This works fine within one working week. But after the weekend, as You can see, it will close trades immediately on monday morning.
Do You know a possibility in mql5 to find out the time, that the trade was active in an open market, so the algo is correct on Christmas etc.
Thanks and best