Skip to content

Commit

Permalink
version 0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
fanggong authored and cran-robot committed Apr 22, 2023
0 parents commit 0ea108e
Show file tree
Hide file tree
Showing 19 changed files with 2,066 additions and 0 deletions.
16 changes: 16 additions & 0 deletions DESCRIPTION
@@ -0,0 +1,16 @@
Package: okxAPI
Title: An Unofficial Wrapper for 'okx exchange v5' API
Version: 0.1.1
Authors@R:
person(family = "Fang", given = "Yongchao", email = "yongchao.fang@outlook.com", role = c("aut", "cre", "cph"))
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
2 changes: 2 additions & 0 deletions LICENSE
@@ -0,0 +1,2 @@
YEAR: 2023
COPYRIGHT HOLDER: Yongchao Fang
18 changes: 18 additions & 0 deletions MD5
@@ -0,0 +1,18 @@
0df37465f88340fdcd308abbca5a1138 *DESCRIPTION
0d3cb8d6ab2e2ec2955fd3e7e0674652 *LICENSE
c43b22885efa36e5be67cb4e5beb4b99 *NAMESPACE
a42ec1c4244360d3eec4031cb5e03648 *R/funcs.R
70f6b0fb294838cf04f5b782e6eb0559 *R/restAPI.R
2ac6566ea0e1e3230d60124baa76eae8 *R/utils.R
580132d53dc4d1455d2dca1e5fb724f5 *R/websockerAPIprivate.R
13873099c8fc3e9b2466cdbd66237ae6 *R/websocketAPIpublic.R
703ddbf7088aff8797ce4d6315b172ca *README.md
eae3f11c522c7f9c34fcdeb24e231041 *man/get_functions.Rd
45700e5426fda3522acedec9ff6636b0 *man/get_history_candles.Rd
5c9e06661a94a771518e944398790373 *man/get_positions_history.Rd
07f49942d8cb0a6400ae4a680b0007e0 *man/restAPI.Rd
96d6b96cc4d461b2cbe4908b0c26fe67 *man/restAPIaccount.Rd
44dfd99788fe451e16578f879fe46d13 *man/restAPImarket.Rd
3b0c114f825b8f93f4dabafd04bc162b *man/restAPItrade.Rd
6883bf6c50b48bf01093925f03111910 *man/websocketAPIprivate.Rd
6c3bdcc93739a40a916d4761aa47471f *man/websocketAPIpublic.Rd
17 changes: 17 additions & 0 deletions NAMESPACE
@@ -0,0 +1,17 @@
# Generated by roxygen2: do not edit by hand

export(get_history_candles)
export(get_positions_history)
export(restAPI)
export(restAPIaccount)
export(restAPImarket)
export(restAPItrade)
export(websocketAPIprivate)
export(websocketAPIpublic)
import(R6)
import(base64enc)
import(data.table)
import(digest)
import(httr)
import(jsonlite)
import(websocket)
133 changes: 133 additions & 0 deletions R/funcs.R
@@ -0,0 +1,133 @@
#' @name get_functions
#'
#' @title 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.
#' @seealso
#' \code{\link{get_positions_history}}
#' \code{\link{get_history_candles}}
NULL


#' @title Retrieve the position data
#'
#' @description Wrapper for API [Get positions history](https://www.okx.com/docs-v5/en/#rest-api-account-get-positions-history).
#'
#' @param api_key Okx API key.
#' @param secret_key Okx API secret key.
#' @param passphrase Okx API passphrase.
#' @param count Retrieve position data for a specified number of past days, with a maximum of 90(days)
#' @param period Due to the 'Number of results per request' limitation of the API,
#' the \code{period} parameter must be specified to ensure that the number of position data entries within each period does not exceed 100.
#' @param ... Other request parameters to be passed, See
#' [Get positions history](https://www.okx.com/docs-v5/en/#rest-api-account-get-positions-history) for more information.
#'
#' @return Position data
#'
#' @examples
#' \dontrun{
#' positions <- get_positions_history(
#' api_key, secret_key, passphrase, count = 90, period = 10,
#' instType = "SWAP", mgnMode = "isolated"
#' )
#' }
#'
#' @import data.table
#' @export
get_positions_history <- function(
api_key, secret_key, passphrase, count = 90, period = 10, ...
) {
account <- restAPIaccount$new(api_key, secret_key, passphrase)
now <- time2ts(Sys.time())
start <- now - count*24*60*60
end <- start + period*24*60*60 - 0.001
dat <- list()
while (start < now) {
result <- account$positions_history(
before = as.character(1000*start - 1), after = as.character(1000*end + 1), ...
)
if (result$code == "0") {
dat <- c(dat, result$data)
message("From ", ts2time(start), " to ", ts2time(end), " complete")
start <- end + 0.001
end <- start + period*24*60*60 - 0.001
Sys.sleep(15)
}
}
dat <- lapply(dat, data.table::as.data.table)
dat <- data.table::rbindlist(dat)
to_time <- c("cTime", "uTime")
dat[, (to_time) := lapply(.SD, ts2time), .SDcols = to_time]
to_numeric <- c("closeAvgPx", "closeTotalPos", "lever", "openAvgPx", "openMaxPos", "pnl", "pnlRatio")
dat[, (to_numeric) := lapply(.SD, as.numeric), .SDcols = to_numeric]
dat
}

#' @title Retrieve the candlestick charts
#'
#' @description Wrapper for API [Get candlesticks](https://www.okx.com/docs-v5/en/#rest-api-market-data-get-candlesticks)
#' and [Get candlesticks history](https://www.okx.com/docs-v5/en/#rest-api-market-data-get-candlesticks-history).
#'
#' @param api_key Okx API key.
#' @param secret_key Okx API secret key.
#' @param passphrase Okx API passphrase.
#' @param 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.
#' @param count Number of Bars.
#' @param instId Instrument ID, e.g. BTC-USDT-SWAP.
#' @param ... Other request parameters to be passed, See [Get candlesticks history](https://www.okx.com/docs-v5/en/#rest-api-market-data-get-candlesticks-history) for more information.
#'
#' @return Candlestick charts data
#'
#' @examples
#' \dontrun{
#' candles <- get_history_candles(
#' api_key, secret_key, passphrase, bar = "1m",
#' count = 24*60, instId = "CFX-USDT-SWAP"
#' )
#' }
#'
#' @import data.table
#' @export
get_history_candles <- function(
api_key, secret_key, passphrase,
bar = c("1m", "3m", "5m", "15m", "30m", "1H", "4H", "6H", "12H", "1D", "2D", "3D"),
count, instId, ...
) {
bar <- match.arg(bar)
period <- str2period(bar)

market <- restAPImarket$new(api_key, secret_key, passphrase)

now <- time2ts(Sys.time())
end <- now
start <- end - ifelse(count >= 100, 100, count) * period + 0.001
dat <- list()
for (i in 1:ceiling(count / 100)) {
if (i == 1) {
result <- market$candles(
instId = instId, before = as.character(1000*start - 1),
after = as.character(1000*end + 1), bar = bar, ...
)
} else {
result <- market$history_candles(
instId = instId, before = as.character(1000*start - 1),
after = as.character(1000*end + 1), bar = bar, ...
)
}
if (result$code == "0") {
dat <- c(dat, result$data)
message("From ", ts2time(start), " to ", ts2time(end), " complete")
count <- count - 100
end <- start - 0.001
start <- end - ifelse(count >= 100, 100, count) * period + 0.001
# Sys.sleep(1/20)
}
}
col_names <- c("ts", "open", "high", "low", "close", "vol", "volCcy", "volCcyQuote", "confirm")
dat <- lapply(dat, as.data.frame, col.names = c(col_names))
dat <- data.table::rbindlist(dat)
dat$ts <- ts2time(dat$ts)
to_numeric <- c("open", "high", "low", "close", "vol", "volCcy", "volCcyQuote")
dat[, (to_numeric) := lapply(.SD, as.numeric), .SDcols = to_numeric]
dat
}

0 comments on commit 0ea108e

Please sign in to comment.