pyquotex
pyquotex copied to clipboard
get_candles` occasionally hangs without response or error
Hi, I’m experiencing an issue where the get_candles method occasionally hangs. Most of the time it works correctly, but sometimes the script just freezes after printing:
Fetching most recent candle for time: <timestamp>
No error is thrown and no further output is logged. Below is my script and sample output. I’ve removed unrelated lines for clarity.
💻 Script (Relevant Part)
# Fetch the most recent candle
end_from_time = time.time() # Directly use the current time
print(f"Fetching most recent candle for time: {end_from_time}")
# Fetch the candles data (only the most recent one)
candles = await client.get_candles(asset, end_from_time, offset, period)
candles_data = candles
if len(candles_data) > 0:
if not candles_data[0].get("open"):
candles = process_candles(candles_data, period)
candles_data = candles # Update the candles data with processed data
# In the place where you're getting the data
recent_candle = candles_data[-1]
if not has_printed:
print(f"Most recent candle for {asset}: {recent_candle}")
has_printed = True
if first_fetch:
# Calculate the next 5-minute interval to align with the next candle
current_time = time.time()
next_interval_start_time = (current_time // period) * period + period
# Calculate time to wait until the next 5-minute interval
time_to_next_candle = next_interval_start_time - current_time
time_to_next_candle += 10 # Adding 10 extra seconds for the wait
if time_to_next_candle > 0:
print(f"Waiting for {time_to_next_candle:.2f} seconds until the new candle starts.")
await asyncio.sleep(time_to_next_candle)
first_fetch = False
# After the initial wait, fetch the next candles without delay calculations
candles = await client.get_candles(asset, time.time(), offset, period)
candles_data = candles
if len(candles_data) > 0:
recent_candle = candles_data[-1]
print(f"Most recent candle for {asset}: {recent_candle}")
color = get_color(recent_candle)
candles_color.append(color)
print(f"Color of the most recent candle: {color}")
🧪 Sample Output
Fetching most recent candle for time: 1745601008.5277383
Most recent candle for USDINR_otc: {'time': 1745600700, 'open': 90.7622, 'close': 90.7474, 'high': 90.7622, 'low': 90.7387, 'ticks': 688}
Waiting for 290.19 seconds until the new candle starts.
Most recent candle for USDINR_otc: {'time': 1745600700, 'open': 90.7622, 'close': 90.7474, 'high': 90.7622, 'low': 90.7387, 'ticks': 387}
Color of the most recent candle: red
Fetching most recent candle for time: 1745601601.1610038
Most recent candle for USDINR_otc: {'time': 1745601300, 'open': 90.7629, 'close': 90.7722, 'high': 90.7752, 'low': 90.7568, 'ticks': 713}
Color of the most recent candle: green
Fetching most recent candle for time: 1745601904.2905717
Most recent candle for USDINR_otc: {'time': 1745601600, 'open': 90.7722, 'close': 90.7546, 'high': 90.7729, 'low': 90.7536, 'ticks': 705}
Color of the most recent candle: red
Fetching most recent candle for time: 1745602206.4088607
Most recent candle for USDINR_otc: {'time': 1745601900, 'open': 90.7544, 'close': 90.7673, 'high': 90.7689, 'low': 90.7509, 'ticks': 703}
Color of the most recent candle: green
Fetching most recent candle for time: 1745602508.3360462 <-- Script freezes here
🔎 Summary
- The script consistently prints the
Fetching most recent candle for time:log, then hangs. - It doesn't print the actual candle data afterward.
- There’s no exception or traceback — it just stops.
- This seems to happen randomly after several successful fetches.
Is this a known issue, or could this be due to an unstable API response, rate limiting, or something else?
Would you recommend using a timeout or some advise for get_candles and fix this problem?
Thanks for your support!