Skip to content

Commit d6a3c89

Browse files
Merge pull request #7 from dereckdemezquita/dev
Documentation and re-organisation
2 parents e6a117e + d1615e9 commit d6a3c89

37 files changed

+746
-366
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*html
33
.DS_Store
44
deprecated/
5-
dev/
5+
66
# sensitive files and tokens
77
*.sensitive.*
88

NAMESPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Generated by roxygen2: do not edit by hand
22

3-
export(.get_orders_all)
43
export(cancel_all_orders)
54
export(cancel_order)
65
export(get_account_balances)
@@ -9,6 +8,8 @@ export(get_deposit_address)
98
export(get_kucoin_time)
109
export(get_market_data)
1110
export(get_market_metadata)
11+
export(get_market_metadata.deprecated)
12+
export(get_orders_all)
1213
export(get_orders_by_id)
1314
export(kucoin_time_to_datetime)
1415
export(submit_limit_order)

R/cancel_all_orders.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#' @title Cancel all order(s) for a symbol
2+
#'
3+
#' @description
4+
#'
5+
#' TODO: in development.
26
#'
37
#' @param symbols A `character` vector of one or more symbols; format "BTC/USDT" (optional - default `NULL`).
48
#' @param tradeType A `character` vector of one either "TRADE" or "MARGIN_ISOLATED_TRADE" (optional - default `NULL`).
@@ -16,16 +20,15 @@
1620
#' # ---------------
1721
#' For more information see documentation: [KuCoin - cancel-all-orders](https://docs.kucoin.com/#cancel-all-orders)
1822
#'
23+
#' TODO: if no symbols provided cancel all
24+
#'
1925
#' @examples
2026
#'
2127
#' \dontrun{
2228
#'
2329
#' # to run this example, make sure
2430
#' # you already setup the API key
2531
#' # in a proper .Renviron file
26-
#'
27-
#' # import library
28-
#' library("kucoin")
2932
#'
3033
#' symbol <- "ETH/USDT"
3134
#'
@@ -95,7 +98,10 @@ cancel_all_orders <- function(symbols = NULL, tradeType = NULL, delay = 0, retri
9598

9699
# prepare query params
97100
query_params <- list(
98-
symbol = prep_symbols(symbol),
101+
symbol = (\() {
102+
if (!is.null(symbol)) prep_symbols(symbol)
103+
return(NULL)
104+
})(),
99105
tradeType = tradeType
100106
)
101107

R/cancel_order.R

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
#' # to run this example, make sure
2727
#' # you already setup the API key
2828
#' # in a proper .Renviron file
29-
#'
30-
#' # import library
31-
#' library("kucoin")
3229
#'
3330
#' # set a limit order
3431
#' order_id1 <- kucoin::post_kucoin_limit_order(

R/deprecated-get_market_metadata.R

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#' @title Get all market symbols' metadata --deprecated--
2+
#'
3+
#' @param retries A `numeric` value to specify the number of retries in case of failure (optional - default `3`).
4+
#'
5+
#' @return A `data.table` with metadata
6+
#'
7+
#' @details
8+
#'
9+
#' TODO: this function needs to be updated to v2 of the API.
10+
#'
11+
#' # ---------------
12+
#' For more information see documentation: [KuCoin - get-symbols-list-deprecated](https://docs.kucoin.com/#get-symbols-list-deprecated)
13+
#'
14+
#' @examples
15+
#' # import library
16+
#' library("kucoin")
17+
#'
18+
#' # get all symbols' most recent metadata
19+
#' metadata <- kucoin::get_market_metadata()
20+
#'
21+
#' # quick check
22+
#' metadata
23+
#'
24+
#' @export
25+
26+
get_market_metadata.deprecated <- function(retries = 3) {
27+
# get server response
28+
response <- httr::RETRY(
29+
verb = "GET",
30+
url = get_base_url(),
31+
path = get_paths("symbols-deprecated"),
32+
times = retries
33+
)
34+
35+
# analyze response
36+
response <- analyze_response(response)
37+
38+
# parse json result
39+
parsed <- jsonlite::fromJSON(httr::content(response, "text", encoding = "UTF-8"))
40+
41+
# tidy the parsed data
42+
results <- data.table::data.table(parsed$data, check.names = FALSE)
43+
44+
# seems that the only thing that changes in colnames is they are made to snake_case
45+
# https://github.com/dereckdemezquita/kucoin/issues/1
46+
# Error in setnames(x, value) :
47+
# Can't assign 14 names to a 17 column data.table
48+
# colnames(results) <- c(
49+
# "symbol", "quote_max_size", "enable_trading", "price_increment",
50+
# "fee_currency", "base_max_size", "base_currency", "quote_currency",
51+
# "market", "quote_increment", "base_min_size", "quote_min_size",
52+
# "name", "base_increment"
53+
# )
54+
55+
colnames(results) <- to_snake_case(colnames(results))
56+
57+
# since we are not sure to get the same data from the api forever
58+
# I will programmatically modify what we receive rather than set colnames manually
59+
60+
# will no longer re-order the table
61+
# common_cols <- c(
62+
# "symbol", "name", "enable_trading",
63+
# "base_currency", "quote_currency",
64+
# "market", # TOOD: added market column; might remove later
65+
# "base_min_size", "quote_min_size",
66+
# "base_max_size", "quote_max_size",
67+
# "base_increment", "quote_increment",
68+
# "price_increment", "fee_currency"
69+
# )
70+
# data.table::setcolorder(results, c(common_cols, setdiff(colnames(results), common_cols)))
71+
72+
numeric_cols <- c(
73+
"base_min_size", "quote_min_size", "base_max_size",
74+
"quote_max_size", "base_increment", "quote_increment",
75+
"price_increment", "price_limit_rate", "min_funds"
76+
)
77+
78+
# sandbox api does not have "min_funds" column
79+
# filter out columns that are not in the data; warn user that they are not in the data
80+
numeric_missing_cols <- setdiff(numeric_cols, colnames(results))
81+
82+
if (length(numeric_missing_cols) > 0) {
83+
rlang::warn(stringr::str_interp("The following columns are not in the data: ${collapse(numeric_missing_cols)}"))
84+
85+
# keep only columns that are in the data
86+
numeric_cols <- numeric_cols[!numeric_cols %in% numeric_missing_cols]
87+
}
88+
89+
logical_cols <- c("is_margin_enabled", "enable_trading")
90+
91+
logical_missing_cols <- setdiff(logical_cols, colnames(results))
92+
93+
if (length(logical_missing_cols) > 0) {
94+
rlang::warn(stringr::str_interp("The following columns are not in the data: ${collapse(logical_missing_cols)}"))
95+
96+
# keep only columns that are in the data
97+
logical_cols <- logical_cols[!logical_cols %in% logical_missing_cols]
98+
}
99+
100+
## -----------------
101+
results[, (numeric_cols) := lapply(.SD, as.numeric), .SDcols = numeric_cols]
102+
# results[, colnames(results)[6:12] := lapply(.SD, as.numeric), .SDcols = 6:12]
103+
104+
results[, c("symbol", "name") := lapply(.SD, prep_symbols, revert = TRUE), .SDcols = c("symbol", "name")]
105+
# results[, colnames(results)[1:2] := lapply(.SD, prep_symbols, revert = TRUE), .SDcols = 1:2]
106+
107+
results[, (logical_cols) := lapply(.SD, as.logical), .SDcols = logical_cols]
108+
109+
# data.table::setorder(results, base_currency, quote_currency)
110+
data.table::setorder(results, symbol, fee_currency)
111+
112+
# return the result
113+
return(results[])
114+
}

R/get_account_balances.R

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,14 @@
1313
#' # you already setup the API key
1414
#' # in a proper .Renviron file
1515
#'
16-
#' # import library
17-
#' library("kucoin")
18-
#'
1916
#' # get user's balance details
20-
#' balances <- kucoin::get_account_balances()
21-
#'
22-
#' # quick check
23-
#' balances
17+
#' kucoin::get_account_balances()
2418
#'
2519
#' # get user's balance details for BTC only
26-
#' balances <- kucoin::get_account_balances(
27-
#' currency = "BTC"
28-
#' )
29-
#'
30-
#' # quick check
31-
#' balances
20+
#' kucoin::get_account_balances(currency = "BTC")
3221
#'
3322
#' # get user's balance details for trade account only
34-
#' balances <- kucoin::get_account_balances(
35-
#' type = "trade"
36-
#' )
37-
#'
38-
#' # quick check
39-
#' balances
23+
#' kucoin::get_account_balances(type = "trade")
4024
#'
4125
#' }
4226
#'

R/get_currency_details.R

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@
2020
#'
2121
#' For more information see documentation: [KuCoin - get-currency-detail](https://docs.kucoin.com/#get-currency-detail-recommend)
2222
#'
23+
#' Using v2 of the api.
24+
#'
2325
#' @examples
24-
#' # import library
25-
#' library("kucoin")
2626
#'
2727
#' # get a currencies' details
28-
#' currencies_details <- kucoin::get_currency_details(c("BTC", "XMR"))
29-
#'
30-
#' currencies_details
28+
#' kucoin::get_currency_details(c("BTC", "XMR"))
3129
#'
3230
#' @export
3331

R/get_deposit_address.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
#' # you already setup the API key
2424
#' # in a proper .Renviron file
2525
#'
26-
#' # import library
27-
#' library("kucoin")
26+
#' # check market metadata
27+
#' kucoin::get_currency_details("BTC")
2828
#'
2929
#' # get a deposit address for a currency
3030
#' deposit_address <- kucoin::get_deposit_address("BTC", "btc")

R/get_market_data.R

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,22 @@
3232
#'
3333
#' @examples
3434
#'
35-
#' # import library
36-
#' library("kucoin")
37-
#'
38-
#' # get one pair of symbol prices
39-
#' prices <- get_market_data(
40-
#' symbols = "KCS/USDT",
41-
#' from = "2022-11-05 00:00:00",
42-
#' to = "2022-11-06 00:00:00",
43-
#' frequency = "1 hour"
35+
#' # get one symbol
36+
#' kucoin::get_market_data(
37+
#' symbols = "BTC/USDT",
38+
#' from = "2022-11-05 00:00:00",
39+
#' to = "2022-11-06 00:00:00",
40+
#' frequency = "1 hour"
4441
#' )
45-
#'
46-
#' # quick check
47-
#' prices
48-
#'
49-
#' # get multiple pair of symbols prices
50-
#' prices <- get_market_data(
51-
#' symbols = c("KCS/USDT", "BTC/USDT", "KCS/BTC"),
52-
#' from = "2022-11-05 00:00:00",
53-
#' to = "2022-11-06 00:00:00",
54-
#' frequency = "1 hour"
42+
#'
43+
#' # get multiple symbols
44+
#' kucoin::get_market_data(
45+
#' symbols = c("BTC/USDT", "XMR/BTC", "KCS/USDT"),
46+
#' from = "2022-11-05 00:00:00",
47+
#' to = "2022-11-06 00:00:00",
48+
#' frequency = "1 hour"
5549
#' )
5650
#'
57-
#' # quick check
58-
#' prices
59-
#'
6051
#' @export
6152

6253
get_market_data <- function(symbols = NULL, from = NULL, to = NULL, frequency = NULL, delay = 0, retries = 3) {

R/get_market_metadata.R

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,13 @@
66
#'
77
#' @details
88
#'
9-
#' TODO: this function needs to be updated to v2 of the API.
10-
#'
119
#' # ---------------
1210
#' For more information see documentation: [KuCoin - get-symbols-list-deprecated](https://docs.kucoin.com/#get-symbols-list-deprecated)
1311
#'
1412
#' @examples
15-
#' # import library
16-
#' library("kucoin")
1713
#'
1814
#' # get all symbols' most recent metadata
19-
#' metadata <- kucoin::get_market_metadata()
20-
#'
21-
#' # quick check
22-
#' metadata
15+
#' kucoin::get_market_metadata()
2316
#'
2417
#' @export
2518

@@ -38,37 +31,10 @@ get_market_metadata <- function(retries = 3) {
3831
# parse json result
3932
parsed <- jsonlite::fromJSON(httr::content(response, "text", encoding = "UTF-8"))
4033

41-
# tidy the parsed data
42-
results <- data.table::data.table(parsed$data, check.names = FALSE)
43-
44-
# seems that the only thing that changes in colnames is they are made to snake_case
45-
# https://github.com/dereckdemezquita/kucoin/issues/1
46-
# Error in setnames(x, value) :
47-
# Can't assign 14 names to a 17 column data.table
48-
# colnames(results) <- c(
49-
# "symbol", "quote_max_size", "enable_trading", "price_increment",
50-
# "fee_currency", "base_max_size", "base_currency", "quote_currency",
51-
# "market", "quote_increment", "base_min_size", "quote_min_size",
52-
# "name", "base_increment"
53-
# )
34+
results <- data.table::as.data.table(parsed$data, check.names = FALSE)
5435

5536
colnames(results) <- to_snake_case(colnames(results))
5637

57-
# since we are not sure to get the same data from the api forever
58-
# I will programmatically modify what we receive rather than set colnames manually
59-
60-
# will no longer re-order the table
61-
# common_cols <- c(
62-
# "symbol", "name", "enable_trading",
63-
# "base_currency", "quote_currency",
64-
# "market", # TOOD: added market column; might remove later
65-
# "base_min_size", "quote_min_size",
66-
# "base_max_size", "quote_max_size",
67-
# "base_increment", "quote_increment",
68-
# "price_increment", "fee_currency"
69-
# )
70-
# data.table::setcolorder(results, c(common_cols, setdiff(colnames(results), common_cols)))
71-
7238
numeric_cols <- c(
7339
"base_min_size", "quote_min_size", "base_max_size",
7440
"quote_max_size", "base_increment", "quote_increment",
@@ -99,14 +65,11 @@ get_market_metadata <- function(retries = 3) {
9965

10066
## -----------------
10167
results[, (numeric_cols) := lapply(.SD, as.numeric), .SDcols = numeric_cols]
102-
# results[, colnames(results)[6:12] := lapply(.SD, as.numeric), .SDcols = 6:12]
10368

10469
results[, c("symbol", "name") := lapply(.SD, prep_symbols, revert = TRUE), .SDcols = c("symbol", "name")]
105-
# results[, colnames(results)[1:2] := lapply(.SD, prep_symbols, revert = TRUE), .SDcols = 1:2]
10670

10771
results[, (logical_cols) := lapply(.SD, as.logical), .SDcols = logical_cols]
10872

109-
# data.table::setorder(results, base_currency, quote_currency)
11073
data.table::setorder(results, symbol, fee_currency)
11174

11275
# return the result

0 commit comments

Comments
 (0)