简体   繁体   English

如何每天在mql5中运行一次此代码?

[英]How do I run this code once per day in mql5?

I have a cycle with Print(). 我有一个循环Print()。 This string should be printed once per day only. 该字符串应每天仅打印一次。 There is the code: 有代码:

int Hour = 20;
int Minute = 20;

int OnInit()
{
Hour = timeTemp.hour;
Minute = timeTemp.min;

EventSetTimer(60);
}

void OnTimer()
{

MqlDateTime ActivationTime;
TimeToStruct(TimeCurrent(), ActivationTime);

if (ActivationTime.hour == hour && ActivationTime.min == minute)
{
Print("This code should be printed once per day");
}
}

void OnDeinit(const int reason)
{ 
}

It has 2 issues: 它有2个问题:

1) When chart gets alot of ticks, it do more then one Print(). 1)当图表出现很多刻度时,它会执行多个操作,然后执行一个Print()。

2) When disconnect happens or just there is a end of trading session on current instrument, time in terminal freezes and this cycle run without ending untill it gets tics and time becomes unfreezed. 2)当断开连接发生或当前工具的交易时段即将结束时,终端中的时间冻结,并且该循环一直运行到结束为止,直到它变为可交易状态且时间变为冻结。

So how do I run this code once per day even if there is disconnect happens? 那么,即使发生断开连接,我如何每天运行一次此代码?

there is multiple ways to do that, I prefer this one for "one time per day" means 有多种方法可以实现,我更喜欢“每天一次”

string lastRunDate = null;
int Hour = 20;
int Minute = 20;

int OnInit()
{
  Hour = timeTemp.hour;
  Minute = timeTemp.min;

  EventSetTimer(60);
}

void OnTimer()
{

  MqlDateTime ActivationTime;
  TimeToStruct(TimeLocal(), ActivationTime);

  if (lastRunDate!=TimeToString(TimeCurrent(), TIME_DATE) && ActivationTime.hour == hour && ActivationTime.min == minute)
  {
    Print("This code should be printed once per day");
    // ... after your codes, `remember today` !
    lastRunDate = TimeToString(TimeCurrent(), TIME_DATE);
  }
}

void OnDeinit(const int reason) {
  EventKillTimer();
}
  • use TimeLocal() . 使用TimeLocal() if you only know times on your server, calculate time offset on OnInit function between TimeLocal and TimeCurrent (if market is not closed on init time.*) 如果您只知道服务器上的时间,请在OnInit函数上计算TimeLocalTimeCurrent之间的时间偏移(如果在开始时间市场未关闭)。

  • you can use TimeGMT() if you can define your conditions with GMT times. 如果可以使用GMT时间定义条件,则可以使用TimeGMT()

  • to detect closed market, you can check last incoming tick time. 要检测封闭市场,您可以检查最后一个传入的报价时间。 for forex pairs, you receive many ticks in every minute. 对于外汇交易对,您每分钟都会收到许多滴答声。 so, if last tick is older than one minute, market is closed. 因此,如果最后一个刻度超过1分钟,则市场关闭。 to check temporary internet disrupts, you can check last tick time with more than one minute trigger. 要检查互联网暂时中断,您可以使用超过一分钟的触发时间来检查上次滴答时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM