how can i detect that 10 seconds to bar close? MT4/EA

 
hi
If there are 10 seconds left until the bar closing, I want to do something, like get position...
how can i detect 10 seconds left to 5M bar closing???

i have no idea,I couldn't find codes.
 
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. There is no bar closing. Only a new bar opening. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific.) requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles
              No candle if open = close ? - MQL4 programming forum
  3. The bar should end at bar start + PeriodSeconds. You're asking about 10 seconds less. But if there is no tick, nothing has changed, why do you care? If you do something, what happens if you get (multiple) ticks after; do you care? There is only one tick between the last and the start of a new bar; why do you care that you are one tick late?
  4. If you insist, you are going to have to use a timer and server to local conversion. See attached and:
              Indicators: Zero Lag Timer - Indices - Articles, Library comments - MQL5 programming forum
Files:
 
William Roeder:
  1. There is no bar closing. Only a new bar opening. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific.) requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles
              No candle if open = close ? - MQL4 programming forum
  2. The bar should end at bar start + PeriodSeconds. You're asking about 10 seconds less. But if there is no tick, nothing has changed, why do you care? If you do something, what happens if you get (multiple) ticks after; do you care? There is only one tick between the last and the start of a new bar; why do you care that you are one tick late?
  3. If you insist, you are going to have to use a timer and server to local conversion. See attached and:
              Indicators: Zero Lag Timer - Indices - Articles, Library comments - MQL5 programming forum

Can I detect how many seconds have passed from candle open??

thanks

 
hamit0111: Can I detect how many seconds have passed from candle open??

When in doubt, think! TimeCurrent() - Time[0]

 
hamid khateri:
hi
If there are 10 seconds left until the bar closing, I want to do something, like get position...
how can i detect 10 seconds left to 5M bar closing???

i have no idea,I couldn't find codes.

You can do the math to get the close time for the current bar or any previous bar using PeriodSeconds(), however it gives you the standard close time and not the actual close time.

that means, by the given time the bar is closed for certain but the actual closing time could be earlier than that. consider a function like this:

//--- Returns bar close time
datetime barCloseTime(ENUM_TIMEFRAMES timeframe, int index) {
    int period_seconds = PeriodSeconds(timeframe);
    datetime open_time = iTime(_Symbol, timeframe, index);
    datetime output = (ulong)open_time + period_seconds - 1;
    return(output);
}

this would give me 10 seconds before the current PERIOD_D1 bar close "standard" time :

Print(barCloseTime(PERIOD_D1, 0) - 10)
// returns 2024.02.06 23:59:49 

but what if the market gets closed sooner?