TimeToString

转变值包括从01.01.1970起已消耗的秒数,以字符串格式"yyyy.mm.dd hh:mi"

string  TimeToString(
   datetime  value,                           // 数字
   int       mode=TIME_DATE|TIME_MINUTES      // 输出形式
   );

参量

[in]  时间从00:00 1970/01/01开始

mode=TIME_DATE|TIME_MINUTES

[in] 额外数据输入模式,可以是一个也可以包括以
"yyyy.mm.dd"为形式的标签TIME_DATE ,
以"hh:mi"为结果的 TIME_MINUTES ,
以 "hh:mi:ss"为结果的TIME_SECONDS 。

返回值

字符串。

 

示例:

datetime ExtBarTimeOpen;
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- 将timer设置为秒
   EventSetTimer(1);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Comment("");
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---获取当前柱形图的开仓时间
   ExtBarTimeOpen=time[rates_total-1];
//--- 为下一次调用返回前次已计算值
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer函数                                                         |
//+------------------------------------------------------------------+
void OnTimer()
  {
//--- 设置之前柱形图开仓时间
   static datetime bar_open_time=ExtBarTimeOpen;
//--- 记录柱形图开仓后经过的秒数
   static int seconds=int(TimeCurrent()-ExtBarTimeOpen);
//--- 如果之前的开仓时间不等于当前时间,则这是一个新柱形图
//--- 将新开仓时间写为之前的开仓时间,并将秒数设置为零
   if(bar_open_time!=ExtBarTimeOpen)
     {
      bar_open_time=ExtBarTimeOpen;
      seconds=0;
     }
//--- 增加并调整自柱形图开仓以来经过的秒数
   seconds++;
   if(seconds>PeriodSeconds(PERIOD_CURRENT))
      seconds=0;
//--- 柱形图开仓时间为yyyy.mm.dd hh:mi
   string bar_time_open=TimeToString(ExtBarTimeOpen);
//--- 当前时间为yyyy.mm.dd hh:mi:ss
   string time_current=TimeToString(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS);
//--- 距离新柱形图开仓还剩多少秒
   int    sec_left=PeriodSeconds(PERIOD_CURRENT)-seconds;
//--- 当前柱形图关闭前剩余的时间,格式为 hh:mm:ss
   string time_left=TimeToString(sec_left,TIME_MINUTES|TIME_SECONDS);
//--- 创建输出字符串
   string txt=StringFormat("Opening time of the current bar: %s\n"+
                           "Time Current: %s\n"+
                           "Seconds have passed since the bar opened: %d\n"+
                           "Approximately seconds left before bar closes: %d\n"+
                           "Time remaining until bar closes: %s",bar_time_open,time_current,seconds,sec_left,time_left);
//--- 显示柱形图开仓时间和当前时间,
//--- 显示当前柱形图开仓到关闭所经过的秒数,
//--- 在注释中显示当前柱线关闭前剩余的时间
   Comment(txt);
   /*
   result on M1:
   Opening time of the current bar2024.02.22 18:06
   Time Current2024.02.22 18:06:24
   Seconds have passed since the bar opened25
   Approximately seconds left before bar closes35
   Time remaining until bar closes00:00:35
 
   result on M5:
   Opening time of the current bar2024.02.22 18:05
   Time Current2024.02.22 18:07:28
   Seconds have passed since the bar opened149
   Approximately seconds left before bar closes151
   Time remaining until bar closes00:02:31
 
   result on H1:
   Opening time of the current bar2024.02.22 18:00
   Time Current2024.02.22 18:08:13
   Seconds have passed since the bar opened494
   Approximately seconds left before bar closes3106
   Time remaining until bar closes00:51:46
   
   result on D1:
   Opening time of the current bar2024.02.22 00:00
   Time Current2024.02.22 18:11:01
   Seconds have passed since the bar opened65462
   Approximately seconds left before bar closes20938
   Time remaining until bar closes05:48:58
   */
  }

另见

StringToTimeTimeToStruct