Advertisement
Guest User

Untitled

a guest
Sep 17th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. require(quantmod)
  2.  
  3. #*****************************************************************************
  4. # ydata for more see yahooQF()
  5. # usage x=ydata("3983.HK")
  6.  
  7. ydata<-function(aname){
  8. x=getQuote(aname,what=yahooQF(c("Name","Date","Last Trade (Price Only)","Change",
  9. "Days Range","52-week Range","Ex-Dividend Date","Dividend/Share",
  10. "Stock Exchange","P/E Ratio","PEG Ratio","Ticker Trend"
  11. )))
  12. # Remove html ascii tags from ticker trend column
  13. x$"Ticker Trend"=substr(x$"Ticker Trend", start=7, stop=12)
  14. return(x)
  15. }
  16.  
  17. #*****************************************************************************
  18.  
  19. # A portfolio
  20. codes=c("GOOG","0005.HK","BBD-B.TO")
  21. # some start date
  22. fromd=Sys.Date()-365*3
  23. # get some names for the stock codes
  24. tix=ydata(codes)
  25. # extract the names , remove any spaces and make all uppercase
  26. tickers=toupper(gsub("\\s","", tix$Name))
  27. # now remove any dots and commas
  28. tickers=gsub("\\.","",tickers)
  29. tickers=gsub("\\,","",tickers)
  30.  
  31. # see what we get
  32. show(tickers)
  33.  
  34. # setup setSymbolLookup and write data to distinct csv files
  35. for (i in 1:length(tickers)) {
  36. eval(parse(text=paste("setSymbolLookup(",tickers[i],"=list(name='",codes[i],"',src='yahoo' ))",sep="")))
  37. getSymbols(tickers[i],from=fromd)
  38. write.csv(file=paste(tickers[i],'.csv',sep=""), data.frame(get(tickers[i] )) )
  39. }
  40.  
  41.  
  42. # now read it back from csv
  43. # setup setSymbolLookup for csv read back and create a vector of close prices
  44. closePrices=xts()
  45. for (i in 1:length(tickers)) {
  46. eval(parse(text=paste("setSymbolLookup(",tickers[i],"=list(src='csv',format='%Y-%m-%d' ))",sep="") ))
  47. s=getSymbols(tickers[i])
  48. ss=get(s)
  49. closePrices=cbind(closePrices,Ad(ss))
  50.  
  51. }
  52.  
  53. # see if it worked
  54. # we show historical AdjustedClose prices
  55. tail(closePrices)
  56.  
  57. # current data
  58. show(tix)
  59.  
  60. #*************************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement