如何使用EA交易从MQL4获取当前毫秒数。
即:在Java中,我们可以使用system.currenttimemillis()
答案 0 :(得分:2)
这MT4"得到毫秒"问题已存在多年。这是我为解决这个问题而创建的一个黑客。
//+------------------------------------------------------------------+
//| 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();
}
}
答案 1 :(得分:1)
要小心,因为两者都是相对的,但是一个是关于系统启动的,另一个是关于MQL4代码执行单元的启动。
GetTickCount()
函数返回自系统启动以来经过的毫秒数。<强>
uint GetTickCount();
强>计数器受系统计时器限制的限制。时间存储为无符号整数,因此如果计算机不间断运行,则每49.7天就会过满。
GetMicrosecondCount()
函数返回自MQL程序启动以来经过的的微秒数。<强>
ulong GetMicrosecondCount();
强>
对于FOREX域名来说,这不是很好吗,毫秒是&#34;充满了事件&#34; 和微秒(近期专业级设计中的纳秒)很重要?!
// -----------------------------------------------------------------
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
);
}
答案 2 :(得分:0)
类似的东西:
ulong time = GetTickCount();
// function();
time = GetTickCount()-time;
答案 3 :(得分:0)
dt1 = TimeLocal()+2;
do
{
dt2 = TimeLocal();
}
while(TimeSecons(dt2) < TimeSecons(dt1));
完成后,您可以从0.000开始计时
答案 4 :(得分:0)
只需将值转换为 ulong
并确保将 TimeGMT()
与 1000
相乘
将打印结果转换为 string
:
ulong time = (ulong) TimeGMT()*1000 - (ulong) GetTickCount() ;
Print("milliseconds: ", (string)time);