TradeCity 交易城市

交易之城,智慧之城,明天之城

mt4软件的当前K线周期倒计时的指标

image


//+------------------------------------------------------------------+

//|                                           K线剩余时间显示系统.mq4 |

//|                                        Copyright © 2024, 量化交易助手 |

//+------------------------------------------------------------------+

#property copyright "量化交易助手"

#property link      "https://www.tradecity.online"

#property version   "1.00"

#property strict

#property indicator_chart_window


// 界面配置参数

input color    TextColor = clrWhite;     // 字体颜色

input int      FontSize  = 20;               // 字体大小

input string   FontFace  = "Arial Bold";       // 字体类型


// 全局变量

datetime currentBarTime;                     // 当前K线开盘时间

int      objTimeLabel;                       // 图形对象ID


//+------------------------------------------------------------------+

//| 初始化函数                                                       |

//+------------------------------------------------------------------+

int OnInit()

{

  // 创建文本标签对象

  objTimeLabel = ObjectCreate("TimeRemainingLabel", OBJ_LABEL, 0, 0, 0);

 

  // 配置对象属性

  ObjectSet("TimeRemainingLabel", OBJPROP_CORNER, CORNER_RIGHT_UPPER); // 右上角定位

  ObjectSet("TimeRemainingLabel", OBJPROP_XDISTANCE, 20);              // 右侧偏移20像素

  ObjectSet("TimeRemainingLabel", OBJPROP_YDISTANCE, 20);              // 顶部偏移20像素

  ObjectSetText("TimeRemainingLabel", "加载中...", FontSize, FontFace, TextColor);

 

  return(INIT_SUCCEEDED);

}


//+------------------------------------------------------------------+

//| 反初始化函数                                                     |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

  ObjectDelete("TimeRemainingLabel");       // 删除图形对象

}


//+------------------------------------------------------------------+

//| 主计算函数                                                       |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

               const int prev_calculated,

               const datetime &time[],

               const double &open[],

               const double &high[],

               const double &low[],

               const double &close[],

               const long &tick_volume[],

               const long &volume[],

               const int &spread[])

{

  UpdateTimeDisplay();                      // 调用时间更新函数

  return(rates_total);

}


//+------------------------------------------------------------------+

//| 自定义时间显示函数                                               |

//+------------------------------------------------------------------+

void UpdateTimeDisplay()

{

  datetime now = TimeCurrent();             // 获取当前服务器时间

  datetime barStart = iTime(Symbol(), 0, 0); // 当前K线开盘时间

 

  // 计算剩余时间

  int periodSeconds = PeriodSeconds(Period());

  int elapsed = int(now - barStart);

  int remaining = periodSeconds - elapsed;

 

  // 格式化时间显示

  string timeStr = StringFormat("%02d:%02d:%02d",

                              remaining/3600,

                              (remaining%3600)/60,

                              remaining%60);

 

  // 更新显示内容

  ObjectSetText("TimeRemainingLabel", "剩余时间: " + timeStr, FontSize, FontFace, TextColor);

}


//+------------------------------------------------------------------+

//| 获取周期对应秒数                                                 |

//+------------------------------------------------------------------+

int PeriodSeconds(int period)

{

  switch(period)

  {

     case PERIOD_M1:  return(60);

     case PERIOD_M5:  return(300);

     case PERIOD_M15: return(900);

     case PERIOD_M30: return(1800);

     case PERIOD_H1:  return(3600);

     case PERIOD_H4:  return(14400);

     case PERIOD_D1:  return(86400);

     case PERIOD_W1:  return(604800);

     case PERIOD_MN1: return(2592000);

     default:         return(0);

  }

}

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-BlogPHP 1.7.4

Copyright Your WebSite.Some Rights Reserved.