mtwspczh 发表于 2016-9-26 21:16:39

关于OnTimer的用法

最近我搜索OnTimer的用法搜到这个帖子,里面提到OnTimer不能用到Indicator,只能用在EA.
http://www.520fx.com/thread-68514-1-1.html
实际上,目前的mql4已经可以把OnTimer用在Indicator, 我用的MetaEditor版本是5.0 build 1384。如果说不能在Indicator里面写OnTimer,可能有两个原因:
1) 可能旧版本不支持
2) 写程序的人习惯写成了onTimer,第一个字母没大写,结果找不到timer handler. (我犯过这个错误,因为以前写程序习惯把函数或变量的第一个字母小写)

我测试过以下代码段,证明OnTimer可以被击中:

void OnInit()
{
   bool result = EventSetTimer(1);
   Print("OnInit EventSetTimer result:", result);
}


void OnTimer()
{
   Print("OnTimer hit");
}


从帮助文件中也可看到



写在Indicator里面有一些好处,比如一个chart可以加载多个Indicators,却只能加载一个EA.

haoofz 发表于 2016-10-1 14:40:49

ontime做什么用?

mtwspczh 发表于 2016-10-1 20:35:16

haoofz 发表于 2016-10-1 14:40
ontime做什么用?

比如启动1秒的timer, 提供tick间隔不均匀或较大时的K线剩余时间倒数,确保在K线即将走完是判断,可掐好时间做数据行情

qw1282 发表于 2016-10-5 18:17:29

谢谢分享
页: [1]
查看完整版本: 关于OnTimer的用法