Binance WebSocket 和 REST API
通过单个类型化组件流式获取 Binance 市场数据(成交、K 线、行情、部分深度、差异深度),并调用签名 REST API。
通过单个类型化组件流式获取 Binance 市场数据(成交、K 线、行情、部分深度、差异深度),并调用签名 REST API。
现货交易的 WebSocket 市场流加 REST_API 访问。配合 TsgcWebSocketClient 作为传输层,使用 HMAC-SHA256 对您的 ApiKey/ApiSecret 进行签名。
TsgcWSAPI_Binance
Windows, macOS, Linux, iOS, Android
Standard / Professional / Enterprise
将 TsgcWebSocketClient 连接到 API 组件,如需签名频道则设置 Binance.ApiKey/ApiSecret,然后调用 SubscribeTicker / SubscribeKLine / SubscribeDiffDepth。
uses
sgcWebSocket, sgcWebSocket_API_Binance;
var
WSClient: TsgcWebSocketClient;
Binance: TsgcWSAPI_Binance;
begin
WSClient := TsgcWebSocketClient.Create(nil);
Binance := TsgcWSAPI_Binance.Create(nil);
Binance.Client := WSClient;
Binance.Binance.ApiKey := 'your-api-key';
Binance.Binance.ApiSecret := 'your-api-secret';
WSClient.Active := True;
// public market streams
Binance.SubscribeTicker('bnbbtc');
Binance.SubscribeKLine('bnbbtc', bci5m);
Binance.SubscribeDiffDepth('bnbbtc');
// REST_API call (signed)
ShowMessage(Binance.REST_API.GetAccountInformation);
end;
// uses: sgcWebSocket, sgcWebSocket_API_Binance
TsgcWebSocketClient *WSClient = new TsgcWebSocketClient(this);
TsgcWSAPI_Binance *Binance = new TsgcWSAPI_Binance(this);
Binance->Client = WSClient;
Binance->Binance->ApiKey = "your-api-key";
Binance->Binance->ApiSecret = "your-api-secret";
WSClient->Active = true;
Binance->SubscribeTicker("bnbbtc");
Binance->SubscribeKLine("bnbbtc", bci5m);
Binance->SubscribeDiffDepth("bnbbtc");
ShowMessage(Binance->REST_API->GetAccountInformation());
using esegece.sgcWebSockets;
var WSClient = new TsgcWebSocketClient();
var Binance = new TsgcWSAPI_Binance();
Binance.Client = WSClient;
Binance.Binance.ApiKey = "your-api-key";
Binance.Binance.ApiSecret = "your-api-secret";
WSClient.Active = true;
Binance.SubscribeTicker("bnbbtc");
Binance.SubscribeKLine("bnbbtc", TBinanceCryptoInterval.bci5m);
Binance.SubscribeDiffDepth("bnbbtc");
WebSocket 市场流和签名 REST API 均由同一组件公开。
SubscribeAggregateTrades、SubscribeTrades、SubscribeKLine、SubscribeTicker、SubscribeAllMarketTickers、SubscribePartialBookDepth 和 SubscribeDiffDepth 涵盖 Binance 全部公开流。
Binance.REST_API 公开完整现货 REST 接口:GetAccountInformation、Get24hrTicker、GetKLines、GetOrderBook、GetOpenOrders、WalletWithdraw、GetWalletDepositHistory 等 40 余个类型化方法。
切换 Binance.BinanceUS 使用美国端点,切换 Binance.TestNet 使用沙盒。组件会同时切换 WebSocket 和 REST 基础 URL。
Set Binance.ApiKey and Binance.ApiSecret — the component computes the HMAC-SHA256 signature Binance requires for signed REST endpoints and User Data streams.
使用 StartUserDataStream 获取 listenKey,然后订阅以获取实时账户、订单和余额更新。KeepAliveUserDataStream 保持密钥有效。
对于 USDT/币本位合约期货,使用兄弟组件 TsgcWSAPI_Binance_Futures — 相同模式,通过 FuturesContracts 选择 bfcUSDT 或 bfcCOIN。
本组件所实现 API 的权威参考来源。