As an example, suppose you want to query the date of the google stock start from 06-01-2009 to 03-06-2010. Simply replace the string after
- s = with "GOOG", the symbol of google
- a = start month minus one
- b = start day
- c = start year
- d = until month minus one
- e = until day
- f = until year
The resultant query URL will look like below:
http://ichart.finance.yahoo.com/table.csv?s=GOOG&a=05&b=01&c=2009&d=02&e=06&f=2010&g=d&ignore=.csv
The R code is attached below:
http://ichart.finance.yahoo.com/table.csv?s=GOOG&a=05&b=01&c=2009&d=02&e=06&f=2010&g=d&ignore=.csv
The R code is attached below:
# TODO: Query Stock Date # # Author: Everett Roger Qin ############################################################################### rm(list = ls(all = TRUE)) #set the working directory setwd('Your working directory') ############################################################################### # symbol-the symbol of the stock # fromDate, Date Class of the starting date of the query # untilDate, Date Class of the ending date of the query ############################################################################### stockURL <- function(symbol,fromDate, untilDate) { # parse the input Date to day/month/year fromDate <- as.Date(fromDate) untilDate <- as.Date(untilDate) from_day <- as.integer(format(fromDate,"%d")) # a= the start month (minus 1) from_month <- as.integer(format(fromDate,"%m"))- 1 from_year <- as.integer(format(fromDate,'%Y')) until_day <- as.integer(format(untilDate,"%d")) until_month <- as.integer(format(untilDate,"%m")) - 1 until_year <- as.integer(format(untilDate,'%Y')) queryURL <- sprintf('http://ichart.finance.yahoo.com/table.csv?s=%s&a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&g=d&ignore=.csv', symbol,from_month,from_day,from_year, until_month,until_day,until_year) return(queryURL) } data <- read.csv(stockURL('CFW','2010-02-05',Sys.Date()),header = TRUE)
No comments:
Post a Comment