From deba044a7b20c7b2487fb46f0635af3ac7393441 Mon Sep 17 00:00:00 2001 From: pie Date: Wed, 6 May 2026 09:39:57 +0100 Subject: [PATCH] fixes --- main.py | 20 +++++++++++++------- src/execution/manager.py | 8 ++++---- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/main.py b/main.py index ef06938..c8d1e38 100644 --- a/main.py +++ b/main.py @@ -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) diff --git a/src/execution/manager.py b/src/execution/manager.py index b806016..23f13f3 100644 --- a/src/execution/manager.py +++ b/src/execution/manager.py @@ -21,8 +21,8 @@ class ExecutionManager: self.params = params ticker = params['ticker'] direction = params['direction'] - entry_price = params['entry_price'] - stop_loss = params['stop_loss'] + entry_price = round(params['entry_price'], 2) + stop_loss = round(params['stop_loss'], 2) # Calculate Risk per share risk_per_share = abs(entry_price - stop_loss) @@ -70,8 +70,8 @@ class ExecutionManager: return False ticker = params['ticker'] - tp_price = params['target_price'] - sl_price = params['stop_loss'] + tp_price = round(params['target_price'], 2) + sl_price = round(params['stop_loss'], 2) quantity = getattr(self, 'current_quantity', 1.0)