Get value of last closed candlestick

Hello, to get the value of the last bar closed in ETHUSDT parity every five minutes, I take the value of the last 500 bars from the database and store this data in the candles list.

Since the list starts from 0, the value 499 corresponds to the currently active bar and I use the value 498 to get the value of the last bar that closed. For example, I get the timestamp value of the last bar closed with candles[498][0].

My program prints the time of the transaction and the date value of bar 498 to the screen.

For example as follows;

Runtime: 18:50:1
CS Datetime: 2021-08-20 18:45:00

Normally, there should be 5 minutes difference between run time and cs datetime. But for some reason sometimes this difference is 10 minutes and even though I select bar 498, the program prints the values ​​of bar 497 to the screen.

For example as follows;

Runtime: 18:50:1
CS Datetime: 2021-08-20 18:40:00

When it is like this, I miss the value of the bar at 18:45. What do you think is causing my program to run unstable like this?

The codes of my program are below;

import ops

import time

import urllib.request

from datetime import datetime

from binance.client import Client

while True:
    # Run the program every 1 second to check the current time.
    time.sleep(1)

    # Pass current time to variable "now".
    now = datetime.now()

    # Run the below codes every 5 minutes.
    if (now.minute % 5) == 0 and now.second == 0:
        # Check internet connection.
        ops.check_internet_conn(urllib, time)

        # Check the time again for latency in case of internet connection loss.
        now = ops.check_time(datetime, time, now, 5)

        # Connect to API.
        client = Client(config.api_key, config.api_secret)

        # Pass the value of the last 500 bars of the ETHUSDT parity on the 5-minute chart to the "candles" list.
        while True:
            try:
                candles = client.futures_klines(symbol='ETHUSDT', interval=Client.KLINE_INTERVAL_5MINUTE)

                if len(candles) > 0:
                    break

            except Exception as e:
                print("Error:", e)
                time.sleep(5)
                pass

        selected_cs = 498

        # Pass the date value of the selected candlestick to the dt_obj variable.
        ts = int(candles[selected_cs][0])
        dt_obj = datetime.fromtimestamp(ts / 1000)

        print("Run Time:{0}:{1}:{2}".format(now.hour, now.minute, now.second))
        print("CS Datetime:", dt_obj)

ops.py file

def check_internet_conn(urllib, time):
    print("\n----------------------------------------")
    while True:
        try:
            urllib.request.urlopen('http://google.com')
            print("Internet connection available.")
            break
        except Exception:
            print("No Internet connection. Trying to connect again..")
            time.sleep(5)


def check_time(datetime, time, old_now, mn):
    # Pass current time to variable now.
    now = datetime.now()

    if old_now.minute != now.minute:
        while True:
            # Run the program every 1 second to check the current time.
            time.sleep(1)

            # Pass current time to variable now.
            now = datetime.now()

            if (now.minute % mn) == 0 and now.second == 0:
                break

    return now