Lightweight quantitative backtesting framework with PTrade API simulation | 轻量级量化回测框架
Project description
📈 SimTradeLab
Lightweight Quantitative Backtesting Framework — Local PTrade API Simulation
Full PTrade API simulation — strategies transfer seamlessly between SimTradeLab and PTrade. See also: ptradeAPI
🎯 Why SimTradeLab?
| SimTradeLab | PTrade | |
|---|---|---|
| Speed | 100–160x faster | Baseline |
| Startup | Sub-second (data persists in memory) | Minutes |
| API Coverage | 46 backtest/research APIs | Full platform |
| Strategy Porting | Zero code changes | Zero code changes |
| Environment | Local, free, open-source | Cloud, licensed |
Core capabilities:
- ✅ 46 APIs — 100% coverage of stock backtesting scenarios (daily & minute bars)
- ⚡ 100–160x faster than PTrade platform
- 🚀 In-memory data persistence — singleton pattern, sub-second startup after first load
- 💾 Multi-level caching — LRU caches for MA/VWAP/adjustment factors/history, >95% hit rate
- 🧠 Smart data loading — AST analysis of strategy code, loads only required data
- 🔧 Lifecycle control — 7 lifecycle phases, strict simulation of PTrade's API restrictions
- 📊 Full stats reporting — returns, risk metrics (Sharpe/Sortino/Calmar), trade details, FIFO dividend tax, CSV export
- 🔌 Multi-market — Built-in CN (A-shares) and US market profiles with automatic trading rule adaptation
- 🌐 i18n — Backtest output in Chinese, English, or German
🚀 Need More? Try SimTradeDesk
SimTradeDesk is a professional desktop application built on SimTradeLab — no coding required.
| Feature | SimTradeLab (this repo) | SimTradeDesk |
|---|---|---|
| Target users | Developers & quant engineers | All traders |
| Interface | Python API | Desktop GUI |
| Strategy editing | Code editor | Built-in editor with syntax highlighting |
| Visualization | PNG charts | Interactive real-time charts |
| Data management | Manual setup | One-click download & update |
| Parameter tuning | Write code | Visual optimizer |
📦 Quick Start
pip install simtradelab
# Optional: technical indicators (requires system ta-lib)
pip install simtradelab[indicators]
# Optional: parameter optimizer
pip install simtradelab[optimizer]
Data: Use SimTradeData to download China A-share and US stock historical data.
Run a backtest:
from simtradelab.backtest.runner import BacktestRunner
from simtradelab.backtest.config import BacktestConfig
config = BacktestConfig(
# --- Required ---
strategy_name='my_strategy', # Strategy folder name under strategies/
start_date='2024-01-01', # Backtest start date
end_date='2024-12-31', # Backtest end date
# --- Capital & Market ---
# initial_capital=100000.0, # Starting capital (must be > 0)
# market='CN', # Market: 'CN' (A-shares) | 'US'
# t_plus_1=None, # T+1 override: None=market default (CN=True, US=False)
# benchmark_code='', # Benchmark code, empty=market default
# --- Frequency ---
# frequency='1d', # Bar frequency: '1d' (daily) | '1m' (minute)
# --- Paths ---
# data_path='~/.simtradelab/data', # Market data directory
# strategies_path='./strategies', # Strategies root directory
# --- Performance ---
# enable_multiprocessing=True, # Enable parallel data loading
# num_workers=None, # Worker count (None=auto, must be >= 1)
# use_data_server=True, # Use in-memory data server (singleton)
# --- Output ---
# enable_charts=True, # Generate PNG chart
# enable_logging=True, # Write log file
# enable_export=False, # Export trade details to CSV
# --- Sandbox & i18n ---
# sandbox=True, # PTrade sandbox: restrict imports & builtins
# locale='auto', # Log language: 'zh' | 'en' | 'de' (auto: CN market→zh, else system locale)
# --- Entry file ---
# strategy_file='backtest.py', # Entry file: 'backtest.py' | 'live.py'
)
runner = BacktestRunner()
report = runner.run(config=config)
📚 API Overview
46 backtest/research APIs — 100% stock backtesting coverage.
| Category | APIs |
|---|---|
| Trading | order, order_target, order_value, order_target_value, cancel_order, get_positions, get_trades |
| Data | get_price, get_history, get_fundamentals, get_stock_info |
| Sector | get_index_stocks, get_industry_stocks, get_stock_blocks |
| Indicators | get_MACD, get_KDJ, get_RSI, get_CCI |
| Config | set_benchmark, set_commission, set_slippage, set_universe, set_parameters |
| Lifecycle | initialize, before_trading_start, handle_data, after_trading_end |
📄 License
Dual license model:
- AGPL-3.0 — Free for open-source projects and personal research. See LICENSE
- Commercial License — For closed-source / commercial use. See LICENSE-COMMERCIAL.md or contact kayou@duck.com
🤝 Contributing
- 🐛 Report issues
- 💻 Implement missing API features
- 📚 Improve documentation
See CONTRIBUTING.md for CLA details.
⚖️ Disclaimer
SimTradeLab is a community-developed, open-source backtesting framework inspired by PTrade's event-driven design. It does not contain PTrade's source code, trademarks, or any protected content. This project is not affiliated with or endorsed by PTrade. Users are responsible for compliance with local regulations and platform terms.
⭐ Star this project if you find it useful!
🐛 Report Issue | 💡 Feature Request | 🖥️ SimTradeDesk
💖 Sponsor
If this project helps you, consider sponsoring!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simtradelab-2.10.1.tar.gz.
File metadata
- Download URL: simtradelab-2.10.1.tar.gz
- Upload date:
- Size: 112.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
359aeb8dbc65aacb80814391a1392b2602ad7be4a6de893db5cd25d18bb64036
|
|
| MD5 |
ae8003152aa82f6259b3a971f42e5d0b
|
|
| BLAKE2b-256 |
d1f0b05ca55d782ec1964c6ca48b912846849fba9ade5a683f203913f0bc3baf
|
Provenance
The following attestation bundles were made for simtradelab-2.10.1.tar.gz:
Publisher:
publish.yml on kay-ou/SimTradeLab
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simtradelab-2.10.1.tar.gz -
Subject digest:
359aeb8dbc65aacb80814391a1392b2602ad7be4a6de893db5cd25d18bb64036 - Sigstore transparency entry: 1186699994
- Sigstore integration time:
-
Permalink:
kay-ou/SimTradeLab@5b374683c11a783502c7e056b62589d6c5617951 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/kay-ou
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5b374683c11a783502c7e056b62589d6c5617951 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file simtradelab-2.10.1-py3-none-any.whl.
File metadata
- Download URL: simtradelab-2.10.1-py3-none-any.whl
- Upload date:
- Size: 136.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c52c37c52579ff37dfbb01aca700966ff2258513556f259f9aa7000dcb0c86a9
|
|
| MD5 |
1ef5a70f941e966c32f1eafd5962745b
|
|
| BLAKE2b-256 |
e494535987c9f00297264db42e643ef7c7890a19ce0006b8bb9b91325195bfae
|
Provenance
The following attestation bundles were made for simtradelab-2.10.1-py3-none-any.whl:
Publisher:
publish.yml on kay-ou/SimTradeLab
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simtradelab-2.10.1-py3-none-any.whl -
Subject digest:
c52c37c52579ff37dfbb01aca700966ff2258513556f259f9aa7000dcb0c86a9 - Sigstore transparency entry: 1186699996
- Sigstore integration time:
-
Permalink:
kay-ou/SimTradeLab@5b374683c11a783502c7e056b62589d6c5617951 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/kay-ou
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5b374683c11a783502c7e056b62589d6c5617951 -
Trigger Event:
workflow_dispatch
-
Statement type: