An unofficial wrapper for Okx exchange v5 API
You can install the released version of okxAPI from CRAN with:
install.packages("okxAPI")
And the development version from GitHub with:
# install.packages("devtools")
::install_github("fanggong/okxAPI") devtools
Please refer to okx my api page regarding V5 API Key creation.
You can implement all REST API requests in Okx exchange by inheriting
the restAPI
class. For example, to implement Get
currencies, you can define the class and call the method as
follows:
<- R6::R6Class(
myRestAPI inherit = restAPI,
public = list(
get_currencies = function(ccy, process = "identity") {
$get_result(
selfapi = "/api/v5/asset/currencies", method = "GET", process = process,
ccy = ccy
)
}
)
)<- myRestAPI$new(api_key, secret_key, passphrase)
tmp $get_currencies("BTC") tmp
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.
A websocketAPIpublic
class based on websocket package is
used here.
Create new websocketAPIpublic
object and initiate the
connection to the server.
<- websocketAPIpublic$new()
tmp $connect() tmp
Subscribe BTC-USDT-SWAP 5m candlesticks data.
<- list(
msg op = "subscribe",
args = list(
list(channel = "candle5m", instId = "BTC-USDT-SWAP")
)
)<- jsonlite::toJSON(msg, auto_unbox = TRUE, pretty = TRUE)
msg $send(msg) tmp
Pass your own callback function
$on_message(function(event) {
tmpif (event$data == "pong") {
cat("Bingo!!")
}
})$send("ping") tmp
Close the connection.
$close() tmp
A websocketAPIprivate
class based on websocket package is
used here.
Create new websocketAPIprivate
object and initiate the
connection to the server.
<- websocketAPIprivate$new(api_key, secret_key, passphrase)
tmp $connect()
tmpSys.sleep(1) # Waiting for connection success
$login() tmp
Subscribe account information.
<- list(
msg op = "subscribe",
args = list(
list(channel = "account", ccy = "USDT")
)
)<- jsonlite::toJSON(msg, auto_unbox = TRUE, pretty = TRUE)
msg $send(msg) tmp
Pass your own callback function.
$on_message(function(event) {
tmpif (event$data == "pong") {
cat("Bingo!!")
}
})$send("ping") tmp
Close the connection.
$close() tmp