Skip to main content

大连商品交易所 (DCE) API v1.0 Python SDK

Project description

dceapi-py

大连商品交易所 (DCE) API v1.0 Python SDK

功能特性

  • 类型提示支持(Type Hints)
  • 自动 Token 管理和刷新
  • 完整的错误处理
  • 支持期货和期权交易类型
  • 支持中英文语言切换
  • 异步和同步请求支持

安装

pip install dceapi

或从源码安装:

git clone https://github.com/pseudocodes/dceapi-py.git
cd dceapi-py
pip install -e .

快速开始

环境变量配置

export DCE_API_KEY="your-api-key"
export DCE_SECRET="your-secret"

基本使用

import os
from dceapi import Client, Config

# 方法 1: 从环境变量创建客户端(推荐)
client = Client.from_env()

# 方法 2: 使用配置对象
config = Config(
    api_key="your-api-key",
    secret="your-secret"
)
client = Client(config)

# 获取当前交易日期
trade_date = client.common.get_curr_trade_date()
print(f"当前交易日期: {trade_date.trade_date}")

# 获取品种列表
varieties = client.common.get_variety_list(trade_type=1)  # 1=期货
print(f"品种数量: {len(varieties)}")

# 获取日行情
from dceapi.models import QuotesRequest
quotes_req = QuotesRequest(
    variety_id="c",
    trade_date="20240115",
    trade_type="1"
)
quotes = client.market.get_day_quotes(quotes_req)
for quote in quotes:
    print(f"{quote.contract_id}: {quote.last_price}")

自定义配置

from dceapi import Client, Config

config = Config(
    api_key="your-api-key",
    secret="your-secret",
    base_url="http://www.dce.com.cn",
    timeout=30.0,
    lang="zh",      # "zh" 或 "en"
    trade_type=1    # 1=期货, 2=期权
)

client = Client(config)

错误处理

from dceapi import Client, APIError, AuthError, NetworkError, ValidationError

client = Client.from_env()

try:
    trade_date = client.common.get_curr_trade_date()
except ValidationError as e:
    print(f"验证错误: {e}")
except AuthError as e:
    print(f"认证错误: {e}")
except APIError as e:
    print(f"API 错误 {e.code}: {e.message}")
except NetworkError as e:
    print(f"网络错误: {e}")

项目结构

dceapi-py/
├── src/
│   └── dceapi/              # SDK 实现代码
│       ├── __init__.py      # 包入口
│       ├── client.py        # 客户端入口
│       ├── config.py        # 配置管理
│       ├── errors.py        # 错误类型定义
│       ├── http.py          # HTTP 请求处理
│       ├── models.py        # 数据模型
│       ├── token.py         # Token 管理
│       └── services/        # API 服务
│           ├── __init__.py
│           ├── common.py
│           ├── news.py
│           ├── market.py
│           ├── trade.py
│           ├── settle.py
│           ├── member.py
│           └── delivery.py
├── examples/                # 使用示例
│   ├── basic.py
│   └── complete.py
├── tests/                   # 测试
├── pyproject.toml          # 项目配置
├── setup.py                # 安装脚本
└── README.md               # 文档

API 服务

Common Service (通用服务)

# 获取当前交易日期
trade_date = client.common.get_curr_trade_date()

# 获取品种列表
varieties = client.common.get_variety_list(trade_type=1)

News Service (资讯服务)

from dceapi.models import GetArticleByPageRequest

# 获取文章列表
req = GetArticleByPageRequest(
    column_id="244",  # 交易所公告
    page_no=1,
    page_size=10,
    site_id=5
)
result = client.news.get_article_by_page(req)

# 获取文章详情
article = client.news.get_article_detail(article_id="12345")

Market Service (行情服务)

from dceapi.models import QuotesRequest, WeekQuotesRequest

# 获取日行情
req = QuotesRequest(variety_id="c", trade_date="20240115", trade_type="1")
quotes = client.market.get_day_quotes(req)

# 获取夜盘行情
quotes = client.market.get_night_quotes(req)

# 获取周行情
req = WeekQuotesRequest(variety_code="c", year=2024, week=2)
quotes = client.market.get_week_quotes(req)

开发

安装开发依赖

pip install -e ".[dev]"

运行测试

pytest

代码格式化

black src/ tests/

类型检查

mypy src/

许可证

MIT License

相关项目

贡献

欢迎提交 Issue 和 Pull Request!

支持

如有问题,请提交 Issue 或联系维护者。

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

dceapi-0.1.0.tar.gz (25.4 kB view details)

Uploaded Source

Built Distribution

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

dceapi-0.1.0-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

File details

Details for the file dceapi-0.1.0.tar.gz.

File metadata

  • Download URL: dceapi-0.1.0.tar.gz
  • Upload date:
  • Size: 25.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for dceapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ae3216e5589667562f92ad58df7f58600e70061df02fe2a2609bc15d8d1d062b
MD5 fa3bce744b8d05db59d655781d5d59fd
BLAKE2b-256 1d1f9b05c2cab21f1a8274a6a21ae0fa8e2adc2c28b849ee59412a582dfb2141

See more details on using hashes here.

File details

Details for the file dceapi-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dceapi-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for dceapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1743000a55275a0fe49548bfe4eeeaa33ced2111e67fd432400902d863ec7eb
MD5 73e98d44fb80340b43818cf2740efb39
BLAKE2b-256 3d10707404a4a088e4294722c7824df961756218d1635cb4b2a6c735443f1bb2

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