| Title: | An Unofficial Wrapper for 'okx exchange v5' API |
|---|---|
| Description: | An unofficial wrapper for 'okx exchange v5' API <https://www.okx.com/docs-v5/en/>, including 'REST' API and 'WebSocket' API. |
| Authors: | Yongchao Fang [aut, cre, cph] |
| Maintainer: | Yongchao Fang <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1.9000 |
| Built: | 2026-05-26 08:49:29 UTC |
| Source: | https://github.com/fanggong/okxapi |
The main purpose is to handle APIs that have limitations on the number of results returned per single request.
get_positions_history
get_history_candles
Wrapper for API Get candlesticks and Get candlesticks history.
get_history_candles( api_key, secret_key, passphrase, instId, before, after = Sys.time(), bar = c("1m", "3m", "5m", "15m", "30m", "1H", "4H", "6H", "12H", "1D", "2D", "3D"), ... )get_history_candles( api_key, secret_key, passphrase, instId, before, after = Sys.time(), bar = c("1m", "3m", "5m", "15m", "30m", "1H", "4H", "6H", "12H", "1D", "2D", "3D"), ... )
api_key |
Okx API key. |
secret_key |
Okx API secret key. |
passphrase |
Okx API passphrase. |
instId |
Instrument ID, e.g. BTC-USDT-SWAP. |
before |
POSIXct type. Return records newer than |
after |
POSIXct type. Return records earlier than |
bar |
Bar size, the default is 1m, e.g. 1m/3m/5m/15m/30m/1H/2H/4H, Hong Kong time opening price k-line: 6H/12H/1D/2D/3D. |
... |
Other request parameters to be passed, See Get candlesticks history for more information. |
Candlestick charts data
## Not run: candles <- get_history_candles( api_key, secret_key, passphrase, bar = "1m", count = 24*60, instId = "CFX-USDT-SWAP" ) ## End(Not run)## Not run: candles <- get_history_candles( api_key, secret_key, passphrase, bar = "1m", count = 24*60, instId = "CFX-USDT-SWAP" ) ## End(Not run)
Wrapper for API Get positions history.
get_positions_history( api_key, secret_key, passphrase, before, after, period, ... )get_positions_history( api_key, secret_key, passphrase, before, after, period, ... )
api_key |
Okx API key. |
secret_key |
Okx API secret key. |
passphrase |
Okx API passphrase. |
before |
POSIXct type. Return records newer than |
after |
POSIXct type. Return records earlier than |
period |
Due to the 'Number of results per request' limitation of the API,
the |
... |
Other request parameters to be passed, See Get positions history for more information. |
Position data
## Not run: positions <- get_positions_history( api_key, secret_key, passphrase, count = 90, period = 10, instType = "SWAP", mgnMode = "isolated" ) ## End(Not run)## Not run: positions <- get_positions_history( api_key, secret_key, passphrase, count = 90, period = 10, instType = "SWAP", mgnMode = "isolated" ) ## End(Not run)
Base class for Okx exchange v5 API.
You can implement all REST API requests in Okx exchange by inheriting the restAPI class.
Please refer to the example provided at the end of the document.
For commonly used interfaces, they have been implemented in this package.
The naming convention is as follows: for "/api/v5/AAA/BBB", a new class named restAPIAAA,
which inherits from restAPI, has been defined in this package.
The BBB method in this new class is used to call the API.
urlOkx REST API url, which is https://www.okx.com.
api_keyOkx API key.
secret_keyOkx API secret key.
passphraseOkx API passphrase.
simulateWhether to use demo trading service.
new()
Create a new REST API object.
restAPI$new(api_key, secret_key, passphrase, simulate = FALSE)
api_keyOkx API key.
secret_keyOkx API secret key.
passphraseOkx API passphrase.
simulateWhether to use demo trading service.
get_timestamp()
Get UTC timestamp.
restAPI$get_timestamp()
get_request_path()
Get request path.
restAPI$get_request_path(api, method = c("GET", "POST"), ...)apiRequest api e.g. /api/v5/account/positions-history.
methodRequest method, GET or POST.
...Other request parameters.
get_body()
Get request body.
restAPI$get_body(method = c("GET", "POST"), ...)methodRequest method, GET or POST.
...Other request parameters.
get_message()
Get the signing messages.
restAPI$get_message(timestamp, request_path, body, method = c("GET", "POST"))timestampRetrieve through method get_timestamp.
request_pathRetrieve through method get_request_path.
bodyRetrieve through method get_body.
methodRequest method, GET or POST.
get_signature()
Get the signature.
restAPI$get_signature(msg, secret_key = self$secret_key)
msgRetrieve through method get_message.
secret_keyOkx API secret key.
get_header()
Get request headers.
restAPI$get_header(timestamp, msg)
timestampRetrieve through method get_timestamp.
msgRetrieve through method get_message.
get_result()
Retrieve data from api.
restAPI$get_result(api, method = c("GET", "POST"), process, ...)apiRequest api e.g. /api/v5/account/positions-history.
methodRequest method, GET or POST.
processA function to process the data received from the API.
...Other request parameters.
clone()
The objects of this class are cloneable with this method.
restAPI$clone(deep = FALSE)
deepWhether to make a deep clone.
restAPItrade
restAPIaccount
restAPImarket
## Not run: # for [Get currencies](https://www.okx.com/docs-v5/en/#rest-api-funding-get-currencies) # you can define the class like this myRestAPI <- R6::R6Class( inherit = restAPI, public = list( get_currencies = function(ccy, process = "identity") { self$get_result( api = "/api/v5/asset/currencies", method = "GET", process = process, ccy = ccy ) } ) ) # And call it like this tmp <- myRestAPI$new(api_key, secret_key, passphrase) tmp$get_currencies("BTC") ## End(Not run)## Not run: # for [Get currencies](https://www.okx.com/docs-v5/en/#rest-api-funding-get-currencies) # you can define the class like this myRestAPI <- R6::R6Class( inherit = restAPI, public = list( get_currencies = function(ccy, process = "identity") { self$get_result( api = "/api/v5/asset/currencies", method = "GET", process = process, ccy = ccy ) } ) ) # And call it like this tmp <- myRestAPI$new(api_key, secret_key, passphrase) tmp$get_currencies("BTC") ## End(Not run)
Wrapper for REST API ACCOUNT.
okxAPI::restAPI -> restAPIaccount
balance()
See Get balance for more information.
restAPIaccount$balance(ccy, process = "identity")
ccySingle currency or a vector composed of multiple currencies. (no more than 20).
processA function to process the data received from the API. Default to identity.
positions()
See Get positions for more information.
restAPIaccount$positions(process = "identity", ...)
processA function to process the data received from the API. Default to identity.
...Other request parameters.
positions_history()
See Get positions history for more information.
restAPIaccount$positions_history(process = "identity", ...)
processA function to process the data received from the API. Default to identity.
...Other request parameters.
clone()
The objects of this class are cloneable with this method.
restAPIaccount$clone(deep = FALSE)
deepWhether to make a deep clone.
Wrapper for REST API MARKET.
okxAPI::restAPI -> restAPImarket
books()
See Get order book for more information.
restAPImarket$books(instId, process = "identity", ...)
instIdInstrument ID, e.g. CFX-USDT
processA function to process the data received from the API. Default to identity.
...Other request parameters.
candles()
See Get candlesticks for more information.
restAPImarket$candles(instId, process = "identity", ...)
instIdInstrument ID, e.g. BTC-USD-190927-5000-C.
processA function to process the data received from the API. Default to identity.
...Other request parameters.
history_candles()
See Get candlesticks history for more information.
restAPImarket$history_candles(instId, process = "identity", ...)
instIdInstrument ID, e.g. BTC-USD-190927-5000-C.
processA function to process the data received from the API. Default to identity.
...Other request parameters.
clone()
The objects of this class are cloneable with this method.
restAPImarket$clone(deep = FALSE)
deepWhether to make a deep clone.
Wrapper for REST API TRADE.
okxAPI::restAPI -> restAPItrade
order()
See Place order for more information.
restAPItrade$order(
instId,
tdMode = c("isolated", "cross", "cash"),
side = c("buy", "sell"),
sz,
ordType = c("market", "limit", "post_only", "fok", "ioc", "optimal_limit_ioc"),
process = "identity",
...
)instIdInstrument ID, e.g. BTC-USD-190927-5000-C.
tdModeTrade mode. Margin mode: cross or isolated. Non-Margin mode: cash.
sideOrder side, buy or sell.
szQuantity to buy or sell.
ordTypeOrder type. market: Market order, limit: Limit order, post_only: Post-only order,
fok: Fill-or-kill order, ioc: Immediate-or-cancel order,
optimal_limit_ioc: Market order with immediate-or-cancel order (applicable only to Futures and Perpetual swap).
processA function to process the data received from the API. Default to identity.
...Other request parameters.
cancel_order()
See Cancel order for more information.
restAPItrade$cancel_order(instId, ordId, clOrdId, process = "identity", ...)
instIdInstrument ID, e.g. BTC-USD-190927.
ordIdOrder ID, Either ordId or clOrdId is required. If both are passed, ordId will be used.
clOrdIdClient Order ID as assigned by the client.
processA function to process the data received from the API. Default to identity.
...Other request parameters.
orders()
See Get order details for more information.
restAPItrade$orders(process = "identity", ...)
processA function to process the data received from the API. Default to identity.
...Other request parameters.
orders_pending()
See Get order List for more information.
restAPItrade$orders_pending(process = "identity", ...)
processA function to process the data received from the API. Default to identity.
...Other request parameters.
orders_algo_pending()
See Get algo order list for more information.
restAPItrade$orders_algo_pending(process = "identity", ...)
processA function to process the data received from the API. Default to identity.
...Other request parameters.
clone()
The objects of this class are cloneable with this method.
restAPItrade$clone(deep = FALSE)
deepWhether to make a deep clone.
Private channel of WebSocket API for Okx exchange v5 API. See Private Channel for more information.
channelPrivate WebSocket url.
api_keyOkx API key.
secret_keyOkx API secret key.
passphraseOkx API passphrase.
simulateWhether to use demo trading service.
wsA websocket::WebSocket object to establish a connection to the server.
new()
Craeate a new websocketAPIprivate object.
websocketAPIprivate$new(api_key, secret_key, passphrase, simulate = FALSE)
api_keyOkx API key.
secret_keyOkx API secret key.
passphraseOkx API passphrase.
simulateWhether to use demo trading service.
get_timestamp()
Get UTC timestamp.
websocketAPIprivate$get_timestamp()
get_message()
Get the signing messages.
websocketAPIprivate$get_message(timestamp)
timestampRetrieve through method get_timestamp.
get_signature()
Get the signature.
websocketAPIprivate$get_signature(secret_key, msg)
secret_keyOkx API secret key.
msgRetrieve through method get_message.
connect()
Initiate the connection to the server.
websocketAPIprivate$connect()
login()
Log in.
websocketAPIprivate$login()
on_open()
Called when the connection is established.
websocketAPIprivate$on_open(func)
funcA Callback function.
on_close()
Called when a previously-opened connection is closed. The event will have 'code' (integer) and 'reason' (one-element character) elements that describe the remote's reason for closing.
websocketAPIprivate$on_close(func)
funcA Callback function.
on_message()
Called each time a message is received from the server. The event will have a 'data' element, which is the message content.
websocketAPIprivate$on_message(func)
funcA Callback function.
on_error()
Called when the connection fails to be established. The event will have an 'message' element, a character vector of length 1 describing the reason for the error.
websocketAPIprivate$on_error(func)
funcA Callback function.
send()
Send a message to the server.
websocketAPIprivate$send(msg)
msgMessages.
close()
Close the connection.
websocketAPIprivate$close()
clone()
The objects of this class are cloneable with this method.
websocketAPIprivate$clone(deep = FALSE)
deepWhether to make a deep clone.
## Not run: tmp <- websocketAPIprivate$new(api_key, secret_key, passphrase) tmp$connect() Sys.sleep(1) tmp$login() # subscribe account information msg <- list( op = "subscribe", args = list( list(channel = "account", ccy = "USDT") ) ) msg <- jsonlite::toJSON(msg, auto_unbox = TRUE, pretty = TRUE) tmp$send(msg) # pass your own callback function tmp$on_message(function(event) { if (event$data == "pong") { cat("Bingo!!\n") } }) tmp$send("ping") tmp$close() ## End(Not run)## Not run: tmp <- websocketAPIprivate$new(api_key, secret_key, passphrase) tmp$connect() Sys.sleep(1) tmp$login() # subscribe account information msg <- list( op = "subscribe", args = list( list(channel = "account", ccy = "USDT") ) ) msg <- jsonlite::toJSON(msg, auto_unbox = TRUE, pretty = TRUE) tmp$send(msg) # pass your own callback function tmp$on_message(function(event) { if (event$data == "pong") { cat("Bingo!!\n") } }) tmp$send("ping") tmp$close() ## End(Not run)
Public channel of WebSocket API for Okx exchange v5 API. See Public Channel for more information.
channelPublic WebSocket url.
simulateWhether to use demo trading service.
wsA websocket::WebSocket object to establish a connection to the server.
new()
Create a new websocketAPIpublic object.
websocketAPIpublic$new(simulate = FALSE)
simulateWhether to use demo trading service.
connect()
Initiate the connection to the server.
websocketAPIpublic$connect()
on_open()
Called when the connection is established.
websocketAPIpublic$on_open(func)
funcA Callback function.
on_close()
Called when a previously-opened connection is closed. The event will have 'code' (integer) and 'reason' (one-element character) elements that describe the remote's reason for closing.
websocketAPIpublic$on_close(func)
funcA Callback function.
on_message()
Called each time a message is received from the server. The event will have a 'data' element, which is the message content.
websocketAPIpublic$on_message(func)
funcA Callback function.
on_error()
Called when the connection fails to be established. The event will have an 'message' element, a character vector of length 1 describing the reason for the error.
websocketAPIpublic$on_error(func)
funcA Callback function.
send()
Send a message to the server.
websocketAPIpublic$send(msg)
msgMessages.
close()
Close the connection.
websocketAPIpublic$close()
clone()
The objects of this class are cloneable with this method.
websocketAPIpublic$clone(deep = FALSE)
deepWhether to make a deep clone.
## Not run: tmp <- websocketAPIpublic$new() tmp$connect() # subscribe BTC-USDT-SWAP 5m candlesticks data msg <- list( op = "unsubscribe", args = list( list(channel = "candle5m", instId = "BTC-USDT-SWAP") ) ) msg <- jsonlite::toJSON(msg, auto_unbox = TRUE, pretty = TRUE) tmp$send(msg) # pass your own callback function tmp$on_message(function(event) { if (event$data == "pong") { cat("Bingo!!\n") } }) tmp$send("ping") tmp$close() ## End(Not run)## Not run: tmp <- websocketAPIpublic$new() tmp$connect() # subscribe BTC-USDT-SWAP 5m candlesticks data msg <- list( op = "unsubscribe", args = list( list(channel = "candle5m", instId = "BTC-USDT-SWAP") ) ) msg <- jsonlite::toJSON(msg, auto_unbox = TRUE, pretty = TRUE) tmp$send(msg) # pass your own callback function tmp$on_message(function(event) { if (event$data == "pong") { cat("Bingo!!\n") } }) tmp$send("ping") tmp$close() ## End(Not run)