Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. ## loop thru list and use quantmod to calculate performance from 1/2/14 to 12/31/14
  2. for(i in 1:4881){
  3. ticker <- tickernames[i]
  4. getSymbols(ticker)
  5. Open <- ticker["2014-01-02",1]
  6. Close <- ticker["2014-12-31",4]
  7.  
  8. performance2014[i] = (Open - Close)/Open
  9. }
  10.  
  11. require(quantmod)
  12.  
  13. #Vector of symbols to fetch prices for
  14. symbols <- c('MSFT','SBUX','GOOGL')
  15.  
  16. #Initialize a list to store the fetched prices
  17. myList <- list()
  18.  
  19. #Loop through symbols, fetch prices, and store in myList
  20. myList <-lapply(symbols, function(x) {getSymbols(x,auto.assign=FALSE)} )
  21.  
  22. #Housekeeping
  23. names(myList) <- symbols
  24.  
  25. #Access MSFT prices
  26. myList[['MSFT']]
  27.  
  28. #Access SBUX prices
  29. myList[['SBUX']]
  30.  
  31. #Access GOOGL prices
  32. myList[['GOOGL']]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement