| Title: | An Unofficial Wrapper for 'okx exchange v5' API |
| Version: | 0.1.1 |
| Description: | An unofficial wrapper for 'okx exchange v5' API https://www.okx.com/docs-v5/en/, including 'REST' API and 'WebSocket' API. |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.2.3 |
| Imports: | R6, data.table, httr, base64enc, jsonlite, websocket, digest |
| NeedsCompilation: | no |
| Packaged: | 2023-04-21 12:04:16 UTC; fangyongchao |
| Author: | Yongchao Fang [aut, cre, cph] |
| Maintainer: | Yongchao Fang <yongchao.fang@outlook.com> |
| Repository: | CRAN |
| Date/Publication: | 2023-04-22 07:10:02 UTC |
Wrapper for some frequently used APIs to get data easily
Description
The main purpose is to handle APIs that have limitations on the number of results returned per single request.
See Also
get_positions_history
get_history_candles
Retrieve the candlestick charts
Description
Wrapper for API Get candlesticks and Get candlesticks history.
Usage
get_history_candles(
api_key,
secret_key,
passphrase,
bar = c("1m", "3m", "5m", "15m", "30m", "1H", "4H", "6H", "12H", "1D", "2D", "3D"),
count,
instId,
...
)
Arguments
api_key |
Okx API key. |
secret_key |
Okx API secret key. |
passphrase |
Okx API passphrase. |
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. |
count |
Number of Bars. |
instId |
Instrument ID, e.g. BTC-USDT-SWAP. |
... |
Other request parameters to be passed, See Get candlesticks history for more information. |
Value
Candlestick charts data
Examples
## Not run:
candles <- get_history_candles(
api_key, secret_key, passphrase, bar = "1m",
count = 24*60, instId = "CFX-USDT-SWAP"
)
## End(Not run)
Retrieve the position data
Description
Wrapper for API Get positions history.
Usage
get_positions_history(
api_key,
secret_key,
passphrase,
count = 90,
period = 10,
...
)
Arguments
api_key |
Okx API key. |
secret_key |
Okx API secret key. |
passphrase |
Okx API passphrase. |
count |
Retrieve position data for a specified number of past days, with a maximum of 90(days) |
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. |
Value
Position data
Examples
## Not run:
positions <- get_positions_history(
api_key, secret_key, passphrase, count = 90, period = 10,
instType = "SWAP", mgnMode = "isolated"
)
## End(Not run)
restAPI Class
Description
Base class for Okx exchange v5 API.
Details
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.
Public fields
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.
Methods
Public methods
Method new()
Create a new REST API object.
Usage
restAPI$new(api_key, secret_key, passphrase, simulate = FALSE)
Arguments
api_keyOkx API key.
secret_keyOkx API secret key.
passphraseOkx API passphrase.
simulateWhether to use demo trading service.
Method get_timestamp()
Get UTC timestamp.
Usage
restAPI$get_timestamp()
Method get_request_path()
Get request path.
Usage
restAPI$get_request_path(api, method = c("GET", "POST"), ...)Arguments
apiRequest api e.g. /api/v5/account/positions-history.
methodRequest method,
GETorPOST....Other request parameters.
Method get_body()
Get request body.
Usage
restAPI$get_body(method = c("GET", "POST"), ...)Arguments
methodRequest method,
GETorPOST....Other request parameters.
Method get_message()
Get the signing messages.
Usage
restAPI$get_message(timestamp, request_path, body, method = c("GET", "POST"))Arguments
timestampRetrieve through method
get_timestamp.request_pathRetrieve through method
get_request_path.bodyRetrieve through method
get_body.methodRequest method,
GETorPOST.
Method get_signature()
Get the signature.
Usage
restAPI$get_signature(msg, secret_key = self$secret_key)
Arguments
msgRetrieve through method
get_message.secret_keyOkx API secret key.
Method get_header()
Get request headers.
Usage
restAPI$get_header(timestamp, msg)
Arguments
timestampRetrieve through method
get_timestamp.msgRetrieve through method
get_message.
Method get_result()
Retrieve data from api.
Usage
restAPI$get_result(api, method = c("GET", "POST"), process, ...)Arguments
apiRequest api e.g. /api/v5/account/positions-history.
methodRequest method,
GETorPOST.processA function to process the data received from the API.
...Other request parameters.
Method clone()
The objects of this class are cloneable with this method.
Usage
restAPI$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
See Also
restAPItrade
restAPIaccount
restAPImarket
Examples
## 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)
restAPIaccount Class
Description
Wrapper for REST API ACCOUNT.
Super class
okxAPI::restAPI -> restAPIaccount
Methods
Public methods
Inherited methods
Method balance()
See Get balance for more information.
Usage
restAPIaccount$balance(ccy, process = "identity")
Arguments
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.
Method positions()
See Get positions for more information.
Usage
restAPIaccount$positions(process = "identity", ...)
Arguments
processA function to process the data received from the API. Default to
identity....Other request parameters.
Method positions_history()
See Get positions history for more information.
Usage
restAPIaccount$positions_history(process = "identity", ...)
Arguments
processA function to process the data received from the API. Default to
identity....Other request parameters.
Method clone()
The objects of this class are cloneable with this method.
Usage
restAPIaccount$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
restAPImarket Class
Description
Wrapper for REST API MARKET.
Super class
okxAPI::restAPI -> restAPImarket
Methods
Public methods
Inherited methods
Method candles()
See Get candlesticks for more information.
Usage
restAPImarket$candles(instId, process = "identity", ...)
Arguments
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.
Method history_candles()
See Get candlesticks history for more information.
Usage
restAPImarket$history_candles(instId, process = "identity", ...)
Arguments
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.
Method clone()
The objects of this class are cloneable with this method.
Usage
restAPImarket$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
restAPItrade Class
Description
Wrapper for REST API TRADE.
Super class
okxAPI::restAPI -> restAPItrade
Methods
Public methods
Inherited methods
Method order()
See Place order for more information.
Usage
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",
...
)Arguments
instIdInstrument ID, e.g. BTC-USD-190927-5000-C.
tdModeTrade mode. Margin mode:
crossorisolated.Non-Margin mode:cash.sideOrder side,
buyorsell.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.
Method cancel_order()
See Cancel order for more information.
Usage
restAPItrade$cancel_order(instId, ordId, clOrdId, process = "identity", ...)
Arguments
instIdInstrument ID, e.g. BTC-USD-190927.
ordIdOrder ID, Either
ordIdorclOrdIdis required. If both are passed,ordIdwill 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.
Method clone()
The objects of this class are cloneable with this method.
Usage
restAPItrade$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
websocketAPIprivate Class
Description
Private channel of WebSocket API for Okx exchange v5 API. See Private Channel for more information.
Public fields
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.
Methods
Public methods
Method new()
Craeate a new websocketAPIprivate object.
Usage
websocketAPIprivate$new(api_key, secret_key, passphrase, simulate = FALSE)
Arguments
api_keyOkx API key.
secret_keyOkx API secret key.
passphraseOkx API passphrase.
simulateWhether to use demo trading service.
Method get_timestamp()
Get UTC timestamp.
Usage
websocketAPIprivate$get_timestamp()
Method get_message()
Get the signing messages.
Usage
websocketAPIprivate$get_message(timestamp)
Arguments
timestampRetrieve through method
get_timestamp.
Method get_signature()
Get the signature.
Usage
websocketAPIprivate$get_signature(secret_key, msg)
Arguments
secret_keyOkx API secret key.
msgRetrieve through method
get_message.
Method connect()
Initiate the connection to the server.
Usage
websocketAPIprivate$connect()
Method login()
Log in.
Usage
websocketAPIprivate$login()
Method on_open()
Called when the connection is established.
Usage
websocketAPIprivate$on_open(func)
Arguments
funcA Callback function.
Method 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.
Usage
websocketAPIprivate$on_close(func)
Arguments
funcA Callback function.
Method on_message()
Called each time a message is received from the server. The event will have a 'data' element, which is the message content.
Usage
websocketAPIprivate$on_message(func)
Arguments
funcA Callback function.
Method 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.
Usage
websocketAPIprivate$on_error(func)
Arguments
funcA Callback function.
Method send()
Send a message to the server.
Usage
websocketAPIprivate$send(msg)
Arguments
msgMessages.
Method close()
Close the connection.
Usage
websocketAPIprivate$close()
Method clone()
The objects of this class are cloneable with this method.
Usage
websocketAPIprivate$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
## 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)
websocketAPIpublic Class
Description
Public channel of WebSocket API for Okx exchange v5 API. See Public Channel for more information.
Public fields
channelPublic WebSocket url.
simulateWhether to use demo trading service.
wsA websocket::WebSocket object to establish a connection to the server.
Methods
Public methods
Method new()
Create a new websocketAPIpublic object.
Usage
websocketAPIpublic$new(simulate = FALSE)
Arguments
simulateWhether to use demo trading service.
Method connect()
Initiate the connection to the server.
Usage
websocketAPIpublic$connect()
Method on_open()
Called when the connection is established.
Usage
websocketAPIpublic$on_open(func)
Arguments
funcA Callback function.
Method 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.
Usage
websocketAPIpublic$on_close(func)
Arguments
funcA Callback function.
Method on_message()
Called each time a message is received from the server. The event will have a 'data' element, which is the message content.
Usage
websocketAPIpublic$on_message(func)
Arguments
funcA Callback function.
Method 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.
Usage
websocketAPIpublic$on_error(func)
Arguments
funcA Callback function.
Method send()
Send a message to the server.
Usage
websocketAPIpublic$send(msg)
Arguments
msgMessages.
Method close()
Close the connection.
Usage
websocketAPIpublic$close()
Method clone()
The objects of this class are cloneable with this method.
Usage
websocketAPIpublic$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
## 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)