datetime ExtBarTimeOpen;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- establecemos el temporizador en un segundo
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[])
{
//--- obtenemos la hora de apertura de la barra actual
ExtBarTimeOpen=time[rates_total-1];
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//--- anotamos la hora anterior de apertura de la barra
static datetime bar_open_time=ExtBarTimeOpen;
//--- calculamos el número de segundos transcurridos desde la apertura de la barra
static int seconds=int(TimeCurrent()-ExtBarTimeOpen);
//--- si la hora anterior de apertura de la barra no es igual a la actual, significa que tenemos una nueva barra
//--- anotamos la nueva hora de apertura como la anterior y ponemos los segundos a cero
if(bar_open_time!=ExtBarTimeOpen)
{
bar_open_time=ExtBarTimeOpen;
seconds=0;
}
//--- aumentamos y corregimos el número de segundos transcurridos desde el momento de la apertura de la barra
seconds++;
if(seconds>PeriodSeconds(PERIOD_CURRENT))
seconds=0;
//--- descripción de la hora de apertura de la barra en formato yyyy.mm.dd hh:mi
string bar_time_open=TimeToString(ExtBarTimeOpen);
//--- descripción de la barra actual en formato yyyy.mm.dd hh:mi:ss
string time_current=TimeToString(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS);
//--- número de segundos restante hasta la apertura de una nueva barra
int sec_left=PeriodSeconds(PERIOD_CURRENT)-seconds;
//--- descripción de la cantidad de tiempo restante hasta la apertura de una nueva barra en el formato hh:mm:ss
string time_left=TimeToString(sec_left,TIME_MINUTES|TIME_SECONDS);
//--- creamos una línea de introducción
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);
//--- mostramos la hora de apertura de la barra, la hora actual en el comentario del gráfico,
//--- el número de segundos transcurridos desde la apertura de la barra actual y los que quedan hasta su cierre, y
//--- la hora hasta el cierre de la barra actual
Comment(txt);
/*
resultado en M1:
Opening time of the current bar: 2024.02.22 18:06
Time Current: 2024.02.22 18:06:24
Seconds have passed since the bar opened: 25
Approximately seconds left before bar closes: 35
Time remaining until bar closes: 00:00:35
resultado en M5:
Opening time of the current bar: 2024.02.22 18:05
Time Current: 2024.02.22 18:07:28
Seconds have passed since the bar opened: 149
Approximately seconds left before bar closes: 151
Time remaining until bar closes: 00:02:31
resultado en H1:
Opening time of the current bar: 2024.02.22 18:00
Time Current: 2024.02.22 18:08:13
Seconds have passed since the bar opened: 494
Approximately seconds left before bar closes: 3106
Time remaining until bar closes: 00:51:46
resultado en D1:
Opening time of the current bar: 2024.02.22 00:00
Time Current: 2024.02.22 18:11:01
Seconds have passed since the bar opened: 65462
Approximately seconds left before bar closes: 20938
Time remaining until bar closes: 05:48:58
*/
}
|