【MT4指标使用】显示当前K线剩余时间

2
回复
9211
查看
[复制链接]

95

主题

18

回帖

515

积分

高级会员

积分
515
来源: 2019-7-25 18:36:42 显示全部楼层 |阅读模式
本帖最后由 geek001 于 2019-7-25 18:39 编辑

该指标的作用是显示当前周期K线走完剩余的时间。

我们在交易的时候经常要要关注什么时候收当前周期的K线,但是一般来说都是预估值,该指标把当前周期K线的剩余时间在图表中运用明显的数字标识出来。另外通过双击可以随便移动该显示的位置。

K线剩余时间.png

下面是本策略的源码:


  1. //+------------------------------------------------------------------+
  2. //|                                                   geekquant-KTime.mq4 |
  3. //|                                          Copyright 2019,geek001. |
  4. //|                            http://www.geekquant.com/forum.php?mod=viewthread&tid=307&extra=|
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2019,geek001."
  7. #property link      "http://www.geekquant.com/forum.php?mod=viewthread&tid=307&extra="
  8. #property version   "1.00"
  9. #property strict
  10. #property indicator_chart_window
  11. #define  OBJ_NAME "time_left_label"
  12. #define  FONT_NAME "Microsoft YaHei"
  13. //+------------------------------------------------------------------+
  14. //|                                                                  |
  15. //+------------------------------------------------------------------+
  16. enum ENUM_POS_TL
  17.   {
  18.    FOLLOW_PRICE,
  19.    FIXED_POSITION
  20.   };

  21. input color  LabelColor=clrOrangeRed;
  22. input ENUM_POS_TL LabelPosition=1;
  23. //+------------------------------------------------------------------+
  24. //| Custom indicator initialization function                         |
  25. //+------------------------------------------------------------------+
  26. int OnInit()
  27.   {
  28. //--- indicator buffers mapping  
  29.    EventSetTimer(1);
  30. //---
  31.    return(INIT_SUCCEEDED);
  32.   }
  33. //+------------------------------------------------------------------+
  34. //|                                                                  |
  35. //+------------------------------------------------------------------+
  36. void OnDeinit(const int reason)
  37.   {
  38.    EventKillTimer();
  39.    if(ObjectFind(0,OBJ_NAME)==0)
  40.       ObjectDelete(0,OBJ_NAME);

  41.   }
  42. //+------------------------------------------------------------------+
  43. //| Custom indicator iteration function                              |
  44. //+------------------------------------------------------------------+
  45. int OnCalculate(const int rates_total,
  46.                 const int prev_calculated,
  47.                 const datetime &time[],
  48.                 const double &open[],
  49.                 const double &high[],
  50.                 const double &low[],
  51.                 const double &close[],
  52.                 const long &tick_volume[],
  53.                 const long &volume[],
  54.                 const int &spread[])
  55.   {
  56. //---

  57. //--- return value of prev_calculated for next call
  58.    return(rates_total);
  59.   }
  60. //+------------------------------------------------------------------+
  61. //| Timer function                                                   |
  62. //+------------------------------------------------------------------+
  63. void OnTimer()
  64.   {
  65. //---
  66.    UpdateTimeLeft();
  67.   }
  68. //+------------------------------------------------------------------+
  69. //|                                                                  |
  70. //+------------------------------------------------------------------+
  71. void UpdateTimeLeft()
  72.   {

  73.    int seconds=0;// the left seconds of the current bar
  74.    int h = 0; //Hour
  75.    int m = 0; //Minute
  76.    int s = 0; //Second  hh:mm:ss   

  77.    datetime time  = iTime(Symbol(),PERIOD_CURRENT,0);
  78.    double   close = iClose(Symbol(),PERIOD_CURRENT,0);

  79.    seconds=PeriodSeconds(PERIOD_CURRENT) -(int)(TimeCurrent()-time);

  80.    h = seconds/3600;
  81.    m = (seconds - h*3600)/60;
  82.    s = (seconds - h*3600 - m*60);

  83.    string text="                        >>> "
  84.                +StringFormat("%02d",h)+":"
  85.                +StringFormat("%02d",m)+":"
  86.                +StringFormat("%02d",s);

  87.    if(LabelPosition==FOLLOW_PRICE)
  88.      {
  89.       if(ObjectFind(0,OBJ_NAME)!=0)
  90.         {
  91.          ObjectCreate(0,OBJ_NAME,OBJ_TEXT,0,time,close+_Point);
  92.          ObjectSetString(0,OBJ_NAME,OBJPROP_TEXT,text);
  93.          ObjectSetString(0,OBJ_NAME,OBJPROP_FONT,FONT_NAME);
  94.          ObjectSetInteger(0,OBJ_NAME,OBJPROP_COLOR,LabelColor);
  95.          ObjectSetInteger(0,OBJ_NAME,OBJPROP_SELECTABLE,false);
  96.          ObjectSetInteger(0,OBJ_NAME,OBJPROP_FONTSIZE,12);

  97.         }
  98.       else
  99.         {
  100.          ObjectSetString(0,OBJ_NAME,OBJPROP_TEXT,text);
  101.          ObjectMove(0,OBJ_NAME,0,time,close+_Point);
  102.         }
  103.      }
  104.    else if(LabelPosition==FIXED_POSITION)
  105.      {
  106.       if(ObjectFind(0,OBJ_NAME)!=0)
  107.         {
  108.          ObjectCreate(0,OBJ_NAME,OBJ_LABEL,0,0,0);
  109.          ObjectSetInteger(0,OBJ_NAME,OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
  110.          ObjectSetInteger(0,OBJ_NAME,OBJPROP_CORNER,CORNER_RIGHT_UPPER);
  111.          ObjectSetInteger(0,OBJ_NAME,OBJPROP_XDISTANCE,200);
  112.          ObjectSetInteger(0,OBJ_NAME,OBJPROP_YDISTANCE,2);
  113.          ObjectSetString(0,OBJ_NAME,OBJPROP_TEXT,text);
  114.          ObjectSetString(0,OBJ_NAME,OBJPROP_FONT,FONT_NAME);
  115.          ObjectSetInteger(0,OBJ_NAME,OBJPROP_COLOR,LabelColor);
  116.          ObjectSetInteger(0,OBJ_NAME,OBJPROP_SELECTABLE,true);
  117.          ObjectSetInteger(0,OBJ_NAME,OBJPROP_FONTSIZE,12);
  118.         }
  119.       else
  120.          ObjectSetString(0,OBJ_NAME,OBJPROP_TEXT,text);
  121.      }
  122.   }
  123. //+------------------------------------------------------------------+
复制代码





回复

使用道具 举报

19

主题

36

回帖

201

积分

中级会员

积分
201
2019-7-25 19:11:43 来自手机 显示全部楼层
学习学习
回复

使用道具 举报

196

主题

140

回帖

1444

积分

管理员

积分
1444
2019-7-26 07:11:55 显示全部楼层
感谢分享,可以用
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册
关注微信