Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require(quantmod)
- #*****************************************************************************
- # ydata for more see yahooQF()
- # usage x=ydata("3983.HK")
- ydata<-function(aname){
- x=getQuote(aname,what=yahooQF(c("Name","Date","Last Trade (Price Only)","Change",
- "Days Range","52-week Range","Ex-Dividend Date","Dividend/Share",
- "Stock Exchange","P/E Ratio","PEG Ratio","Ticker Trend"
- )))
- # Remove html ascii tags from ticker trend column
- x$"Ticker Trend"=substr(x$"Ticker Trend", start=7, stop=12)
- return(x)
- }
- #*****************************************************************************
- # A portfolio
- codes=c("GOOG","0005.HK","BBD-B.TO")
- # some start date
- fromd=Sys.Date()-365*3
- # get some names for the stock codes
- tix=ydata(codes)
- # extract the names , remove any spaces and make all uppercase
- tickers=toupper(gsub("\\s","", tix$Name))
- # now remove any dots and commas
- tickers=gsub("\\.","",tickers)
- tickers=gsub("\\,","",tickers)
- # see what we get
- show(tickers)
- # setup setSymbolLookup and write data to distinct csv files
- for (i in 1:length(tickers)) {
- eval(parse(text=paste("setSymbolLookup(",tickers[i],"=list(name='",codes[i],"',src='yahoo' ))",sep="")))
- getSymbols(tickers[i],from=fromd)
- write.csv(file=paste(tickers[i],'.csv',sep=""), data.frame(get(tickers[i] )) )
- }
- # now read it back from csv
- # setup setSymbolLookup for csv read back and create a vector of close prices
- closePrices=xts()
- for (i in 1:length(tickers)) {
- eval(parse(text=paste("setSymbolLookup(",tickers[i],"=list(src='csv',format='%Y-%m-%d' ))",sep="") ))
- s=getSymbols(tickers[i])
- ss=get(s)
- closePrices=cbind(closePrices,Ad(ss))
- }
- # see if it worked
- # we show historical AdjustedClose prices
- tail(closePrices)
- # current data
- show(tix)
- #*************************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement