---
title: "Usecase"
subtitle: "What happens when Elon Musk Tweets about Dogecoin?"
output: rmarkdown::html_vignette
always_allow_html: true
vignette: >
  %\VignetteIndexEntry{Usecase}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---
  
```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse   = FALSE,
  out.width  = "100%",
  comment    = "#>",
  message    = FALSE,
  out.height = "620px",
  fig.align  = "center"
)
```


```{r, message=FALSE}
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 either `binance`, `bybit`, `bitmart`, `kucoin` and `kraken` are available.

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

<div align="center">
```{r, echo=FALSE, out.width="80%", fig.cap="Tweet by Elon Musk - the timezone is CET.",fig.align='center'}
## include tweet from
## Elon Musk
knitr::include_graphics(
  path = "elonTweet.png"
)
```
</div>

## 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 ```getQuotes()```,



```{r, eval=FALSE}
## 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 the how much it rallied,


```{r}
## 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 illustrate this with candlestick charts using the ```chart()```- and ```kline()```-function, 

```{r, fig.align='center', fig.alt= "Elon Musk DOGE"}
## 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,

```{r}
## 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,

```{r, fig.alt= "Elon Musk DOGE"}
## 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
  )
)
```

