# datetime calculations

1144

Is there a way to get an open time of a weekly bar for instance, given a day and time on that week?

for instance, if I have the date D'2010.03.18 11:23' and I want to get the open date of that week, or that month and then feeding this date

to COPYHIGH to get the weekly high, but the resulting date does not fit the COPYHIGH to copy the high of the weekly bar of that week because it is not a starting of a week.

NextDayTime =D'2010.03.18 11:23' ;

NextDayTime = time[i] - 7 * 24 * 60 * 60;
TimeToStruct(NextDayTime,BarTime);
BarTime.hour = 0;
BarTime.min = 0;
BarTime.sec = 0;
BarTime.day_of_week = MONDAY;
NextDayTime = StructToTime(BarTime);

but the resulting date would be '2010.03.11 00:00' (which is thursday) without taking the monday into account. Feeding that date to the copyhigh, trying to get the high of the week, not working.

Anyone can tell how to perform date & time calculations?

Also, for subtructing for instance one month from a date, any solution? I tried date - 30 * 24 * 60 * 60, is this the way?

Thanks.

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Position Properties
• www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Position Properties - Documentation on MQL5
784

amir_avatar posted # :

Is there a way to get an open time of a weekly bar for instance, given a day and time on that week?

for instance, if I have the date D'2010.03.18 11:23' and I want to get the open date of that week, or that month and then feeding this date

to COPYHIGH to get the weekly high, but the resulting date does not fit the COPYHIGH to copy the high of the weekly bar of that week because it is not a starting of a week.

NextDayTime =D'2010.03.18 11:23' ;

NextDayTime = time[i] - 7 * 24 * 60 * 60;
TimeToStruct(NextDayTime,BarTime);
BarTime.hour = 0;
BarTime.min = 0;
BarTime.sec = 0;
BarTime.day_of_week = MONDAY;
NextDayTime = StructToTime(BarTime);

but the resulting date would be '2010.03.11 00:00' (which is thursday) without taking the monday into account. Feeding that date to the copyhigh, trying to get the high of the week, not working.

Anyone can tell how to perform date & time calculations?

Also, for subtructing for instance one month from a date, any solution? I tried date - 30 * 24 * 60 * 60, is this the way?

Thanks.

I would write it like this, although I haven't tested it

datetime dtArr[];
CopyTime(PERIOD_W1,dtDate,1,dtArr); // dtDate is the date during the week in question
datetime dtStartOfWeek=dtArr[0];

Paul

1144

phampton posted # :

I would write it like this, although I haven't tested it

Paul

The problem is that for CopyTime the date parameter dtDate should be exact start of a week bar. It will not accept a date like D'2010.03.18 11:23' .
784

amir_avatar posted # :
The problem is that for CopyTime the date parameter dtDate should be exact start of a week bar. It will not accept a date like D'2010.03.18 11:23' .

Ah!  So this goes back to the reason for your code - now I understand.  I would have thought that MONDAY should work, but I do note that this is of type ENUM_DAY_OF_WEEK, whereas mqltime.day_of.week is of type int.  Have you tried making it equal to 1?

Paul

1144

phampton posted # :

Ah!  So this goes back to the reason for your code - now I understand.  I would have thought that MONDAY should work, but I do note that this is of type ENUM_DAY_OF_WEEK, whereas mqltime.day_of.week is of type int.  Have you tried making it equal to 1?

Paul

It's the same. I don't think there's a straight forward way. Probably should find the difference between mqltime.day_of_week and MONDAY and reduce those days from the date.

In order to reduce one day from a date, is this the way?

dtDate = dtDate - 24 * 60 * 60,

or is there another way?

784

amir_avatar posted # :

It's the same. I don't think there's a straight forward way. Probably should find the difference between mqltime.day_of_week and MONDAY and reduce those days from the date.

In order to reduce one day from a date, is this the way?

dtDate = dtDate - 24 * 60 * 60,

or is there another way?

Here's some code that works.  I learnt a bit about date handling as a result, so thanks for that.

Paul

void OnStart()
{
datetime dtDate=D'2010.03.18 11:23';
Print("Monday start of week for",TimeToString(dtDate,TIME_DATE|TIME_SECONDS),"is",TimeToString(MondayStartOfWeek(dtDate),TIME_DATE|TIME_SECONDS));
}

datetime MondayStartOfWeek(datetime dtDuringWeek)
{
const int OneDayInSeconds=24*60*60;
int nDaysSince1970=((int)dtDuringWeek)/OneDayInSeconds;
MqlDateTime mdt;
TimeToStruct(dtDuringWeek,mdt);
// subtract back to Monday
return((nDaysSince1970-mdt.day_of_week+1)*OneDayInSeconds);
}

1578

phampton:

Here's some code that works.  I learnt a bit about date handling as a result, so thanks for that.

Paul

Thanks for your code. I've learned a bit about date handling too..

You can also add and substract int values from all fields in MqlDateTime, then convert back to datetime with StructToTime function. MT5 will take care and will calculate the resulting datetime.

datetime t;
MqlDateTime mdt;
TimeToStruct(t,mdt);

mdt.day += 45;
//or mdt.day = 50; //50 days from the beginning of the month
//or mdt.hour += 134; //134 hours in the future
//or mdt.hour = 1000; //1000 hours from the beginning of the day

t = StructToTime(mdt);
Print(TimeToString(t,TIME_DATE | TIME_MINUTES));
Documentation on MQL5: Date and Time / StructToTime
• www.mql5.com
Date and Time / StructToTime - Documentation on MQL5
7072
Alexander Piechotta

Difference between two dates calculated?

D'2010.03.18 11:23' and D'2010.03.20 23:50'