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.2.tar.gz (37.7 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.2-py3-none-any.whl (33.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dceapi-0.1.2.tar.gz
  • Upload date:
  • Size: 37.7 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.2.tar.gz
Algorithm Hash digest
SHA256 fbde0e11b56b598a2e5cd63877d56513e3f6d37daf85d4f3f9c0bc3309cecab1
MD5 4c99f242a8f677056b13b69534985b3e
BLAKE2b-256 68e795efba79063b6b96c3b6d308bbf075728b7f8b8fed9ee843cdcac229d045

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dceapi-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 33.0 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5eaed0b820b7d4cba95be5887a412dd0a44b145294c8c2a4a6bd28b5eb93bfb9
MD5 acbea89cf8f9921bbc2f46bda9a8a313
BLAKE2b-256 747b391be93bc3573719154cebfe09b303c11000a97be798f74396d4c9582552

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