3

如何使用智能交易系统从 MQL4 获取当前毫秒数。

即:在Java中,我们可以使用当前毫秒system.currenttimemillis()

4

5 回答 5

2

可以有相对 [ms] 甚至 [us]:

请小心,因为两者都是相对的,但一个与系统启动有关,另一个与 MQL4 代码执行单元启动有关。

该函数返回自系统启动以来GetTickCount()经过的毫秒数。

uint GetTickCount();

计数器受系统定时器的限制。时间存储为无符号整数,因此如果计算机不间断地工作,它每 49.7 天就会溢出一次。


GetMicrosecondCount()函数返回自 MQL 程序启动以来经过的微秒数。

ulong GetMicrosecondCount();


可以有绝对 [ms] 甚至 [us],带有 const(!) ABSOLUTE ERROR,

在精确的时间测量中既不会出现任何漂移,也不会出现抖动。

这对 FOREX 领域来说不是很好吗,毫秒是“充满事件”和微秒(最近的专业级设计中的纳秒)很重要?!

// -----------------------------------------------------------------
ulong system_currenttimemillis(){
      return(   OnStart_GLOB_MILLISECONDS       // ABS [ms] SYNC-ed OnStart() WITH [s]-EDGE-ALIGNMENT
            + (           GetMicrosecondCount() // + DELTA ------------------
              - OnStart_BASE_MICROSECONDS       //   since SYNC-ing OnStart()
                ) / 1000 // =================== //   DELTA [ms] =============
              );
}
// -----------------------------------------------------------------
static ulong    OnStart_GLOB_MICROSECONDS;
static ulong    OnStart_GLOB_MILLISECONDS;
static ulong    OnStart_EoDY_MICROSECONDS;
static datetime OnStart_EoDY_DATETIME;
static datetime OnStart_BASE_DATETIME;
static uint     OnStart_BASE_MILLISECONDS;
static ulong    OnStart_BASE_MICROSECONDS;
// -----------------------------------------------------------------
void            OnStart(){ /* { SCRIPT | EXPERT ADVISOR | CUSTOM INDICATOR } CALL
                                                                             THIS */
                OnStart_BASE_DATETIME      = TimeLocal();           // .SET int == the number of seconds elapsed since January 01, 1970.
         while( OnStart_BASE_DATETIME     == TimeLocal() ){ // ---- // EDGE-ALIGNMENT -------------------------------------------------------
                OnStart_BASE_MICROSECONDS  = GetMicrosecondCount(); //      .SET ulong, since MQL4 program launch
                OnStart_BASE_MILLISECONDS  = GetTickCount();        //      .SET uint,  since system start
         } // ==[ MAX 1 SECOND ]=============================== NOW // EDGE-ALIGNED TO [s] ==================================================
                OnStart_BASE_DATETIME      = TimeLocal();           // .SET date and time as the number of seconds elapsed since January 01, 1970.
                OnStart_GLOB_MICROSECONDS  = (       (ulong) OnStart_BASE_DATETIME ) * 1000000;
                OnStart_GLOB_MILLISECONDS  = (       (ulong) OnStart_BASE_DATETIME ) * 1000;
                OnStart_EoDY_DATETIME      =                 OnStart_BASE_DATETIME
                                           - (               OnStart_BASE_DATETIME % 86400 );
                OnStart_EoDY_MICROSECONDS  = (   TimeSecond( OnStart_BASE_DATETIME )
                                             + ( TimeMinute( OnStart_BASE_DATETIME )
                                               + TimeHour(   OnStart_BASE_DATETIME ) * 60 ) * 60 ) * 1000000;
}
// -----------------------------------------------------------------
int OnInit()    {
    OnStart();             /*   HACK 4 { EXPERT ADVISOR | CUSTOM INDICATOR } CALL
    ...                                                                      THAT */
    ..
    return( INIT_SUCCEEDED );
}
// -----------------------------------------------------------------
ulong Get_a_Microsecond_of_a_Day(){           // THIS HAS A !!_CONSTANT_!! ONLY ABSOLUTE SYSTEMATIC TIMING ERROR
      return( (   OnStart_EoDY_MICROSECONDS   // EDGE-SYNC OnStart_EoDY + DELTA-SINCE-OnStart-SYNC-ed:
              + (           GetMicrosecondCount()                       // == NOW ( 8B ulong ) ROLL-OVER ~ 213M504 DAYS AFTER THE PROGRAM START, WAY LONGER, THAN WE WILL LIVE UNDER THE SUN
                - OnStart_BASE_MICROSECONDS   //                        //  - OnStart_BASE_MICROSECONDS
                  )
                ) // ================== // SECONDS-EDGE-SYNC-ed DISTANCE FROM EoDY-EDGE
              % 86400000000             // MODULO DAY-LENGTH ROLL-OVER
              );                        // ALL DST-MOVs TAKE PLACE OVER WEEKENDS, SO NOT DURING TRADING-HOURS, SHOULD BE JUST-ENOUGH GOOD SOLUTION :o)
}
// -----------------------------------------------------------------
uint Get_a_Millisecond_of_a_Day(){      // IMMUNE TO A uint ROLL-OVER ~ 49.7 DAYS
     return( Get_a_Microsecond_of_a_Day()
           / 1000
             );
}

+ 此解决方案在所有 { Script | 专家顾问 | 自定义指标 }

于 2017-05-23T14:41:36.373 回答
2

这个 MT4“获取毫秒”问题已经存在了很长时间。这是我为解决此问题而创建的 hack。

//+------------------------------------------------------------------+
//|                                                     timeInMs.mq4 |
//|                                       Copyright 2017, Joseph Lee |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Joseph Lee"
#property link      "https://www.facebook.com/joseph.fhlee"
#property version   "1.00"
#property strict

int     prevSecondTime      = 0;
uint    prevSecondTick      = 0;


int OnInit()    {
    //Create an Event that triggers every 1 millisecond.
    // **NOTE: GetTickCount() is accurate to +-16ms only, so
    // in practice, no need to trigger every 1ms.
    EventSetMillisecondTimer(1);
    return(INIT_SUCCEEDED);
}

void OnTick() {
    Comment( "Now: " + TimeLocal() + " :: " + getCurrentMs() + " ms. +- 16ms accuracy.");
}

int getCurrentMs() {
    return(GetTickCount() - prevSecondTick);
}

//This is an EVENT function that will be called every
// x millisecond(s) [as stated in teh EventSetMillisecondTimer()
// in the OnInit()
void OnTimer() {
    //If a new "second" occurs, record down the GetTickCount()
    if(TimeLocal() > prevSecondTime) {
        prevSecondTick  = GetTickCount();
        prevSecondTime  = TimeLocal();
    }
}
于 2017-06-09T18:25:15.140 回答
0
dt1 = TimeLocal()+2;
do
{
  dt2 = TimeLocal();
}
while(TimeSecons(dt2) < TimeSecons(dt1));

完成后,您可以从 0.000 开始计算时间

于 2019-08-29T15:48:50.007 回答
0

something like this:

ulong time = GetTickCount(); // function(); time = GetTickCount()-time;

于 2017-05-20T12:04:49.960 回答
0

只需将值转换为并ulong确保乘以TimeGMT()1000

打印结果转换为string

ulong time = (ulong) TimeGMT()*1000 - (ulong) GetTickCount() ; 
Print("milliseconds: ",  (string)time);
于 2021-05-05T04:46:26.163 回答