TradeSight

repository·main·Indexed 41 days ago

https://github.com/rmbell09-lang/tradesight

A self-hosted strategy research and broker-simulated paper-trading lab version 1.2.0. It features explicit evidence gates for strategy lifecycles (Candidate to Champion), data provenance labeling (REAL, PAPER, DEMO, STALE, UNVERIFIED), and a file-based polling notification system. Designed strictly as a research tool, it supports Alpaca paper endpoints and includes safety boundaries such as a 15% position sizing cap and a 7-day stop-loss cooldown.

Tokens
31.4K
Snippets
100
Records
151
Agent score
51%

What's inside TradeSight

  1. Overview of TradeSight core features

    main

    TradeSight is a self-hosted system designed for tournament-style trading strategy evolution. It automates the process of defining a base strategy, generating parameter variations, backtesting them against historical data, and evolving the 'winners' to find optimal parameters.

    Key Features:

    • Strategy Evolution: Automatically generates and tests 100+ parameter variations, using a tournament-style elimination and 'breeding' process.
    • Web Dashboard: Accessible at localhost:5000 for monitoring markets, tournaments, and paper trades.
    • Market Coverage: Includes Polymarket prediction markets and a US equities scanner.
    • Technical Analysis: Automatically applies 15+ technical indicators.
    • Paper Trading: Integrates with Alpaca's free API for simulated trading with real P&L tracking.
    • Automation: Supports overnight automation via cron.
    • Demo Mode: Allows running the system without requiring any API keys.

    Technology Stack: Python, Flask, and SQLite. The system is designed to run locally without Docker, Redis, or cloud dependencies.

  2. Core features and capabilities of TradeSight

    main

    TradeSight is a self-hosted strategy research and broker-simulated paper trading system. It is designed to provide an inspectable local environment for algorithmic trading hobbyists.

    Key Capabilities

    • Strategy Evaluation: Evaluates strategy families (RSI, MACD, momentum, Bollinger, confluence, etc.) against real historical market data.
    • Evidence Preservation: Maintains out-of-sample, walk-forward, regime, multiple-testing, and Monte Carlo evidence to support promotion decisions.
    • Alpaca Integration: Connects to Alpaca's paper endpoint to simulate orders, positions, and fills.
    • Accounting Reconciliation: Reconciles broker and local accounting starting from a defined paper-account epoch. It excludes legacy local profit that lacks complete broker proof from trusted performance.
    • Trade Explanation: Provides detailed records of trades including strategy, signal, regime, sizing, risk, and broker evidence.
    • Stock Scanning: Includes a stock scanner powered by verified Alpaca data.
    • Safety & Readiness: Features a 'Live Trading Readiness' checklist and a paper-only lock to prevent accidental live execution.
  3. Core features of TradeSight

    main

    TradeSight includes the following capabilities:

    • Scanning: Supports Polymarket and US equities scanning.
    • Technical Analysis: Automatically applies 15+ technical indicators.
    • Paper Trading: Integrates with Alpaca (using a free account) for non-live testing.
    • Automation: Uses cron for overnight strategy evolution.
    • Dashboard: A web-based dashboard accessible at localhost:5000.
    • Demo Mode: Allows testing the system without requiring any API keys.
  4. Avoid Signal-at-Close / Entry-at-Close Bias in Backtesting

    main

    A critical lookahead bias exists in the current BacktestEngine.run_backtest() implementation. The engine calls the strategy function with data.iloc[i], which includes the closing price of bar i, and then immediately fills the entry at that same close. This allows the strategy to 'know' the close before trading it, artificially inflating PnL.

    To ensure realistic results, you must ensure that a signal generated at bar i is executed at the earliest possible price in the future (typically the open of bar i+1).

    # Recommended Fix Pattern:
    # Instead of filling at bar i's close:
    # entry_price = data.iloc[i]['close'] 
    
    # Use bar i+1's open:
    entry_price = data.iloc[i+1]['open']
  5. The Strategy Lifecycle

    main

    Strategies in TradeSight progress through a rigorous multi-stage lifecycle. A strategy must pass through several evidence gates (including out-of-sample testing, walk-forward analysis, Monte Carlo simulations, and forward-paper trading) before it can be promoted to a 'Champion' status. A challenger cannot promote automatically.

    Lifecycle stages: Candidate → Backtest → Out-of-sample → Shadow paper → Qualified → Champion → Retired

  6. How the Circuit Breaker logic works

    main

    The Circuit Breaker is designed to protect capital by halting new entries once a specific loss threshold is met, while still allowing existing positions to be managed.

    Core Behavior

    • New Entries: Blocked. When the daily_loss_limit is reached, scan_and_trade() returns early and execute_signal() returns False.
    • Existing Positions: Protected. The circuit breaker does not block _check_stop_loss_take_profit(). This ensures that Stop Loss (SL) and Take Profit (TP) orders continue to run to manage current exposure.
    • Reset Mechanism: The breaker is session-based (using an instance variable _daily_loss_limit_reached). For long-running sessions, it should check if the current date has changed (Midnight ET) to reset the flag.

    Implementation Logic

    1. The system calculates daily_pnl by summing realized_pnl from the positions table in positions.db where status='closed' and the exit_time is within the current day.
    2. If daily_pnl <= -limit, the _daily_loss_limit_reached flag is set to True and a warning is logged.
    # Logic summary for _check_daily_loss_limit()
    # 1. Check if already triggered
    # 2. Query SQLite: SELECT SUM(realized_pnl) FROM positions WHERE status='closed' AND exit_time >= today_start
    # 3. If daily_pnl <= -limit: set _daily_loss_limit_reached = True
  7. Parse Alpaca trade_updates fill and partial_fill events

    main

    When listening to the trade_updates stream, you must handle two distinct event types to track order execution: fill and partial_fill. Both events follow a similar structure within the order object. To ensure accurate accounting, you should accumulate partial fills before posting the final state to the MC activity endpoint.

    // Example fill event shape
    {
      "stream": "trade_updates",
      "data": {
        "event": "fill",
        "order": {
          "symbol": "AAPL",
          "filled_qty": 1,
          "filled_avg_price": 150.00
        }
      }
    }
    // Note: partial_fill uses event="partial_fill"
  8. How TradeSight strategy evolution works

    main

    TradeSight uses a tournament-style genetic algorithm to evolve trading strategies overnight. The process follows these steps:

    1. Define Base Strategy: You provide a starting point (e.g., RSI Mean Reversion).
    2. Variation Generation: The system generates 100+ variations by mutating parameters such as thresholds, timeouts, and position sizes.
    3. Backtesting: Each variation is backtested against historical market data.
    4. Selection & Breeding: Losers are eliminated, and winning variations are used to breed the next generation of variations.
    5. Reporting: A morning report is generated showing the 'champion' strategy along with full performance statistics (e.g., P&L, Sharpe ratio).
  9. Understand TradeSight data labels

    main

    TradeSight uses specific labels to categorize the provenance and reliability of data. This ensures that unverified or synthetic data is not confused with broker-backed evidence.

    • REAL: verified external market evidence.
    • PAPER: broker-simulated trading evidence (e.g., from Alpaca paper endpoints).
    • DEMO: explicit development-only synthetic data.
    • STALE: retained data too old to support a current decision.
    • UNVERIFIED: local evidence that has not met broker-proof requirements.
  10. Redesign confidence scoring logic

    main

    The current confidence scoring algorithm is flawed because it correlates confidence with volume/liquidity rather than price edge. High-volume markets that are near 50/50 (e.g., 0.48/0.52) are currently receiving high confidence scores, which produces false signals.

    Correct Logic Pattern: Real opportunity should be defined as price mispricing + edge (e.g., the market implies a 35% probability, but your model estimates 60%). Confidence scoring should be based on the deviation of the market price from your 'fair value' estimate, rather than raw volume.

  11. Understand Paper Trader safety boundaries and constraints

    main

    The Paper Trader includes several hardcoded safety guards and constraints to manage risk and diversification:

    • Position Sizing Cap: Maximum position size is enforced at 15% (0.15) to ensure diversification.
    • Cash Reserve Guard: A guard is in place to protect cash reserves.
    • Stop-Loss Cooldown: A 7-day stop-loss cooldown is implemented to prevent re-entry loops after a loss.
    • PDT Emergency Override: Includes an emergency override for 2x stop-loss and reduced position-size search ranges.
    • Short-Side Restriction: Short-side signal generation is removed in the paper trader environment.