# How many days gone after ... Time[i] ?

254

I a on XYZ minute timeframe chart.

how can I:

1) calculate how many days gone from Time[xyz] ?

2) calculate how many days gone from first bar of chart

Moderator
2293

There are 86400 seconds in a day.

Compare the timestamp of the bar you are interested in and the current time.

Divide the difference by 86400 to get days.

254

oh,  .... this is  the way no-one should rely on...

Moderator
2293

selnomeria:

oh,  .... this is  the way no-one should rely on...

Because sometimes you don't have 86400 seconds in a day? Those pesky leap seconds...

16539

 honest_knave: Because sometimes you don't have 86400 seconds in a day? Those pesky leap seconds... Exactly "because you don't have 86400 seconds in some days!" FX opens 5pm ET Sunday and ends 5pm ET Friday. Some brokers start after (6pm is common/end before (up to 15 minutes) due to low volatility.Swap is computed 5pm ET. No swap if no open orders at that time.Brokers use a variety of timezones. Their local time (with or without DST,) GMT/UTC, GMT+2, NY+7.Only with NY+7 does the broker's 00:00 equals 5pm ET and the start of a daily bar is the start of a new FX day.GMT brokers, means there is a 1 or 2 hour D1/H4 bar on Sunday (depending on NY DST,) and a short Friday bar.GMT+2 is close but doesn't adjust for NY DST.EET is closer except when their DST doesn't match NY's. Last Sunday of March and 1:00 on the last Sunday of October vs second Sunday in March and return at 2:00 a.m. EDT to 1:00 a.m. EST on the first Sunday in November.Non-NY+7, means the chart daily bar overlaps the start, and converting broker time to NY time requires broker to GMT to NY timezone conversions. If you search the web you will find differing answers. Those are all wrong (half the year) because they do not take NY DST into account (or that it changed in 2007 [important when testing history.]) ```while(!download_history(PERIOD_D1) ){ Sleep 1000; RefreshRates(); } int DaysOpen = iBarShift(_Symbol, PERIOD_D1, OrderOpenTime() );```
Moderator
2293

whroeder1:
 Exactly "because you don't have 86400 seconds in some days!" FX opens 5pm ET Sunday and ends 5pm ET Friday. Some brokers start after (6pm is common/end before (up to 15 minutes) due to low volatility.Swap is computed 5pm ET. No swap if no open orders at that time.Brokers use a variety of timezones. Their local time (with or without DST,) GMT/UTC, GMT+2, NY+7.Only with NY+7 does the broker's 00:00 equals 5pm ET and the start of a daily bar is the start of a new FX day.GMT brokers, means there is a 1 or 2 hour D1/H4 bar on Sunday (depending on NY DST,) and a short Friday bar.GMT+2 is close but doesn't adjust for NY DST.EET is closer except when their DST doesn't match NY's. Last Sunday of March and 1:00 on the last Sunday of October vs second Sunday in March and return at 2:00 a.m. EDT to 1:00 a.m. EST on the first Sunday in November.Non-NY+7, means the chart daily bar overlaps the start, and converting broker time to NY time requires broker to GMT to NY timezone conversions. If you search the web you will find differing answers. Those are all wrong (half the year) because they do not take NY DST into account (or that it changed in 2007 [important when testing history.])

All quite useful, however the OP talked about days, not trading days. The latter has a plethora of meanings. The former is rather definite (leap seconds to one side).

254

I didnt know if such simple thing was so hard in MT to achieve...

Even ThinkOrSwim has that feature...

16539

honest_knave: All quite useful, however the OP talked about days, not trading days. The latter has a plethora of meanings. The former is rather definite (leap seconds to one side).

True but what good is days? Orders kept over weekend or market holidays, shouldn't be treated differently.

Because of the problems with broker TZs mentioned above, what I would to is get iBarShift That gives elapsed bars, and for days multiply by _Period/PERIOD_D1.

254

whroeder1:

True but what good is days? Orders kept over weekend or market holidays, shouldn't be treated differently.

Because of the problems with broker TZs mentioned above, what I would to is get iBarShift That gives elapsed bars, and for days multiply by _Period/PERIOD_D1.

no, i just use that variable in calculations... When NewDay comes, then I save a new value in array, then I calculate something from this array... and I needed the exact solution.

for example, when NewDay starts, I want to draw horizontal line on the low of that bar, till the next day..

20169

selnomeria:

no, i just use that variable in calculations... When NewDay comes, then I save a new value in array, then I calculate something from this array... and I needed the exact solution.

for example, when NewDay starts, I want to draw horizontal line on the low of that bar, till the next day..

for MT4

```if (day!=Day())
{
day=Day();
//new day
}```

for MT5

```MqlDateTime dt_struct;
TimeToStruct(TimeCurrent(),dt_struct);

if (day!=dt_struct.day)
{
day=dt_struct.day;
//new day
}```
16539

 selnomeria: no, i just use that variable in calculations... When NewDay comes, then I save a new value in array, then I calculate something from this array... and I needed the exact solution. for example, when NewDay starts, I want to draw horizontal line on the low of that bar, till the next day.. If it's new day, save the value in the buffer, otherwise copy the value from the previous bar.