CEX.IO Plus WebSocket API Integration in Delphi
CEX.IO Plus is the advanced trading platform from CEX.IO, designed for professional and institutional traders. The TsgcWSAPI_CexPlus component provides comprehensive Delphi access to its WebSocket API, covering market data subscriptions, order management, account operations, and fund transfers — all through a single persistent connection.
Table of Contents
Overview
The CEX.IO Plus API expands significantly on the standard CEX.IO offering. It provides three tiers of functionality: public subscriptions for real-time order book and trade feeds, market data queries for on-demand snapshots of tickers, candles, and exchange information, and account methods for full trading operations including order management, fee queries, transaction history, and fund transfers. The TsgcWSAPI_CexPlus component encapsulates all of these capabilities.
Getting Started
Create a TsgcWebSocketClient and a TsgcWSAPI_CexPlus component, link them, and configure your API credentials. The component connects to the CEX.IO Plus WebSocket endpoint automatically.
oClient := TsgcWebSocketClient.Create(nil);
oCexPlus := TsgcWSAPI_CexPlus.Create(nil);
oCexPlus.Client := oClient;
oCexPlus.CexPlus.ApiKey := 'your_api_key';
oCexPlus.CexPlus.ApiSecret := 'your_api_secret';
oClient.Active := True;
Public WebSocket Methods
Public subscription methods provide streaming real-time data without requiring authentication. These are ideal for market monitoring and building live data feeds.
| Method | Description |
|---|---|
SubscribeOrderBook |
Subscribe to real-time order book updates for a trading pair. |
UnSubscribeOrderBook |
Unsubscribe from order book updates for a trading pair. |
SubscribeTrade |
Subscribe to live trade execution feed for a trading pair. |
UnSubscribeTrade |
Unsubscribe from the trade execution feed. |
// Subscribe to BTC-USD order book and trades
oCexPlus.SubscribeOrderBook('BTC-USD');
oCexPlus.SubscribeTrade('BTC-USD');
// Unsubscribe when no longer needed
oCexPlus.UnSubscribeOrderBook('BTC-USD');
oCexPlus.UnSubscribeTrade('BTC-USD');
Market Data Methods
Market data methods return on-demand snapshots of exchange data. Unlike subscriptions, these are request-response calls that return a single result.
| Method | Description |
|---|---|
GetTicker |
Retrieve the current ticker (bid, ask, last price) for a pair. |
GetOrderBook |
Retrieve the current order book snapshot for a pair. |
GetCandles |
Retrieve OHLCV candle data for charting. |
GetTradeHistory |
Retrieve recent public trade history for a pair. |
GetServerTime |
Retrieve the current server timestamp for synchronization. |
GetPairsInfo |
Retrieve information about all available trading pairs. |
GetCurrenciesInfo |
Retrieve information about all supported currencies. |
GetProcessingInfo |
Retrieve deposit and withdrawal processing details. |
Querying Market Data
// Get current ticker for BTC-USD
oCexPlus.GetTicker('BTC-USD');
// Get order book snapshot
oCexPlus.GetOrderBook('BTC-USD');
// Get candle data for charting
oCexPlus.GetCandles('BTC-USD');
// Get available trading pairs
oCexPlus.GetPairsInfo;
Account and Trading Methods
Account methods require authentication and provide full trading capabilities, including order placement, account status checks, fee management, and fund transfers.
Account Management
| Method | Description |
|---|---|
Ping |
Send a keepalive ping to maintain the connection. |
CreateAccount |
Create a new sub-account on the platform. |
GetAccountStatus |
Retrieve the current account status and details. |
GetCurrentFee |
Retrieve the current trading fee rate for the account. |
GetFeeStrategy |
Retrieve the fee strategy and tier information. |
GetVolume |
Retrieve the trading volume for fee tier calculation. |
Order Management
| Method | Description |
|---|---|
GetOrders |
Retrieve all open orders on the account. |
NewOrder |
Place a new order with full parameter control. |
NewMarketOrder |
Place a market order that executes at the current best price. |
NewLimitOrder |
Place a limit order at a specified price. |
CancelOrder |
Cancel a specific open order by its identifier. |
CancelAllOrders |
Cancel all open orders at once. |
History and Funds
| Method | Description |
|---|---|
GetTransactionHistory |
Retrieve the account's transaction history. |
GetFundingHistory |
Retrieve deposit and withdrawal history. |
InternalTransfer |
Transfer funds between sub-accounts internally. |
GetDepositAddress |
Generate or retrieve a deposit address for a currency. |
FundsDepositFromWallet |
Deposit funds from an external wallet into the trading account. |
FundsWithdrawalToWallet |
Withdraw funds from the trading account to an external wallet. |
Code Example
The following example demonstrates connecting to CEX.IO Plus, checking account status, subscribing to the order book, and placing a market order.
oClient := TsgcWebSocketClient.Create(nil);
oCexPlus := TsgcWSAPI_CexPlus.Create(nil);
oCexPlus.Client := oClient;
oCexPlus.CexPlus.ApiKey := 'your_api_key';
oCexPlus.CexPlus.ApiSecret := 'your_api_secret';
oClient.Active := True;
// Get account status
oCexPlus.GetAccountStatus;
// Subscribe to order book
oCexPlus.SubscribeOrderBook('BTC-USD');
// Place a market order
oCexPlus.NewMarketOrder('BTC', 'USD', cxpsBuy);
Placing Different Order Types
CEX.IO Plus supports multiple order types. Use NewMarketOrder for immediate execution at the best available price, NewLimitOrder for price-specific orders, or NewOrder for full parameter control.
// Market order: buy BTC with USD at market price
oCexPlus.NewMarketOrder('BTC', 'USD', cxpsBuy);
// Limit order: buy BTC at a specific price
oCexPlus.NewLimitOrder('BTC', 'USD', cxpsBuy, 0.01, 30000);
// Cancel a specific order
oCexPlus.CancelOrder('order-id-here');
// Cancel all open orders
oCexPlus.CancelAllOrders;
Fund Transfers
The component supports internal fund transfers between sub-accounts as well as deposit and withdrawal operations.
// Get deposit address for BTC
oCexPlus.GetDepositAddress('BTC');
// Retrieve funding history
oCexPlus.GetFundingHistory;
// Transfer funds between sub-accounts
oCexPlus.InternalTransfer('from-account', 'to-account', 'BTC', 0.5);
Configuration and Notes
API Credentials
Generate your API key and secret from the CEX.IO Plus platform. These are distinct from standard CEX.IO credentials. Ensure the API key has the appropriate permissions enabled for the operations you intend to perform (e.g., trading, withdrawal).
Pair Naming Convention
CEX.IO Plus uses a hyphenated pair format such as BTC-USD, unlike the standard CEX.IO API which uses separate base and quote currency parameters. Always use the hyphenated format when calling subscription methods and market data queries.
Order Side Enumeration
Order methods use the cxpsBuy and cxpsSell enumeration values to specify the order direction. These are specific to the CEX.IO Plus component and differ from the standard CEX.IO enumeration.
Connection Keepalive
Use the Ping method at regular intervals to keep the authenticated session alive. This prevents the server from closing the connection due to inactivity.
GetPairsInfo and GetCurrenciesInfo at startup to dynamically discover available trading pairs and supported currencies, rather than hardcoding them in your application.
Error Handling
All responses from CEX.IO Plus include a status field. Handle error responses in the component's message event by checking the response status and error details. Common errors include insufficient balance, invalid pair names, and authentication failures.