Usecase

knitr::opts_chunk$set(
  collapse   = FALSE,
  out.width  = "100%",
  comment    = "#>",
  message    = FALSE,
  out.height = "620px",
  fig.align  = "center"
)
library(cryptoQuotes)

Introduction

This high-level API-client provides open access to cryptocurrency market data without relying on low-level coding and API-keys. Currently all actively traded cryptocurrencies on r paste(length(invisible(available_exchanges))) major exchanges are available, see the wiki for more details.

In this vignette we will explore a case study to showcase the capabilities of {cryptoQuotes}; how did the Dogecoin-market react to Elon Musks following tweet,

wzxhzdk:2

Cryptocurrency Market Analysis in R

Elon Musk tweeted (Well, now he X'ed) about Dogecoin January 14, 06.18 AM (UTC) - and Dogecoin rallied. To determine how fast the markets reacted to his tweets, we could get the market data for Dogecoin in 1 minute intervals the day he tweeeted using the get_quotes()-function,

## DOGEUSDT the day
## of the tweet on the
## 1m chart
DOGE <- cryptoQuotes::get_quote(
  ticker   = 'DOGE-USDT',
  interval = '1m',
  source   = 'kucoin',
  futures  = FALSE,
  from     = '2022-01-14 07:00:00',
  to       = '2022-01-14 08:00:00'
)

This returns an object of class r paste(class(DOGE),collapse = ' and ') with r nrow(DOGE) rows. To calculate the rally within the first minute of the tweet, we can use {xts}-syntax to determine its magnitude,

## extrat the
## tweet moment
tweet_moment <- DOGE["2022-01-14 07:18:00"]

## calculate 
## rally
cat(
  "Doge closed:", round((tweet_moment$close/tweet_moment$open - 1),4) * 100, "%"
)

Dogecoin rallied r paste0(round((tweet_moment$close/tweet_moment$open - 1),4) * 100, "%") within the minute Elon Musk tweeted.

Charting price action with candlesticks

We can visualize the rally this with candlestick charts using the chart()- and kline()-function,

## chart the
## price action
## using klines
cryptoQuotes::chart(
  ticker     = DOGE,
  main       = cryptoQuotes::kline(),
  indicator  = list(
    cryptoQuotes::bollinger_bands()
  ),
  sub  = list(
    cryptoQuotes::volume()
  ),
  options = list(
    dark = FALSE
  )
)

Charting price action with event lines

To create a, presumably, better visual overview we can add event lines using the event_data-argument, which takes a data.frame of any kind as argument,

## 1) create event data.frame
## by subsetting the data
event_data <- as.data.frame(
  zoo::coredata(
    DOGE["2022-01-14 07:18:00"]
  )
)

## 1.1) add the index 
## to the event_data
event_data$index <- zoo::index(
  DOGE["2022-01-14 07:18:00"]
)

# 1.2) add event label
# to the data
event_data$event <- 'Elon Musk Tweets'

# 1.3) add color to the
# event label
event_data$color <- 'steelblue'

This event data, can be passed into the chart as follows,

## 1) chart the
## price action
## using klines
cryptoQuotes::chart(
  ticker     = DOGE,
  event_data = event_data,
  main       = cryptoQuotes::kline(),
  indicator  = list(
    cryptoQuotes::bollinger_bands()
  ),
  sub = list(
    cryptoQuotes::volume()
  ),
  options = list(
    dark = FALSE
  )
)


Try the cryptoQuotes package in your browser

Any scripts or data that you put into this service are public.

cryptoQuotes documentation built on April 4, 2025, 2:33 a.m.