This commit is contained in:
pie
2026-05-06 09:39:57 +01:00
parent d1a141a669
commit deba044a7b
2 changed files with 17 additions and 11 deletions
+13 -7
View File
@@ -99,13 +99,16 @@ def run_ticker_lifecycle(client, yf_ticker, t212_ticker, tz):
for attempt in range(3):
try:
account_info = client.get_account_info()
virtual_balance = float(os.getenv("VIRTUAL_STARTING_BALANCE", 0))
# Trading212 returns totalValue for the account equity
actual_balance = float(account_info.get('totalValue', 5000.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
# Simulate starting with 250 by subtracting the demo excess (4750)
virtual_balance = max(0, actual_balance - 4750.0)
# Risk 1% of this adjusted virtual balance
risk_amount = virtual_balance * 0.01
logger.info(f"Account: {actual_balance:.2f} | Virtual Balance: {virtual_balance:.2f} | Risk (1%): {risk_amount:.2f}")
break # Success
except Exception as e:
if '429' in str(e):
@@ -140,7 +143,10 @@ def run_ticker_lifecycle(client, yf_ticker, t212_ticker, tz):
logger.info(f"Waiting {wait_seconds:.0f} seconds until 11:00 EST forced exit...")
time.sleep(wait_seconds)
# 3. 11:00 EST - Cleanup
# 3. 11:00 EST - Cleanup (with jitter to prevent 429s from parallel threads)
import random
time.sleep(random.uniform(0.1, 5.0))
logger.info(f"Time exit reached for {yf_ticker}. Cleaning up.")
if execution.is_in_position:
exit_price = execution.close_all(t212_ticker)