Quantcast
Channel: Business Data Insights
Viewing all articles
Browse latest Browse all 5

Capturing free security history data with R

$
0
0

One need I’ve found for trading strategy development is to down-load specific lists of financial securities by symbol and save out the data for future use.  The following process does just that by down-loading history from Yahoo finance and saving it out as a text file.

This process uses a list of desired symbols saved as a “.csv” file in the “C:/TradeData” directory (note: the “/” symbol is used for R to separate directories, while “\” is used in Windows).  Symbols are replace with integers, based on their position in the list since “xts” objects don’t allow strings.  I trust you find this helpful for your own testing and development needs.

require(quantmod)
require(tseries)
require(timeDate)
require(date)

# Financial instrument data download from Yahoo

# Read in the security symbols

symbols <- read.csv("C:/TradeData/instrSymbols.csv", header = F, stringsAsFactors = F)
nbrSymbols = length(symbols[,1])

start_t<-Sys.time()
dateStart <- "20000101"

instrHist<-xts()
for (i in 1:nbrSymbols) {

  cat("Downloading ", i, " out of ", nbrSymbols , "\n")
  x<- getYahooData(symbol = symbols[i,], start=dateStart, type="price", quiet=TRUE)
  x<-x[,1:4]
  x$symbol<-i # Add an integer to represent the security symbol
  x$dt<-as.numeric(index(x))/(24*60*60) # Turn the rowname date into a number
  ifelse(symbols[1,]==symbols[i,],instrHist<-x,instrHist<-rbind(x,instrHist))
}

dt.rownames<-as.character(as.Date(coredata(instrHist$dt))) #Capture the numeric date separately
instrHist<-coredata(instrHist)  #Remove the dates

write.table(instrHist, "C:/TradeData/instrHist.txt", sep="\t")

 


Viewing all articles
Browse latest Browse all 5

Trending Articles