def fix_live_trading(): try: # Read the file content as a single string with open('main.py', 'r') as f: content = f.read() print(f"Read {len(content)} characters from main.py") # Fix the live_trading function signature live_trading_pos = content.find('async def live_trading(') if live_trading_pos != -1: print(f"Found live_trading function at position {live_trading_pos}") content = content.replace('async def live_trading(', 'async def live_trading(agent=None, env=None, exchange=None, ') print("Updated live_trading function signature") else: print("WARNING: Could not find live_trading function!") # Fix the TradingEnvironment initialization env_init_pos = content.find('env = TradingEnvironment(') if env_init_pos != -1: print(f"Found env initialization at position {env_init_pos}") # Find the closing parenthesis paren_depth = 0 close_pos = env_init_pos for i in range(env_init_pos, len(content)): if content[i] == '(': paren_depth += 1 elif content[i] == ')': paren_depth -= 1 if paren_depth == 0: close_pos = i + 1 break # Calculate indentation line_start = content.rfind('\n', 0, env_init_pos) + 1 indent = ' ' * (env_init_pos - line_start) # Create the new environment initialization code new_env_init = f'''if env is None: {indent} env = TradingEnvironment( {indent} initial_balance=initial_balance, {indent} leverage=leverage, {indent} window_size=window_size, {indent} commission=commission, {indent} symbol=symbol, {indent} timeframe=timeframe {indent} )''' # Replace the old code with the new code content = content[:env_init_pos] + new_env_init + content[close_pos:] print("Updated TradingEnvironment initialization") else: print("WARNING: Could not find TradingEnvironment initialization!") # Write the updated content back to the file with open('main.py', 'w') as f: f.write(content) print(f"Wrote {len(content)} characters back to main.py") print('Fixed live_trading function and TradingEnvironment initialization') return True except Exception as e: print(f'Error fixing file: {e}') import traceback print(traceback.format_exc()) return False if __name__ == "__main__": fix_live_trading()