Download Stock Price Online with R

October 11, 2012
By

(This article was first published on Yet Another Blog in Statistical Computing » S+/R, and kindly contributed to R-bloggers)

library(chron)
library(zoo)

# STOCK TICKER OF Fifth Third Bancorp 
stock <- 'FITB'

# DEFINE STARTING DATE
start.date  <- 1
start.month <- 1
start.year  <- 2012

# DEFINE ENDING DATE
end.date  <- 11
end.month <- 10
end.year  <- 2012

# DEFINE URL LINK
link <- paste("http://ichart.finance.yahoo.com/table.csv?s=", stock,
              "&a=", as.character(start.month - 1), 
              "&b=", as.character(start.date), 
              "&c=", as.character(start.year),
              "&d=", as.character(end.month - 1),
              "&e=", as.character(end.date), 
              "&f=", as.character(end.year),
              "&g=d&ignore=.csv", sep = '')

# DOWNLOAD STOCK PRICE AS CSV FILE
download.file(link, "c:/projects/data.csv")

# READ THE CSV FILE INTO R
data <- read.csv("c:/projects/data.csv")

# CONVERT CHARACTER INTO DATE
dt <- dates(as.character(data[, 1]), format = "y-m-d")

# CONVERT DATA FRAME INTO TS OBJECT
ts <- zoo(data[, 2:5], dt)

# CREATE A PLOT FOR OPEN/CLOSE/HIGH/LOW PRICES
plot(ts, main = stock)

To leave a comment for the author, please follow the link and comment on their blog: Yet Another Blog in Statistical Computing » S+/R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)