вторник, 8 декември 2015 г.

R Script: Get Prices of a List of Companies from Yahoo.

There are numerous codes for getting data from finance yahoo. We share a code, which downloads price data from yahoo for a list of companies, specified in a txt file. The symbols in the  tickers.txt must be presented in a column, as you may see in the picture.



# UPDATE 2015-12-08
# The script imports in a zoo data frame adjusted close prices of a list of companies. 
# Companies tickers are stored in a txt file in the working directory. The list could be
# extended depending on the portfolio under consideration
# finance.yahoo.com is the source of information.
# The user should specify:
# (1) the companies, whose data will be downloaded. The list of tickers is saved as a txt file;
# (2) start date;
# (3) end date;
# (4) frequency (compression) = d for daily, w for weekly, and m for monthly
# (5) quote ("Open", "High", "Low", "Close", "AdjClose", "Volume")
# (6) provider - yahoo for stocks, oanda for fx
# (7) origin - "1970-01-01"
# (8) retclass zoo, 

              #---------------------------------START-----------------------------------

# set working directory. One should make a folder temp first and then to set it as a working directory.
setwd("C:\\temp")

# load packages
suppressPackageStartupMessages(require(tseries))
suppressPackageStartupMessages(require(zoo))

# get the list with tickers from ticker.txt. The txt file is saved in the working directory
      ticker<-read.table("ticker.txt", header=FALSE, stringsAsFactors=FALSE, colClasses="character")

# specify the input parameters. Dates are in a yyyy-mm-dd format
       start.date<-"2007-01-01"
       end.date<-Sys.Date()
              
# add or delete quotes you need in c("Open", "High", "Low", "Close","AdjClose","Volume").
# For example, if one need only adjusted close price data he should delete
# "Open", "High", "Low", "Close","Volume". Thus....
       quotes<-c("AdjClose")

# frequency "d"aily, "w"eekly, or "m"onthly.
       frequency<-c("m")

# get data with the tseries function get.hist.quote. The retclass is set to "zoo".
# create an empty zoo data frame
       price.data <- zoo()

# first specify the date format
       index(price.data) <- as.Date(format(time(price.data)),tz="")

# count the number of the tickers that will be called
       n<-nrow(ticker)
              
# loop through the tickers, get quotes and merge each zoo data frame together in price.data
       for (i in 1:n) {
       tick<-ticker[i,]
       
       ticker.prices<- try(get.hist.quote(instrument=ticker[i,], start=start.date, quote=quotes, provider="yahoo", origin="1970-01-01", compression=frequency, retclass="zoo",quiet=FALSE), silent=FALSE)
          
       colnames(ticker.prices) <- ticker[i,]
       price.data <- try(merge(price.data,ticker.prices))       
       }
# Save the data in a csv file named djia.csv in the specified folder.

       file.folder<-"C:\\temp\\djia.csv"
              
       write.zoo(price.data,file=file.folder ,sep=",",row.names=FALSE)

Няма коментари:

Публикуване на коментар