Вы находитесь на странице: 1из 3

##Script to Load Data etc

## file is separated with | and rows have different number of columns, we're onl
y interested in the first 2
stock_symbols=read.csv("nasdaq_and_other.txt",sep="|",fill=TRUE,header=FALSE)
row.names(stock_symbols)<-stock_symbols[,1]
colnames(stock_symbols)[1:2]<-c("Symbol","Description")
favs=stock_symbols[!is.na(match(row.names(stock_symbols),c("FB","YELP","RENN","D
ANG","GRPN","LITB"))),]
#####First Load conglomerate file with Stock Symbols "nasdaq_and_other.txt", dow
nloaded from
##### 1) ftp://ftp.nasdaqtrader.com/SymbolDirectory/nasdaqlisted.txt
##### 2) ftp://ftp.nasdaqtrader.com/SymbolDirectory/otherlisted.txt

###########GET HISTORICAL STOCK DATA#################


###http://rss.acs.unt.edu/Rdoc/library/tseries/html/get.hist.quote.html
library(tseries)
################################################################################
################################################
################################################################################
################################################
#Function for downloading raw ("Open", "High", "Low", "Close") historical stock
data in bulk
# Usage: get_historical_stock_data_return_merged_dataframe(dataframe_with_rownam
es_as_stock_symbols)
################################################################################
################################################
################################################################################
################################################
################################################################################
################################################
get_raw_historical_stock_data_return_merged_dataframe<-function(stock_symbols_da
taframe_with_rownames_as_symbols,start_date="2013-04-01",qoute_type="Close",prov
ider_type="yahoo",data_resolution="d"){
##Helpful print outs for user
print("Historical Stock Bulk Download by GSJBRana Aug, 17, 2013")
print("#####################################################")
print(paste("Start Date:(start_date) ",start_date,sep=" "))
print(paste("Quote Type:(qoute_type) ",qoute_type,sep=" "))
print(paste("Data Resolution:(data_resolution), options=d,w,m",data_resolution,s
ep=" "))
print(paste("Database:(provider_type) ",provider_type,sep=" "))

num_of_stocks_to_download=(dim(stock_symbols_dataframe_with_rownames_as_symbols)
[1])
mock_historical_stock=get.hist.quote("GIS",quote=qoute_type,provider =provider_t
ype,start = start_date, compression = data_resolution,retclass ="zoo", quiet = T
RUE, drop = FALSE)
merged_data_frame=mock_historical_stock
names(merged_data_frame)<-"Mock_Data_GIS"
for(i in 1:num_of_stocks_to_download){
current_stock=rownames(stock_symbols_dataframe_with_rownames_as_symbols[i,
])
cache_historical_stock=try(get.hist.quote(current_stock,quote=qoute_type,p

rovider =provider_type,start = start_date, compression = data_resolution,retclas


s ="zoo", quiet = TRUE, drop = FALSE),TRUE)
if(class(cache_historical_stock)=="try-error"){ print(paste("Failed with s
tock: ",current_stock, " ... ", (i*100.000)/num_of_stocks_to_download,"%",sep="
"))}
else{
names(cache_historical_stock)=current_stock
merged_data_frame=merge(merged_data_frame,cache_historical_stock,al
l=TRUE)
print(paste("Success with stock: ",current_stock, " ... ", (i*100.0
00)/num_of_stocks_to_download,"%",sep=" "))
}
}
return( as.data.frame(t(merged_data_frame)) )
}
################################################################################
################################################
################################################################################
################################################
################################################################################
################################################
################################################################################
################################################

################################################################################
################################################
################################################################################
################################################
################################################################################
################################################
#Function for filtering out only those stock symbols that yahoo can download dat
a from
# Usage: get_historical_stock_data_return_merged_dataframe(dataframe_with_rownam
es_as_stock_symbols)
################################################################################
################################################
################################################################################
################################################
################################################################################
################################################
filter_out_usable_stock_symbols<-function(stock_symbols_dataframe_with_rownames_
as_symbols,provider_type="yahoo"){
list_of_stocks_that_dont_work=""
num_of_stocks_to_download=(dim(stock_symbols_dataframe_with_rowna
mes_as_symbols)[1])
for(i in 1:num_of_stocks_to_download){
current_stock=rownames(stock_symbols_dataframe_wit
h_rownames_as_symbols[i,])
cache_historical_stock=try(get.hist.quote(current_
stock,quote="Open",provider =provider_type,start = format(Sys.Date()-2, format="
%Y-%m-%d"), compression = "d",retclass ="zoo", quiet = TRUE, drop = FALSE),TRUE)
if(class(cache_historical_stock)=="try-error"){
print(paste("Error with: ",current_stock,"
... ", (i*100.000)/num_of_stocks_to_download,"%",sep=" "))
list_of_stocks_that_dont_work=c(list_of_st
ocks_that_dont_work,current_stock)
}

else{
print(paste("Success with: ",current_stoc
k," ... ", (i*100.000)/num_of_stocks_to_download,"%",sep=" "))
}
}
indexs_for_bad_stocks <- rownames(stock_symbols_dataframe_with_rownames_as_symbo
ls) %in% list_of_stocks_that_dont_work
num_of_supplied_stocks<-dim(stock_symbols_dataframe_with_rownames_as_symbols)[1]
num_of_stocks_that_dont_work<-(length(list_of_stocks_that_dont_work))-1
print(paste("Supplied Stocks: ",num_of_supplied_stocks,sep=" "))
print(paste("Number of Stocks that work: ",(num_of_supplied_stocks-num_of_stocks
_that_dont_work)," .... ",((num_of_supplied_stocks-num_of_stocks_that_dont_work)
*100.0)/num_of_supplied_stocks ,sep=" "))
return(stock_symbols_dataframe_with_rownames_as_symbols[!indexs_for_bad_stocks,]
)
}
################################################################################
################################################
################################################################################
################################################
################################################################################
################################################
################################################################################
################################################

Вам также может понравиться