Skip to content

About

SPY-holdings trading bot + ThomasAi self-learning research + local agents (one project)

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

TradingSystem

📄 Technical White Paper: For the formal neuro-symbolic specification, mathematical derivations, and architecture details, read the ThomasAi & Local Foundation Model Technical White Paper.

An autonomous, multi-agent quantitative research and paper-trading system running natively on Apple Silicon macOS. The entire live trading execution pipeline, OpenD protocol client, and mathematical engine are compiled in 100% pure native Rust (crates/) with Apple AMX and Apple Neural Engine acceleration:

  • The Native Autonomous Trading Daemon (thomas bot), in crates/cli/src/bot.rs:
    • com.thomasai.bot: The unified, single native Rust daemon replacing legacy Python bots.
    • Maintains a persistent TCP wire session to Moomoo OpenD (127.0.0.1:11111) via native Protobuf framing with zero Python interpreter or GIL latency.
    • Ingests real-time top movers and order books directly as Protobuf structs.
    • Enforces the pure mathematical Early Inflection Gate (pure_math.rs), blocking late breakout chases ($>2.2%$ from consolidation base) and requiring $\ge 3.5\times$ RVOL surge.
    • Executes the Apple Accelerate AMX Matrix Connectome (neural_decide.rs) on hardware CPU matrix registers in ~28 microseconds.
    • Submits and manages orders strictly for simulated competition account 538882 (TrdEnv.SIMULATE). Zero live capital.
  • ThomasAi, the cognitive decision and continuous learning engine:
    • A local neural connectome (papercompmoomoo/data/brain.sqlite3) accelerated by Apple Accelerate AMX BLAS/vDSP matrix mathematics (crates/engine/src/accelerate.rs) and Darwin QoS thread scheduling (crates/engine/src/darwin_qos.rs).
    • Evaluates 21+ market and temporal sensors on closed bars only, gates entries against bear-sentry traps, dynamically sizes orders (0.25x–1.5x), and learns continuously (thomas neural-step, thomas neural-train).
  • The Native OpenD Protocol Crate (thomas-moomoo), in crates/moomoo/:
    • Zero-dependency, pure Rust wire client compiling all 184 official Moomoo Protobuf definitions via prost.
    • Implements OpenD's exact 44-byte binary wire framing with SHA-1 checksum verification and async Tokio multiplexing.
  • Apple Developer & Cloud Infrastructure:
    • Apple CloudKit: Server-to-Server ECDSA P-256 cloud sync writing trades and debriefs directly to iCloud container iCloud.com.proper.thomasai.
    • Apple Push Notifications (APNs): Direct HTTP/2 push dispatcher (tools/apns_alert.py) with developer token AuthKey_7B5B772A6F.p8.
    • Apple On-Device Foundation Model (AFM): Native Swift binary (tools/apple_afm/afm) codesigned with official team entitlements (PATRICK M PROSPER - 72P56XSFX5).
    • Web Dashboard & AI Console: Loopback HTTP server (http://127.0.0.1:8790) featuring real-time telemetry, doctor health checks, and interactive /chat interface.

All operations use real historical and live market data. Unclosed or fabricated bars are never admitted.


  • Simulated Trading Only: Strictly bound to Moomoo SIMULATE competition account 538882. Zero live-money interaction.
  • Empirical Backtest Performance: Real candle momentum backtest delivers +$31,235.72 (+31.24% net return) on $100,000 capital, 55.8% win rate (738 wins / 1,323 trades), 1.083% max drawdown, and 100% positive out-of-sample forward blocks (+0.17%, +6.53%, +8.20%, +3.38%, +9.95%).
  • Autonomous Walk-Forward Deliberation: +31.92% net alpha over baseline market (-3.43%) across multi-model consensus walk-forward backtests.
  • Pure Mathematical Core (pure_math.rs): Grounded in 4 foundational NeurIPS discoveries:
    1. Online Optimization under Delayed Bandit Feedback (NeurIPS Vol. 37): Minimax-optimal decoupled blocked updates eliminating asynchronous delay regret.
    2. Snell Envelope Martingale Optimal Stopping (NeurIPS Vol. 38): Parabolic giveback clamps and fee-covering floors (+0.15% at +0.75% MFE) in exit_mind.py and bracket.rs.
    3. Exogenous Covariate Decoupling via 2D Kinematic Kalman Filters (NeurIPS Vol. 37): Separates idiosyncratic mover acceleration from market index tide ($a_t - a_{\text{benchmark}} > 0$).
    4. Non-Exchangeable Conformal Risk Prediction & Drawdown Bounds (NeurIPS Vol. 37 & 38): Dynamic fractional Kelly drawdown throttling scaling position sizes before drawdowns occur.
  • Hardware Acceleration: Hardware-accelerated Apple Silicon AMX BLAS and vDSP matrix routines (cblas_dger, cblas_daxpy, cblas_dgemv, vDSP_dotprD) providing sub-microsecond covariance updates with verified scalar fallback.
  • Database-Level Immutability: SQLite triggers (protect_immutable_memories_del, protect_immutable_memories_upd) enforce permanent retention of NeurIPS foundational mathematical literature in brain.sqlite3.
  • Immediate Live Paper Execution: Both bots (moocomp and gainers) execute live paper trades immediately upon discovering opportunities. Live paper trading is never delayed, paused, gated, or held back by soak tests or scoreboard thresholds. Scoreboards observe and measure; they never gate live execution.
  • Pure Cognitive Exits: Positions are monitored in real-time. Candidate exit evaluations evaluate closed 15-minute bars (EXIT_PROMPT); a unanimous vote among available cognitive reviewers is required to trigger an exit. If any reviewer votes hold or fails to answer, the position is held.
  • Execution Telemetry: Every trade records Maximum Favorable Excursion (MFE %), Maximum Adverse Excursion (MAE %), and holding efficiency in data/debrief.sqlite3.
  • Continuous Learning: The connectome learns continuously from stored candles in common15m.sqlite3, closed trades in journal.sqlite3, and resolved teacher counterfactuals without waiting for new sessions.
  • Exhaustive Test Coverage: 110 Rust unit tests passing across all workspace crates; 420 Python unit tests passing. System doctor: healthy: no problems found.

Contents

  1. Technical White Paper
  2. System Architecture
  3. Repository Layout
  4. Prerequisites & Machine Setup
  5. Autonomous Background Daemons
  6. The Autonomous Bots (MooComp & Gainers)
  7. ThomasAi Connectome & Learning
  8. The Teacher Committee & Apple AI
  9. Local Models: Setup, Execution & Teaching
  10. Python to Rust Porting Matrix
  11. Data Stores & Schemas
  12. Web Portal & Dashboard Isolation
  13. Command Reference
  14. Disaster Recovery & iCloud Backups
  15. Verification, Health & Deployment

System Architecture

Moomoo OpenD (127.0.0.1:11111, SIMULATE Competition Account 538882)
   │  Real-time quote pushes, 15m candles, intraday leaderboards
   ▼
converter (launchd)  ──► common15m.sqlite3  ◄── Post-close Yahoo 15m universe catchup
                               │
         ┌─────────────────────┼──────────────────────────────┐
         ▼                     ▼                              ▼
  Hourly Rust cycle        15-min learn loop           15-min health loop
  import, merge, verify,   neural-step (trades),       soak report, checklist,
  walkforward, tune        neural-train (holdout)      teachers' outcome join
         │                     │                              │
         ▼                     ▼                              │
  brain.sqlite3  ◄──── ThomasAi Connectome ◄──────────────────┘
         │               ▲                ▲
         │               │ (evidence)     │ (pure cognitive exits)
         ▼               ▼                ▼
  portal (8790)  ◄── Dual Bots: moocomp & gainers ◄── Cognitive Teachers (Codex, Antigravity, Apple AI)

Each arrow represents file-backed, decoupled SQLite or IPC transactions. No process relies on shared in-memory state; each reads what the previous step persisted.


Repository Layout

Path Description
crates/core Core Rust library: candle storage, ingest, verification, failure logging (failures.rs), config (config.rs), and Yahoo client.
crates/engine Quantitative research engine: neural connectome (neural.rs, neural_decide.rs), pure mathematical engine (pure_math.rs), Apple Accelerate SIMD math (accelerate.rs), Darwin QoS (darwin_qos.rs), momentum selection (momentum.rs), bracket calculations (bracket.rs), planner gates (planner_gate.rs).
crates/cli The thomas executable: cycle runner, watchdog, portal web server (portal.rs), diagnostics (doctor.rs), noticeboard (board.rs), momentum backtest engine (momentum_backtest.rs).
papercompmoomoo/src/papercompmoomoo Python paper trading bots (bot.py, supervisor.py), planner (planner.py), exit mind with Snell optimal stopping (exit_mind.py), cognitive exit gates (second_opinion.py), deliberation engine (deliberation.py), telemetry (debrief.py), and teacher gateway (llm.py).
tools/neurips_learner.py Permanent NeurIPS proceedings crawler and immutable database ingestion engine.
tools/apple_afm Swift wrapper afm.swift interfacing with macOS FoundationModels (Apple AI on-device model). Built via build.sh.
ops/ Launchd background scripts: every.sh, learn_loop.sh.
teachers/ Teacher role definitions: ANTIGRAVITY.md, APPLE_AI.md, NEURIPS_HARD_FACTS.md.
docs/ System specifications: NEW_MACHINE.md, THOMASAI.md, BOT.md, TEACHERS.md, ALPHA_STRATEGIES.md, NO_BIAS.md, neurips_foundation/KNOWLEDGE_BASE.md.
data/ Durable runtime databases and cycle outputs.
CLAUDE.md, AGENTS.md Authoritative operational briefs and binding agent instructions.

Prerequisites & Machine Setup

Prerequisites

  • Operating System: macOS Sequoia (macOS 15+) on Apple Silicon (M1/M2/M3/M4).
  • Homebrew: System package manager (/opt/homebrew/bin/brew).
  • Rust Toolchain: Cargo 1.85+ (rustup).
  • Python 3.15: Installed via Homebrew (/opt/homebrew/bin/python3.15).
  • Xcode Command Line Tools: For Swift compilation (xcode-select --install).
  • Moomoo OpenD: Version 10.11.7108 installed at /Applications/OpenD.app, configured for simulated account 538882.

Step-by-Step Installation

# 1. Clone repository
git clone https://github.com/ProperProperProper/TradingSystem.git
cd TradingSystem

# 2. Build and codesign Rust release binary
PATH="/opt/homebrew/bin:$HOME/.cargo/bin:$PATH" cargo build --release
codesign -f -s - -i com.thomasai.thomas target/release/thomas

# 3. Build Apple AI on-device binary
./tools/apple_afm/build.sh

# 4. Create and populate Python virtual environment
cd papercompmoomoo
/opt/homebrew/bin/python3.15 -m venv .venv
.venv/bin/pip install --upgrade pip
.venv/bin/pip install -e .
cd ..

# 5. Restore databases (from iCloud or GitHub release)
./target/release/thomas restore --force

# 6. Verify health
./target/release/thomas doctor

Autonomous Background Daemons

The live system runs natively under macOS launchd in ~/Library/LaunchAgents:

Service Label Interval / Trigger Purpose
com.thomasai.bot KeepAlive The Native Rust Autonomous Trading Bot. Communicates directly with OpenD over binary Protobuf TCP, runs the Early Inflection Gate & AMX connectome, and trades account 538882.
com.thomasai.portal KeepAlive Native Rust Web Portal & AI Console on http://127.0.0.1:8790.
com.thomasai.watchdog Every 10 min Verifies daemon processes, checks for updated binaries, restarts stale services.
com.thomasai.cycle Hourly (ops/every.sh 3600) Refreshes data, verifies invariants, runs walk-forward search and momentum selection.
com.thomasai.learn Every 15 min (ops/learn_loop.sh) Runs thomas neural-step (trade learning) and thomas neural-train (holdout training).
com.thomasai.health Every 15 min (ops/every.sh 900) Evaluates capability checklist, computes soak metrics, and joins teacher outcomes.
com.thomasai.backup Daily Backs up SQLite stores to local snapshot bundles and iCloud sync.

Note

All legacy Python bots (local.papercompmoomoo.*) have been retired and removed from LaunchAgents. All live trading and quote handling is executed exclusively by the compiled native binary (target/release/thomas bot).


The Autonomous Native Trading Bot (thomas bot)

The native bot daemon lives in crates/cli/src/bot.rs and interfaces directly with crates/moomoo and crates/engine:

  1. Direct Protobuf Wire Connection:
    • Establishes a persistent TCP connection to Moomoo OpenD (127.0.0.1:11111) with native 44-byte binary framing (FT magic flag, Proto ID, SHA-1 checksum).
    • Ingests real-time top movers and order books directly as Protobuf structs in sub-milliseconds.
  2. Pure Mathematical Early Inflection Gate:
    • Automatically determines each mover's intraday consolidation base.
    • Blocks late breakout chases whenever price is $> 2.20%$ extended from the base (late_chase_avoided), eliminating peak buying.
    • Requires an authentic volume burst ($\text{rvol} \ge 3.50\times$) before admitting any candidate.
  3. Apple Silicon AMX Connectome Pass:
    • Multiplies synaptic weights across layers via hardware matrix registers (cblas_dgemv in Apple's Accelerate.framework).
    • Balances entry conviction against the adversarial bear sentry and selective abstention to scale position sizing ($0.25\times$ to $1.5\times$).
  4. Order Execution:
    • Routes orders with anti-replay PacketID directly to Moomoo simulated competition account 538882 (TrdEnv.SIMULATE).

ThomasAi Connectome & Learning

Details in docs/THOMASAI.md.

  • Topology: 21+ sensory nodes, archetypes, decisions, and protective reflexes (bear_sentry, floor_reflex).
  • Activation: Bounded non-linear activation computed via Apple Accelerate SIMD matrix math (cblas_dgemv).
  • Autonomous Learning:
    • thomas neural-step: Reinforces winning pathways and penalizes failures from closed trades in journal.sqlite3. Synaptic apoptosis prunes obsolete paths.
    • thomas neural-train: Walk-forward sensor training on stored historical bars with an untouchable 20-day holdout (--holdout-days 20). Discovers named skills and optimizes margin policies.

The Teacher Committee & Apple AI

Details in docs/TEACHERS.md.

  • Active Reviewers: Codex (codex) and Antigravity (agy CLI) serve as active voting cognitive models for candidate entry reviews and pure cognitive exits.
  • Apple AI On-Device (tools/apple_afm/afm): Runs locally on Apple Silicon without network or quotas. Operates under NOT_DECIDING = frozenset({"apple"}), generating advisory evaluations and building a scored track record. Includes an automated circuit breaker if Apple's safety guardrails refuse a prompt ("May contain unsafe content").
  • Track Record & Counterfactuals (teachers.py): Unfilled or skipped entries are judged against a causal 2-hour (8-bar) counterfactual to measure false-negative rates and dynamically tune teacher weights.

Local Models: Setup, Execution & Teaching

Details in docs/LOCAL_MODEL.md.

  • Local MLX Language Model (papercompmoomoo/models): Qwen2.5-1.5B 4-bit with Apple MLX LoRA fine-tuning. Evaluates live setups via next-token logits (thomasai_decide.py) and held positions (exit_view.py).
  • Teaching Loop: Extracts empirical training datasets from closed trades and teacher opinions (thomasai_dataset.py), distills alpha rules (alpha_knowledge.py), and executes continual LoRA fine-tuning (thomasai_model.py).
  • Dual Holdout Gate: Adapters must pass two non-negotiable out-of-sample gates (Loss_adapted < Loss_base and AUC >= 0.55) before promotion to live active status.
  • On-Device Apple Intelligence (tools/apple_afm): Native Swift bridge grounded in immutable NeurIPS mathematical theorems (pure_math.rs, neurips_learner.py).
  • Multi-Model AI Console: Interactive chat console (tools/ai_chat.py) supporting local Apple AI, local Qwen, Antigravity, and Codex.

Data Stores & Schemas

Path Purpose
papercompmoomoo/data/common15m.sqlite3 Canonical 15-minute candle store (Moomoo candidate bars + Yahoo universe bars).
papercompmoomoo/data/brain.sqlite3 ThomasAi connectome: sensors, archetypes, synapses, memories, learned skills, and policy.
papercompmoomoo/data/debrief.sqlite3 Detailed session debriefs, operational decisions, teacher scorecards, MFE/MAE telemetry.
papercompmoomoo/data/journal.sqlite3 MooComp bot journal: orders, trades, executions, and trade lessons.
papercompmoomoo/data/gainers_journal.sqlite3 Gainers bot journal: orders, trades, executions, and real-time breakout logs.
papercompmoomoo/data/teachers.sqlite3 Teacher opinions, verdicts, and resolved counterfactual results.
papercompmoomoo/data/board.sqlite3 Multi-agent noticeboard, commander rules, and violation audits.
papercompmoomoo/data/failures.sqlite3 Captured failure ledger: zero silent failures.
data/thomas.sqlite3 Rust results store: walk-forward evaluations, cycle logs, symbol parameter fits.

Web Portal & Dashboard Isolation

The read-only loopback portal is served at http://127.0.0.1:8790:

  • Strict Bot Isolation:
    • /comp: Dedicated view for the main competition bot (moocomp only).
    • /gainers: Dedicated view for the breakout bot (gainers only), featuring intraday surge timeline SVG charts.
    • /: Individual session progress charts for both bots presented side-by-side with no combined distortion.
  • /brain: Real-time 3D orbital canvas projection of ThomasAi's neural connectome, rendering active nodes, synaptic pulses, and layer activations.
  • /alpha: Alpha strategy walk-forward performance, holdout correlation trends, and skill metrics.
  • /learnings: Trade lessons and deliberation consensus.

Command Reference

# Diagnostics & Operations
./target/release/thomas doctor          # Complete system health check
./target/release/thomas verify          # Verify data invariants and candle schemas
./target/release/thomas watchdog        # Audit processes and restart stale binaries
./target/release/thomas portal          # Start loopback web portal

# Quantitative & Learning
./target/release/thomas neural-step     # Autonomous neural learning step on closed trades
./target/release/thomas neural-train    # Sensor training with 20-day holdout
./target/release/thomas neural-decide   # Forward connectome pass on live candidates
./target/release/thomas deliberate      # Multi-model parameter deliberation
./target/release/thomas momentum-select # Generate frozen momentum candidate ranking
./target/release/thomas walkforward     # Execute 5-session walk-forward test

# Disaster Recovery & Logging
./target/release/thomas bundle --publish # Snapshot databases to archive and GitHub release
./target/release/thomas restore [file]   # Restore database bundle
./target/release/thomas log "<msg>" --by <agent> # Append to JOURNAL.md

Disaster Recovery & iCloud Backups

  • Automated Bundling: ./target/release/thomas bundle --publish packages all SQLite databases (brain.sqlite3, debrief.sqlite3, journal.sqlite3, gainers_journal.sqlite3, common15m.sqlite3, teachers.sqlite3, thomas.sqlite3) into compressed archives in data/bundles/ and creates a release on the private GitHub repository.
  • iCloud Sync: Database snapshots and state files synchronize to Apple iCloud Drive (~/Library/Mobile Documents/com~apple~CloudDocs/TradingSystem/).
  • Restoration: On a new machine, running ./target/release/thomas restore automatically retrieves and unpacks the latest release bundle.

Verification, Health & Deployment

Always complete this sequence before ending a turn or committing code:

# 1. Python unit tests (420 tests)
PYTHONPATH=papercompmoomoo/src:papercompmoomoo/.venv/lib/python3.15/site-packages /opt/homebrew/bin/python3.15 -m unittest discover -s papercompmoomoo/tests

# 2. Rust unit tests (110 tests across workspace)
PATH="/opt/homebrew/bin:$PATH" cargo test --workspace

# 3. Build release binary and ad-hoc sign
PATH="/opt/homebrew/bin:$PATH" cargo build --release
codesign -f -s - -i com.thomasai.thomas target/release/thomas

# 4. System verification and doctor check
./target/release/thomas verify
./target/release/thomas doctor # Must report: "healthy: no problems found"

# 5. Log, commit, and push
./target/release/thomas log "Completed verification" --by <agent>
git add -A && git commit -m "docs/infra: comprehensive update" && git push origin main

About

SPY-holdings trading bot + ThomasAi self-learning research + local agents (one project)

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages