How to know how many candles there are between two points?

 
Hi,

I'm developing a system which includes drawing figures and, for that, I need to pick the angle between two selected points in the graph. To do that, I need to calculate the difference between the second point and the first one, something like 

double(datetimeOfSecondPoint - datetimeOfFirstPoint)

Everything was doing fine until I started to pick the second point from a different day in a intraday graph. In that case, the above subtraction don't actually give me the correct number of candles between point 1 and 2. 

So how can I correctly get the exact number of candles in the current graph periodicity while having the datetime of the two points?
 
Martin Bittencourt:
Hi,

I'm developing a system which includes drawing figures and, for that, I need to pick the angle between two selected points in the graph. To do that, I need to calculate the difference between the second point and the first one, something like 


Everything was doing fine until I started to pick the second point from a different day in a intraday graph. In that case, the above subtraction don't actually give me the correct number of candles between point 1 and 2. 

So how can I correctly get the exact number of candles in the current graph periodicity while having the datetime of the two points?
int  Bars(
   string           symbol_name,     // symbol name
   ENUM_TIMEFRAMES  timeframe,       // period
   datetime         start_time,      // start date and time
   datetime         stop_time        // end date and time
   );

https://www.mql5.com/en/docs/series/bars

Documentation on MQL5: Timeseries and Indicators Access / Bars
Documentation on MQL5: Timeseries and Indicators Access / Bars
  • www.mql5.com
If the start_time and stop_time parameters are defined, the function returns the number of bars in the specified time interval, otherwise it returns the total number of bars. If data for the timeseries with specified parameters are not formed in the terminal by the time of the Bars() function call, or data of the timeseries are not synchronized...
 
Martin Bittencourt the above subtraction don't actually give me the correct number of candles between point 1 and 2.

The difference between two datetimes will never give candle numbers. They give seconds. Dividing by PeriodSeconds() assumes every bar every exists — they don't. 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

Either Martin's suggestion, or for MT4/5 subtract iBarShift()

 

Thanks. Unfortunately, it didn't work. I substituted the call to this function whenever needed and I end up getting an unprecise draw. Nevertheless I suppose I may apply a correction using this function. So, thanks!


EDIT: Now I managed to correct the erroneous draw I mentioned above, but a new problem happened: whenever I click  outside of the graph (i.e. 'in the future', where candles have not yet been drawn), which is necessary for my purposes, the Bars function returns a misleading candlestick count: it only counts till the last drawn candle.
 
You can't click in the future. There is no way to know how many candles that represents. You would have to assume that every bar exists. 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
 
William Roeder #:
You can't click in the future. 

Gosh of course I can ^.^ We do that all the time when drawing graph symbols on the chart using that function to displace the chart to the left!

a

B

Reason: