使用MQL4时,我在处理方面遇到了麻烦datetime
。
我想做的是datetime
按月或按年排列。
现在我以这种方式做。
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';
.
.
但是我想在下面这样
myDate[0] = D'2010.01.01 00:00';
for (int i = 1;i < 6 ;i+=){
myDate[i] = myDate[i - 1] + 1year;
}
如果是一个月,
myDate[0] = D'2010.01.01 00:00';
for (int i = 1; i < 12 ; i++){
myDate[i] = myDate[i - 1] + 1month
}
问:如何计算加法1month
或1year
?
MQL4文档声明的datetime
类型在内部表示为经过约定的时间刻度基准数据(秒)的秒数1970-01-01 00:00
。
这段代码可以读懂(并稍微完善了语法)
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)
为了以
datetime
更
舒适的方式进行操作的另一种选择,解决
datetime
的自然成分是很棘手的,但值得:StringToTime
string TimeToString( datetime aDatetimeVALUE,
int aModeOfDISPLAY = TIME_DATE|TIME_MINUTES
)
将包含自1970年1月1日起经过的时间(以秒为单位)的值转换
string
为"yyyy.mm.dd hh:mi"
格式。
在这里,人们可以简单地添加+1这个中间格式(适当的位置,而不需要处理所有的衍生和影响的数值为存在于struct MqlDateTime
其中day_of_week
而day_of_year
绝对不是我喜欢的人重新计算一次移动1个月等。
aCurrentYEAR = int( StringSubstr( aDatetimeSTRING, 0, 4 ) );
aCurrentMONTH = int( StringSubstr( aDatetimeSTRING, 5, 2 ) );
aCurrentDAY = int( StringSubstr( aDatetimeSTRING, 8, 2 ) );
aNextYEAR = aCurrentYEAR + 1;
aNextMONTH = aCurrentMONTH + 1;
最后
StringFormat( "%04d.%02d.%02d 00:00", aYearNUMBER, aMonthNUMBER, aDayNUMBER )
将进行重组以调用另一个MQL4标准函数:
datetime StringToTime( string aDatetimeSTRING )
该函数将
"yyyy.mm.dd [hh:mi]"
格式为时间或日期的字符串转换为datetime
类型。
另一种方法可以通过使用完全分解的datetime
算术
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
}
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句