Interactive Cryptocurrency Charts in R
The cryptoQuotes way
Source:vignettes/articles/02-charting.Rmd
02-charting.Rmd
## load library
library(cryptoQuotes)
As an experimental
feature, the
cryptoQuotes
-package has a variety of
chart
-functions built on top of plotly
and
TTR
to visualize cryptocurrency market data and trading
indicators interactively.
Throughout this article we will analyze Bitcoin on the hourly chart
called by the get_quotes
-function,
## Get the
## SPOT price of
## Bitcoin on the hourly
BTC <- get_quote(
ticker = "BTCUSD",
source = "kraken",
futures = FALSE,
interval = "1h",
from = Sys.Date() - 7
)
Price charts
There are two main price charts. The Japanese candlestick chart,
kline()
, and the basic OHLC chart, ohlc()
,
charts. A chart has to be wrapped in the
chart()
-function.
All charts comes with
volume
andbollinger bands
bydefault
.
Charting indicators
All the charts supports various indicators such as RSI
,
MACD
and MA
. These can be passed into their
respective arguments as follows,
Charting events
All charts supports passing event_data
, which are any
type of data.frame
, that can be plotted on the chart. Below
is an example of plotting the crossing of the MA
,
## Calculate moving averages
## SMA(7) and SMA(10)
BTC$short_SMA <- TTR::SMA(
x = BTC$close,
n = 7
)
BTC$long_SMA <- TTR::SMA(
x = BTC$close,
n = 21
)
## Determine the cross
## from below
BTC$cross <- as.numeric(
lag(BTC$short_SMA) < BTC$long_SMA &
BTC$short_SMA > BTC$long_SMA
)
Based on the cross
indicator, the BTC
pair
can now be subset, and processed accordingly.
## create event data
## and store it as a data.rfrae
event_data <- subset(
BTC,
cross == 1
)
# 1.1) Extract the index
# from the event data
index <- zoo::index(
event_data
)
# 1.2) Convert the coredata
# into a data.frame
event_data <- as.data.frame(
zoo::coredata(event_data)
)
# 1.3) Add the index into the data.frame
# case insensitive
event_data$index <- index
# 1.4) add events to the data.
# here we use Buys and Sells.
event_data$event <- 'Bull Cross'
event_data$color <- 'darkgray'
Finally, the crosses can be plotted using the chart()
and addEvents()
functions, like below,