A Python library for quantitative trading simulation and backtesting
Project description
EmuTrader
EmuTrader 是一个专为量化交易设计的Python独立账户管理库,提供强大的模拟交易和回测功能。提供100%兼容JoinQuant API的统一接口,通过模拟真实的交易环境,帮助交易者和开发者测试交易策略,而无需承担实际资金风险。支持多平台部署和高性能数据持久化。
✨ 核心特性
- 🔄 JoinQuant完全兼容 - 现有策略零修改迁移,API 100%兼容
- 🏗️ 适配器架构 - 统一接口支持QMT、聚宽等多平台无缝集成
- 💾 数据持久化 - SQLite可靠存储 + 智能缓存优化
- ⚡ 高性能优化 - LRU缓存 + 批量操作,查询响应 < 10ms
- 🔧 多账户类型 - STOCK/FUTURE/CREDIT/OPTION等完整支持
- 🛡️ 线程安全 - 支持多策略并发运行
- 📊 风险管理 - 内置风险控制和性能追踪
主要特性
- 🎯 模拟交易环境 - 完全模拟真实交易场景
- 📊 策略回测 - 支持历史数据回测分析
- 💰 资金管理 - 灵活的账户资金管理
- 📈 实时监控 - 实时跟踪交易表现
- 🔧 易于扩展 - 模块化设计,便于自定义扩展
- 📝 详细日志 - 完整的交易记录和日志
快速开始
安装
pip install emutrader
基础使用
from emutrader import get_jq_account
# 获取JoinQuant兼容账户
account = get_jq_account(
strategy_name="my_strategy",
initial_cash=100000,
account_type="STOCK"
)
# 标准JQ接口 - 零学习成本
print(f"总资产: {account.account_info.total_value}")
print(f"可用资金: {account.account_info.available_cash}")
# JQ标准下单
order_id = account.order_shares("000001.SZ", 1000)
print(f"订单ID: {order_id}")
高级使用
from emutrader import AccountHandlerFactory, SQLiteStorage, CacheManager
# 使用工厂模式创建处理器
handler = AccountHandlerFactory.create_handler("STOCK", "strategy", 0)
# 配置持久化存储
storage = SQLiteStorage("accounts.db")
cache_manager = CacheManager(storage)
# 查看支持的账户类型
print("支持的账户类型:", AccountHandlerFactory.get_supported_types())
📊 系统架构
用户接口层 (JQ兼容API)
┌─────────────────────────┐
│ get_jq_account() │
│ AccountFactory │
└─────────┬───────────────┘
│
处理器层 (业务逻辑)
┌─────────▼───────────────┐
│ StockHandler │
│ FutureHandler │
│ CreditHandler │
└─────────┬───────────────┘
│
适配器层 (平台集成)
┌─────────▼───────────────┐
│ JQAdapter │
│ QMTAdapter │
│ MockAdapter │
└─────────┬───────────────┘
│
存储&缓存层 (数据持久化)
┌─────────▼───────────────┐
│ CacheManager │
│ SQLiteStorage │
│ MemoryStorage │
└─────────────────────────┘
🔧 支持的功能
账户类型
- ✅ STOCK - 股票账户
- ✅ FUTURE - 期货账户
- ✅ CREDIT - 融资融券账户 (开发中)
- 🔄 OPTION - 期权账户 (开发中)
- 🔄 CRYPTO - 数字货币账户 (开发中)
平台适配器
- ✅ JoinQuant - 聚宽平台完全兼容
- ✅ QMT - 迅投QMT平台支持
- ✅ Mock - 模拟交易环境
- 🔄 更多平台 - 持续扩展中
存储后端
- ✅ SQLite - 轻量级数据库,适合生产环境
- ✅ Memory - 内存存储,适合测试和临时使用
- 🔄 MySQL/PostgreSQL - 企业级数据库支持 (规划中)
📈 性能指标
| 指标 | 性能 | 说明 |
|---|---|---|
| 查询响应时间 | < 10ms | 基于智能缓存优化 |
| 下单执行时间 | < 100ms | 异步处理 + 批量操作 |
| 缓存命中率 | > 90% | LRU + TTL 双重策略 |
| 并发支持 | 10+ 策略 | 线程安全设计 |
| 内存占用 | < 50MB | 轻量级设计 |
🎯 使用场景
1. JoinQuant策略迁移
# 原JQ策略代码保持完全不变
def initialize(context):
pass
def handle_data(context, data):
# 使用标准JQ接口,零修改运行
account_info = context.portfolio.account_info
order_shares("000001.XSHE", 1000)
2. 多账户策略管理
# 同时管理多个账户类型
accounts = {
"股票": get_jq_account("stock_strategy", 100000, "STOCK"),
"期货": get_jq_account("future_strategy", 200000, "FUTURE"),
"信用": get_jq_account("credit_strategy", 150000, "CREDIT")
}
for name, account in accounts.items():
print(f"{name}账户总资产: {account.account_info.total_value}")
3. 高性能量化系统
# 配置高性能缓存和存储
from qsm_account import CacheManager, SQLiteStorage
storage = SQLiteStorage("prod_accounts.db")
cache_manager = CacheManager(
storage,
max_size=5000, # 大容量缓存
ttl_seconds=60 # 1分钟TTL
)
# 性能监控
stats = cache_manager.get_cache_stats()
print(f"缓存命中率: {stats['hit_rate']:.2%}")
文档
详细的文档请访问:https://emutrader.readthedocs.io/
贡献
我们欢迎所有形式的贡献!请查看 CONTRIBUTING.md 了解如何参与项目开发。
许可证
本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。
联系方式
- 项目主页: https://github.com/yourusername/EmuTrader
- 问题反馈: https://github.com/yourusername/EmuTrader/issues
致谢
感谢所有为这个项目做出贡献的开发者和用户!
Project details
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 emutrader-0.1.0.tar.gz.
File metadata
- Download URL: emutrader-0.1.0.tar.gz
- Upload date:
- Size: 76.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5dbfc24bdc4285f12c8e00e5dbeff3b44f7c36cd0f80f203e13f4f90aed58618
|
|
| MD5 |
a38301ec3c33c03c0c1cfebf297c9b00
|
|
| BLAKE2b-256 |
03ff860f9df98dcbe2d68ba74aa2cda28e915d8b6bebebef0745133f0c9e6af0
|
File details
Details for the file emutrader-0.1.0-py3-none-any.whl.
File metadata
- Download URL: emutrader-0.1.0-py3-none-any.whl
- Upload date:
- Size: 56.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fdb10a7baf890f0e85fd24488012fa48bfdda9d0e952542172804378e1527e9e
|
|
| MD5 |
877d7f5e726c1bae265d3178dd440f4e
|
|
| BLAKE2b-256 |
193582cf468b609ef520c5484910a0beccddd03df9dc042c85eaabfdd74b520e
|