Live Trading·Trading Infrastructure·Intermediate

Live Trading Loop

Build the central live trading execution loop that continuously fetches real-time market data, runs the signal generation pipeline, evaluates all pre-trade risk checks, places and manages orders, and monitors open positions in a robust event-driven cycle suitable for production algorithmic trading.

live-tradingtrading-strategies

Main Live Trading Execution Loop

This notebook outlines the design and implementation of a main execution loop for live trading, focusing on robustness, error handling, and modularity. The core idea is to create a stateful system that continuously fetches market data, evaluates trading signals, executes orders, and manages positions.

Key Concepts:

ConceptDescription
Execution LoopThe continuous process of monitoring, decision-making, and action-taking.
State ManagementUsing dictionaries to hold the current status of the trading system (positions, cash, orders).
Market Data FetchingRetrieving real-time or near real-time price and volume data.
Signal EvaluationDetermining whether to buy, sell, or hold based on a predefined strategy.
Order ExecutionPlacing and managing trade orders with a broker, including retry logic.
Position ManagementTracking and adjusting current holdings and P&L.
Error HandlingImplementing robust try/except blocks with exponential backoff for resilience.
LoggingRecording critical events, data, and decisions for monitoring and debugging.
Metrics TrackingCalculating and summarizing performance indicators.

Dependency Installation

First, we install the necessary Python libraries. We'll need pandas for data manipulation, numpy for numerical operations, collections for deque, logging for logging, time for delays, random for jitter, matplotlib and seaborn for visualizations.

[1]
# Install necessary libraries
!pip install pandas numpy matplotlib seaborn --quiet

Library Imports

Next, we import all required libraries. Standard libraries are listed first, followed by third-party libraries.

[2]
# Standard library imports
import collections
import logging
import time
import random
import datetime

# Third-party library imports
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

Core Functions

This section defines the core functions that make up the live trading execution loop. Each function is presented in its own code block, adhering to the specified format including detailed docstrings, type hints, and logging statements.

Function Name: setup_logging

This function initializes and configures a basic logger for the trading application. It ensures that log messages are output to the console with appropriate timestamps and levels, facilitating debugging and monitoring.

Parameters: logger_name (str): The name of the logger to configure. log_level (int): The logging level (e.g., logging.INFO, logging.DEBUG).

Returns: logging.Logger: The configured logger instance.

[3]
def setup_logging(logger_name: str = 'trading_app', log_level: int = logging.INFO) -> logging.Logger:
    """
    Sets up and configures a basic logger.

    Parameters
    ----------
    logger_name : str, optional
        The name of the logger, defaults to 'trading_app'.
    log_level : int, optional
        The logging level (e.g., logging.INFO, logging.DEBUG), defaults to logging.INFO.

    Returns
    -------
    logging.Logger
        The configured logger instance.
    """
    logger = logging.getLogger(logger_name)
    logger.setLevel(log_level)
    if not logger.handlers:
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        ch = logging.StreamHandler()
        ch.setFormatter(formatter)
        logger.addHandler(ch)
    return logger

logger = setup_logging()

Function Name: create_execution_state

This function initializes the trading system's state dictionary. It sets up initial values for cash, positions, order history, and other relevant parameters needed for the trading loop.

Parameters: initial_cash (float): The starting capital for trading. symbol (str): The trading symbol (e.g., 'AAPL', 'SPY').

Returns: dict: An initialized state dictionary.

[4]
def create_execution_state(initial_cash: float, symbol: str) -> dict:
    """
    Initializes the trading system's state dictionary.

    Parameters
    ----------
    initial_cash : float
        The starting capital for trading.
    symbol : str
        The trading symbol (e.g., 'AAPL', 'SPY').

    Returns
    -------
    dict
        An initialized state dictionary.
    """
    logger.info(f"Initializing execution state with cash: ${initial_cash:.2f} for symbol: {symbol}")
    state = {
        'cash': initial_cash,
        'position': 0, # Quantity of symbol held
        'symbol': symbol,
        'order_history': [],
        'trade_log': collections.deque(maxlen=1000), # Store recent trades
        'metrics': {'profit_loss': 0.0, 'total_trades': 0},
        'market_data_buffer': collections.deque(maxlen=100) # Rolling window for market data
    }
    logger.debug("Execution state initialized.")
    return state

Function Name: get_market_data

This function simulates fetching real-time market data for a given symbol. In a live environment, this would interact with a broker's API. For demonstration, it generates mock price data with random fluctuations. It includes retry logic with exponential backoff.

Parameters: state (dict): The current trading state dictionary. simulated_price (float): The base price for simulation.

Returns: dict: The updated state dictionary containing the latest market data.

[5]
def get_market_data(state: dict, simulated_price: float) -> dict:
    """
    Simulates fetching real-time market data for a given symbol.
    Includes retry logic with exponential backoff.

    Parameters
    ----------
    state : dict
        The current trading state dictionary.
    simulated_price : float
        The base price for simulation.

    Returns
    -------
    dict
        The updated state dictionary containing the latest market data.
    """
    max_retries = 3
    base_delay = 1 # seconds

    for attempt in range(max_retries):
        try:
            # Simulate API call delay with random jitter
            time.sleep(base_delay + random.uniform(0, 0.5))

            # Simulate market data (price, volume)
            price_fluctuation = (random.random() - 0.5) * 2 # -1 to 1
            current_price = simulated_price * (1 + 0.005 * price_fluctuation)
            volume = int(random.uniform(100, 10000))

            market_data = {
                'timestamp': datetime.datetime.now(),
                'price': round(current_price, 2),
                'volume': volume
            }
            state['market_data_buffer'].append(market_data)
            logger.debug(f"Fetched market data for {state['symbol']}: {market_data}")
            return state

        except Exception as e:
            logger.warning(f"Attempt {attempt + 1}/{max_retries}: Failed to fetch market data: {e}")
            if attempt < max_retries - 1:
                delay = base_delay * (2 ** attempt) + random.uniform(0, 0.5)
                logger.info(f"Retrying in {delay:.2f} seconds...")
                time.sleep(delay)
            else:
                logger.error("Max retries reached. Could not fetch market data.")
                raise # Re-raise the last exception if all retries fail
    return state # Should not be reached if exceptions are handled or retried

Function Name: evaluate_trade_signal

This function evaluates a trading signal based on the current market data. For demonstration, it implements a simple moving average crossover strategy using the market_data_buffer. It determines whether to generate a 'BUY', 'SELL', or 'HOLD' signal.

Parameters: state (dict): The current trading state dictionary. short_window (int): The period for the short moving average. long_window (int): The period for the long moving average.

Returns: str: The generated trade signal ('BUY', 'SELL', or 'HOLD').

[6]
def evaluate_trade_signal(state: dict, short_window: int = 5, long_window: int = 10) -> str:
    """
    Evaluates a trading signal based on the current market data (e.g., SMA crossover).

    Parameters
    ----------
    state : dict
        The current trading state dictionary.
    short_window : int, optional
        The period for the short moving average, defaults to 5.
    long_window : int, optional
        The period for the long moving average, defaults to 10.

    Returns
    -------
    str
        The generated trade signal ('BUY', 'SELL', or 'HOLD').
    """
    # Ensure enough data for moving averages
    if len(state['market_data_buffer']) < long_window:
        logger.debug("Not enough market data for signal evaluation. Holding.")
        return 'HOLD'

    prices = [data['price'] for data in state['market_data_buffer']]
    prices_series = pd.Series(prices)

    short_sma = prices_series.iloc[-short_window:].mean()
    long_sma = prices_series.iloc[-long_window:].mean()

    last_price = state['market_data_buffer'][-1]['price']

    signal = 'HOLD'
    if short_sma > long_sma and state['position'] <= 0: # Buy if not already holding or short
        signal = 'BUY'
        logger.info(f"BUY signal: Short SMA ({short_sma:.2f}) > Long SMA ({long_sma:.2f}) at price {last_price:.2f}")
    elif short_sma < long_sma and state['position'] >= 0: # Sell if not already shorting or holding
        signal = 'SELL'
        logger.info(f"SELL signal: Short SMA ({short_sma:.2f}) < Long SMA ({long_sma:.2f}) at price {last_price:.2f}")
    else:
        logger.debug(f"HOLD signal: Short SMA ({short_sma:.2f}), Long SMA ({long_sma:.2f})")

    return signal

Function Name: execute_order

This function simulates the execution of a trade order (buy or sell) with a specified quantity. It handles potential order failures with an exponential backoff retry mechanism. It updates the state dictionary with the order details and modifies cash and position.

Parameters: state (dict): The current trading state dictionary. order_type (str): The type of order ('BUY' or 'SELL'). quantity (int): The number of units to trade.

Returns: dict: The updated state dictionary after attempting order execution.

[7]
def execute_order(state: dict, order_type: str, quantity: int) -> dict:
    """
    Simulates the execution of a trade order (buy or sell).
    Includes exponential backoff for retries.

    Parameters
    ----------
    state : dict
        The current trading state dictionary.
    order_type : str
        The type of order ('BUY' or 'SELL').
    quantity : int
        The number of units to trade.

    Returns
    -------
    dict
        The updated state dictionary after attempting order execution.
    """
    max_retries = 3
    base_delay = 0.5 # seconds
    current_price = state['market_data_buffer'][-1]['price'] if state['market_data_buffer'] else 0.0

    if current_price == 0.0:
        logger.error("Cannot execute order: Market data not available.")
        return state

    for attempt in range(max_retries):
        try:
            # Simulate order execution delay with random jitter
            time.sleep(base_delay + random.uniform(0, 0.2))

            # Simulate potential order failure (e.g., 10% chance of failure)
            if random.random() < 0.1 and attempt < max_retries - 1: # Fail only if there are retries left
                raise ConnectionError("Simulated API connection error.")

            cost = quantity * current_price
            order_id = f"ORD-{int(time.time() * 1000)}-{random.randint(0, 999)}"

            if order_type == 'BUY':
                if state['cash'] >= cost:
                    state['cash'] -= cost
                    state['position'] += quantity
                    status = 'FILLED'
                    logger.info(f"Order {order_id} FILLED: BUY {quantity} {state['symbol']} at ${current_price:.2f} (Cost: ${cost:.2f})")
                else:
                    status = 'REJECTED'
                    logger.warning(f"Order {order_id} REJECTED: Insufficient cash for BUY {quantity} {state['symbol']}.")
            elif order_type == 'SELL':
                if state['position'] >= quantity:
                    state['cash'] += cost
                    state['position'] -= quantity
                    status = 'FILLED'
                    logger.info(f"Order {order_id} FILLED: SELL {quantity} {state['symbol']} at ${current_price:.2f} (Revenue: ${cost:.2f})")
                else:
                    status = 'REJECTED'
                    logger.warning(f"Order {order_id} REJECTED: Insufficient position for SELL {quantity} {state['symbol']}.")
            else:
                status = 'REJECTED'
                logger.error(f"Invalid order type: {order_type}")

            order_record = {
                'order_id': order_id,
                'timestamp': datetime.datetime.now(),
                'symbol': state['symbol'],
                'type': order_type,
                'quantity': quantity,
                'price': current_price,
                'cost_revenue': cost,
                'status': status
            }
            state['order_history'].append(order_record)
            if status == 'FILLED':
                state['trade_log'].append(order_record)
                state['metrics']['total_trades'] += 1
            return state

        except Exception as e:
            logger.warning(f"Attempt {attempt + 1}/{max_retries}: Order execution failed for {order_type} {quantity} {state['symbol']}: {e}")
            if attempt < max_retries - 1:
                delay = base_delay * (2 ** attempt) + random.uniform(0, 0.5)
                logger.info(f"Retrying in {delay:.2f} seconds...")
                time.sleep(delay)
            else:
                logger.error(f"Max retries reached. Order {order_type} {quantity} {state['symbol']} failed permanently.")
                order_record = {
                    'order_id': f"ORD-FAIL-{int(time.time() * 1000)}",
                    'timestamp': datetime.datetime.now(),
                    'symbol': state['symbol'],
                    'type': order_type,
                    'quantity': quantity,
                    'price': current_price,
                    'cost_revenue': 0.0,
                    'status': 'FAILED_RETRY'
                }
                state['order_history'].append(order_record)
                # Do not re-raise, allow loop to continue with error logged
    return state

Function Name: manage_position

This function updates the profit and loss (P&L) and other position-related metrics in the trading state. It uses the latest market data to calculate the current value of the position and its impact on the overall portfolio P&L.

Parameters: state (dict): The current trading state dictionary.

Returns: dict: The updated state dictionary with refreshed position metrics.

[8]
def manage_position(state: dict) -> dict:
    """
    Manages and updates the current position, including P&L calculation.

    Parameters
    ----------
    state : dict
        The current trading state dictionary.

    Returns
    -------
    dict
        The updated state dictionary with refreshed position metrics.
    """
    if not state['market_data_buffer']:
        logger.warning("No market data to manage position. Skipping P&L calculation.")
        return state

    current_price = state['market_data_buffer'][-1]['price']
    current_position_value = state['position'] * current_price

    # A simplistic P&L calculation based on current position and initial cash + realized P&L
    # For a more robust P&L, you'd track average entry price etc.
    # Here, we'll track 'unrealized' P&L relative to a simplified 'cost basis' or just total equity
    realized_pnl = sum([order['cost_revenue'] if order['type'] == 'SELL' else -order['cost_revenue']
                        for order in state['order_history'] if order['status'] == 'FILLED'])

    # This is a very simplified P&L. In a real system, you'd track average entry price for open positions
    # For demonstration, we'll track the change in equity from initial cash.
    current_equity = state['cash'] + current_position_value
    state['metrics']['profit_loss'] = current_equity - state['initial_cash'] # Assuming initial_cash stored in state

    logger.debug(f"Position managed: Current P&L: ${state['metrics']['profit_loss']:.2f}, Position: {state['position']}")
    return state

Function Name: log_metrics

This function collects and logs key trading metrics, such as current cash, position, P&L, and number of trades. It provides a snapshot of the system's performance at regular intervals.

Parameters: state (dict): The current trading state dictionary.

Returns: dict: The updated state dictionary, primarily for consistency.

[9]
def log_metrics(state: dict) -> dict:
    """
    Collects and logs key trading metrics.

    Parameters
    ----------
    state : dict
        The current trading state dictionary.

    Returns
    -------
    dict
        The updated state dictionary (primarily for consistency).
    """
    current_price = state['market_data_buffer'][-1]['price'] if state['market_data_buffer'] else 0.0
    unrealized_pnl = state['position'] * (current_price - (state['cash'] / state['position'] if state['position'] != 0 else 0)) # simplified

    logger.info(
        f"""--- Metrics Snapshot ---
        Cash: ${state['cash']:.2f}
        Position ({state['symbol']}): {state['position']} units
        Current Price: ${current_price:.2f}
        Total Trades: {state['metrics']['total_trades']}
        Portfolio P&L: ${state['metrics']['profit_loss']:.2f}
        ----------------------"""
    )
    return state

Function Name: run_trading_loop

This is the main orchestrator for the live trading execution loop. It continuously fetches data, evaluates signals, executes trades, manages positions, and logs metrics. It includes a keep_running flag for graceful shutdown and a loop_interval for controlling execution frequency.

Parameters: state (dict): The initial trading state dictionary. iterations (int): The number of times the loop should run. loop_interval (float): The base delay in seconds between loop iterations. simulated_base_price (float): The base price for market data simulation.

Returns: dict: The final state dictionary after the loop completes.

[10]
def run_trading_loop(state: dict, iterations: int, loop_interval: float, simulated_base_price: float) -> dict:
    """
    The main orchestrator for the live trading execution loop.

    Parameters
    ----------
    state : dict
        The initial trading state dictionary.
    iterations : int
        The number of times the loop should run.
    loop_interval : float
        The base delay in seconds between loop iterations.
    simulated_base_price : float
        The base price for market data simulation.

    Returns
    -------
    dict
        The final state dictionary after the loop completes.
    """
    state['initial_cash'] = state['cash'] # Store initial cash for P&L calculation
    logger.info(f"Starting trading loop for {iterations} iterations with interval {loop_interval}s.")

    for i in range(iterations):
        logger.debug(f"Loop iteration {i + 1}/{iterations}")
        try:
            state = get_market_data(state, simulated_base_price)
            signal = evaluate_trade_signal(state)

            # Example trading logic: trade a fixed quantity
            trade_quantity = 10
            if signal == 'BUY':
                state = execute_order(state, 'BUY', trade_quantity)
            elif signal == 'SELL':
                state = execute_order(state, 'SELL', trade_quantity)

            state = manage_position(state)
            state = log_metrics(state)

            # Add random jitter to loop interval
            time.sleep(loop_interval + random.uniform(0, 0.1))

        except Exception as e:
            logger.error(f"Critical error in trading loop at iteration {i+1}: {e}", exc_info=True)
            # Decide whether to continue or break on critical errors
            break

    logger.info("Trading loop finished.")
    return state

Demonstration/Visualization

This section demonstrates the complete trading execution loop with simulated data. It will initialize the trading state, run the loop for a specified number of iterations, and then visualize the trading activity, portfolio value, and key metrics.

[11]
# Configure logger to debug level for demonstration
logger.setLevel(logging.DEBUG)

# --- Simulation Parameters ---
INITIAL_CASH = 100000.0
TRADING_SYMBOL = 'DEMO'
SIMULATED_BASE_PRICE = 100.0
LOOP_ITERATIONS = 50
LOOP_INTERVAL = 0.2 # seconds

# --- Run the Trading Loop ---
logger.info("Starting trading loop demonstration...")
final_state = create_execution_state(INITIAL_CASH, TRADING_SYMBOL)
final_state = run_trading_loop(final_state, LOOP_ITERATIONS, LOOP_INTERVAL, SIMULATED_BASE_PRICE)
logger.info("Demonstration complete. Final state and metrics:")

# --- Display Summary Statistics ---
print("\n--- Final Summary ---")
print(f"Initial Cash: ${INITIAL_CASH:.2f}")
print(f"Final Cash: ${final_state['cash']:.2f}")
print(f"Final Position ({TRADING_SYMBOL}): {final_state['position']} units")
current_price_at_end = final_state['market_data_buffer'][-1]['price'] if final_state['market_data_buffer'] else 0.0
print(f"Current Market Price: ${current_price_at_end:.2f}")
print(f"Unrealized Position Value: ${final_state['position'] * current_price_at_end:.2f}")
print(f"Total Trades Executed: {final_state['metrics']['total_trades']}")
print(f"Total Portfolio P&L: ${final_state['metrics']['profit_loss']:.2f}")

# --- Prepare Data for Visualization ---
market_data_df = pd.DataFrame(final_state['market_data_buffer'])
order_history_df = pd.DataFrame(final_state['order_history'])

if not market_data_df.empty:
    market_data_df['timestamp'] = pd.to_datetime(market_data_df['timestamp'])
    market_data_df.set_index('timestamp', inplace=True)

if not order_history_df.empty:
    order_history_df['timestamp'] = pd.to_datetime(order_history_df['timestamp'])
    order_history_df.set_index('timestamp', inplace=True)


# --- Visualizations ---
plt.style.use('seaborn-v0_8-darkgrid')

# 1. Price History with Trades
if not market_data_df.empty:
    fig, ax1 = plt.subplots(figsize=(14, 7))
    ax1.plot(market_data_df.index, market_data_df['price'], label='Price', color='skyblue')
    ax1.set_xlabel('Time')
    ax1.set_ylabel('Price', color='skyblue')
    ax1.tick_params(axis='y', labelcolor='skyblue')

    # Plot Buy/Sell signals
    if not order_history_df.empty:
        buys = order_history_df[order_history_df['type'] == 'BUY']
        sells = order_history_df[order_history_df['type'] == 'SELL']
        ax1.scatter(buys.index, buys['price'], marker='^', color='green', s=100, label='Buy', alpha=0.7)
        ax1.scatter(sells.index, sells['price'], marker='v', color='red', s=100, label='Sell', alpha=0.7)

    ax1.set_title(f'{TRADING_SYMBOL} Price History with Trade Signals')
    fig.autofmt_xdate() # Auto-format x-axis labels for dates
    fig.tight_layout()
    plt.legend()
    plt.show()
else:
    print("No market data to plot price history.")

# 2. Portfolio Value Over Time (Simplified - using P&L from state)
# For a more accurate portfolio value, you'd track equity at each step
# Here we just show the final P&L
if 'profit_loss' in final_state['metrics']:
    fig, ax2 = plt.subplots(figsize=(14, 6))
    portfolio_value = [INITIAL_CASH + final_state['metrics']['profit_loss']]
    x_axis = [datetime.datetime.now()]
    ax2.plot(x_axis, portfolio_value, marker='o', linestyle='-', color='purple')
    ax2.set_xlabel('Time')
    ax2.set_ylabel('Portfolio Value', color='purple')
    ax2.set_title('Portfolio Value at End of Simulation')
    ax2.tick_params(axis='y', labelcolor='purple')
    ax2.grid(True)
    fig.autofmt_xdate()
    plt.show()
else:
    print("No P&L data to plot portfolio value.")

# 3. Trade Summary DataFrame
if not order_history_df.empty:
    print("\n--- Order History ---")
    display(order_history_df)
else:
    print("No orders executed.")
2026-06-12 11:25:15,148 - trading_app - INFO - Starting trading loop demonstration...
INFO:trading_app:Starting trading loop demonstration...
2026-06-12 11:25:15,152 - trading_app - INFO - Initializing execution state with cash: $100000.00 for symbol: DEMO
INFO:trading_app:Initializing execution state with cash: $100000.00 for symbol: DEMO
2026-06-12 11:25:15,156 - trading_app - DEBUG - Execution state initialized.
DEBUG:trading_app:Execution state initialized.
2026-06-12 11:25:15,160 - trading_app - INFO - Starting trading loop for 50 iterations with interval 0.2s.
INFO:trading_app:Starting trading loop for 50 iterations with interval 0.2s.
2026-06-12 11:25:15,164 - trading_app - DEBUG - Loop iteration 1/50
DEBUG:trading_app:Loop iteration 1/50
2026-06-12 11:25:16,495 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 16, 495206), 'price': 99.82, 'volume': 9814}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 16, 495206), 'price': 99.82, 'volume': 9814}
2026-06-12 11:25:16,498 - trading_app - DEBUG - Not enough market data for signal evaluation. Holding.
DEBUG:trading_app:Not enough market data for signal evaluation. Holding.
2026-06-12 11:25:16,500 - trading_app - DEBUG - Position managed: Current P&L: $0.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $0.00, Position: 0
2026-06-12 11:25:16,501 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.82
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.82
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
2026-06-12 11:25:16,705 - trading_app - DEBUG - Loop iteration 2/50
DEBUG:trading_app:Loop iteration 2/50
2026-06-12 11:25:18,205 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 18, 205137), 'price': 99.8, 'volume': 8819}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 18, 205137), 'price': 99.8, 'volume': 8819}
2026-06-12 11:25:18,210 - trading_app - DEBUG - Not enough market data for signal evaluation. Holding.
DEBUG:trading_app:Not enough market data for signal evaluation. Holding.
2026-06-12 11:25:18,217 - trading_app - DEBUG - Position managed: Current P&L: $0.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $0.00, Position: 0
2026-06-12 11:25:18,221 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.80
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.80
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
2026-06-12 11:25:18,467 - trading_app - DEBUG - Loop iteration 3/50
DEBUG:trading_app:Loop iteration 3/50
2026-06-12 11:25:19,563 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 19, 563184), 'price': 99.52, 'volume': 7201}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 19, 563184), 'price': 99.52, 'volume': 7201}
2026-06-12 11:25:19,574 - trading_app - DEBUG - Not enough market data for signal evaluation. Holding.
DEBUG:trading_app:Not enough market data for signal evaluation. Holding.
2026-06-12 11:25:19,593 - trading_app - DEBUG - Position managed: Current P&L: $0.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $0.00, Position: 0
2026-06-12 11:25:19,612 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.52
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.52
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
2026-06-12 11:25:19,821 - trading_app - DEBUG - Loop iteration 4/50
DEBUG:trading_app:Loop iteration 4/50
2026-06-12 11:25:21,249 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 21, 249364), 'price': 99.85, 'volume': 8102}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 21, 249364), 'price': 99.85, 'volume': 8102}
2026-06-12 11:25:21,253 - trading_app - DEBUG - Not enough market data for signal evaluation. Holding.
DEBUG:trading_app:Not enough market data for signal evaluation. Holding.
2026-06-12 11:25:21,255 - trading_app - DEBUG - Position managed: Current P&L: $0.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $0.00, Position: 0
2026-06-12 11:25:21,262 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.85
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.85
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
2026-06-12 11:25:21,526 - trading_app - DEBUG - Loop iteration 5/50
DEBUG:trading_app:Loop iteration 5/50
2026-06-12 11:25:22,621 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 22, 621224), 'price': 99.57, 'volume': 5517}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 22, 621224), 'price': 99.57, 'volume': 5517}
2026-06-12 11:25:22,624 - trading_app - DEBUG - Not enough market data for signal evaluation. Holding.
DEBUG:trading_app:Not enough market data for signal evaluation. Holding.
2026-06-12 11:25:22,629 - trading_app - DEBUG - Position managed: Current P&L: $0.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $0.00, Position: 0
2026-06-12 11:25:22,634 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.57
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.57
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
2026-06-12 11:25:22,935 - trading_app - DEBUG - Loop iteration 6/50
DEBUG:trading_app:Loop iteration 6/50
2026-06-12 11:25:24,102 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 24, 102360), 'price': 99.93, 'volume': 195}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 24, 102360), 'price': 99.93, 'volume': 195}
2026-06-12 11:25:24,104 - trading_app - DEBUG - Not enough market data for signal evaluation. Holding.
DEBUG:trading_app:Not enough market data for signal evaluation. Holding.
2026-06-12 11:25:24,106 - trading_app - DEBUG - Position managed: Current P&L: $0.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $0.00, Position: 0
2026-06-12 11:25:24,110 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.93
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.93
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
2026-06-12 11:25:24,362 - trading_app - DEBUG - Loop iteration 7/50
DEBUG:trading_app:Loop iteration 7/50
2026-06-12 11:25:25,743 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 25, 743685), 'price': 99.51, 'volume': 3198}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 25, 743685), 'price': 99.51, 'volume': 3198}
2026-06-12 11:25:25,745 - trading_app - DEBUG - Not enough market data for signal evaluation. Holding.
DEBUG:trading_app:Not enough market data for signal evaluation. Holding.
2026-06-12 11:25:25,748 - trading_app - DEBUG - Position managed: Current P&L: $0.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $0.00, Position: 0
2026-06-12 11:25:25,751 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.51
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.51
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
2026-06-12 11:25:25,983 - trading_app - DEBUG - Loop iteration 8/50
DEBUG:trading_app:Loop iteration 8/50
2026-06-12 11:25:27,424 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 27, 424160), 'price': 100.07, 'volume': 8653}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 27, 424160), 'price': 100.07, 'volume': 8653}
2026-06-12 11:25:27,427 - trading_app - DEBUG - Not enough market data for signal evaluation. Holding.
DEBUG:trading_app:Not enough market data for signal evaluation. Holding.
2026-06-12 11:25:27,429 - trading_app - DEBUG - Position managed: Current P&L: $0.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $0.00, Position: 0
2026-06-12 11:25:27,431 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $100.07
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $100.07
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
2026-06-12 11:25:27,724 - trading_app - DEBUG - Loop iteration 9/50
DEBUG:trading_app:Loop iteration 9/50
2026-06-12 11:25:28,972 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 28, 971950), 'price': 99.79, 'volume': 6875}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 28, 971950), 'price': 99.79, 'volume': 6875}
2026-06-12 11:25:28,975 - trading_app - DEBUG - Not enough market data for signal evaluation. Holding.
DEBUG:trading_app:Not enough market data for signal evaluation. Holding.
2026-06-12 11:25:28,977 - trading_app - DEBUG - Position managed: Current P&L: $0.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $0.00, Position: 0
2026-06-12 11:25:28,979 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.79
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $100000.00
        Position (DEMO): 0 units
        Current Price: $99.79
        Total Trades: 0
        Portfolio P&L: $0.00
        ----------------------
2026-06-12 11:25:29,266 - trading_app - DEBUG - Loop iteration 10/50
DEBUG:trading_app:Loop iteration 10/50
2026-06-12 11:25:30,461 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 30, 461217), 'price': 99.93, 'volume': 4340}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 30, 461217), 'price': 99.93, 'volume': 4340}
2026-06-12 11:25:30,465 - trading_app - INFO - BUY signal: Short SMA (99.85) > Long SMA (99.78) at price 99.93
INFO:trading_app:BUY signal: Short SMA (99.85) > Long SMA (99.78) at price 99.93
2026-06-12 11:25:31,078 - trading_app - INFO - Order ORD-1781263531078-776 FILLED: BUY 10 DEMO at $99.93 (Cost: $999.30)
INFO:trading_app:Order ORD-1781263531078-776 FILLED: BUY 10 DEMO at $99.93 (Cost: $999.30)
2026-06-12 11:25:31,081 - trading_app - DEBUG - Position managed: Current P&L: $0.00, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $0.00, Position: 10
2026-06-12 11:25:31,085 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99000.70
        Position (DEMO): 10 units
        Current Price: $99.93
        Total Trades: 1
        Portfolio P&L: $0.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99000.70
        Position (DEMO): 10 units
        Current Price: $99.93
        Total Trades: 1
        Portfolio P&L: $0.00
        ----------------------
2026-06-12 11:25:31,334 - trading_app - DEBUG - Loop iteration 11/50
DEBUG:trading_app:Loop iteration 11/50
2026-06-12 11:25:32,837 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 32, 837054), 'price': 100.02, 'volume': 7680}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 32, 837054), 'price': 100.02, 'volume': 7680}
2026-06-12 11:25:32,840 - trading_app - DEBUG - HOLD signal: Short SMA (99.86), Long SMA (99.80)
DEBUG:trading_app:HOLD signal: Short SMA (99.86), Long SMA (99.80)
2026-06-12 11:25:32,843 - trading_app - DEBUG - Position managed: Current P&L: $0.90, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $0.90, Position: 10
2026-06-12 11:25:32,846 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99000.70
        Position (DEMO): 10 units
        Current Price: $100.02
        Total Trades: 1
        Portfolio P&L: $0.90
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99000.70
        Position (DEMO): 10 units
        Current Price: $100.02
        Total Trades: 1
        Portfolio P&L: $0.90
        ----------------------
2026-06-12 11:25:33,103 - trading_app - DEBUG - Loop iteration 12/50
DEBUG:trading_app:Loop iteration 12/50
2026-06-12 11:25:34,144 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 34, 143966), 'price': 100.15, 'volume': 6546}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 34, 143966), 'price': 100.15, 'volume': 6546}
2026-06-12 11:25:34,147 - trading_app - DEBUG - HOLD signal: Short SMA (99.99), Long SMA (99.83)
DEBUG:trading_app:HOLD signal: Short SMA (99.99), Long SMA (99.83)
2026-06-12 11:25:34,150 - trading_app - DEBUG - Position managed: Current P&L: $2.20, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $2.20, Position: 10
2026-06-12 11:25:34,154 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99000.70
        Position (DEMO): 10 units
        Current Price: $100.15
        Total Trades: 1
        Portfolio P&L: $2.20
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99000.70
        Position (DEMO): 10 units
        Current Price: $100.15
        Total Trades: 1
        Portfolio P&L: $2.20
        ----------------------
2026-06-12 11:25:34,392 - trading_app - DEBUG - Loop iteration 13/50
DEBUG:trading_app:Loop iteration 13/50
2026-06-12 11:25:35,535 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 35, 535204), 'price': 100.46, 'volume': 6572}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 35, 535204), 'price': 100.46, 'volume': 6572}
2026-06-12 11:25:35,538 - trading_app - DEBUG - HOLD signal: Short SMA (100.07), Long SMA (99.93)
DEBUG:trading_app:HOLD signal: Short SMA (100.07), Long SMA (99.93)
2026-06-12 11:25:35,540 - trading_app - DEBUG - Position managed: Current P&L: $5.30, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $5.30, Position: 10
2026-06-12 11:25:35,541 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99000.70
        Position (DEMO): 10 units
        Current Price: $100.46
        Total Trades: 1
        Portfolio P&L: $5.30
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99000.70
        Position (DEMO): 10 units
        Current Price: $100.46
        Total Trades: 1
        Portfolio P&L: $5.30
        ----------------------
2026-06-12 11:25:35,779 - trading_app - DEBUG - Loop iteration 14/50
DEBUG:trading_app:Loop iteration 14/50
2026-06-12 11:25:37,172 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 37, 172570), 'price': 99.53, 'volume': 2684}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 37, 172570), 'price': 99.53, 'volume': 2684}
2026-06-12 11:25:37,176 - trading_app - DEBUG - HOLD signal: Short SMA (100.02), Long SMA (99.90)
DEBUG:trading_app:HOLD signal: Short SMA (100.02), Long SMA (99.90)
2026-06-12 11:25:37,178 - trading_app - DEBUG - Position managed: Current P&L: $-4.00, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $-4.00, Position: 10
2026-06-12 11:25:37,179 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99000.70
        Position (DEMO): 10 units
        Current Price: $99.53
        Total Trades: 1
        Portfolio P&L: $-4.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99000.70
        Position (DEMO): 10 units
        Current Price: $99.53
        Total Trades: 1
        Portfolio P&L: $-4.00
        ----------------------
2026-06-12 11:25:37,410 - trading_app - DEBUG - Loop iteration 15/50
DEBUG:trading_app:Loop iteration 15/50
2026-06-12 11:25:38,571 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 38, 571383), 'price': 99.63, 'volume': 872}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 38, 571383), 'price': 99.63, 'volume': 872}
2026-06-12 11:25:38,575 - trading_app - DEBUG - HOLD signal: Short SMA (99.96), Long SMA (99.90)
DEBUG:trading_app:HOLD signal: Short SMA (99.96), Long SMA (99.90)
2026-06-12 11:25:38,577 - trading_app - DEBUG - Position managed: Current P&L: $-3.00, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $-3.00, Position: 10
2026-06-12 11:25:38,579 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99000.70
        Position (DEMO): 10 units
        Current Price: $99.63
        Total Trades: 1
        Portfolio P&L: $-3.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99000.70
        Position (DEMO): 10 units
        Current Price: $99.63
        Total Trades: 1
        Portfolio P&L: $-3.00
        ----------------------
2026-06-12 11:25:38,856 - trading_app - DEBUG - Loop iteration 16/50
DEBUG:trading_app:Loop iteration 16/50
2026-06-12 11:25:40,339 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 40, 338882), 'price': 100.06, 'volume': 6225}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 40, 338882), 'price': 100.06, 'volume': 6225}
2026-06-12 11:25:40,342 - trading_app - DEBUG - HOLD signal: Short SMA (99.97), Long SMA (99.92)
DEBUG:trading_app:HOLD signal: Short SMA (99.97), Long SMA (99.92)
2026-06-12 11:25:40,344 - trading_app - DEBUG - Position managed: Current P&L: $1.30, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $1.30, Position: 10
2026-06-12 11:25:40,345 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99000.70
        Position (DEMO): 10 units
        Current Price: $100.06
        Total Trades: 1
        Portfolio P&L: $1.30
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99000.70
        Position (DEMO): 10 units
        Current Price: $100.06
        Total Trades: 1
        Portfolio P&L: $1.30
        ----------------------
2026-06-12 11:25:40,606 - trading_app - DEBUG - Loop iteration 17/50
DEBUG:trading_app:Loop iteration 17/50
2026-06-12 11:25:41,650 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 41, 650367), 'price': 100.04, 'volume': 8633}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 41, 650367), 'price': 100.04, 'volume': 8633}
2026-06-12 11:25:41,653 - trading_app - INFO - SELL signal: Short SMA (99.94) < Long SMA (99.97) at price 100.04
INFO:trading_app:SELL signal: Short SMA (99.94) < Long SMA (99.97) at price 100.04
2026-06-12 11:25:42,316 - trading_app - INFO - Order ORD-1781263542316-491 FILLED: SELL 10 DEMO at $100.04 (Revenue: $1000.40)
INFO:trading_app:Order ORD-1781263542316-491 FILLED: SELL 10 DEMO at $100.04 (Revenue: $1000.40)
2026-06-12 11:25:42,318 - trading_app - DEBUG - Position managed: Current P&L: $1.10, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $1.10, Position: 0
2026-06-12 11:25:42,321 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $100001.10
        Position (DEMO): 0 units
        Current Price: $100.04
        Total Trades: 2
        Portfolio P&L: $1.10
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $100001.10
        Position (DEMO): 0 units
        Current Price: $100.04
        Total Trades: 2
        Portfolio P&L: $1.10
        ----------------------
2026-06-12 11:25:42,526 - trading_app - DEBUG - Loop iteration 18/50
DEBUG:trading_app:Loop iteration 18/50
2026-06-12 11:25:43,544 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 43, 544398), 'price': 100.36, 'volume': 1308}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 43, 544398), 'price': 100.36, 'volume': 1308}
2026-06-12 11:25:43,547 - trading_app - INFO - SELL signal: Short SMA (99.92) < Long SMA (100.00) at price 100.36
INFO:trading_app:SELL signal: Short SMA (99.92) < Long SMA (100.00) at price 100.36
2026-06-12 11:25:44,101 - trading_app - WARNING - Order ORD-1781263544101-977 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263544101-977 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:25:44,104 - trading_app - DEBUG - Position managed: Current P&L: $1.10, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $1.10, Position: 0
2026-06-12 11:25:44,105 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $100001.10
        Position (DEMO): 0 units
        Current Price: $100.36
        Total Trades: 2
        Portfolio P&L: $1.10
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $100001.10
        Position (DEMO): 0 units
        Current Price: $100.36
        Total Trades: 2
        Portfolio P&L: $1.10
        ----------------------
2026-06-12 11:25:44,390 - trading_app - DEBUG - Loop iteration 19/50
DEBUG:trading_app:Loop iteration 19/50
2026-06-12 11:25:45,654 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 45, 654774), 'price': 99.58, 'volume': 8958}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 45, 654774), 'price': 99.58, 'volume': 8958}
2026-06-12 11:25:45,658 - trading_app - INFO - SELL signal: Short SMA (99.93) < Long SMA (99.98) at price 99.58
INFO:trading_app:SELL signal: Short SMA (99.93) < Long SMA (99.98) at price 99.58
2026-06-12 11:25:46,286 - trading_app - WARNING - Order ORD-1781263546286-749 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263546286-749 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:25:46,289 - trading_app - DEBUG - Position managed: Current P&L: $1.10, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $1.10, Position: 0
2026-06-12 11:25:46,293 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $100001.10
        Position (DEMO): 0 units
        Current Price: $99.58
        Total Trades: 2
        Portfolio P&L: $1.10
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $100001.10
        Position (DEMO): 0 units
        Current Price: $99.58
        Total Trades: 2
        Portfolio P&L: $1.10
        ----------------------
2026-06-12 11:25:46,509 - trading_app - DEBUG - Loop iteration 20/50
DEBUG:trading_app:Loop iteration 20/50
2026-06-12 11:25:47,521 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 47, 521856), 'price': 99.98, 'volume': 9628}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 47, 521856), 'price': 99.98, 'volume': 9628}
2026-06-12 11:25:47,526 - trading_app - INFO - BUY signal: Short SMA (100.00) > Long SMA (99.98) at price 99.98
INFO:trading_app:BUY signal: Short SMA (100.00) > Long SMA (99.98) at price 99.98
2026-06-12 11:25:48,202 - trading_app - INFO - Order ORD-1781263548202-619 FILLED: BUY 10 DEMO at $99.98 (Cost: $999.80)
INFO:trading_app:Order ORD-1781263548202-619 FILLED: BUY 10 DEMO at $99.98 (Cost: $999.80)
2026-06-12 11:25:48,204 - trading_app - DEBUG - Position managed: Current P&L: $1.10, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $1.10, Position: 10
2026-06-12 11:25:48,207 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99001.30
        Position (DEMO): 10 units
        Current Price: $99.98
        Total Trades: 3
        Portfolio P&L: $1.10
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99001.30
        Position (DEMO): 10 units
        Current Price: $99.98
        Total Trades: 3
        Portfolio P&L: $1.10
        ----------------------
2026-06-12 11:25:48,507 - trading_app - DEBUG - Loop iteration 21/50
DEBUG:trading_app:Loop iteration 21/50
2026-06-12 11:25:49,806 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 49, 806626), 'price': 100.28, 'volume': 6007}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 49, 806626), 'price': 100.28, 'volume': 6007}
2026-06-12 11:25:49,809 - trading_app - DEBUG - HOLD signal: Short SMA (100.05), Long SMA (100.01)
DEBUG:trading_app:HOLD signal: Short SMA (100.05), Long SMA (100.01)
2026-06-12 11:25:49,810 - trading_app - DEBUG - Position managed: Current P&L: $4.10, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $4.10, Position: 10
2026-06-12 11:25:49,813 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99001.30
        Position (DEMO): 10 units
        Current Price: $100.28
        Total Trades: 3
        Portfolio P&L: $4.10
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99001.30
        Position (DEMO): 10 units
        Current Price: $100.28
        Total Trades: 3
        Portfolio P&L: $4.10
        ----------------------
2026-06-12 11:25:50,101 - trading_app - DEBUG - Loop iteration 22/50
DEBUG:trading_app:Loop iteration 22/50
2026-06-12 11:25:51,501 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 51, 501180), 'price': 100.33, 'volume': 5279}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 51, 501180), 'price': 100.33, 'volume': 5279}
2026-06-12 11:25:51,503 - trading_app - DEBUG - HOLD signal: Short SMA (100.11), Long SMA (100.03)
DEBUG:trading_app:HOLD signal: Short SMA (100.11), Long SMA (100.03)
2026-06-12 11:25:51,506 - trading_app - DEBUG - Position managed: Current P&L: $4.60, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $4.60, Position: 10
2026-06-12 11:25:51,508 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99001.30
        Position (DEMO): 10 units
        Current Price: $100.33
        Total Trades: 3
        Portfolio P&L: $4.60
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99001.30
        Position (DEMO): 10 units
        Current Price: $100.33
        Total Trades: 3
        Portfolio P&L: $4.60
        ----------------------
2026-06-12 11:25:51,759 - trading_app - DEBUG - Loop iteration 23/50
DEBUG:trading_app:Loop iteration 23/50
2026-06-12 11:25:52,766 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 52, 766195), 'price': 100.39, 'volume': 5034}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 52, 766195), 'price': 100.39, 'volume': 5034}
2026-06-12 11:25:52,770 - trading_app - DEBUG - HOLD signal: Short SMA (100.11), Long SMA (100.02)
DEBUG:trading_app:HOLD signal: Short SMA (100.11), Long SMA (100.02)
2026-06-12 11:25:52,772 - trading_app - DEBUG - Position managed: Current P&L: $5.20, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $5.20, Position: 10
2026-06-12 11:25:52,775 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99001.30
        Position (DEMO): 10 units
        Current Price: $100.39
        Total Trades: 3
        Portfolio P&L: $5.20
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99001.30
        Position (DEMO): 10 units
        Current Price: $100.39
        Total Trades: 3
        Portfolio P&L: $5.20
        ----------------------
2026-06-12 11:25:53,058 - trading_app - DEBUG - Loop iteration 24/50
DEBUG:trading_app:Loop iteration 24/50
2026-06-12 11:25:54,196 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 54, 196874), 'price': 100.07, 'volume': 949}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 54, 196874), 'price': 100.07, 'volume': 949}
2026-06-12 11:25:54,199 - trading_app - DEBUG - HOLD signal: Short SMA (100.21), Long SMA (100.07)
DEBUG:trading_app:HOLD signal: Short SMA (100.21), Long SMA (100.07)
2026-06-12 11:25:54,202 - trading_app - DEBUG - Position managed: Current P&L: $2.00, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $2.00, Position: 10
2026-06-12 11:25:54,204 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99001.30
        Position (DEMO): 10 units
        Current Price: $100.07
        Total Trades: 3
        Portfolio P&L: $2.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99001.30
        Position (DEMO): 10 units
        Current Price: $100.07
        Total Trades: 3
        Portfolio P&L: $2.00
        ----------------------
2026-06-12 11:25:54,484 - trading_app - DEBUG - Loop iteration 25/50
DEBUG:trading_app:Loop iteration 25/50
2026-06-12 11:25:55,506 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 55, 506089), 'price': 100.09, 'volume': 1930}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 55, 506089), 'price': 100.09, 'volume': 1930}
2026-06-12 11:25:55,509 - trading_app - DEBUG - HOLD signal: Short SMA (100.23), Long SMA (100.12)
DEBUG:trading_app:HOLD signal: Short SMA (100.23), Long SMA (100.12)
2026-06-12 11:25:55,512 - trading_app - DEBUG - Position managed: Current P&L: $2.20, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $2.20, Position: 10
2026-06-12 11:25:55,516 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99001.30
        Position (DEMO): 10 units
        Current Price: $100.09
        Total Trades: 3
        Portfolio P&L: $2.20
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99001.30
        Position (DEMO): 10 units
        Current Price: $100.09
        Total Trades: 3
        Portfolio P&L: $2.20
        ----------------------
2026-06-12 11:25:55,768 - trading_app - DEBUG - Loop iteration 26/50
DEBUG:trading_app:Loop iteration 26/50
2026-06-12 11:25:57,087 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 57, 87162), 'price': 100.41, 'volume': 6362}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 57, 87162), 'price': 100.41, 'volume': 6362}
2026-06-12 11:25:57,091 - trading_app - DEBUG - HOLD signal: Short SMA (100.26), Long SMA (100.15)
DEBUG:trading_app:HOLD signal: Short SMA (100.26), Long SMA (100.15)
2026-06-12 11:25:57,094 - trading_app - DEBUG - Position managed: Current P&L: $5.40, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $5.40, Position: 10
2026-06-12 11:25:57,096 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99001.30
        Position (DEMO): 10 units
        Current Price: $100.41
        Total Trades: 3
        Portfolio P&L: $5.40
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99001.30
        Position (DEMO): 10 units
        Current Price: $100.41
        Total Trades: 3
        Portfolio P&L: $5.40
        ----------------------
2026-06-12 11:25:57,357 - trading_app - DEBUG - Loop iteration 27/50
DEBUG:trading_app:Loop iteration 27/50
2026-06-12 11:25:58,546 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 58, 546057), 'price': 99.53, 'volume': 9682}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 25, 58, 546057), 'price': 99.53, 'volume': 9682}
2026-06-12 11:25:58,551 - trading_app - INFO - SELL signal: Short SMA (100.10) < Long SMA (100.10) at price 99.53
INFO:trading_app:SELL signal: Short SMA (100.10) < Long SMA (100.10) at price 99.53
2026-06-12 11:25:59,205 - trading_app - INFO - Order ORD-1781263559204-38 FILLED: SELL 10 DEMO at $99.53 (Revenue: $995.30)
INFO:trading_app:Order ORD-1781263559204-38 FILLED: SELL 10 DEMO at $99.53 (Revenue: $995.30)
2026-06-12 11:25:59,207 - trading_app - DEBUG - Position managed: Current P&L: $-3.40, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-3.40, Position: 0
2026-06-12 11:25:59,209 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99996.60
        Position (DEMO): 0 units
        Current Price: $99.53
        Total Trades: 4
        Portfolio P&L: $-3.40
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99996.60
        Position (DEMO): 0 units
        Current Price: $99.53
        Total Trades: 4
        Portfolio P&L: $-3.40
        ----------------------
2026-06-12 11:25:59,469 - trading_app - DEBUG - Loop iteration 28/50
DEBUG:trading_app:Loop iteration 28/50
2026-06-12 11:26:00,933 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 0, 933237), 'price': 100.49, 'volume': 7305}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 0, 933237), 'price': 100.49, 'volume': 7305}
2026-06-12 11:26:00,936 - trading_app - INFO - BUY signal: Short SMA (100.12) > Long SMA (100.11) at price 100.49
INFO:trading_app:BUY signal: Short SMA (100.12) > Long SMA (100.11) at price 100.49
2026-06-12 11:26:01,462 - trading_app - INFO - Order ORD-1781263561462-748 FILLED: BUY 10 DEMO at $100.49 (Cost: $1004.90)
INFO:trading_app:Order ORD-1781263561462-748 FILLED: BUY 10 DEMO at $100.49 (Cost: $1004.90)
2026-06-12 11:26:01,465 - trading_app - DEBUG - Position managed: Current P&L: $-3.40, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $-3.40, Position: 10
2026-06-12 11:26:01,467 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $98991.70
        Position (DEMO): 10 units
        Current Price: $100.49
        Total Trades: 5
        Portfolio P&L: $-3.40
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $98991.70
        Position (DEMO): 10 units
        Current Price: $100.49
        Total Trades: 5
        Portfolio P&L: $-3.40
        ----------------------
2026-06-12 11:26:01,719 - trading_app - DEBUG - Loop iteration 29/50
DEBUG:trading_app:Loop iteration 29/50
2026-06-12 11:26:02,784 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 2, 784516), 'price': 100.33, 'volume': 7821}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 2, 784516), 'price': 100.33, 'volume': 7821}
2026-06-12 11:26:02,789 - trading_app - INFO - SELL signal: Short SMA (100.17) < Long SMA (100.19) at price 100.33
INFO:trading_app:SELL signal: Short SMA (100.17) < Long SMA (100.19) at price 100.33
2026-06-12 11:26:03,416 - trading_app - INFO - Order ORD-1781263563416-417 FILLED: SELL 10 DEMO at $100.33 (Revenue: $1003.30)
INFO:trading_app:Order ORD-1781263563416-417 FILLED: SELL 10 DEMO at $100.33 (Revenue: $1003.30)
2026-06-12 11:26:03,419 - trading_app - DEBUG - Position managed: Current P&L: $-5.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-5.00, Position: 0
2026-06-12 11:26:03,422 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $100.33
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $100.33
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
2026-06-12 11:26:03,670 - trading_app - DEBUG - Loop iteration 30/50
DEBUG:trading_app:Loop iteration 30/50
2026-06-12 11:26:04,960 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 4, 960544), 'price': 99.86, 'volume': 1673}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 4, 960544), 'price': 99.86, 'volume': 1673}
2026-06-12 11:26:04,964 - trading_app - INFO - SELL signal: Short SMA (100.12) < Long SMA (100.18) at price 99.86
INFO:trading_app:SELL signal: Short SMA (100.12) < Long SMA (100.18) at price 99.86
2026-06-12 11:26:05,633 - trading_app - WARNING - Order ORD-1781263565633-111 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263565633-111 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:26:05,635 - trading_app - DEBUG - Position managed: Current P&L: $-5.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-5.00, Position: 0
2026-06-12 11:26:05,639 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $99.86
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $99.86
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
2026-06-12 11:26:05,913 - trading_app - DEBUG - Loop iteration 31/50
DEBUG:trading_app:Loop iteration 31/50
2026-06-12 11:26:07,294 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 7, 294298), 'price': 99.78, 'volume': 5321}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 7, 294298), 'price': 99.78, 'volume': 5321}
2026-06-12 11:26:07,297 - trading_app - INFO - SELL signal: Short SMA (100.00) < Long SMA (100.13) at price 99.78
INFO:trading_app:SELL signal: Short SMA (100.00) < Long SMA (100.13) at price 99.78
2026-06-12 11:26:07,907 - trading_app - WARNING - Order ORD-1781263567907-331 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263567907-331 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:26:07,909 - trading_app - DEBUG - Position managed: Current P&L: $-5.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-5.00, Position: 0
2026-06-12 11:26:07,910 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $99.78
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $99.78
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
2026-06-12 11:26:08,173 - trading_app - DEBUG - Loop iteration 32/50
DEBUG:trading_app:Loop iteration 32/50
2026-06-12 11:26:09,219 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 9, 219076), 'price': 99.83, 'volume': 8634}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 9, 219076), 'price': 99.83, 'volume': 8634}
2026-06-12 11:26:09,222 - trading_app - INFO - SELL signal: Short SMA (100.06) < Long SMA (100.08) at price 99.83
INFO:trading_app:SELL signal: Short SMA (100.06) < Long SMA (100.08) at price 99.83
2026-06-12 11:26:09,758 - trading_app - WARNING - Order ORD-1781263569758-341 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263569758-341 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:26:09,762 - trading_app - DEBUG - Position managed: Current P&L: $-5.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-5.00, Position: 0
2026-06-12 11:26:09,764 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $99.83
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $99.83
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
2026-06-12 11:26:10,008 - trading_app - DEBUG - Loop iteration 33/50
DEBUG:trading_app:Loop iteration 33/50
2026-06-12 11:26:11,122 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 11, 121908), 'price': 99.78, 'volume': 884}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 11, 121908), 'price': 99.78, 'volume': 884}
2026-06-12 11:26:11,125 - trading_app - INFO - SELL signal: Short SMA (99.92) < Long SMA (100.02) at price 99.78
INFO:trading_app:SELL signal: Short SMA (99.92) < Long SMA (100.02) at price 99.78
2026-06-12 11:26:11,669 - trading_app - WARNING - Order ORD-1781263571669-964 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263571669-964 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:26:11,672 - trading_app - DEBUG - Position managed: Current P&L: $-5.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-5.00, Position: 0
2026-06-12 11:26:11,676 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $99.78
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $99.78
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
2026-06-12 11:26:11,924 - trading_app - DEBUG - Loop iteration 34/50
DEBUG:trading_app:Loop iteration 34/50
2026-06-12 11:26:12,978 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 12, 978434), 'price': 99.84, 'volume': 6975}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 12, 978434), 'price': 99.84, 'volume': 6975}
2026-06-12 11:26:12,983 - trading_app - INFO - SELL signal: Short SMA (99.82) < Long SMA (99.99) at price 99.84
INFO:trading_app:SELL signal: Short SMA (99.82) < Long SMA (99.99) at price 99.84
2026-06-12 11:26:13,617 - trading_app - WARNING - Order ORD-1781263573617-922 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263573617-922 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:26:13,619 - trading_app - DEBUG - Position managed: Current P&L: $-5.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-5.00, Position: 0
2026-06-12 11:26:13,622 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $99.84
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $99.84
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
2026-06-12 11:26:13,882 - trading_app - DEBUG - Loop iteration 35/50
DEBUG:trading_app:Loop iteration 35/50
2026-06-12 11:26:15,223 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 15, 222995), 'price': 100.45, 'volume': 7055}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 15, 222995), 'price': 100.45, 'volume': 7055}
2026-06-12 11:26:15,227 - trading_app - INFO - SELL signal: Short SMA (99.94) < Long SMA (100.03) at price 100.45
INFO:trading_app:SELL signal: Short SMA (99.94) < Long SMA (100.03) at price 100.45
2026-06-12 11:26:15,747 - trading_app - WARNING - Order ORD-1781263575747-873 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263575747-873 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:26:15,751 - trading_app - DEBUG - Position managed: Current P&L: $-5.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-5.00, Position: 0
2026-06-12 11:26:15,754 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $100.45
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $100.45
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
2026-06-12 11:26:15,966 - trading_app - DEBUG - Loop iteration 36/50
DEBUG:trading_app:Loop iteration 36/50
2026-06-12 11:26:17,126 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 17, 126000), 'price': 100.05, 'volume': 558}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 17, 126000), 'price': 100.05, 'volume': 558}
2026-06-12 11:26:17,129 - trading_app - INFO - SELL signal: Short SMA (99.99) < Long SMA (99.99) at price 100.05
INFO:trading_app:SELL signal: Short SMA (99.99) < Long SMA (99.99) at price 100.05
2026-06-12 11:26:17,692 - trading_app - WARNING - Order ORD-1781263577692-203 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263577692-203 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:26:17,695 - trading_app - DEBUG - Position managed: Current P&L: $-5.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-5.00, Position: 0
2026-06-12 11:26:17,698 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $100.05
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $100.05
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
2026-06-12 11:26:17,922 - trading_app - DEBUG - Loop iteration 37/50
DEBUG:trading_app:Loop iteration 37/50
2026-06-12 11:26:19,015 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 19, 15367), 'price': 100.01, 'volume': 9540}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 19, 15367), 'price': 100.01, 'volume': 9540}
2026-06-12 11:26:19,017 - trading_app - INFO - SELL signal: Short SMA (100.03) < Long SMA (100.04) at price 100.01
INFO:trading_app:SELL signal: Short SMA (100.03) < Long SMA (100.04) at price 100.01
2026-06-12 11:26:19,551 - trading_app - WARNING - Order ORD-1781263579551-522 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263579551-522 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:26:19,553 - trading_app - DEBUG - Position managed: Current P&L: $-5.00, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-5.00, Position: 0
2026-06-12 11:26:19,556 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $100.01
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.00
        Position (DEMO): 0 units
        Current Price: $100.01
        Total Trades: 6
        Portfolio P&L: $-5.00
        ----------------------
2026-06-12 11:26:19,819 - trading_app - DEBUG - Loop iteration 38/50
DEBUG:trading_app:Loop iteration 38/50
2026-06-12 11:26:21,178 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 21, 177926), 'price': 100.35, 'volume': 1227}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 21, 177926), 'price': 100.35, 'volume': 1227}
2026-06-12 11:26:21,181 - trading_app - INFO - BUY signal: Short SMA (100.14) > Long SMA (100.03) at price 100.35
INFO:trading_app:BUY signal: Short SMA (100.14) > Long SMA (100.03) at price 100.35
2026-06-12 11:26:21,808 - trading_app - INFO - Order ORD-1781263581807-16 FILLED: BUY 10 DEMO at $100.35 (Cost: $1003.50)
INFO:trading_app:Order ORD-1781263581807-16 FILLED: BUY 10 DEMO at $100.35 (Cost: $1003.50)
2026-06-12 11:26:21,811 - trading_app - DEBUG - Position managed: Current P&L: $-5.00, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $-5.00, Position: 10
2026-06-12 11:26:21,813 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $98991.50
        Position (DEMO): 10 units
        Current Price: $100.35
        Total Trades: 7
        Portfolio P&L: $-5.00
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $98991.50
        Position (DEMO): 10 units
        Current Price: $100.35
        Total Trades: 7
        Portfolio P&L: $-5.00
        ----------------------
2026-06-12 11:26:22,084 - trading_app - DEBUG - Loop iteration 39/50
DEBUG:trading_app:Loop iteration 39/50
2026-06-12 11:26:23,517 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 23, 517845), 'price': 100.33, 'volume': 3622}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 23, 517845), 'price': 100.33, 'volume': 3622}
2026-06-12 11:26:23,521 - trading_app - DEBUG - HOLD signal: Short SMA (100.24), Long SMA (100.03)
DEBUG:trading_app:HOLD signal: Short SMA (100.24), Long SMA (100.03)
2026-06-12 11:26:23,524 - trading_app - DEBUG - Position managed: Current P&L: $-5.20, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $-5.20, Position: 10
2026-06-12 11:26:23,528 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $98991.50
        Position (DEMO): 10 units
        Current Price: $100.33
        Total Trades: 7
        Portfolio P&L: $-5.20
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $98991.50
        Position (DEMO): 10 units
        Current Price: $100.33
        Total Trades: 7
        Portfolio P&L: $-5.20
        ----------------------
2026-06-12 11:26:23,760 - trading_app - DEBUG - Loop iteration 40/50
DEBUG:trading_app:Loop iteration 40/50
2026-06-12 11:26:24,777 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 24, 777689), 'price': 100.33, 'volume': 3737}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 24, 777689), 'price': 100.33, 'volume': 3737}
2026-06-12 11:26:24,782 - trading_app - DEBUG - HOLD signal: Short SMA (100.21), Long SMA (100.08)
DEBUG:trading_app:HOLD signal: Short SMA (100.21), Long SMA (100.08)
2026-06-12 11:26:24,786 - trading_app - DEBUG - Position managed: Current P&L: $-5.20, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $-5.20, Position: 10
2026-06-12 11:26:24,787 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $98991.50
        Position (DEMO): 10 units
        Current Price: $100.33
        Total Trades: 7
        Portfolio P&L: $-5.20
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $98991.50
        Position (DEMO): 10 units
        Current Price: $100.33
        Total Trades: 7
        Portfolio P&L: $-5.20
        ----------------------
2026-06-12 11:26:25,046 - trading_app - DEBUG - Loop iteration 41/50
DEBUG:trading_app:Loop iteration 41/50
2026-06-12 11:26:26,215 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 26, 214923), 'price': 100.24, 'volume': 3540}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 26, 214923), 'price': 100.24, 'volume': 3540}
2026-06-12 11:26:26,218 - trading_app - DEBUG - HOLD signal: Short SMA (100.25), Long SMA (100.12)
DEBUG:trading_app:HOLD signal: Short SMA (100.25), Long SMA (100.12)
2026-06-12 11:26:26,220 - trading_app - DEBUG - Position managed: Current P&L: $-6.10, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $-6.10, Position: 10
2026-06-12 11:26:26,223 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $98991.50
        Position (DEMO): 10 units
        Current Price: $100.24
        Total Trades: 7
        Portfolio P&L: $-6.10
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $98991.50
        Position (DEMO): 10 units
        Current Price: $100.24
        Total Trades: 7
        Portfolio P&L: $-6.10
        ----------------------
2026-06-12 11:26:26,483 - trading_app - DEBUG - Loop iteration 42/50
DEBUG:trading_app:Loop iteration 42/50
2026-06-12 11:26:27,746 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 27, 746623), 'price': 99.84, 'volume': 3823}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 27, 746623), 'price': 99.84, 'volume': 3823}
2026-06-12 11:26:27,750 - trading_app - DEBUG - HOLD signal: Short SMA (100.22), Long SMA (100.12)
DEBUG:trading_app:HOLD signal: Short SMA (100.22), Long SMA (100.12)
2026-06-12 11:26:27,756 - trading_app - DEBUG - Position managed: Current P&L: $-10.10, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $-10.10, Position: 10
2026-06-12 11:26:27,758 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $98991.50
        Position (DEMO): 10 units
        Current Price: $99.84
        Total Trades: 7
        Portfolio P&L: $-10.10
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $98991.50
        Position (DEMO): 10 units
        Current Price: $99.84
        Total Trades: 7
        Portfolio P&L: $-10.10
        ----------------------
2026-06-12 11:26:27,983 - trading_app - DEBUG - Loop iteration 43/50
DEBUG:trading_app:Loop iteration 43/50
2026-06-12 11:26:29,171 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 29, 171259), 'price': 100.31, 'volume': 8214}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 29, 171259), 'price': 100.31, 'volume': 8214}
2026-06-12 11:26:29,174 - trading_app - DEBUG - HOLD signal: Short SMA (100.21), Long SMA (100.17)
DEBUG:trading_app:HOLD signal: Short SMA (100.21), Long SMA (100.17)
2026-06-12 11:26:29,177 - trading_app - DEBUG - Position managed: Current P&L: $-5.40, Position: 10
DEBUG:trading_app:Position managed: Current P&L: $-5.40, Position: 10
2026-06-12 11:26:29,179 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $98991.50
        Position (DEMO): 10 units
        Current Price: $100.31
        Total Trades: 7
        Portfolio P&L: $-5.40
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $98991.50
        Position (DEMO): 10 units
        Current Price: $100.31
        Total Trades: 7
        Portfolio P&L: $-5.40
        ----------------------
2026-06-12 11:26:29,454 - trading_app - DEBUG - Loop iteration 44/50
DEBUG:trading_app:Loop iteration 44/50
2026-06-12 11:26:30,657 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 30, 657289), 'price': 100.39, 'volume': 6278}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 30, 657289), 'price': 100.39, 'volume': 6278}
2026-06-12 11:26:30,660 - trading_app - INFO - SELL signal: Short SMA (100.22) < Long SMA (100.23) at price 100.39
INFO:trading_app:SELL signal: Short SMA (100.22) < Long SMA (100.23) at price 100.39
2026-06-12 11:26:31,224 - trading_app - INFO - Order ORD-1781263591223-497 FILLED: SELL 10 DEMO at $100.39 (Revenue: $1003.90)
INFO:trading_app:Order ORD-1781263591223-497 FILLED: SELL 10 DEMO at $100.39 (Revenue: $1003.90)
2026-06-12 11:26:31,225 - trading_app - DEBUG - Position managed: Current P&L: $-4.60, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-4.60, Position: 0
2026-06-12 11:26:31,226 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.40
        Position (DEMO): 0 units
        Current Price: $100.39
        Total Trades: 8
        Portfolio P&L: $-4.60
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.40
        Position (DEMO): 0 units
        Current Price: $100.39
        Total Trades: 8
        Portfolio P&L: $-4.60
        ----------------------
2026-06-12 11:26:31,449 - trading_app - DEBUG - Loop iteration 45/50
DEBUG:trading_app:Loop iteration 45/50
2026-06-12 11:26:32,537 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 32, 537224), 'price': 99.6, 'volume': 768}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 32, 537224), 'price': 99.6, 'volume': 768}
2026-06-12 11:26:32,540 - trading_app - INFO - SELL signal: Short SMA (100.08) < Long SMA (100.15) at price 99.60
INFO:trading_app:SELL signal: Short SMA (100.08) < Long SMA (100.15) at price 99.60
2026-06-12 11:26:33,129 - trading_app - WARNING - Order ORD-1781263593129-623 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263593129-623 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:26:33,131 - trading_app - DEBUG - Position managed: Current P&L: $-4.60, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-4.60, Position: 0
2026-06-12 11:26:33,133 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.40
        Position (DEMO): 0 units
        Current Price: $99.60
        Total Trades: 8
        Portfolio P&L: $-4.60
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.40
        Position (DEMO): 0 units
        Current Price: $99.60
        Total Trades: 8
        Portfolio P&L: $-4.60
        ----------------------
2026-06-12 11:26:33,388 - trading_app - DEBUG - Loop iteration 46/50
DEBUG:trading_app:Loop iteration 46/50
2026-06-12 11:26:34,860 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 34, 860038), 'price': 99.99, 'volume': 8110}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 34, 860038), 'price': 99.99, 'volume': 8110}
2026-06-12 11:26:34,863 - trading_app - INFO - SELL signal: Short SMA (100.03) < Long SMA (100.14) at price 99.99
INFO:trading_app:SELL signal: Short SMA (100.03) < Long SMA (100.14) at price 99.99
2026-06-12 11:26:35,478 - trading_app - WARNING - Attempt 1/3: Order execution failed for SELL 10 DEMO: Simulated API connection error.
WARNING:trading_app:Attempt 1/3: Order execution failed for SELL 10 DEMO: Simulated API connection error.
2026-06-12 11:26:35,481 - trading_app - INFO - Retrying in 0.88 seconds...
INFO:trading_app:Retrying in 0.88 seconds...
2026-06-12 11:26:37,041 - trading_app - WARNING - Order ORD-1781263597041-334 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263597041-334 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:26:37,043 - trading_app - DEBUG - Position managed: Current P&L: $-4.60, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-4.60, Position: 0
2026-06-12 11:26:37,047 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.40
        Position (DEMO): 0 units
        Current Price: $99.99
        Total Trades: 8
        Portfolio P&L: $-4.60
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.40
        Position (DEMO): 0 units
        Current Price: $99.99
        Total Trades: 8
        Portfolio P&L: $-4.60
        ----------------------
2026-06-12 11:26:37,304 - trading_app - DEBUG - Loop iteration 47/50
DEBUG:trading_app:Loop iteration 47/50
2026-06-12 11:26:38,741 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 38, 741413), 'price': 99.79, 'volume': 5848}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 38, 741413), 'price': 99.79, 'volume': 5848}
2026-06-12 11:26:38,745 - trading_app - INFO - SELL signal: Short SMA (100.02) < Long SMA (100.12) at price 99.79
INFO:trading_app:SELL signal: Short SMA (100.02) < Long SMA (100.12) at price 99.79
2026-06-12 11:26:39,394 - trading_app - WARNING - Attempt 1/3: Order execution failed for SELL 10 DEMO: Simulated API connection error.
WARNING:trading_app:Attempt 1/3: Order execution failed for SELL 10 DEMO: Simulated API connection error.
2026-06-12 11:26:39,397 - trading_app - INFO - Retrying in 0.90 seconds...
INFO:trading_app:Retrying in 0.90 seconds...
2026-06-12 11:26:40,968 - trading_app - WARNING - Order ORD-1781263600968-249 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263600968-249 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:26:40,972 - trading_app - DEBUG - Position managed: Current P&L: $-4.60, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-4.60, Position: 0
2026-06-12 11:26:40,973 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.40
        Position (DEMO): 0 units
        Current Price: $99.79
        Total Trades: 8
        Portfolio P&L: $-4.60
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.40
        Position (DEMO): 0 units
        Current Price: $99.79
        Total Trades: 8
        Portfolio P&L: $-4.60
        ----------------------
2026-06-12 11:26:41,232 - trading_app - DEBUG - Loop iteration 48/50
DEBUG:trading_app:Loop iteration 48/50
2026-06-12 11:26:42,264 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 42, 264084), 'price': 100.06, 'volume': 1847}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 42, 264084), 'price': 100.06, 'volume': 1847}
2026-06-12 11:26:42,267 - trading_app - INFO - SELL signal: Short SMA (99.97) < Long SMA (100.09) at price 100.06
INFO:trading_app:SELL signal: Short SMA (99.97) < Long SMA (100.09) at price 100.06
2026-06-12 11:26:42,883 - trading_app - WARNING - Attempt 1/3: Order execution failed for SELL 10 DEMO: Simulated API connection error.
WARNING:trading_app:Attempt 1/3: Order execution failed for SELL 10 DEMO: Simulated API connection error.
2026-06-12 11:26:42,886 - trading_app - INFO - Retrying in 0.54 seconds...
INFO:trading_app:Retrying in 0.54 seconds...
2026-06-12 11:26:43,993 - trading_app - WARNING - Order ORD-1781263603993-353 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263603993-353 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:26:43,996 - trading_app - DEBUG - Position managed: Current P&L: $-4.60, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-4.60, Position: 0
2026-06-12 11:26:43,998 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.40
        Position (DEMO): 0 units
        Current Price: $100.06
        Total Trades: 8
        Portfolio P&L: $-4.60
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.40
        Position (DEMO): 0 units
        Current Price: $100.06
        Total Trades: 8
        Portfolio P&L: $-4.60
        ----------------------
2026-06-12 11:26:44,212 - trading_app - DEBUG - Loop iteration 49/50
DEBUG:trading_app:Loop iteration 49/50
2026-06-12 11:26:45,505 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 45, 505717), 'price': 100.24, 'volume': 4727}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 45, 505717), 'price': 100.24, 'volume': 4727}
2026-06-12 11:26:45,508 - trading_app - INFO - SELL signal: Short SMA (99.94) < Long SMA (100.08) at price 100.24
INFO:trading_app:SELL signal: Short SMA (99.94) < Long SMA (100.08) at price 100.24
2026-06-12 11:26:46,067 - trading_app - WARNING - Order ORD-1781263606067-453 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263606067-453 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:26:46,070 - trading_app - DEBUG - Position managed: Current P&L: $-4.60, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-4.60, Position: 0
2026-06-12 11:26:46,071 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.40
        Position (DEMO): 0 units
        Current Price: $100.24
        Total Trades: 8
        Portfolio P&L: $-4.60
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.40
        Position (DEMO): 0 units
        Current Price: $100.24
        Total Trades: 8
        Portfolio P&L: $-4.60
        ----------------------
2026-06-12 11:26:46,276 - trading_app - DEBUG - Loop iteration 50/50
DEBUG:trading_app:Loop iteration 50/50
2026-06-12 11:26:47,683 - trading_app - DEBUG - Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 47, 683215), 'price': 99.53, 'volume': 803}
DEBUG:trading_app:Fetched market data for DEMO: {'timestamp': datetime.datetime(2026, 6, 12, 11, 26, 47, 683215), 'price': 99.53, 'volume': 803}
2026-06-12 11:26:47,687 - trading_app - INFO - SELL signal: Short SMA (99.92) < Long SMA (100.00) at price 99.53
INFO:trading_app:SELL signal: Short SMA (99.92) < Long SMA (100.00) at price 99.53
2026-06-12 11:26:48,237 - trading_app - WARNING - Order ORD-1781263608237-383 REJECTED: Insufficient position for SELL 10 DEMO.
WARNING:trading_app:Order ORD-1781263608237-383 REJECTED: Insufficient position for SELL 10 DEMO.
2026-06-12 11:26:48,240 - trading_app - DEBUG - Position managed: Current P&L: $-4.60, Position: 0
DEBUG:trading_app:Position managed: Current P&L: $-4.60, Position: 0
2026-06-12 11:26:48,243 - trading_app - INFO - --- Metrics Snapshot ---
        Cash: $99995.40
        Position (DEMO): 0 units
        Current Price: $99.53
        Total Trades: 8
        Portfolio P&L: $-4.60
        ----------------------
INFO:trading_app:--- Metrics Snapshot ---
        Cash: $99995.40
        Position (DEMO): 0 units
        Current Price: $99.53
        Total Trades: 8
        Portfolio P&L: $-4.60
        ----------------------
2026-06-12 11:26:48,523 - trading_app - INFO - Trading loop finished.
INFO:trading_app:Trading loop finished.
2026-06-12 11:26:48,526 - trading_app - INFO - Demonstration complete. Final state and metrics:
INFO:trading_app:Demonstration complete. Final state and metrics:

--- Final Summary ---
Initial Cash: $100000.00
Final Cash: $99995.40
Final Position (DEMO): 0 units
Current Market Price: $99.53
Unrealized Position Value: $0.00
Total Trades Executed: 8
Total Portfolio P&L: $-4.60
cell output
cell output

--- Order History ---
order_id symbol type quantity price cost_revenue status
timestamp
2026-06-12 11:25:31.081730 ORD-1781263531078-776 DEMO BUY 10 99.93 999.3 FILLED
2026-06-12 11:25:42.318563 ORD-1781263542316-491 DEMO SELL 10 100.04 1000.4 FILLED
2026-06-12 11:25:44.103955 ORD-1781263544101-977 DEMO SELL 10 100.36 1003.6 REJECTED
2026-06-12 11:25:46.289193 ORD-1781263546286-749 DEMO SELL 10 99.58 995.8 REJECTED
2026-06-12 11:25:48.204910 ORD-1781263548202-619 DEMO BUY 10 99.98 999.8 FILLED
2026-06-12 11:25:59.207732 ORD-1781263559204-38 DEMO SELL 10 99.53 995.3 FILLED
2026-06-12 11:26:01.465570 ORD-1781263561462-748 DEMO BUY 10 100.49 1004.9 FILLED
2026-06-12 11:26:03.419652 ORD-1781263563416-417 DEMO SELL 10 100.33 1003.3 FILLED
2026-06-12 11:26:05.635075 ORD-1781263565633-111 DEMO SELL 10 99.86 998.6 REJECTED
2026-06-12 11:26:07.909638 ORD-1781263567907-331 DEMO SELL 10 99.78 997.8 REJECTED
2026-06-12 11:26:09.762267 ORD-1781263569758-341 DEMO SELL 10 99.83 998.3 REJECTED
2026-06-12 11:26:11.672631 ORD-1781263571669-964 DEMO SELL 10 99.78 997.8 REJECTED
2026-06-12 11:26:13.619593 ORD-1781263573617-922 DEMO SELL 10 99.84 998.4 REJECTED
2026-06-12 11:26:15.751195 ORD-1781263575747-873 DEMO SELL 10 100.45 1004.5 REJECTED
2026-06-12 11:26:17.695698 ORD-1781263577692-203 DEMO SELL 10 100.05 1000.5 REJECTED
2026-06-12 11:26:19.553839 ORD-1781263579551-522 DEMO SELL 10 100.01 1000.1 REJECTED
2026-06-12 11:26:21.811167 ORD-1781263581807-16 DEMO BUY 10 100.35 1003.5 FILLED
2026-06-12 11:26:31.225433 ORD-1781263591223-497 DEMO SELL 10 100.39 1003.9 FILLED
2026-06-12 11:26:33.131060 ORD-1781263593129-623 DEMO SELL 10 99.60 996.0 REJECTED
2026-06-12 11:26:37.043652 ORD-1781263597041-334 DEMO SELL 10 99.99 999.9 REJECTED
2026-06-12 11:26:40.971996 ORD-1781263600968-249 DEMO SELL 10 99.79 997.9 REJECTED
2026-06-12 11:26:43.996309 ORD-1781263603993-353 DEMO SELL 10 100.06 1000.6 REJECTED
2026-06-12 11:26:46.070213 ORD-1781263606067-453 DEMO SELL 10 100.24 1002.4 REJECTED
2026-06-12 11:26:48.240935 ORD-1781263608237-383 DEMO SELL 10 99.53 995.3 REJECTED

Production Considerations

Moving a live trading execution loop to production requires careful attention to several factors to ensure reliability, security, and performance.

ConsiderationBest Practice
Error Handling & RetriesImplement comprehensive try/except blocks; use exponential backoff for network/API calls.
Logging & MonitoringUse structured logging (JSON format); integrate with monitoring systems (e.g., Prometheus, Grafana).
Latency OptimizationMinimize network hops; colocate with exchange/broker servers if possible; optimize code for speed.
SecurityProtect API keys (environment variables, secret managers); use secure communication (HTTPS); proper access controls.
ConcurrencyUse asynchronous programming (asyncio) or multi-threading/processing for parallel tasks (e.g., fetching data from multiple sources).
State PersistencePeriodically save trading state to a persistent store (database, file) to recover from crashes.
Configuration ManagementExternalize all configurable parameters (e.g., symbols, quantities, API endpoints) from code.
Circuit BreakersImplement circuit breakers to gracefully handle repeated failures from external services.
AlertingSet up alerts for critical events (e.g., execution failures, abnormal P&L, system downtime).
Backtesting & SimulationThoroughly backtest strategies and simulate market conditions before live deployment.
Redundancy & FailoverDesign for high availability; have backup systems or failover mechanisms in place.
Regulatory ComplianceEnsure all trading activities comply with relevant financial regulations.

Conclusion

This notebook has provided a structured approach to building a main live trading execution loop in Google Colab. We covered the essential components: state management, data fetching with error handling, signal evaluation, order execution with retries, position management, and metrics logging. The demonstration illustrated how these components interact, producing mock trading activity and simple visualizations of price action and portfolio value.

Key takeaways include the importance of modularity, robust error handling with exponential backoff, detailed logging for observability, and the use of dictionaries for flexible state management. While this example uses simulated data and a simple strategy, the underlying architecture provides a solid foundation for more complex and production-ready trading systems.