BtApi¶
BtApi 是 bt_api_py 框架的核心入口类,统一管理所有交易所的连接、行情查询、交易操作和账户管理。
使用示例¶
from bt_api_py import BtApi
api = BtApi(exchange_kwargs={
"BINANCE___SPOT": {"api_key": "...", "secret": "..."},
"OKX___SWAP": {"api_key": "...", "secret": "...", "passphrase": "..."},
})
# 查行情
ticker = api.get_tick("BINANCE___SPOT", "BTCUSDT")
ticker.init_data()
print(ticker.get_last_price())
# 下单
order = api.make_order("BINANCE___SPOT", "BTCUSDT", 0.001, 50000, "limit")
bt_api_py.bt_api.BtApi ¶
BtApi(exchange_kwargs: dict[str, Any] | None = None, debug: bool = True, event_bus: EventBus | None = None)
统一多交易所 API 入口,通过 ExchangeRegistry 实现交易所即插即用。
初始化 BtApi 实例。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
exchange_kwargs | dict[str, Any] | None | 交易所配置 dict,key 为 exchange_name,value 为对应参数。 | None |
debug | bool | 是否开启 debug 模式,控制日志输出。 | True |
event_bus | EventBus | None | 事件总线实例,用于 BarEvent/OrderEvent 等回调;None 则创建默认实例。 | None |
init_exchange ¶
根据 exchange_kwargs 初始化并添加交易所。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
exchange_kwargs | dict[str, Any] | {exchange_name: params} 格式的配置。 | 必需 |
add_exchange ¶
Add a new exchange to the API instance.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
exchange_name | str | Exchange identifier (e.g., "BINANCE___SPOT", "OKX___SWAP") | 必需 |
exchange_params | dict[str, Any] | Exchange-specific parameters (api_key, secret, etc.) | 必需 |
Example
api = BtApi() api.add_exchange("BINANCE___SPOT", { ... "api_key": "your_key", ... "secret": "your_secret", ... "testnet": True ... })
get_request_api ¶
Get the REST Feed instance for the specified exchange (synchronous API).
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
exchange_name | str | Exchange identifier (e.g., "BINANCE___SPOT") | 必需 |
返回:
| 类型 | 描述 |
|---|---|
Any | Feed instance if exchange exists, None otherwise |
Example
api = BtApi({"BINANCE___SPOT": {...}}) feed = api.get_request_api("BINANCE___SPOT") ticker = feed.get_tick("BTCUSDT")
get_data_queue ¶
Get the data queue for the specified exchange.
The data queue receives market data and order updates from WebSocket streams.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
exchange_name | str | Exchange identifier (e.g., "BINANCE___SPOT") | 必需 |
返回:
| 类型 | 描述 |
|---|---|
Queue | None | Queue instance if exchange exists, None otherwise |
Example
api = BtApi({"BINANCE___SPOT": {...}}) queue = api.get_data_queue("BINANCE___SPOT") data = queue.get() # Blocks until data arrives
subscribe ¶
通过 ExchangeRegistry 查找订阅处理函数,无需硬编码交易所类型
put_ticker ¶
Push a simulated ticker update into the event bus and optional exchange queue.
get_tick ¶
获取最新行情 :param exchange_name: 交易所标识, 如 "BINANCE___SWAP" :param symbol: 交易对, 如 "BTC-USDT"
get_depth ¶
get_depth(exchange_name: str, symbol: str, count: int = 20, extra_data: Any = None, **kwargs: Any) -> Any
获取深度数据 :param exchange_name: 交易所标识 :param symbol: 交易对 :param count: 深度档数
get_kline ¶
get_kline(exchange_name: str, symbol: str, period: str, count: int = 20, extra_data: Any = None, **kwargs: Any) -> Any
获取K线数据 :param exchange_name: 交易所标识 :param symbol: 交易对 :param period: K线周期, 如 "1m", "5m", "1H", "1D" :param count: K线数量
make_order ¶
make_order(exchange_name: str, symbol: str, volume: float, price: float, order_type: str, offset: str = 'open', post_only: bool = False, client_order_id: str | None = None, extra_data: Any = None, **kwargs: Any) -> Any
下单 :param exchange_name: 交易所标识 :param symbol: 交易对 :param volume: 数量 :param price: 价格 (市价单传0) :param order_type: 订单类型, "limit" / "market" :param offset: 开平方向, "open" / "close" / "close_today" / "close_yesterday" :param post_only: 是否只做 maker :param client_order_id: 客户端自定义订单ID
cancel_order ¶
cancel_order(exchange_name: str, symbol: str, order_id: str, extra_data: Any = None, **kwargs: Any) -> Any
撤单 :param exchange_name: 交易所标识 :param symbol: 交易对 :param order_id: 订单ID
cancel_all ¶
cancel_all(exchange_name: str, symbol: str | None = None, extra_data: Any = None, **kwargs: Any) -> Any
撤销所有订单 :param exchange_name: 交易所标识 :param symbol: 交易对 (None 表示所有品种)
query_order ¶
query_order(exchange_name: str, symbol: str, order_id: str, extra_data: Any = None, **kwargs: Any) -> Any
查询订单 :param exchange_name: 交易所标识 :param symbol: 交易对 :param order_id: 订单ID
get_open_orders ¶
get_open_orders(exchange_name: str, symbol: str | None = None, extra_data: Any = None, **kwargs: Any) -> Any
查询挂单 :param exchange_name: 交易所标识 :param symbol: 交易对 (None 表示所有品种)
get_balance ¶
get_balance(exchange_name: str, symbol: str | None = None, extra_data: Any = None, **kwargs: Any) -> Any
查询余额 :param exchange_name: 交易所标识 :param symbol: 币种 (None 表示全部)
get_account ¶
查询账户信息 :param exchange_name: 交易所标识 :param symbol: 币种
get_position ¶
get_position(exchange_name: str, symbol: str | None = None, extra_data: Any = None, **kwargs: Any) -> Any
查询持仓 :param exchange_name: 交易所标识 :param symbol: 交易对 (None 表示所有品种)
get_all_ticks ¶
从所有已连接的交易所获取行情 :param symbol: 交易对 :return: dict {exchange_name: ticker_data}
get_all_balances ¶
get_all_balances(symbol: str | None = None, extra_data: Any = None, **kwargs: Any) -> dict[str, Any]
从所有已连接的交易所查询余额 :return: dict {exchange_name: balance_data}
get_all_positions ¶
get_all_positions(symbol: str | None = None, extra_data: Any = None, **kwargs: Any) -> dict[str, Any]
从所有已连接的交易所查询持仓 :return: dict {exchange_name: position_data}
cancel_all_orders ¶
cancel_all_orders(symbol: str | None = None, extra_data: Any = None, **kwargs: Any) -> dict[str, Any]
撤销所有已连接交易所的所有订单 :return: dict {exchange_name: result}