My code is the following:
def get_history(symbol,interval,start,end=None):
bars = client.get_historical_klines(symbol = symbol, interval= interval,
start_str= start, end_str= end, limit=1000)
df = pd.DataFrame(bars)
df.columns = ["Open time", "Open", "High", "Low", "Close", "Volume", "Close time", "Quote asset volume",
"Number of trades", "Taker buy base asset volume", "Taker buy quote asset volume", "Ignore"]
df["Date"] = pd.to_datetime(df["Open time"],unit = "ms")
df = df[["Open", "High", "Low", "Close", "Volume", "Date"]].copy()
df.set_index("Date", inplace = True)
for column in df.columns:
df[column] = pd.to_numeric(df[column],errors="coerce")
return df
When I call the function for the latest interval of 2 hours I introduce this for the couple “BTCUSDT”:
now = datetime.now()
two_hours_ago = now - timedelta(hours=2)
get_history(symbol="BTCUSDT", interval="1m", start = str(two_hours_ago))
But when it is displayed the DataFrame it only appears with data from two hours ago.
Example: If I execute the block of code with two_hours_ago at 18:30 and then, the function get_history at 18:31, it returns only the values from 16:30 to 16:31, while it should have returned from 16:30 to 18:31.
Is like the most two recent hours are not allowed for access, why is this happening?