knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
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.
You can install the development version from GitHub with:
# install.packages("devtools") devtools::install_github("UBC-MDS/stockAnalyzer")
The package contains the following five functions:
summaryStats
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.
movingAverage
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).
exponentialSmoothing
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.
visMovingAverage
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).
visExpSmoothing
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.
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')
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
.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.