knitr::opts_chunk$set(
  warning = FALSE,
  message = FALSE,
  fig.path = "man/figures/README-",
  fig.align = "center",
  collapse = TRUE,
  comment = "#>"
)

quantrecipes

Lifecycle: experimental Travis build status AppVeyor build status

Installation

You can install the development version of quantrecipes using:

# install.packages("remotes")
remotes::install_github("bagasbgy/quantrecipes")

Getting Started

Let's start by importing the library:

# import libs
library(quantrecipes)

Here's a quick example for setting-up a recipe to produce ZigZag, Bollinger Bands, and moving average:

# an example recipe
rec <- recipe(. ~ ., data = btcusdt) %>% 
  step_zigzag(close, change = 0.3, state = TRUE) %>% 
  step_bbands(high, low, close) %>% 
  step_ma(close) %>% 
  step_naomit(all_predictors()) %>% 
  prep()

# quick check
juice(rec)

This example recipe would be very helpful for preparing data for further analysis. For example, here is a quick visualization:

# import libs
library(ggplot2)

# quick visualization
data_viz <- juice(rec) %>% 
  tail(12 * 24)

data_highlight <- data_viz %>% 
  drop_na() %>% 
  filter(zigzag_swing != "hold")

ggplot(data_viz, aes(x = datetime, y = close)) +
  geom_ribbon(aes(ymin = bbands_dn, ymax = bbands_up), alpha = 0.15) +
  geom_line(aes(y = ma_close_value), colour = "darkblue", linetype = "dashed", alpha = 0.5) +
  geom_line() +
  geom_point(data = data_highlight, aes(colour = zigzag_swing)) +
  scale_colour_manual(values = c("up" = "blue", "down" = "red")) +
  guides(colour = FALSE) +
  labs(x = NULL, y = NULL) +
  theme_minimal()

Since we have the recipes object, this approach also very helpful to preprocess new data:

# import libs
library(rucoin)

# get new data
btcusdt_new <- get_kucoin_prices(
  symbols = "BTC/USDT",
  from = "2019-08-18",
  to = "2019-08-19",
  frequency = "5 minutes"
)

# get preprocess result for new data
bake(rec, btcusdt_new)

Thus, make a reproducible analysis for new data easier too:

# quick visualization
data_viz <- bake(rec, btcusdt_new)

data_highlight <- data_viz %>% 
  drop_na() %>% 
  filter(zigzag_swing != "hold")

ggplot(data_viz, aes(x = datetime, y = close)) +
  geom_ribbon(aes(ymin = bbands_dn, ymax = bbands_up), alpha = 0.15) +
  geom_line(aes(y = ma_close_value), colour = "darkblue", linetype = "dashed", alpha = 0.5) +
  geom_line() +
  geom_point(data = data_highlight, aes(colour = zigzag_swing)) +
  scale_colour_manual(values = c("up" = "blue", "down" = "red")) +
  guides(colour = FALSE) +
  labs(x = NULL, y = NULL) +
  theme_minimal()

Beside for market descriptive or predictive analysis, this package also provide some tools for performance analytics:

# an example recipe
rec <- recipe(. ~ ., data = actions) %>% 
  step_cumret(benchmark, portfolio, prices = "close") %>%
  step_naomit(all_predictors()) %>%
  prep()

# quick check
juice(rec)

Which, again, could also very helpful to build the related visualizations:

# another helper libs
library(stringr)

# quick visualization
data_viz <- juice(rec) %>% 
  select(datetime, cumret_benchmark, cumret_portfolio) %>% 
  gather(key, value, -datetime) %>% 
  mutate(key =
    str_replace_all(key, "cumret_", "") %>% 
      str_to_title()
  )

ggplot(data_viz, aes(x = datetime, y = value)) +
  geom_line() +
  facet_wrap(vars(key), ncol = 1, scales = "free") +
  labs(title = "Portfolio vs Benchmark Return", x = NULL, y = NULL) +
  theme_minimal()


bagasbgy/quantrecipes documentation built on Dec. 25, 2019, 7:54 a.m.