Skip to main content

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

Project description

dceapi-py

PyPI version Python Support License: MIT Downloads Code style: black Checked with mypy GitHub issues Maintenance

大连商品交易所 (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.1.tar.gz (37.8 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.1-py3-none-any.whl (33.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dceapi-0.1.1.tar.gz
  • Upload date:
  • Size: 37.8 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.1.tar.gz
Algorithm Hash digest
SHA256 30375f210070f36b19a60f9fcd7e805ac0d2cf96b24dbc5aa97fd00f4e37e54a
MD5 6561df2b748025865a6000b7fcfa4a1a
BLAKE2b-256 413174950618e85713848fd6e8f371c587c86841ecc5d9f76e493fdac1a2fd5b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dceapi-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 33.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 68cc35915fbd709453e1b3be7fc72102c67b27e4fc4740645b9337806757360d
MD5 09eb2a378c848538cac3997d90eeed9a
BLAKE2b-256 9b400f93efa276bb4afd6841ee3f6ef38f4c9bf1f6263b2afd23abc86297247a

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