WebSocket channels are divided into two categories: public and private channels.
You can configure the following properties in the OKS property.
When the client successfully connects to OKX servers, the event OnOKXConnect is fired. If there is any error while trying to connect, the event OnOKXError will be fired with the error details.
After the event OnOKXConnect is fired, then you can start to send and receive messages from OKX servers.
oClient := TsgcWebSocketClient.Create(nil);
oOKX := TsgcWSAPI_OKX.Create(nil);
oOKX.Client := oClient;
oOKX.OKX.ApiKey := 'alsdjk23kandfnasbdfdkjhsdf';
oOKX.OKX.ApiSecret := 'aldskjfk3jkadknfajndsjfj23j';
oOKX.OKX.Passphrase := 'secret_passphrase';
oClient.Active := True;
procedure OnOKXConnect(Sender: TObject; aMessage, aCode, aRawMessage: string);
begin
DoLog('#OKX Connected');
end;
procedure OnOKXError(Sender: TObject; aCode, aMessage, aRawMessage: string);
begin
DoLog('#error: ' + aMessage);
end;
The websocket feed provides real-time market data updates for orders and trades. The websocket feed has some public channels like ticker, trades...
You can subscribe to the following Public channels:
Method | Description |
SubscribeInstruments | The full instrument list will be pushed for the first time after subscription. Subsequently, the instruments will be pushed if there is any change to the instrument’s state (such as delivery of FUTURES, exercise of OPTION, listing of new contracts / trading pairs, trading suspension, etc.). |
SubscribeTicker | Retrieve the last traded price, bid price, ask price and 24-hour trading volume of instruments. Data will be pushed every 100 ms. |
SubscribeOpenInterest | Retrieve the open interest. Data will by pushed every 3 seconds. |
SubscribeCandlestick | Retrieve the candlesticks data of an instrument. the push frequency is the fastest interval 500ms push the data. |
SubscribeTrades | Retrieve the recent trades data. Data will be pushed whenever there is a trade. |
SubscribeEstimatedPrices |
Retrieve the estimated delivery/exercise price of FUTURES contracts and OPTION. Only the estimated delivery/exercise price will be pushed an hour before delivery/exercise, and will be pushed if there is any price change. |
SubscribeMarkPrice | Retrieve the mark price. Data will be pushed every 200 ms when the mark price changes, and will be pushed every 10 seconds when the mark price does not change. |
SubscribeMarkPriceCandlestick | Retrieve the candlesticks data of the mark price. Data will be pushed every 500 ms. |
SubscribePriceLimit | Retrieve the maximum buy price and minimum sell price of the instrument. Data will be pushed every 5 seconds when there are changes in limits, and will not be pushed when there is no changes on limit. |
SubscribeOrderBook |
Retrieve order book data. Use books for 400 depth levels, book5 for 5 depth levels, bbo-tbt tick-by-tick 1 depth level, books50-l2-tbt tick-by-tick 50 depth levels, and books-l2-tbt for tick-by-tick 400 depth levels.
|
SubscribeOptionSummary | Retrieve detailed pricing information of all OPTION contracts. Data will be pushed at once. |
SubscribeFundingRate | Retrieve funding rate. Data will be pushed in 30s to 90s. |
SubscribeIndexCandlestick | Retrieve the candlesticks data of the index. Data will be pushed every 500 ms. |
SubscribeIndexTicker | Retrieve index tickers data |
SubscribeStatus | Get the status of system maintenance and push when the system maintenance status changes. First subscription: "Push the latest change data"; every time there is a state change, push the changed content |
SubscribePublicStructureBlockTrades | Data will be pushed whenever there is a block trade. |
SubscribeBlockTickers | Retrieve the latest block trading volume in the last 24 hours. The data will be pushed when triggered by transaction execution event. In addition, it will also be pushed in 5 minutes interval according to subscription granularity. |
oClient := TsgcWebSocketClient.Create(nil);
oOKX := TsgcWSAPI_OKX.Create(nil);
oOKX.Client := oClient;
oOKX.OKX.ApiKey := 'alsdjk23kandfnasbdfdkjhsdf';
oOKX.OKX.ApiSecret := 'aldskjfk3jkadknfajndsjfj23j';
oOKX.OKX.Passphrase := 'secret_passphrase';
oClient.Active := True;
procedure OnOKXConnect(Sender: TObject; aMessage, aCode, aRawMessage: string);
begin
oOKX.SubscribeInstruments(okxitFutures);
end;
Including account channel, order channel, and position channel, etc -- require log in.
You can subscribe to the following Private channels:
Method | Description |
SubscribeAccount | Retrieve account information. Data will be pushed when triggered by events such as placing order, canceling order, transaction execution, etc. It will also be pushed in regular interval according to subscription granularity. |
SubscribePositions | Retrieve position information. Initial snapshot will be pushed according to subscription granularity. Data will be pushed when triggered by events such as placing/canceling order, and will also be pushed in regular interval according to subscription granularity. |
SubscribeBalanceAndPosition | Retrieve account balance and position information. Data will be pushed when triggered by events such as filled order, funding transfer. |
SubscribeOrders | Retrieve order information. Data will not be pushed when first subscribed. Data will only be pushed when triggered by events such as placing/canceling order. |
SubscribeOrdersAlgo | Retrieve algo orders (includes trigger order, oco order, conditional order). Data will not be pushed when first subscribed. Data will only be pushed when triggered by events such as placing/canceling order. |
SubscribeAdvanceAlgo |
Retrieve advance algo orders (including Iceberg order, TWAP order, Trailing order). Data will be pushed when first subscribed. Data will be pushed when triggered by events such as placing/canceling order. |
SubscribePositionRisk | This push channel is only used as a risk warning, and is not recommended as a risk judgment for strategic trading In the case that the market is not moving violently, there may be the possibility that the position has been liquidated at the same time that this message is pushed. |
SubscribeAccountGreeks | Retrieve account greeks information. Data will be pushed when triggered by events such as increase/decrease positions or cash balance in account, and will also be pushed in regular interval according to subscription granularity. |
SubscribeRfqs | Retrieve the Rfqs. |
SubscribeQuotes |
Retrieve the Quotes. |
SubscribePrivateStructureBlockTrades | Retrieve Structure Block Trades. |
SubscribeSpotGridAlgoOrders | Retrieve spot grid algo orders. Data will be pushed when first subscribed. Data will be pushed when triggered by events such as placing/canceling order. |
SubscribeContactGridAlgoOrders | Retrieve contract grid algo orders. Data will be pushed when first subscribed. Data will be pushed when triggered by events such as placing/canceling order. |
SubscribeGridPositions | Retrieve grid positions. Data will be pushed when first subscribed. Data will be pushed when triggered by events such as placing/canceling order. |
SubscribeGridSubOrders | Retrieve grid sub orders. Data will be pushed when first subscribed. Data will be pushed when triggered by events such as placing order. |
oClient := TsgcWebSocketClient.Create(nil);
oOKX := TsgcWSAPI_OKX.Create(nil);
oOKX.Client := oClient;
oOKX.OKX.ApiKey := 'alsdjk23kandfnasbdfdkjhsdf';
oOKX.OKX.ApiSecret := 'aldskjfk3jkadknfajndsjfj23j';
oOKX.OKX.Passphrase := 'secret_passphrase';
oClient.Active := True;
procedure OnOKXConnect(Sender: TObject; aMessage, aCode, aRawMessage: string);
begin
oOKX.SubscribeOrders(okxitFutures, 'BTC-USD', 'BTC-USD-200329');
end;
The WebSocket Trade requires Authentication.
You can place an order only if you have sufficient funds. Find below a table with the request parameters:
Parameter | Type | Required | Description |
---|---|---|---|
id | String | Yes | Unique identifier of the message Provided by client. It will be returned in response message for identifying the corresponding request. A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 32 characters. |
> instId | String | Yes | Instrument ID,e.g. BTC-USD-190927-5000-C |
> tdMode | String | Yes | Trade mode Margin mode isolated cross Non-Margin mode cash |
> ccy | String | No | Margin currency Only applicable to cross MARGIN orders in Single-currency margin . |
> clOrdId | String | No | Client-supplied order ID A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 32 characters. |
> tag | String | No | Order tag A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 16 characters. |
> side | String | Yes | Order side, buy sell |
> posSide | String | Optional | Position side The default is net in the net modeIt is required in the long/short mode, and can only be long or short .Only applicable to FUTURES/SWAP . |
> ordType | String | Yes | Order typemarket : market orderlimit : limit orderpost_only : Post-only orderfok : Fill-or-kill orderioc : Immediate-or-cancel orderoptimal_limit_ioc :Market order with immediate-or-cancel order
|
> sz | String | Yes | Quantity to buy or sell. |
> px | String | Optional | Price Only applicable to limit ,post_only ,fok ,ioc order. |
> reduceOnly | Boolean | No | Whether to reduce position only or not, true false , the default is false .Only applicable to MARGIN orders, and FUTURES/SWAP orders in net modeOnly applicable to Single-currency margin and Multi-currency margin |
> tgtCcy | String | No | Quantity typebase_ccy : Base currency ,quote_ccy : Quote currencyOnly applicable to SPOT traded with Market orderDefault is quote_ccy for buy, base_ccy for sell
|
> banAmend | Boolean | No | Whether to ban amending spot order or not, true or false, the default is false. It will fail to place orders if the balance is not enough when banAmend is true. Only applicable to SPOT market order |
Place Order Example
You can place an order only if you have sufficient funds.
// Place Martket Order
TsgcWSAPI_OKX1.PlaceMarketOrder(okxosBuy, 'ETH-BTC', 1);
// Place Limit Order
TsgcWSAPI_OKX1.PlaceLimitOrder(okxosBuy, 'ETH-BTC', 1, 0.25);
Cancel Order Example
Cancel an incomplete order
TsgcWSAPI_OKX1.CancelOrder('
ETH-BTC
', '457589362405027840');
Amend Order
Amend an incomplete order.
TsgcWSAPI_OKX1.AmendOrder('ETH-BTC', '457589362405027840', '', 2);