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

R Script: Yahoo Finance API for CSV

One of our clients has a strategy, based on the consensus estimates of 1-year target prices of US equities. We were hired for backtesting his investment strategy. For the needs of the backtest we needed a script for downloading data from Yahoo. The post of Kelly Elias on Yahoo Finance API for CSV motivated us to write a function for our in-house use. 
We post a simple script that downloads the 1-year target prices of a list of US companies in a csv file in the working directory. Please note, that not all companies in Yahoo Finance have analyst coverage.


########## Start####################
# Create "temp"  directory with "blog" subdirectory in C and set it as wd. 
# If it exists, then set it as a working directory.
mainDir <- "C:\\temp\\blog"

if(dir.exists(file.path(mainDir))==FALSE){
       dir.create(file.path(mainDir)) 
       setwd(mainDir)
} else
       setwd(mainDir)


# Specify the tickers, tags, and labels of the tags
tickers<- c("AAPL","AXP","BA","CAT","CSCO","CVX","DD","DIS","GE","GS","HD","IBM","INTC","JNJ","JPM","KO","MCD","MMM","MRK","MSFT","NKE","PFE","PG","TRV","UNH","UTX","VZ","WMT","XOM")
tags<-c("sd1t8")
label<-c("Symbol","Date","Target")

# Base url       
base<-"http://download.finance.yahoo.com/d/quotes.csv?s="
url<-paste(base,paste(tickers, collapse="+"),"&f=",tags,"&e=.csv",sep="")

# Export data in a csv file in the working  directory
download.file(url, destfile ="targets.csv", method = "libcurl")

# Read the data from csv file in a data frame format
url.data <-read.csv(file="targets.csv",sep=",",header=FALSE,stringsAsFactors=FALSE)
df <- data.frame(matrix(unlist(url.data), nrow=length(tickers), byrow=F),stringsAsFactors=FALSE)

# Name the data frame and convert dates in a Date format
names(df)<-label
df$Date <- as.Date(df$Date, "%m/%d/%Y")

# View the data in a spreadsheet-style
View(df)

######### The End ##############




Yahoo Tags are as follows:

       a       Ask 
       a2 Average Daily Volume
       a5 Ask Size
       b Bid
       b2 Ask (Real-time)
       b3 Bid (Real-time)
       b4 Book Value
       b6 Bid Size
       c Change & Percent Change
       c1 Change
       c3 Commission
       c4 Currency
       c6 Change (Real-time)
       c8 After Hours Change (Real-time)
       d Dividend/Share
       d1 Last Trade Date
       d2 Trade Date
       e Earnings/Share
       e1 Error Indication (returned for symbol changed / invalid)
       e7 EPS Estimate Current Year
       e8 EPS Estimate Next Year
       e9 EPS Estimate Next Quarter
       f0 Trade Links Additional
       f6 Float Shares
       g Day's Low
       g1 Holdings Gain Percent
       g3 Annualized Gain
       g4 Holdings Gain
       g5 Holdings Gain Percent (Real-time)
       g6 Holdings Gain (Real-time)
       h Day's High
       i       More Info
       i5 Order Book (Real-time)
       j 52-week Low
       j1 Market Capitalization
       j2 Shares Outstanding
       j3 Market Cap (Real-time)
       j4 EBITDA
       j5 Change From 52-week Low
       j6 Percent Change From 52-week Low
       k 52-week High
       k1 Last Trade (Real-time) With Time
       k2 Change Percent (Real-time)
       k3 Last Trade Size
       k4 Change From 52-week High
       k5 Percent Change From 52-week High
       l Last Trade (With Time)
       l1 Last Trade (Price Only)
       l2 High Limit
       l3 Low Limit
       m Day's Range
       m2 Day's Range (Real-time)
       m3 50-day Moving Average
       m4 200-day Moving Average
       m5 Change From 200-day Moving Average
       m6 Percent Change From 200-day Moving Average
       m7 Change From 50-day Moving Average
       m8 Percent Change From 50-day Moving Average
       n       Name
       n4 Notes
       o Open
       p Previous Close
       p1 Price Paid
       p2 Change in Percent
       p5 Price/Sales
       p6 Price/Book
       q Ex-Dividend Date
       r P/E Ratio
       r1 Dividend Pay Date
       r2 P/E Ratio (Real-time)
       r5 PEG Ratio
       r6 Price/EPS Estimate Current Year
       r7 Price/EPS Estimate Next Year
       s Symbol
       s1 Shares Owned
       s6 Revenue
       s7 Short Ratio
       t1 Last Trade Time
       t6 Trade Links
       t7 Ticker Trend
       t8 1 yr Target Price
       v Volume
       v1 Holdings Value
       v7 Holdings Value (Real-time)
       w 52-week Range
       w1 Day's Value Change
       w4 Day's Value Change (Real-time)
       x Stock Exchange
       y Dividend Yield

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

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