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

library(YahooTickers)
library(dplyr)

YahooTickers

Travis build status AppVeyor build status

The goal of YahooTickers is to easily get stock index constituents from Yahoo Finance without having to worry about complicated for loops. This may be useful if you wish to focus as quickly as possible in time-series modeling, but also convenient if data is needed for it's side effects (e.g. plotting).

The package depends heavly on quantmod, rsample, sweep and forecast projects.

Instalation

You can install YahooTickers from github with:

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

Examples

To download all the stock constituents of the Dow Jones Industrial Average use get_tickers() and get_stocks() in one pipeline.

dow <- get_tickers(dow) %>% 
  get_stocks(., periodicity = "monthly")

dow 

With additional functions provided in the package it's possible to fit a model in a rolling or expanding time window. This is done by get_models().

library(forecast)

merval_models <- get_tickers(merval) %>%
  get_stocks(
    tickers     = ., 
    periodicity = "monthly",
    quiet       = TRUE
    ) %>% 
  get_returns(
    .tbl     = ., 
    .group   = tickers, 
    .type    = log, 
    .omit_na = TRUE, 
    ret_adj  = adjusted # change col names on the fly like dplyr!
    ) %>% 
  get_models(
    .tbl        = .,
    .group      = tickers, 
    .col        = ret_adj, 
    .initial    = 100, 
    .assess     = 1, 
    .cumulative = TRUE, 
    .fun        = ets
    ) 

merval_models

In this case, the the ETS function was used to fit the first 100 observations on a expanding window, .cumulative = TRUE. The output contains the fitted parameters, selection criterias, as some traditional in-sample error metrics.

To extract the true out-of-sample forecasts use get_forecasts(). This function automatically identifies the .assess argument in get_models() and uses it as a target for the forecast horizon.

library(forecast)

merval_models  %>% 
  get_forecasts(.)

Currently, YahooTickers supports the following stock indexes:

A Warning

The Yahoo Finance API works reasonably well but there are cases in which it does not cover historical data for all stocks in an index universe. The bel20 it's a good example: despite officially having 20 components, Yahoo Finance only maintains data for 4 stocks. Thus, it is recommended to always check the data after downloading it!



Reckziegel/YahooTickers documentation built on May 28, 2019, noon