Easy Money Trading Agent
Project description
东方财富交易代理 (emta)
东方财富非官方Python交易库
概述
东方财富交易代理(emta)是一个非官方的Python库,提供对东方财富交易平台的编程访问。它支持自动化的交易操作,包括下单、查询账户余额和获取市场数据等功能。
主要特性
- 身份验证:支持验证码识别和密码加密的安全登录
- 订单管理:支持下单、查询和撤单操作
- 账户信息:获取账户余额和持仓信息
- 市场数据:访问股票实时市场数据
- 模块化设计:代码结构清晰,关注点分离明确
- 类型安全:完整的类型提示,提高代码可靠性
- 错误处理:全面的异常处理机制,确保应用稳定性
安装
请确保使用Python 3.11或更高版本(但低于4.0)。
pip install emta
快速开始
from emta import TradingAgent, OrderType
# 创建交易代理
agent = TradingAgent("your_username", "your_password")
# 登录东方财富
if agent.login():
# 获取账户信息
account_info = agent.get_account_info()
print(f"账户余额: {account_info.get('account_balance', 0)}")
# 下买单
order_id = agent.place_order("600000", OrderType.BUY, 100, 12.5)
if order_id:
print(f"订单提交成功,订单号: {order_id}")
# 获取市场数据
market_data = agent.get_market_data("600000")
print(f"当前价格: {market_data}")
# 登出
agent.logout()
完整示例请参见 examples/trading_example.py。
API 参考
TradingAgent
与东方财富交易服务交互的主要接口。
构造函数
TradingAgent(username: str | None = None, password: str | None = None)
方法
| 方法 | 描述 |
|---|---|
login(username=None, password=None, duration=30) |
东方财富身份验证 |
logout() |
结束当前会话 |
get_account_info() |
获取账户余额和持仓信息 |
place_order(stock_code, trade_type, amount, price) |
下达买入或卖出订单 |
query_orders() |
获取最近的订单列表 |
cancel_order(order_id) |
撤销现有订单 |
get_market_data(stock_code) |
获取股票的当前市场数据 |
技术栈
- Python: 3.11+
- 依赖管理: UV 用于快速依赖解析
- HTTP客户端: HTTPX 用于异步/同步HTTP请求
- 测试: Pytest 用于全面的测试覆盖
- 类型检查: Mypy 用于静态类型分析
- 代码质量: Ruff 用于代码检查和格式化
- 验证码识别: ddddocr 用于自动验证码识别
开发环境搭建
-
克隆仓库:
git clone https://github.com/your-username/emta.git cd emta
-
使用UV安装依赖:
uv sync -
运行测试:
uv run pytest
-
类型检查:
uv run mypy src/
-
代码检查:
uv run ruff check src/
项目结构
src/emta/
├── __init__.py # 包初始化文件
├── py.typed # 类型提示标记
├── core/ # 核心交易代理实现
│ └── agent.py # 主要交易代理类
├── models/ # 数据模型和异常
│ ├── trading.py # 交易数据模型
│ └── exceptions.py # 异常类
├── auth/ # 认证模块
│ └── client.py # 认证客户端
├── api/ # API客户端交易操作
│ └── client.py # API客户端实现
└── utils/ # 工具函数
├── encryption.py # 密码加密工具
├── captcha.py # 验证码识别工具
└── stocks.py # 股票市场代码工具
贡献
欢迎贡献!如果您有改进建议、错误修复或新增功能:
- Fork 仓库
- 创建功能分支
- 进行修改
- 如适用,请添加测试
- 确保所有测试通过
- 提交 pull request
请遵循现有的代码风格,并为新功能添加适当的文档。
测试
测试使用 pytest 编写。运行测试:
uv run pytest
运行带覆盖率的测试:
uv run coverage run -m pytest
uv run coverage report
许可证
本项目是根据 MIT 许可证 发布的开源软件。
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 emta-0.5.0.tar.gz.
File metadata
- Download URL: emta-0.5.0.tar.gz
- Upload date:
- Size: 69.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee7736ce2512746d0753db642f36471af98af4ddf8194fcbd121da853681f56e
|
|
| MD5 |
6ef04dfb0b0acf153d62b3c9d3a72904
|
|
| BLAKE2b-256 |
f8cef4f9debed283df7da99637a33c12d8bd7eec4a04334caa79cda925d47817
|
File details
Details for the file emta-0.5.0-py3-none-any.whl.
File metadata
- Download URL: emta-0.5.0-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b783d85d657745f5046950294fd243af7b082187491996d75e530c24aa32d97
|
|
| MD5 |
b1aae5c6745ca2129aa885a1e097085d
|
|
| BLAKE2b-256 |
c619a9f2b727f59067ba13e28223450c11190613562154a6db6c6058bf50a412
|