目前,支持的 API 版本为 V5。V5 API 为 Bybit 的产品线带来了统一性和效率,在一套规范中统一了现货、衍生品和期权。
| OpenAPI 版本 | 账户类型 | 线性 | 反向 | 现货 | 选项 | |||
|---|---|---|---|---|---|---|---|---|
| USDT 永续 | USDC 永续合约 | USDC 期货 | 永续 | 期货 | ||||
| V5 | 统一交易账户 | ✓ | ✓ | ✓ | 见注释 | ✓ | ✓ | |
| 经典账户 | ✓ | ✓ | ✓ | ✓ | ||||
| V3 | 统一交易账户 | ✓ | ✓ | ✓ | ||||
| 经典账户 | ✓ | ✓ | ✓ | ✓ | ||||
*注意:统一账户支持反向交易。但是,使用的保证金来自反向衍生品钱包,而非统一钱包。
您可以在 Bybit 属性中配置以下属性。
当客户端成功连接到 Bybit 服务器时,OnConnect 事件触发。OnConnect 事件触发后,您可以开始向 Bybit 服务器发送和接收消息。如果连接到私有 WebSocket 频道,必须等到 OnBybitAuthentication 事件触发且 success 参数为 true 后,再订阅任何频道。
该客户端支持多个 API,请使用 BybitClient 属性设置您要使用的 API:
以下是连接到 WebSocket Spot 私有 API 的示例。
oClient := TsgcWebSocketClient.Create(nil);
oBybit := TsgcWSAPI_Bybit.Create(nil);
oBybit.Client := oClient;
oBybit.Bybit.ApiKey := 'alsdjk23kandfnasbdfdkjhsdf';
oBybit.Bybit.ApiSecret := 'aldskjfk3jkadknfajndsjfj23j';
oBybit.BybitClient := bybSpot;
oClient.Active := True;
procedure OnConnect(Connection: TsgcWSConnection);
begin
DoLog('#Bybit Connected');
end;
成功连接到现货 WebSocket 服务器后,您可以开始订阅 WebSocket 频道。只需访问 SPOT 属性,然后调用任意可用的订阅/取消订阅方法即可。
bybit 客户端实现了以下事件,用于控制连接流程并获取从 WebSocket 服务器发送的数据:
websocket 数据流提供订单和交易的实时市场数据更新。websocket 数据流有一些公共频道,如 ticker、trades 等。
您可以订阅以下频道:
| 方法 | 公共或私有 | 描述 |
| SubscribeOrderBook | 公开 | 订阅订单簿流。支持不同深度。 |
| SubscribeTrade | 公开 | 订阅最新成交流。 |
| SubscribeTicker | 公开 | 订阅行情流。 |
| SubscribeKLine | 公开 | 订阅 K 线流。 |
| SubscribeLiquidation | 公开 | 订阅强制平仓流 |
| SubscribeLT_KLine | 公开 |
订阅杠杆代币 K 线数据流。 |
| SubscribeLT_Ticker | 公开 | 订阅杠杆代币行情数据流。 |
| SubscribeLT_Nav | 公开 | 订阅杠杆代币行情数据流。 |
| SubscribePosition | 私有 | 订阅杠杆代币净值数据流。 |
| SubscribeExecution | 私有 | 订阅 |
| SubscribeOrder | 私有 | 订阅 |
| SubscribeWallet | 私有 | 订阅 |
| SubscribeGreek | 私有 | 订阅 |
| SubscribeDcp | 私有 | 订阅 |
| SubscribeInsurance | 公开 | 订阅保险基金流。 |
| SubscribeOrderPriceLimit | 公开 | 订阅订单价格限制流。 |
| SubscribeADLAlert | 公开 | 订阅自动减仓预警流。 |
| SubscribeFastExecution | 私有 | 订阅快速执行流。 |
以下是成功身份验证后订阅私有 WebSocket 频道的示例。
oClient := TsgcWebSocketClient.Create(nil);
oBybit := TsgcWSAPI_Bybit.Create(nil);
oBybit.Client := oClient;
oBybit.Bybit.ApiKey := 'alsdjk23kandfnasbdfdkjhsdf';
oBybit.Bybit.ApiSecret := 'aldskjfk3jkadknfajndsjfj23j';
oBybit.BybitClient := bybSpot;
oClient.Active := True;
procedure OnBybitAuthentication(Sender: TObject; aSuccess: Boolean; const aError, aRawMessage: string)
begin
if aSuccess then
begin
oClient.SubscribeOrderBook('BTCUSDT');
oClient.SubscribeTrade('BTCUSDT');
end;
end;
REST API 提供一系列公共和私有方法,用于请求以下数据:市场行情、私有账户和钱包。以下是可用方法的列表。
| 方法 | 公开 / 私有 |
| GetServerTime | 公开 |
| GetKLine | 公开 |
| GetMarkPriceKLine | 公开 |
| GetIndexPriceKLine | 公开 |
| GetPremiumIndexPriceKLine | 公开 |
| GetInstrumentsInfo | 公开 |
| GetOrderBook | 公开 |
| GetTickers | 公开 |
| GetFundingRateHistory | 公开 |
| GetPublicRecentTradingHistory | 公开 |
| GetOpenInterest | 公开 |
| GetHistoricalVolatility | 公开 |
| GetInsurance | 公开 |
| GetRiskLimit | 公开 |
| GetDeliveryPrice | 公开 |
| GetLongShortRatio | 公开 |
| PlaceOrder | 私有 |
| PlaceMarketOrder | 私有 |
| PlaceLimitOrder | 私有 |
| AmendOrder | 私有 |
| CancelOrder | 私有 |
| GetOpenOrders | 私有 |
| CancelAllOrders | 私有 |
| GetOrderHistory | 私有 |
| GetPositionInfo | 私有 |
| SetLeverage | 私有 |
| SwitchCrossIsolatedMargin | 私有 |
| SetTPSLMode | 私有 |
| SwitchPositionMode | 私有 |
| SetRiskLimit | 私有 |
| SetTradingStop | 私有 |
| SetAutoAddMargin | 私有 |
| AddOrReduceMargin | 私有 |
| GetExecution | 私有 |
| GetClosedPNL | 私有 |
| ConfirmNewRiskLimit | 私有 |
| GetWalletBalance | 私有 |
| GetAccountInfo | 私有 |
| GetTransactionLog | 私有 |
| BatchPlaceOrder | 私有 |
| BatchAmendOrder | 私有 |
| BatchCancelOrder | 私有 |
| SetDCP | 私有 |
| GetFeeRate | 私有 |
| GetCollateralInfo | 私有 |
| SetMarginMode | 私有 |
| GetBorrowHistory | 私有 |
| GetCoinGreeks | 私有 |
| GetCoinInfo | 私有 |
| GetAllCoinsBalance | 私有 |
| CreateInternalTransfer | 私有 |
| GetInternalTransferList | 私有 |
| GetDepositRecords | 私有 |
| GetDepositAddress | 私有 |
| CreateWithdrawal | 私有 |
| CancelWithdrawal | 私有 |
| GetWithdrawalRecords | 私有 |
以下是获取未成交订单的示例。
oClient := TsgcWebSocketClient.Create(nil);
oBybit := TsgcWSAPI_Bybit.Create(nil);
oBybit.Client := oClient;
oBybit.Bybit.ApiKey := 'alsdjk23kandfnasbdfdkjhsdf';
oBybit.Bybit.ApiSecret := 'aldskjfk3jkadknfajndsjfj23j';
oBybit.BybitClient := bybSpot;
oBybit.REST_API.GetAccountInfo();