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

stockAnalyzer

R-CMD-check test-coverage codecov

This is an R package that provides basic time series modelling functionalities to analyze historical stock prices. Investment in the stock market requires not only knowledge about the listed companies, but also basic summary statistics and modellings of individual stock prices. Given time-series stock price data, this package provides key summary statistics, applies moving average and exponential smoothing models to the data, and visualizes in-sample moving average as well as exponential smoothing fits. A convenient use case for this package is to combine it with the quantmod library, which can provide well-formated stock price data.

Installation

You can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("UBC-MDS/stockAnalyzer")

Features

The package contains the following five functions:

This function calculates summary statistics including mean price, minimum price, maximum price, volatility and return rate based on daily historical stock prices. Users can specify the stock they are interested in and the exact measurement they prefer to analyze on.

This function applies the moving average model to all measurements of stock price and returns an xts time series object containing in-sample fitted values. Users can specify the length of moving average windows (unit: days).

This function performs exponential smoothing on historical stock price time series data. Users can specify the alpha parameter (which defines the weighting, ranging between 0 and 1) for smoothing.

This function creates a line chart showing the raw historical data and fitted data using the moving average method. Users are able to specify the xts object used, the column of choice for moving average calculation, and the length of moving average window (unit: days).

This function creates a line chart showing the raw historical data and fitted data using the exponential smoothing method. Users are able to specify the xts object used, the column of choice for exponential smoothing calculation, and the alpha parameter (which defines the weighting, ranging between 0 and 1) for smoothing.

You can also find function descriptions and their use cases in package vignettes.

Documentation

Example

This is a basic example which shows how to generate summary statistics, conduct moving average modelling, exponential smoothing modeling, and produce visualizations:

# Download stock price data
library(quantmod)
getSymbols("AAPL")

library(stockAnalyzer)
summaryStats(AAPL)
head(movingAverage(AAPL, 300, paste("movingAverage", colnames(AAPL), sep="_")))
head(exponentialSmoothing(AAPL,paste("expsmoothing", colnames(AAPL), sep="_"), 0.02))
visMovingAverage(AAPL, 300, 'AAPL.Close')
visExpSmoothing(AAPL, 0.02, 'AAPL.Close')

R Ecosystem

There are a number of libraries in the R ecosystem that provide functionalities to analyze time series data. For example, Tidyverse has comprehensive functionalities for basic summary statistics. Libraries including data.table, smooth provide functions to calculate moving average. Libraries including smooth and forecast both provide functions to conduct exponential smoothing. ggplot2 is most widely used for visualizations.

In terms of financial data analysis, there are also a wide range of packages. Widely used ones include RQuantLib, quantmod.



UBC-MDS/stockAnalyzer documentation built on March 21, 2021, 7:14 p.m.