Skip to content

rock-mind/autoquant

Repository files navigation

AutoQuant

Python 3.10+ License: Apache 2.0

Autonomous A-share quantitative trading strategy optimization powered by AI agents.

What Is This?

AutoQuant is inspired by Karpathy's autoresearch -- the "one file, one metric" philosophy applied to quantitative trading. The core idea: an AI Agent iterates on a single strategy file (strategy.py), evaluating each change against a single composite metric (the validation score), keeping improvements and reverting failures.

The AI Agent autonomously proposes strategy modifications -- trying different indicators, parameter combinations, stock selection filters, position sizing methods -- runs a full backtest after every change, and uses the validation score to decide whether to keep or discard the result. Every successful iteration is captured as a git commit, building a complete experiment history. Failed experiments are reverted, so the working tree always reflects the best-known strategy.

While AutoQuant is designed to work with Claude Code as the AI Agent, the core engine (data layer, backtest engine, scoring system) is entirely standalone. You can run backtests, inspect scores, and modify strategies manually without any AI tooling.

Architecture

graph LR
    A[Agent] -->|reads| S[strategy.py]
    A -->|runs| B[backtest.py]
    B -->|returns| SC[score_val]
    SC -->|decides| D{keep or drop?}
    D -->|keep| C[git commit]
    D -->|drop| R[git revert]
    C --> M[modify strategy.py]
    R --> M
    M --> A
Loading

Quick Start

git clone https://github.com/rock-mind/autoquant.git
cd autoquant
uv sync
uv run backtest.py

Pre-Cache Data

Download stock data locally before running backtests to avoid repeated network requests:

# Cache top 500 stocks (recommended)
uv run cache_all.py --limit 500

# Cache all A-share stocks
uv run cache_all.py

Data is stored as Parquet files in data/ and reused automatically on subsequent runs.

Running with AI Agent

AutoQuant is designed for autonomous optimization using an AI coding agent. To start:

  1. Open the project in Claude Code
  2. Ask the Agent to follow the instructions in program.md

The Agent will create an experiment branch, run iterative backtests, and track all results in results.tsv. See docs/architecture.md for details on the optimization loop.

Key Features

  • Autonomous iteration -- AI Agent continuously improves the strategy without human intervention
  • Multi-source data -- AKShare, BaoStock, and Tushare with automatic cross-validation and fallback
  • AST safety sandbox -- forbidden imports and calls are blocked at the syntax tree level before execution
  • Composite scoring -- five metrics normalized and weighted into a single score with train/val split
  • Git experiment tracking -- every improvement is a commit; every failure is reverted
  • Paper trading -- automatic strategy snapshots when quality thresholds are met

Scoring

Five metrics are normalized to [0, 1] and combined with the following weights:

Metric Weight Normalization Range
Sharpe ratio 30% -1.0 to 3.0
Max drawdown 25% 0.5 to 0.0 (inverted)
Annual return 20% -0.2 to 1.0
Win rate 15% 0.2 to 0.8
Profit factor 10% 0.5 to 3.0

The backtest period is split 85% train / 15% validation. Only the validation score (score_val) determines whether a change is kept or dropped. See docs/scoring.md for full details.

A-Share Trading Rules

The backtest engine enforces real-world A-share market constraints:

Rule Value
Settlement T+1 (cannot sell shares bought today)
Lot size 100 shares (orders rounded to nearest lot)
Commission 0.025% (万2.5) both directions
Stamp tax 0.1% (千1) sell-side only
Slippage 0.1% per trade
Limit up/down +/-9.8% (trades blocked at limit)

Project Structure

File Description
strategy.py Trading strategy -- the ONLY file the Agent modifies
backtest.py Backtest engine with AST safety checks and scoring
prepare.py Multi-source data layer with caching and cross-validation
simulate.py Paper trading snapshot and daily settlement
cache_all.py Bulk data pre-download utility
program.md Agent instructions for the autonomous optimization loop
CLAUDE.md Claude Code project configuration
results.tsv Experiment history log (created by Agent)
pyproject.toml Project metadata and dependencies
docs/ Architecture, scoring, and quickstart documentation
data/ Local Parquet cache (created on first run)
tests/ Test suite

Configuration

Environment Variable Default Description
AUTOQUANT_STOCK_POOL 500 Number of stocks in the backtest universe
TUSHARE_TOKEN (empty) Optional Tushare API token for additional data source

Contributing

Contributions are welcome.

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/your-idea
  3. Make your changes
  4. Run tests: uv run pytest
  5. Open a Pull Request

Please keep changes focused and include tests where applicable.

License

Apache License 2.0

Acknowledgments

  • Karpathy's autoresearch -- the "one file, one metric" philosophy that inspired this project
  • AKShare -- open-source A-share financial data
  • BaoStock -- free A-share historical data
  • Tushare -- comprehensive Chinese financial data platform

About

Autonomous A-share quantitative trading strategy optimization powered by AI agents

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors