Yet Another Blog in Statistical Computing

I can calculate the motion of heavenly bodies but not the madness of people. -Isaac Newton

Download Stock Price Online with R

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)

Advertisements

Written by statcompute

October 11, 2012 at 10:46 pm

Posted in S+/R

Tagged with

%d bloggers like this: