//+------------------------------------------------------------------+ //| OnTimer_Sample.mq5 | //| Copyright 2018, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2000-2024, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #property description "거래 서버 시간 계산에 타이머 사용 예제" #property description "EA는 주말 전에 거래 주 말에 실행하는 것이 좋습니다." //+------------------------------------------------------------------+ //| Expert 초기화 함수 | //+------------------------------------------------------------------+ int OnInit() { //--- 1초의 타이머를 생성 EventSetTimer(1); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert 초기화 해제 함수 | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- 작업 종료 후 타이머 제거 EventKillTimer(); } //+------------------------------------------------------------------+ //| Expert 틱 함수 | //+------------------------------------------------------------------+ void OnTick() { //--- } //+------------------------------------------------------------------+ //| 타이머 함수 | //+------------------------------------------------------------------+ void OnTimer() { //--- OnTimer()의 첫 호출 시간 static datetime start_time=TimeCurrent(); //--- 첫 번째 OnTimer() 호출 중 트레이드 서버 시간 static datetime start_tradeserver_time=0; //--- 거래 서버 시간 계산 static datetime calculated_server_time=0; //--- 현지 PC 시간 datetime local_time=TimeLocal(); //--- 현재 예상 거래 서버 시간 datetime trade_server_time=TimeTradeServer(); //--- 어떤 이유로 서버 시간을 알 수 없는 경우 미리 종료 if(trade_server_time==0) return; //--- 초기 트레이드 서버 값이 아직 설정되지 않은 경우 if(start_tradeserver_time==0) { start_tradeserver_time=trade_server_time; //--- 거래 서버의 계산값 설정 Print(trade_server_time); calculated_server_time=trade_server_time; } else { //--- OnTimer()의 첫 호출 시간을 증가시킴 if(start_tradeserver_time!=0) calculated_server_time=calculated_server_time+1;; } //--- string com=StringFormat(" Start time: %s\r\n",TimeToString(start_time,TIME_MINUTES|TIME_SECONDS)); com=com+StringFormat(" Local time: %s\r\n",TimeToString(local_time,TIME_MINUTES|TIME_SECONDS)); com=com+StringFormat("TimeTradeServer time: %s\r\n",TimeToString(trade_server_time,TIME_MINUTES|TIME_SECONDS)); com=com+StringFormat(" EstimatedServer time: %s\r\n",TimeToString(calculated_server_time,TIME_MINUTES|TIME_SECONDS)); //--- 차트에 모든 카운터 값 표시 Comment(com); } |