With MQL4 I have troubles in handling datetime
.
What I want to do is put datetime
in array by month or by year.
For now I do in this way.
datetime myDate;
myDate[0] = D'2010.01.01 00:00';
myDate[1] = D'2010.02.01 00:00';
myDate[2] = D'2010.03.01 00:00';
myDate[3] = D'2010.04.01 00:00';
.
.
However I want to do this like this below
myDate[0] = D'2010.01.01 00:00';
for (int i = 1;i < 6 ;i+=){
myDate[i] = myDate[i - 1] + 1year;
}
in case of month,
myDate[0] = D'2010.01.01 00:00';
for (int i = 1; i < 12 ; i++){
myDate[i] = myDate[i - 1] + 1month
}
Q: How do I calculate adding 1month
or 1year
?
MQL4 documentation declared datetime
type to be internally represented as an amount of seconds since an agreed time-scale datum ( being 1970-01-01 00:00
).
This said ( and polishing a bit the syntax compliance )
the code
may read
oneYear = 60 * 60 * 24 * 365; // yes, astronomers would kill me
// for not solving those seconds,
// that sum up until a leap year
// consumes 'em on Feb-29th day :o)
another option
so as to manipulate
datetime
in a bit more
comfortable manner, addressing
datetime
's natural component is hacky, but worth:
StringToTime
string TimeToString( datetime aDatetimeVALUE,
int aModeOfDISPLAY = TIME_DATE|TIME_MINUTES
)
Converting a value containing time in seconds elapsed since 01.01.1970 into a
string
of"yyyy.mm.dd hh:mi"
format.
Here, one can simply add +1 to proper position of this intermediate format ( without a need to handle all the derived and influenced values as present in the struct MqlDateTime
, where day_of_week
and day_of_year
are definitely not my favourite ones to re-calculate once moving +1 month etc.
aCurrentYEAR = int( StringSubstr( aDatetimeSTRING, 0, 4 ) );
aCurrentMONTH = int( StringSubstr( aDatetimeSTRING, 5, 2 ) );
aCurrentDAY = int( StringSubstr( aDatetimeSTRING, 8, 2 ) );
aNextYEAR = aCurrentYEAR + 1;
aNextMONTH = aCurrentMONTH + 1;
Finally
StringFormat( "%04d.%02d.%02d 00:00", aYearNUMBER, aMonthNUMBER, aDayNUMBER )
will do the re-assembly for calling another MQL4 standard function:
datetime StringToTime( string aDatetimeSTRING )
The function converts a string containing time or date in
"yyyy.mm.dd [hh:mi]"
format intodatetime
type.
Another approach may use a fully-decomposed datetime
aritmetics by using
int aYE = TimeYear( aDatetimeVALUE );
int aMO = TimeMonth( aDatetimeVALUE );
int aDA = TimeDay( aDatetimeVALUE );
int aHO = TimeHour( aDatetimeVALUE );
int aMI = TimeMinute( aDatetimeVALUE );
int aDoW = TimeDayOfWeek( aDatetimeVALUE );
int aDoY = TimeDayOfYear( aDatetimeVALUE );
datetime aSameTimeNextYEAR = StructToTime( (MqlDateTime) { aYE + 1,
aMO,
aDA,
aHO,
aMI,
aDoW,
aDoY
}
);
Thanks I understood. There is a no simple way to add 1month, however this work-around is very helpful for me.
Glad to hear that. Do not hesitate to vote up on the helpfull answers and accept the best elaborated one. Enjoy being an active, contributing member of this great knowledge-focused Community!