Skip to main content

A Python library for quantitative trading simulation and backtesting

Project description

EmuTrader

Python Version License: MIT PyPI version

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 文件了解详情。

联系方式

致谢

感谢所有为这个项目做出贡献的开发者和用户!

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

emutrader-0.1.1.tar.gz (76.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

emutrader-0.1.1-py3-none-any.whl (56.3 kB view details)

Uploaded Python 3

File details

Details for the file emutrader-0.1.1.tar.gz.

File metadata

  • Download URL: emutrader-0.1.1.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

Hashes for emutrader-0.1.1.tar.gz
Algorithm Hash digest
SHA256 efc6a9e821566116768e22da1cae4591c9a042a06ba74274aea1ce42b3cbeac1
MD5 025b9c169c7cd8376b41ba821f0691b2
BLAKE2b-256 70c137dd1faa19f659fbee44d4581f35593bb2cd0ce2f312875508a38e8380c3

See more details on using hashes here.

File details

Details for the file emutrader-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: emutrader-0.1.1-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

Hashes for emutrader-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7e703fa4b5aa118f6c2ad06715714c7483d3b2924dae51af9f2be222b4f42336
MD5 a13dc4634e15136fcb6c7e7dcb2f88a2
BLAKE2b-256 3ee1c74a9b0b61d936745df7a5a7b8395c175a59fe3df4cff4ee8fc9d519cabf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page