datetime ExtBarTimeOpen;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- definimos valor do cronômetro como um 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[])
{
//--- obtemos o tempo de abertura da barra atual
ExtBarTimeOpen=time[rates_total-1];
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//--- registramos o tempo de abertura da barra anterior
static datetime bar_open_time=ExtBarTimeOpen;
//--- calculamos a quantidade de segundos que passaram desde a abertura da barra
static int seconds=int(TimeCurrent()-ExtBarTimeOpen);
//--- se o tempo de abertura anterior não é igual ao atual, então é uma nova barra
//--- registramos o novo tempo de abertura como o anterior e zeramos os segundos
if(bar_open_time!=ExtBarTimeOpen)
{
bar_open_time=ExtBarTimeOpen;
seconds=0;
}
//--- incrementamos e ajustamos a quantidade de segundos passados desde a abertura da barra
seconds++;
if(seconds>PeriodSeconds(PERIOD_CURRENT))
seconds=0;
//--- descrição do tempo de abertura da barra no formato yyyy.mm.dd hh:mi
string bar_time_open=TimeToString(ExtBarTimeOpen);
//--- descrição do tempo atual no formato yyyy.mm.dd hh:mi:ss
string time_current=TimeToString(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS);
//--- quantidade de segundos restantes até a abertura da próxima barra
int sec_left=PeriodSeconds(PERIOD_CURRENT)-seconds;
//--- descrição do tempo restante até o fechamento da barra atual no formato hh:mm:ss
string time_left=TimeToString(sec_left,TIME_MINUTES|TIME_SECONDS);
//--- criamos a string de saída
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);
//--- exibimos no comentário do gráfico o tempo de abertura da barra, o tempo atual,
//--- a quantidade de segundos que passaram desde a abertura da barra atual e os restantes até o seu fechamento,
//--- o tempo restante até o fechamento da barra atual
Comment(txt);
/*
resultado em 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 em 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 em 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 em 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
*/
}
|