knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

snoop

R-CMD-check Lifecycle: experimental

A package for backtesting and datasnooping.

Installation

You can install the development version of snoop from GitHub with:

# install.packages("devtools")
devtools::install_github("Reckziegel/snoop")

Workflow

snoop aims to automate routines for portfolio construction purposes inside the tiyverse.

The current workflow is the following:

  1. Build a rolling tibble with construct_rolling_infrastructure()
  2. Chose a rebalance periodicity with construct_rebalance_infrastructure()
  3. Rebalance with rebalance_portfolio()
  4. Compute the main statistics with extract_statistics()

Toy Example

library(snoop)

# Step 0: Get the data
stocks <- tibble::tibble(
 time = as.Date('2009-01-01') + 0:99,
 X    = stats::rnorm(100, 0, 1),
 Y    = stats::rnorm(100, 0, 2),
 Z    = stats::rnorm(100, 0, 4)
)

# Step 1: Rolling Infraestructure
roll <- construct_rolling_infrastructure(stocks, .initial = 50)
roll

# Step 2: Rebalance Infraestructure
rebal <- construct_rebalance_infrastructure(roll, .by = "week")
rebal # information is under the hood

# Step 3: Rebalance Portfolio
mu_sigma <- function(.data) {
  # Mean Variance Strategy
  list(mu = colMeans(.data), sigma = stats::cov(.data)) 
}

# Step 4: Compute the main statistics
optimal <- rebalance_portfolio(rebal, mu_sigma, .strategy = "mean_variance")
optimal

# Step 4: Compute Statistics
metrics <- extract_statistics(optimal)
metrics


Reckziegel/snoop documentation built on July 1, 2022, 5:32 a.m.