簡體   English   中英

如何在 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
}

問:如何計算添加 1 1month1year

使用MqlDateTimeTimeToStructStructToTime結構,如下所示:

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

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

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
                    )

將包含自 01.01.1970 以來經過的時間(以秒為單位)的值轉換為"yyyy.mm.dd hh:mi"格式的string

在這里,可以簡單地將 +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 )

該函數將"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
                                                           }
                                           );
int month = PeriodSeconds(PERIOD_MONTH);

年份 - 不確定您是否可以使用一些常數,但看起來 12 個月還可以,另一種選擇是使用MqlDateTime分配年份,然后使用StructToTime()轉換為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

您可以使用這些以更少的步驟完成工作

老問題,但我有同樣的問題。

由於它以秒為單位在內部存儲,因此只需添加適當的秒數。 就是這么簡單....

   datetime date_from = TimeCurrent();
   // this adds two days from now (60*60 = 1 Hour) * (24 hours = 1 Day) * (2 = two days)
   date_from += 60*60*24*2;
   
   Comment(date_from);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM