如何在 MQL4 中计算(添加)日期时间值?

How to calculate (add) datetime values in MQL4?

使用 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
}

问:如何计算添加1month1year?

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 的自然成分是 hacky,但值得: 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.

在这里,可以简单地将 +1 添加到此中间格式的适当位置(无需处理 struct MqlDateTime 中存在的所有派生值和影响值,其中 day_of_weekday_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 )

The function converts a string containing time or date in "yyyy.mm.dd [hh:mi]" format into datetime type.

另一种方法可以通过使用

来使用完全分解的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
                                                           }
                                           );
int month = PeriodSeconds(PERIOD_MONTH);

for year - 不确定你是否可以使用一些常量,但看起来 12 个月没问题,另一种选择是使用 MqlDateTime 分配年份然后使用 [=13 转换为 datetime =];

更新: 月份不是一个好方法,因为每个月的秒数都不同

我附上了说明循环的脚本。

基本逻辑:

  1. 将 31 天添加到定义的日期时间
  2. 获取新月份,忽略具体日期
  3. 将新日期生成为带有新月份的字符串
  4. 将字符串转换为新的日期时间
  5. 这个新日期时间添加 31 天
  6. 从 2 开始重复

    void OnStart(){

       datetime myDate[6];
       myDate[0] = D'2010.01.01 00:00';
       Print(TimeToStr(myDate[0]));

       datetime dTempDate = myDate[0];
       int iSecondsIn31Days = 24*60*60*31, iTempMonth = 0;

       for(int i=1; i<6; i++){
          iTempMonth = TimeMonth(dTempDate + iSecondsIn31Days); //add 31 days and get the month, we don't care whether the day is the 1st, 2nd, or 3rd, we just want the month
          myDate[i] = dTempDate = StrToTime(TimeYear(myDate[0]) +"." +iTempMonth +"." +TimeDay(myDate[0])); //format the string to datetime
          Print(TimeToStr(myDate[i])); //to verify
       }

    }

你需要使用的方法

MqlDateTime、StructToTime、TimeToStruct

您可以使用这些

以更少的步骤完成您的工作

使用 MqlDateTimeTimeToStructStructToTime 结构如下:

MqlDateTime time;
TimeToStruct(TimeCurrent(),time);

time.year++;
time.mon++;
datetime newTime = StructToTime(time);