识别新K线的出现在EA程序中是非常重要的功能,K线的是整个交易市场,趋势判断的主要依据。识别和分析K线的意义也是交易的基础。
自定义方法
- datetime timelast,timenow;
复制代码
- bool isNewK(){
- bool isnewk=false;
- timenow=Time[0];
- if(timenow!=timelast){
- timelast=Time[0];
- isnewk=true;
- }
- else isnewk=false;
- return isnewk;
- }
复制代码
EA实例
- //+------------------------------------------------------------------+
- //| NewK.mq4 |
- //| Copyright 2021, MetaQuotes Software Corp. |
- //| https://www.mql5.com |
- //+------------------------------------------------------------------+
- #property copyright "Copyright 2021, MetaQuotes Software Corp."
- #property link "https://www.mql5.com"
- #property version "1.00"
- #property strict
-
- datetime timelast,timenow;
-
- //+------------------------------------------------------------------+
- //| Expert initialization function |
- //+------------------------------------------------------------------+
- int OnInit()
- {
- //--- create timer
- timelast=Time[0];
- timenow=Time[0];
- //---
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Expert deinitialization function |
- //+------------------------------------------------------------------+
- void OnDeinit(const int reason)
- {
- //--- destroy timer
-
-
- }
- //+------------------------------------------------------------------+
- //| Expert tick function |
- //+------------------------------------------------------------------+
- void OnTick()
- {
- //---
- if(isNewK())printf("新的K线出现!");
- }
-
- bool isNewK(){
- bool isnewk=false;
- timenow=Time[0];
- if(timenow!=timelast){
- timelast=Time[0];
- isnewk=true;
- }
- else isnewk=false;
- return isnewk;
- }
复制代码
执行效果: 一分钟周期图表加载之后的执行效果:新K线出现时打印一次:新的K线出现!
|