How to get the first or second of bar time on the chart right?

 

Just like the Pic:


 
Yu Zhang:

Just like the Pic:


   int      right_shift    = ChartGetInteger(0,CHART_WIDTH_IN_BARS) - ChartGetInteger(0,CHART_VISIBLE_BARS) + 1;
   datetime right_time     = time[0] + right_shift*PeriodSeconds();
 
  1. You squared the triangle. That points to the first candle on the chart. There is no future.
  2. As Ezatrimeo stated, you can use shift*PS, but that assumes that all bars will exist in the future — 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 (2006)
              No candle if open = close ? - MQL4 programming forum (2010)