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

 

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

 

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.

 

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

 
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...

 
honest_knave: Because sometimes you don't have 86400 seconds in a day? Those pesky leap seconds...
  1. 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.])

  2. while(!download_history(PERIOD_D1) ){ Sleep 1000; RefreshRates(); }
    int DaysOpen = iBarShift(_Symbol, PERIOD_D1, OrderOpenTime() );


 
whroeder1:
  1. 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).


 

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

Even ThinkOrSwim has that feature...

 
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.

 
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..

 
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
      }
 
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.
Reason: