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: | 2025-02-02 04:10:27 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.
url
Okx REST API url, which is https://www.okx.com.
api_key
Okx API key.
secret_key
Okx API secret key.
passphrase
Okx API passphrase.
simulate
Whether to use demo trading service.
new()
Create a new REST API object.
restAPI$new(api_key, secret_key, passphrase, simulate = FALSE)
api_key
Okx API key.
secret_key
Okx API secret key.
passphrase
Okx API passphrase.
simulate
Whether 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"), ...)
api
Request api e.g. /api/v5/account/positions-history.
method
Request method, GET
or POST
.
...
Other request parameters.
get_body()
Get request body.
restAPI$get_body(method = c("GET", "POST"), ...)
method
Request method, GET
or POST
.
...
Other request parameters.
get_message()
Get the signing messages.
restAPI$get_message(timestamp, request_path, body, method = c("GET", "POST"))
timestamp
Retrieve through method get_timestamp
.
request_path
Retrieve through method get_request_path
.
body
Retrieve through method get_body
.
method
Request method, GET
or POST
.
get_signature()
Get the signature.
restAPI$get_signature(msg, secret_key = self$secret_key)
msg
Retrieve through method get_message
.
secret_key
Okx API secret key.
get_header()
Get request headers.
restAPI$get_header(timestamp, msg)
timestamp
Retrieve through method get_timestamp
.
msg
Retrieve through method get_message
.
get_result()
Retrieve data from api.
restAPI$get_result(api, method = c("GET", "POST"), process, ...)
api
Request api e.g. /api/v5/account/positions-history.
method
Request method, GET
or POST
.
process
A 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)
deep
Whether 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")
ccy
Single currency or a vector composed of multiple currencies. (no more than 20).
process
A function to process the data received from the API. Default to identity
.
positions()
See Get positions for more information.
restAPIaccount$positions(process = "identity", ...)
process
A 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", ...)
process
A 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)
deep
Whether 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", ...)
instId
Instrument ID, e.g. CFX-USDT
process
A 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", ...)
instId
Instrument ID, e.g. BTC-USD-190927-5000-C.
process
A 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", ...)
instId
Instrument ID, e.g. BTC-USD-190927-5000-C.
process
A 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)
deep
Whether 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", ... )
instId
Instrument ID, e.g. BTC-USD-190927-5000-C.
tdMode
Trade mode. Margin mode: cross
or isolated.
Non-Margin mode: cash
.
side
Order side, buy
or sell
.
sz
Quantity to buy or sell.
ordType
Order 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).
process
A 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", ...)
instId
Instrument ID, e.g. BTC-USD-190927.
ordId
Order ID, Either ordId
or clOrdId
is required. If both are passed, ordId
will be used.
clOrdId
Client Order ID as assigned by the client.
process
A 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", ...)
process
A 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", ...)
process
A 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", ...)
process
A 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)
deep
Whether to make a deep clone.
Private channel of WebSocket API for Okx exchange v5 API. See Private Channel for more information.
channel
Private WebSocket url.
api_key
Okx API key.
secret_key
Okx API secret key.
passphrase
Okx API passphrase.
simulate
Whether to use demo trading service.
ws
A 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_key
Okx API key.
secret_key
Okx API secret key.
passphrase
Okx API passphrase.
simulate
Whether to use demo trading service.
get_timestamp()
Get UTC timestamp.
websocketAPIprivate$get_timestamp()
get_message()
Get the signing messages.
websocketAPIprivate$get_message(timestamp)
timestamp
Retrieve through method get_timestamp
.
get_signature()
Get the signature.
websocketAPIprivate$get_signature(secret_key, msg)
secret_key
Okx API secret key.
msg
Retrieve 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)
func
A 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)
func
A 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)
func
A 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)
func
A Callback function.
send()
Send a message to the server.
websocketAPIprivate$send(msg)
msg
Messages.
close()
Close the connection.
websocketAPIprivate$close()
clone()
The objects of this class are cloneable with this method.
websocketAPIprivate$clone(deep = FALSE)
deep
Whether 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.
channel
Public WebSocket url.
simulate
Whether to use demo trading service.
ws
A websocket::WebSocket object to establish a connection to the server.
new()
Create a new websocketAPIpublic object.
websocketAPIpublic$new(simulate = FALSE)
simulate
Whether 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)
func
A 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)
func
A 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)
func
A 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)
func
A Callback function.
send()
Send a message to the server.
websocketAPIpublic$send(msg)
msg
Messages.
close()
Close the connection.
websocketAPIpublic$close()
clone()
The objects of this class are cloneable with this method.
websocketAPIpublic$clone(deep = FALSE)
deep
Whether 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)