认证配置¶
不同交易所使用不同的认证方式,bt_api_py 通过统一的认证配置类封装各交易所的连接参数。
认证类层次结构¶
AuthConfig (基类)
├── CryptoAuthConfig # Binance、OKX 等加密货币交易所
├── CtpAuthConfig # 中国期货 CTP 协议
├── IbAuthConfig # Interactive Brokers TWS/Gateway
└── IbWebAuthConfig # Interactive Brokers Web API
使用示例¶
认证配置 — 统一管理不同交易所的认证方式。
加密货币交易所使用 API Key,CTP 使用 Broker/User/Password,IB 使用 TWS 连接参数.
AuthConfig ¶
认证配置基类.
Initialize authentication configuration.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
exchange | str | Exchange identifier (e.g., "BINANCE", "OKX"). | 必需 |
asset_type | str | Asset type (e.g., "SWAP", "SPOT", "FUTURE"). Defaults to "SWAP". | 'SWAP' |
**kwargs | Any | Additional keyword arguments. | {} |
get_exchange_name ¶
Return exchange identifier with asset type.
返回:
| 类型 | 描述 |
|---|---|
str | Exchange identifier in format "EXCHANGE___ASSET_TYPE" (e.g., "BINANCE___SWAP"). |
to_dict ¶
Convert configuration to dictionary.
返回:
| 类型 | 描述 |
|---|---|
dict[str, Any] | Dictionary containing all non-private attributes for feed constructor. |
CryptoAuthConfig ¶
CryptoAuthConfig(exchange: str, asset_type: str = 'SWAP', public_key: str | None = None, private_key: str | None = None, passphrase: str | None = None, **kwargs: Any)
Bases: AuthConfig
加密货币交易所认证配置(Binance, OKX 等).
Initialize cryptocurrency exchange authentication configuration.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
exchange | str | Exchange identifier (e.g., "BINANCE", "OKX"). | 必需 |
asset_type | str | Asset type (e.g., "SWAP", "SPOT"). Defaults to "SWAP". | 'SWAP' |
public_key | str | None | API public key. | None |
private_key | str | None | API private key. | None |
passphrase | str | None | API passphrase (required for OKX). | None |
**kwargs | Any | Additional keyword arguments passed to parent. | {} |
CtpAuthConfig ¶
CtpAuthConfig(exchange: str = 'CTP', asset_type: str = 'FUTURE', broker_id: str = '', user_id: str = '', password: str = '', auth_code: str = '', app_id: str = '', md_front: str = '', td_front: str = '', product_info: str = '', **kwargs: Any)
Bases: AuthConfig
CTP 认证配置.
Initialize CTP authentication configuration.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
exchange | str | Exchange identifier. Defaults to "CTP". | 'CTP' |
asset_type | str | Asset type. Defaults to "FUTURE". | 'FUTURE' |
broker_id | str | Broker identifier. | '' |
user_id | str | User identifier. | '' |
password | str | User password. | '' |
auth_code | str | Authentication code. | '' |
app_id | str | Application identifier. | '' |
md_front | str | Market data front address (e.g., "tcp://180.168.146.187:10131"). | '' |
td_front | str | Trading front address (e.g., "tcp://180.168.146.187:10130"). | '' |
product_info | str | Product information. | '' |
**kwargs | Any | Additional keyword arguments passed to parent. | {} |
IbAuthConfig ¶
IbAuthConfig(exchange: str = 'IB', asset_type: str = 'STK', host: str = '127.0.0.1', port: int = 7497, client_id: int = 1, **kwargs: Any)
Bases: AuthConfig
Interactive Brokers 认证配置.
Initialize Interactive Brokers authentication configuration.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
exchange | str | Exchange identifier. Defaults to "IB". | 'IB' |
asset_type | str | Asset type (e.g., "STK", "FUT", "OPT"). Defaults to "STK". | 'STK' |
host | str | TWS/Gateway host address. Defaults to "127.0.0.1". | '127.0.0.1' |
port | int | TWS/Gateway port (TWS=7497, Gateway=4001). Defaults to 7497. | 7497 |
client_id | int | Client ID for connection. Defaults to 1. | 1 |
**kwargs | Any | Additional keyword arguments passed to parent. | {} |
IbWebAuthConfig ¶
IbWebAuthConfig(exchange: str = 'IB_WEB', asset_type: str = 'STK', base_url: str = 'https://localhost:5000', account_id: str | None = None, access_token: str | None = None, client_id: str | None = None, private_key_path: str | None = None, verify_ssl: bool = False, proxies: dict[str, str] | None = None, timeout: int = 10, cookies: dict[str, str] | None = None, cookie_source: str | None = None, cookie_browser: str = 'chrome', cookie_path: str = '/sso', **kwargs: Any)
Bases: AuthConfig
Interactive Brokers Web API 认证配置.
支持三种认证方式
- Client Portal Gateway (个人客户): base_url="https://localhost:5000", verify_ssl=False
- OAuth 2.0 (机构客户): base_url="https://api.interactivebrokers.com", 需提供 client_id + private_key_path 或 access_token
- 浏览器 Cookie (个人客户增强认证): 从已登录浏览器提取 cookie, 用于访问需要浏览器会话的端点 (/portfolio/{id}/summary 等)
Initialize Interactive Brokers Web API authentication configuration.
Supports three authentication methods
- Client Portal Gateway (individual): base_url="https://localhost:5000", verify_ssl=False
- OAuth 2.0 (institutional): base_url="https://api.interactivebrokers.com", requires client_id + private_key_path or access_token
- Browser Cookie (enhanced auth): extract cookies from logged-in browser, used for endpoints requiring browser session (/portfolio/{id}/summary etc.)
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
exchange | str | Exchange identifier. Defaults to "IB_WEB". | 'IB_WEB' |
asset_type | str | Asset type (e.g., "STK", "FUT"). Defaults to "STK". | 'STK' |
base_url | str | API base URL. Defaults to "https://localhost:5000". | 'https://localhost:5000' |
account_id | str | None | IBKR account ID (e.g., "U1234567"). | None |
access_token | str | None | OAuth 2.0 access token. | None |
client_id | str | None | OAuth 2.0 client ID. | None |
private_key_path | str | None | OAuth 2.0 private key file path. | None |
verify_ssl | bool | Whether to verify SSL (set False for Gateway). Defaults to False. | False |
proxies | dict[str, str] | None | HTTP proxy configuration. | None |
timeout | int | Request timeout in seconds. Defaults to 10. | 10 |
cookies | dict[str, str] | None | Cookie dictionary. | None |
cookie_source | str | None | Cookie source configuration. | None |
cookie_browser | str | Browser type (chrome, firefox, etc.). Defaults to "chrome". | 'chrome' |
cookie_path | str | Cookie path. Defaults to "/sso". | '/sso' |
**kwargs | Any | Additional keyword arguments passed to parent. | {} |