forked from serkor1/cryptoQuotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
493 lines (409 loc) · 13.8 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
---
output: github_document
always_allow_html: true
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
# set options
Sys.setenv(OPENSSL_CONF="/dev/null")
knitr::opts_chunk$set(
collapse = FALSE,
comment = "#>",
fig.path = "man/figures/README-",
message = FALSE,
warning = FALSE
)
```
# cryptoQuotes: Open access to cryptocurrency market data <a href="https://serkor1.github.io/cryptoQuotes/"><img src="man/figures/logo.png" align="right" height="154" alt="cryptocurrency in R"/></a>
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/cryptoQuotes)](https://CRAN.R-project.org/package=cryptoQuotes)
[![CRAN RStudio mirror downloads](https://cranlogs.r-pkg.org/badges/last-month/cryptoQuotes?color=blue)](https://r-pkg.org/pkg/cryptoQuotes)
[![R-CMD-check](https://github.com/serkor1/cryptoQuotes/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/serkor1/cryptoQuotes/actions/workflows/R-CMD-check.yaml)
[![codecov](https://codecov.io/gh/serkor1/cryptoQuotes/graph/badge.svg?token=D7NF1BPVL5)](https://app.codecov.io/gh/serkor1/cryptoQuotes)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
![GitHub last commit (branch)](https://img.shields.io/github/last-commit/serkor1/cryptoQuotes/development)
<!-- badges: end -->
## :information_source: About
The `cryptoQuotes`-package is a high-level API client for accessing public market data endpoints on major cryptocurrency exchanges. It supports open, high, low, close and volume (OHLC-V) data and a variety of sentiment indicators; the market data is high quality and can be retrieved in intervals ranging from *seconds* to *months*. All the market data is accessed and processed without relying on crawlers, or API keys, ensuring an open, and reliable, access for researchers, traders and students alike. There are currently `r length(invisible(cryptoQuotes::available_exchanges(type = "ohlc")))` supported cryptocurrency exchanges,
<div align="center">
```{r, echo=FALSE}
## available exchanges
invisible(
all_exchanges <- cryptoQuotes::available_exchanges()
)
# Convert the character vector to a single-row data frame
exchanges_df <- data.frame(t(all_exchanges))
# Set column names to be the exchange names
colnames(exchanges_df) <- all_exchanges
# Create a horizontal table
kableExtra::kable_styling(
knitr::kable(
x = exchanges_df,
digits = 2,
col.names = NULL,
caption = 'Supported exchanges',
align = 'c',
table.attr = "style='width:100%;'",
format = 'html'
),
full_width = TRUE,
position = 'center'
)
```
</div>
All data is returned as [`xts`](https://github.com/joshuaulrich/xts)-objects which enables seamless interaction with with [`quantmod`](https://github.com/joshuaulrich/quantmod) and [`TTR`](https://github.com/joshuaulrich/TTR), for developing and evaluating trading strategies or general purpose cryptocurrency market analysis with a historical or temporal perspective.
## :information_source: Overview
The `cryptoQuotes`-package has *two* main features; retrieving cryptocurrency market data, and charting. The market data consists of *OHLC-V* data and sentiment indicators; including, but not limited to, cryptocurrency *fear and greed index*, *long-short ratio* and *open interest*. All market data is retrieved using the family of `get_*`-functions. To get a full overview of the package and functionality please see the documentation on [pkgdown](https://serkor1.github.io/cryptoQuotes/).
> **Note:** Given the nature of crypotcurrency data and general legislative restrictions, some `exchanges` may not work in your geolocation.
Below is a quick overview of the package and basic usage examples on retrieving and charting Bitcoin (BTC) *OHLC-V* and *long-short ratio* in 30 minute intervals.
### :information_source: Cryptocurrency market data
#### OHLC-V
All supported exchanges and markets are listed in the table below, alongside the available range of intervals available from the respective exchanges,
<div align="center">
```{r supported calls, echo=FALSE,fig.alt="Cryptocurrency market data in R"}
# function to make
# first letter uppercase without
# declaring dependency on stringr
# Thank you Stackoverflow
# https://stackoverflow.com/a/18509816
firstup <- function(x) {
substr(x, 1, 1) <- toupper(substr(x, 1, 1))
x
}
sort_granularities <- function(granularity_vector) {
# Define a custom ordering based on granularity
custom_order <- c("s","m", "h", "d", "w", "M")
# Extract numeric and non-numeric parts
numeric_part <- as.numeric(gsub("[^0-9]", "", granularity_vector))
non_numeric_part <- gsub("[0-9]", "", granularity_vector)
# Map non-numeric part to custom order
non_numeric_part_order <- match(non_numeric_part, custom_order)
# Create a sorting index
sorting_index <- order(non_numeric_part_order, numeric_part)
# Sort the vector
sorted_granularity_vector <- granularity_vector[sorting_index]
return(sorted_granularity_vector)
}
# 1) Supported Exchanges
# sorted alphabetically
exchange <- firstup(
sort(
suppressMessages(
cryptoQuotes::available_exchanges()
)
)
)
# 2) calculate available
# intervals
intervals <- sapply(
exchange,
function(x){
suppressMessages(
{
futures = cryptoQuotes::available_intervals(
type = 'ohlc',
source = tolower(x),
futures = TRUE
)
spot = cryptoQuotes::available_intervals(
type = 'ohlc',
source = tolower(x),
futures = FALSE
)
}
)
length(union(futures, spot))
}
)
# 2) calculate available
# intervals
granularity <- function(
lowest = TRUE
)
{
sapply(
exchange,
function(x){
suppressMessages(
{
futures = cryptoQuotes::available_intervals(
source = tolower(x),
futures = TRUE
)
spot = cryptoQuotes::available_intervals(
source = tolower(x),
futures = FALSE
)
}
)
available_granularity <- sort_granularities(union(
futures,
spot
))
if (lowest) {
output <- available_granularity[1]
} else {
output <- available_granularity[length(available_granularity)]
}
output <- gsub("(.)(.)", "\\1 \\2", output)
output <- strsplit(
output,
split = " "
)[[1]]
number <- output[1]
granularity <- switch(
EXPR = output[2],
s = "second(s)",
m = "minute(s)",
h = "hour(s)",
w = "week(s)",
d = "day(s)",
M = "month(s)"
)
paste(number, granularity)
}
)
}
# 2) create
# table
kableExtra::kable_styling(
knitr::kable(
digits = 2,
caption = 'Supported exchanges, markets and intervals.',
align = 'lccccc',
table.attr = "style='width:100%;'",
col.names = c('Exchange', 'Spot', 'Futures', 'Available Intervals' ,'Smallest Interval', 'Biggest Interval'),
x = data.frame(
row.names = NULL,
Exchange = exchange,
Spot = rep(x = ":white_check_mark:",length(exchange)),
Futures = rep(x = ":white_check_mark:",length(exchange)),
`Available Intervals` = c(intervals),
`Smallest Interval` = c(granularity(TRUE)),
`Biggest Interval` = c(granularity(FALSE))
),
format = 'html'
),
full_width = TRUE,
position = 'center'
)
```
</div>
<details>
<summary>Example: Bitcoin OHLC-V</summary>
Get USDT denominated Bitcoin (BTC) on the spot market from Binance in `30m`-intervals using the `get_quote()`-function,
```{r cryptocurrency market data in R, eval=TRUE}
## BTC OHLC prices
## from Binance spot market
## in 30 minute intervals
BTC <- cryptoQuotes::get_quote(
ticker = 'BTCUSDT',
source = 'binance',
futures = FALSE,
interval = '30m',
from = Sys.Date() - 1
)
```
<div align="center">
```{r print table, echo=FALSE, fig.alt="cryptocurrency prices in R"}
kableExtra::kable_styling(
knitr::kable(
caption = 'Bitcoin (BTC) OHLC-V data',
align = 'lcccc',
table.attr = "style='width:100%;'",
x = data.frame(
cbind(
index = paste(tail(zoo::index(BTC))),
tail(zoo::coredata(BTC))
),row.names = NULL
),
format = 'html'
),
full_width = TRUE,
position = 'center'
)
```
</div>
__________
</details>
#### Sentiment indicators
The sentiment indicators available in the `cryptoQuotes`-package can be divided in two; *derived indicators* and *market indicators*. The former is calculated based on, for example, the price actions such as the *Moving Average Convergence Divergence* (MACD) indicator. The latter are public indicators such as the *long-short ratio* or *fear and greed index*; these are retrieved using the family of `get_*`-functions, while the derived indicators can be created using, for example, [`TTR`](https://github.com/joshuaulrich/TTR).
In this overview we are focusing on *market indicators* made public by the cryptocurrency exchanges. For a full overview of sentiment indicators please refer to the documentation on [pkgdown](https://serkor1.github.io/cryptoQuotes/). All supported *market indicators* by exchange are listed in the table below,
<div align="center">
```{r, echo = FALSE}
## 1) list all available
## exchanges and the links
available_endpoint <- data.frame(
Endpoint = c(
"Long-Short Ratio",
"Open Interest",
"Funding Rate"
),
Binance = c(
":white_check_mark:",
":white_check_mark:",
":white_check_mark:"
),
Bitmart = c(
":x:",
":x:",
":x:"
),
Bybit = c(
":white_check_mark:",
":white_check_mark:",
":white_check_mark:"
),
Kraken = c(
":white_check_mark:",
":white_check_mark:",
":x:"
),
Kucoin = c(
":x:",
":x:",
":white_check_mark:"
)
)
## 4) present table
## for as is
kableExtra::kable_styling(
knitr::kable(
digits = 0,
caption = 'Available sentiment indicators by exchange',
align = 'lccccc',
table.attr = "style='width:100%;'",
x = available_endpoint,
format = 'markdown'
),
full_width = TRUE,
position = 'center'
)
```
</div>
<details>
<summary>Example: Bitcoin Long-Short Ratio</summary>
Get the *long-short ratio* on Bitcoin (BTC) using the `get_lsratio()`-function,
```{r cryptocurrency sentiment indicators in R, eval=TRUE}
## BTC OHLC prices
## from Binance spot market
## in 30 minute intervals
BTC_LS <- cryptoQuotes::get_lsratio(
ticker = 'BTCUSDT',
source = 'binance',
interval = '30m',
from = Sys.Date() - 1
)
```
<div align="center">
```{r print LS table, echo=FALSE, fig.alt="cryptocurrency prices in R"}
kableExtra::kable_styling(
knitr::kable(
caption = 'Long-Short Ratio on Bitcoin (BTC)',
align = 'lccc',
table.attr = "style='width:100%;'",
x = data.frame(
cbind(
index = paste(tail(zoo::index(BTC_LS))),
tail(zoo::coredata(round(BTC_LS,3)))
),row.names = NULL
),
format = 'html'
),
full_width = TRUE,
position = 'center'
)
```
</div>
__________
</details>
### :information_source: Charting
```{r, echo=FALSE, include=FALSE}
## Chart BTC
## using klines, SMA,
## MACD and Bollinger Bands
cryptoQuotes::chart(
ticker = BTC,
main = cryptoQuotes::kline(),
sub = list(
cryptoQuotes::lsr(ratio = BTC_LS),
cryptoQuotes::volume()
),
indicator = list(
cryptoQuotes::sma(n = 7),
cryptoQuotes::sma(n = 14),
cryptoQuotes::sma(n = 21),
cryptoQuotes::bollinger_bands()
)
)
```
Charting in the `cryptoQuotes`-package is built on [`plotly`](https://github.com/plotly/plotly.R) for interactivity. It supports *light* and *dark* themes, and accounts for *color-deficiency* via the `options`-argument in the `chart()`-function.
#### Charting with indicators
The OHLC-V data and the sentiment indicator can be charted using the `chart()`-function,
```{r chartquote, fig.align='center',fig.dpi=180, fig.alt="cryptocurrency charts in R"}
## Chart BTC
## using klines, SMA
## Bollinger Bands and
## long-short ratio
cryptoQuotes::chart(
ticker = BTC,
main = cryptoQuotes::kline(),
sub = list(
cryptoQuotes::lsr(ratio = BTC_LS),
cryptoQuotes::volume()
),
indicator = list(
cryptoQuotes::sma(n = 7),
cryptoQuotes::sma(n = 14),
cryptoQuotes::sma(n = 21),
cryptoQuotes::bollinger_bands()
)
)
```
<details>
<summary>Colorblind friendly version</summary>
#### Charting with indicators (colorblind friendly)
```{r chartquote(deficiency), fig.align='center', fig.dpi=180, fig.alt="cryptocurrency charts in R"}
## Chart BTC
## using klines, SMA
## Bollinger Bands and
## ling-short ratio with color-deficiency
cryptoQuotes::chart(
ticker = BTC,
main = cryptoQuotes::kline(),
sub = list(
cryptoQuotes::lsr(ratio = BTC_LS),
cryptoQuotes::volume()
),
indicator = list(
cryptoQuotes::sma(n = 7),
cryptoQuotes::sma(n = 14),
cryptoQuotes::sma(n = 21),
cryptoQuotes::bollinger_bands()
),
options = list(
deficiency = TRUE
)
)
```
__________
</details>
## :information_source: Installation
### :shield: Stable version
```{r stable version guide, eval = FALSE}
## install from CRAN
install.packages(
pkgs = 'cryptoQuotes',
dependencies = TRUE
)
```
### :hammer_and_wrench: Development version
```{r development version guide, eval = FALSE}
## install from github
devtools::install_github(
repo = 'https://github.com/serkor1/cryptoQuotes/',
ref = 'development'
)
```
## :information_source: Code of Conduct
Please note that the `cryptoQuotes` project is released with a [Contributor Code of Conduct](https://serkor1.github.io/cryptoQuotes/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.