A research-oriented paper trading lab for US equities that combines deterministic market-data features, LLM-based trade analysis, rule-based intent gating, Alpaca paper execution, and post-trade journaling in PostgreSQL.
trade-lab is not just a backtest script and not just an execution bot. It is a trading research framework that:
- ingests and stores OHLCV candle data in PostgreSQL
- builds deterministic feature packets from local market data
- enriches each packet with account context, optional news, and symbol memory
- asks one or more LLMs for a structured trade thesis and plan
- applies deterministic gating and sizing rules before accepting a trade intent
- optionally sends accepted intents to Alpaca paper trading
- syncs orders and fills back into PostgreSQL
- records and evaluates decisions over time for learning and review
Core flow:
- Pull account state, positions, and open orders from Alpaca paper
- Ingest latest candles and backfill stale windows into PostgreSQL
- Build a feature packet for each symbol from local candle data
- Add account context, optional news, and historical symbol memory
- Run structured LLM analysis (single model or ensemble)
- Convert the result into a trade intent
- Apply deterministic rules, freshness checks, and sizing constraints
- Optionally execute accepted intents on Alpaca paper
- Sync orders/fills and evaluate pending journal entries
- Unified
candlestable for multiple timeframes (1m,5m,15m,1h, etc.) - Latest-bar ingestion and stale-window backfill
- Local PostgreSQL store used as the source of truth for features and evaluation
Feature packets are built from local OHLCV data and currently include:
- latest candle snapshot
- 1d / 5d / 20d returns
- approximate daily volatility
- SMA20 / SMA50 trend summary
- liquidity proxy (average daily volume)
- basic data-quality metadata
- notable recent hourly moves
This keeps the LLM prompt compact, auditable, and reproducible.
The LLM layer produces structured JSON only, including:
- bull / base / bear thesis
- risk flags
- a conditional trade plan
- suggested side (
buy,sell,hold,none) - setup / entry / exit / invalidation
- confidence score
Supported providers in the current code:
- OpenAI
- Anthropic
- Google Gemini
It also supports:
- retries and backoff
- provider/model fallback chains
- partial ensemble execution when one model fails
- ensemble selection policies across model outputs
LLM output is not executed directly.
Accepted intents pass through deterministic checks such as:
- packet/data existence
- market-data freshness
- minimum candle count sanity
- LLM side / confidence validation
- max open positions
- minimum cash buffer
- open-buy-order dedupe
- symbol exposure cap
- position-aware sell limits
Sizing is derived from a deterministic notional budget with confidence-based scaling and account constraints.
Accepted intents can be submitted to Alpaca paper trading.
Execution behavior includes:
- idempotent
client_order_idbased onintent_id - paper order submission
- local order persistence in PostgreSQL
- later synchronization of orders and fills from Alpaca
The repo includes a learning/journal loop that records and later evaluates decisions.
Current journal features include:
- decision timestamp and entry price
- side / timeframe / horizon
- MFE / MAE
- end-of-horizon return
- outcome labels such as
hit_profit,stopped_out,small_win,small_loss,chop - per-symbol recent trade memory and 90-day summary statistics
This memory is reinjected into later LLM prompts to provide symbol context.
- Python 3
- SQLAlchemy
- psycopg2-binary
- requests
- python-dotenv
- PyYAML
- OpenAI SDK
- Anthropic SDK
- Google GenAI SDK
- moomoo-api
- pandas / numpy / scipy / scikit-learn (installed dependencies)
- PostgreSQL
- Alpaca paper trading API
- Alpaca market data API
- Optional Yahoo Finance RSS headlines
- Optional Moomoo OpenD connection (see
test/test_connection.py)
Important files and directories currently identified:
src/paper_loop.py— main orchestration loopsrc/features_packet.py— deterministic feature packet buildersrc/llm_analyze.py— provider wrappers, retries, ensemble logic, output normalizationsrc/paper_intents.py— deterministic rules gate, sizing, and intent persistencesrc/learning.py— decision journaling, evaluation, and symbol memorysrc/risk.py— basic standalone intent checkssrc/alpaca_paper_exec.py— execution adapter for Alpaca papersrc/config.py— YAML config loader and typed config modelssrc/db.py— database connection and query helperspostgres/init.sql— base schemapostgres/migration.sql— migration into the unified candles/trading schemapostgres/migration_learning.sql— journal/learning schema additionstest/test_connection.py— Moomoo/OpenD connectivity smoke testtrade_lab.dump— database dump artifact included in repo
-
candles- symbol, timeframe, ts, OHLCV, source
-
trade_intents- normalized model outputs and rule-engine acceptance/rejection
-
orders- broker orders persisted locally
-
fills- fill/activity records linked to orders
trade_journal- decision records, evaluation timestamps, end price, return, MAE, MFE, and outcome labels
This repo expects a .env file at the repo root.
At minimum, the current codebase may need values for:
DATABASE_URLALPACA_API_KEY/ALPACA_API_SECRET- or Alpaca equivalents used by specific modules
ALPACA_BASE_URL(optional; defaults to paper API)ALPACA_DATA_BASE(optional)ALPACA_DATA_FEED(optional)OPENAI_API_KEY(if using OpenAI)ANTHROPIC_API_KEY(if using Anthropic)GEMINI_API_KEY/ Google GenAI key (if using Gemini)LLM_MODELS/ provider-specific model settingsGOOGLE_GENAI_FALLBACK_MODELS(optional)LLM_ALLOW_PARTIAL(optional)TRADING_UNLOCK_FILE(optional)
The app also reads a config.yaml file (or CONFIG_PATH) with settings for:
- symbol universe
- bar interval
- horizon
- trading mode
- risk controls
- moomoo host/port
- loop timing
Expected config areas from the current code:
symbolsbar_intervalhorizon_mintradingriskmoomooloop
Example:
python -m venv .venv
source .venv/bin/activateIf requirements.txt is present, install from it:
pip install -r requirements.txtCreate a PostgreSQL database and set DATABASE_URL in .env.
Then apply schema scripts as needed:
psql "$DATABASE_URL" -f postgres/init.sql
psql "$DATABASE_URL" -f postgres/migration.sql
psql "$DATABASE_URL" -f postgres/migration_learning.sqlIf you want to inspect or restore the included dump, use PostgreSQL tooling such as pg_restore depending on the dump format.
Add the API credentials and runtime settings needed for your chosen providers and broker/data paths.
Set your symbol universe, horizon, bar interval, trading mode, and risk constraints.
Example single-cycle run:
python -m src.paper_loop --run-onceUseful flags currently visible in the code:
--cycle-min--stale-min--backfill-days--features-days--sync-hours--news--news-items--alpaca-feed--execute--stop-when-closed--allow-when-closed--run-once
Typical safe workflow:
- Start with
--run-once - Verify database writes and generated intents
- Review
trade_intents,orders,fills, andtrade_journal - Only then consider repeated loop execution in paper mode
- This repo is designed around paper trading first.
- Model outputs are advisory and pass through deterministic gates before execution.
- The repo includes explicit live-trading safety latch conventions in environment comments.
- Do not enable live trading without reviewing every execution and risk path.
- Clear separation between features, LLM analysis, intents, execution, and learning
- PostgreSQL-backed auditability
- Deterministic pre/post-processing around LLM usage
- Journal and memory loop for trade review
- Ensemble and fallback support for model availability
Based on current code review, important limitations remain:
- feature packets are still fairly generic and not yet strongly setup-specific
- LLMs still occupy a high-level directional decision role
- risk logic is practical but not yet portfolio-grade
- learning currently functions more like evaluation + memory injection than a full adaptive optimizer
- repo appears better suited as a trading research lab than a production trading engine
This repository currently looks best understood as:
- a serious paper-trading research framework
- an experimentation platform for LLM-assisted trade analysis
- a foundation that can be refactored into a stronger strategy engine, risk engine, and skill-oriented architecture
This project is for research and experimentation. It is not financial advice, and it should not be treated as a production-ready live trading system without substantial additional validation, controls, and review.