fix: resolve 429 rate limit errors with API backoff and correct timezone handling for 09:30 candle

This commit is contained in:
pie
2026-05-01 15:18:33 +01:00
parent be4df42e01
commit ede9933c88
4 changed files with 75 additions and 14 deletions
+25 -14
View File
@@ -75,7 +75,7 @@ def run_ticker_lifecycle(client, yf_ticker, t212_ticker, tz):
if now.hour == 9 and now.minute >= 45:
logger.info(f"Evaluating opening candle for {yf_ticker}...")
# Retry loop: wait for yfinance to publish the 09:30-09:45 candle (up to 3 minutes)
# Retry loop: wait for yfinance to publish the 09:30-09:45 candle
setup_found = False
max_retries = 12
for attempt in range(max_retries):
@@ -90,19 +90,30 @@ def run_ticker_lifecycle(client, yf_ticker, t212_ticker, tz):
params = strategy.get_trade_params()
params['ticker'] = t212_ticker
# Fetch Account Balance to calculate risk
try:
account_info = client.get_account_info()
virtual_balance = float(os.getenv("VIRTUAL_STARTING_BALANCE", 0))
if virtual_balance > 0:
risk_amount = virtual_balance * 0.01
else:
available_cash = account_info.get('cash', {}).get('availableToTrade', 1000)
risk_amount = available_cash * 0.01
except Exception as e:
logger.error(f"Failed to fetch account info for risk calculation: {e}. Defaulting to £2.50 risk.")
risk_amount = 2.50
# Anti-thundering-herd: Random jitter to prevent 429s from parallel threads
import random
time.sleep(random.uniform(0.1, 3.0))
# Fetch Account Balance to calculate risk with backoff
risk_amount = 2.50 # Fallback
for attempt in range(3):
try:
account_info = client.get_account_info()
virtual_balance = float(os.getenv("VIRTUAL_STARTING_BALANCE", 0))
if virtual_balance > 0:
risk_amount = virtual_balance * 0.01
else:
available_cash = account_info.get('cash', {}).get('availableToTrade', 1000)
risk_amount = available_cash * 0.01
break # Success
except Exception as e:
if '429' in str(e):
logger.warning(f"Rate limited on account fetch for {yf_ticker}. Retrying in {2**(attempt+1)}s...")
time.sleep(2**(attempt+1))
else:
logger.error(f"Failed to fetch account info: {e}. Defaulting to £2.50 risk.")
break
if execution.execute_trade(params, target_risk_amount=risk_amount):
# monitor_and_bracket is blocking, wait for fill (times out at 11:00)