stockdata_source: Source code for stock prices of S&P 500 companies from 2007...

Description Author(s) References Examples

Description

This is the source code for obtaining the stock closing prices and company information of S&P 500 companies in stockdata.

Author(s)

Yang, J. and Peng, J.

References

Yang, J. & Peng, J. (2018), 'Estimating Time-Varying Graphical Models', arXiv preprint arXiv:1804.03811

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
library(XML)
library(RCurl)
library(quantmod)

# select one stock of interest
stock.ticker <- "MSFT"

# extract stock information from wiki
url <- "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
tabs <- getURL(url)
stock.info <- readHTMLTable(tabs, stringsAsFactors = FALSE)[[1]]
stock.ticker.all <- stock.info$`Ticker symbol`
stock.name.all <- stock.info$Security
stock.sector.all <- stock.info$`GICS Sector`
stock.index <- which(stock.ticker.all == stock.ticker)
stock.name <- stock.name.all[stock.index]
stock.sector <- stock.sector.all[stock.index]

# extract stock closing prices and indices of dates
stock.price <- getSymbols(stock.ticker, auto.assign=FALSE, 
from='2007-01-01', to="2017-01-01")[, 4]
date.index <- as.character(index(stock.price))

# summary of selected stock
cat(sprintf("Ticker: %s\nName: %s\nSector: %s\nMin Price: %.2f (%s)
Max Price: %.2f (%s)", stock.ticker, stock.name, stock.sector, 
min(stock.price), date.index[which.min(stock.price)], 
max(stock.price), date.index[which.max(stock.price)]))

loggle documentation built on May 2, 2019, 9:27 a.m.