跳转至

认证配置

不同交易所使用不同的认证方式,bt_api_py 通过统一的认证配置类封装各交易所的连接参数。

认证类层次结构

AuthConfig (基类)
├── CryptoAuthConfig   # Binance、OKX 等加密货币交易所
├── CtpAuthConfig      # 中国期货 CTP 协议
├── IbAuthConfig       # Interactive Brokers TWS/Gateway
└── IbWebAuthConfig    # Interactive Brokers Web API

使用示例

exchange_kwargs = {
    "BINANCE___SPOT": {
        "api_key": "your_api_key",
        "secret": "your_secret",
        "testnet": True,
    },
    "OKX___SWAP": {
        "api_key": "your_api_key",
        "secret": "your_secret",
        "passphrase": "your_passphrase",
    },
}
from bt_api_py import CtpAuthConfig

exchange_kwargs = {
    "CTP___FUTURE": {
        "auth_config": CtpAuthConfig(
            broker_id="9999",
            user_id="your_user_id",
            password="your_password",
            md_front="tcp://180.168.146.187:10211",
            td_front="tcp://180.168.146.187:10201",
        )
    }
}
from bt_api_py import IbWebAuthConfig

exchange_kwargs = {
    "IB_WEB___STK": {
        "auth_config": IbWebAuthConfig(
            account_id="U1234567",
            base_url="https://localhost:5000",
            verify_ssl=False,
        )
    }
}

认证配置 — 统一管理不同交易所的认证方式。

加密货币交易所使用 API Key,CTP 使用 Broker/User/Password,IB 使用 TWS 连接参数.

AuthConfig

AuthConfig(exchange: str, asset_type: str = 'SWAP', **kwargs: Any)

认证配置基类.

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.

{}

exchange instance-attribute

exchange = _require_non_empty_str(exchange, 'exchange')

asset_type instance-attribute

asset_type = _require_non_empty_str(asset_type, 'asset_type')

get_exchange_name

get_exchange_name() -> str

Return exchange identifier with asset type.

返回:

类型 描述
str

Exchange identifier in format "EXCHANGE___ASSET_TYPE" (e.g., "BINANCE___SWAP").

to_dict

to_dict() -> dict[str, Any]

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.

{}

public_key instance-attribute

public_key = public_key

private_key instance-attribute

private_key = private_key

passphrase instance-attribute

passphrase = _require_non_empty_str(passphrase, 'passphrase') if passphrase is not None else None

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.

{}

broker_id instance-attribute

broker_id = _require_non_empty_str(broker_id, 'broker_id')

user_id instance-attribute

user_id = _require_non_empty_str(user_id, 'user_id')

password instance-attribute

password = _require_non_empty_str(password, 'password')

auth_code instance-attribute

auth_code = auth_code

app_id instance-attribute

app_id = app_id

md_front instance-attribute

md_front = _validate_tcp_front(md_front, 'md_front')

td_front instance-attribute

td_front = _validate_tcp_front(td_front, 'td_front')

product_info instance-attribute

product_info = product_info

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.

{}

host instance-attribute

host = _require_non_empty_str(host, 'host')

port instance-attribute

port = _validate_port(port, 'port')

client_id instance-attribute

client_id = client_id

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 认证配置.

支持三种认证方式
  1. Client Portal Gateway (个人客户): base_url="https://localhost:5000", verify_ssl=False
  2. OAuth 2.0 (机构客户): base_url="https://api.interactivebrokers.com", 需提供 client_id + private_key_path 或 access_token
  3. 浏览器 Cookie (个人客户增强认证): 从已登录浏览器提取 cookie, 用于访问需要浏览器会话的端点 (/portfolio/{id}/summary 等)

Initialize Interactive Brokers Web API authentication configuration.

Supports three authentication methods
  1. Client Portal Gateway (individual): base_url="https://localhost:5000", verify_ssl=False
  2. OAuth 2.0 (institutional): base_url="https://api.interactivebrokers.com", requires client_id + private_key_path or access_token
  3. 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.

{}

base_url instance-attribute

base_url = _validate_url(base_url, 'base_url')

account_id instance-attribute

account_id = account_id

access_token instance-attribute

access_token = access_token

client_id instance-attribute

client_id = client_id

private_key_path instance-attribute

private_key_path = private_key_path

verify_ssl instance-attribute

verify_ssl = verify_ssl

proxies instance-attribute

proxies = proxies

timeout instance-attribute

timeout = timeout

cookies instance-attribute

cookies = cookies

cookie_source instance-attribute

cookie_source = cookie_source

cookie_browser instance-attribute

cookie_browser = cookie_browser

cookie_path instance-attribute

cookie_path = cookie_path