Binance WebSocket 和 REST API

通过单个类型化组件流式获取 Binance 市场数据(成交、K 线、行情、部分深度、差异深度),并调用签名 REST API。

TsgcWSAPI_Binance

现货交易的 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 均由同一组件公开。

市场数据流

SubscribeAggregateTradesSubscribeTradesSubscribeKLineSubscribeTickerSubscribeAllMarketTickersSubscribePartialBookDepthSubscribeDiffDepth 涵盖 Binance 全部公开流。

签名 REST API

Binance.REST_API 公开完整现货 REST 接口:GetAccountInformationGet24hrTickerGetKLinesGetOrderBookGetOpenOrdersWalletWithdrawGetWalletDepositHistory 等 40 余个类型化方法。

现货、Binance.US 和测试网

切换 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 的权威参考来源。

文档与演示

直接访问组件参考文档,获取即用型演示项目,并下载试用版。

Online Help — API_Binance 本组件的完整属性、方法和事件参考。
Demo Project — Demos\05.Crypto\01.Binance 即用型示例项目,随 sgcWebSockets 包一起提供 — 请在下方下载试用版。
技术文档(PDF) 功能概述、快速入门、Delphi、C++ Builder 和 .NET 代码示例及主要参考资源 — 仅适用于本组件。
用户手册(PDF) 涵盖库中每个组件的综合手册。

准备好在 Binance 上交易了吗?

下载免费试用版,将 Binance 现货、期货和账户流集成到您的 Delphi 应用程序中。