Chapter 3 Open Data Sources

To increase reproducibility, all data are free and can be loaded from the quantmod R package with the function getSymbols(). It is possible to choose between different data sources like yahoo-finance (default), alpha-vantage and others.

3.1 R Functions

The following functions were created to increase the ease of data collection with the quantmod R package, which can be found in the R/ directory in the dedicated GitHub repository (Roth, 2022a).

3.1.1 get_yf()

This function is the main wrapper for collecting data with getSymbols() from yahoo-finance, and converts prices to returns with the pri_to_ret() function explained in section 4.6.2. The output is a list containing prices and returns as xts objects, which is the usual object in R for storing time series. The arguments that can be passed to get_yf() are:

  • tickers: Vector of symbols (asset names, e.g. “APPL,” “GOOG,” …)
  • from ="2018-01-01": R Date
  • to = "2019-12-31": R Date
  • price_type = "close": Type of prices to be recorded (e.g. “open,” “high,” “low,” “close,” “adjusted”)
  • return_type = "adjusted": Type of prices used to calculate the returns (e.g. “open,” “high,” “low,” “close,” “adjusted”)
  • print = F: Should the function print the return of getSymbols()

3.1.2 buffer()

To make data reusable and reduce compilation time, the buffer() function stores the data collected with get_yf(). It receives an R expression, evaluates it and stores it in the buffer_data/ directory under the specified name. If this name already exists, it loads the R object from the RData files without evaluating the expression. The evaluation and overwriting of the existing RData file can be forced with force=T.

References

Roth, A. (2022a). Asset allocation using particle swarm optimization in r. https://github.com/AxelCode-R/Asset-Allocation-using-Particle-Swarm-Optimization-in-R.