knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-", warning = FALSE, message = FALSE ) library(YahooTickers) library(dplyr)
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).
You can install YahooTickers from github with:
# install.packages("devtools") devtools::install_github("Reckziegel/YahooTickers")
To download all the stock constituents of the Dow Jones Industrial Average use
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
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(.)
YahooTickers supports the following stock indexes:
dow: Dow Jones Industrial Average (United States)
sp500: S&P 500 (United States)
nasdaq: NASDAQ Composite Index (United States)
nyse: NYSE Composite Index (United States)
amex: AMEX Composite Index (United States)
russell2000: Russell 2000 Index (United States)
ftse100: FTSE 100 Index (United Kingdom)
dax: Deutsche Boerse AG German Stock (Germany)
cac40: CAC 40 Index (France)
bel20: BEL 20 Index (Belgium)
topix: TOPIX Core 30 Index (Japan)
hangseng: HangSeng Composite Index (Hong Kong)
sensex: S&P BSE SENSEX Index (India)
jakarta: Jakarta Stock Exchange Stock Index (Indonesia)
bursa: Bursa Malaysia Bhd (Malaysia)
nzx50: S&P/NZX 50 Gross Index (New Zealand)
kospi: Korea Stock Exchange KOSPI Index (south Korea)
taiex: Taiwan Stock Exchange Weighted Index (Taiwan)
tsx: S&P/TSX Composite Index (Canada)
ibovespa: Ibovespa Brasil Sao Paulo Stock Exchange Index (Brazil)
ipc: S&P/BMV IPC (Mexico)
ipsa: S&P/CLX IPSA (Chile)
merval: Buenos Aires Stock Exchange Merval Index (Argentina)
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!
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.