I am a complete beginner. This was an assignment and the goal is to create a For Loop to pull daily stock returns for each of these stocks, starting from 2012 until today.
I could get the returns for each year individually by simply pulling those years, but I'm not sure how to create a For Loop. I tried creating one but could only get it to work by repeating code for each year.
The code below is what I started with to pull those years, but then I would repeat it for each year after that. My process seemed completely redundant and inefficient.
MSFT = Microsoft
GOOG = Google
AAPL = Apple Inc.
FB = Facebook Inc.
X = United States Steel Inc.
SBUX = Starbucks Inc.
TGT = Target Stores Inc.
DNKN = Dunkin' Donuts
library(quantmod)
setwd("C:/TEMP")
Microsoft
MSFT1 <- getSymbols( "MSFT", auto.assign = FALSE )
MFSFT2012 <- MSFT1$MSFT.Close['2012']
MFSFT2012
GOOG1 <- getSymbols( "GOOG", auto.assign = FALSE )
GOOG2012 <- GOOG1$GOOG.Close['2012']
GOOG2012
APPLE
AAPL1 <- getSymbols( "AAPL", auto.assign = FALSE )
AAPL2012 <- AAPL1$AAPL.Close['2012']
AAPL2012
FB1 <- getSymbols( "FB", auto.assign = FALSE )
FB2012 <- FB1$FB.Close['2012']
FB2012
U.S. Steel
XSTEEL1 <- getSymbols( "X", auto.assign = FALSE )
XSTEEL12012 <- XSTEEL1$X.Close['2012']
XSTEEL12012
Starbucks
SBUX1 <- getSymbols( "SBUX", auto.assign = FALSE )
SBUX12012 <- SBUX1$SBUX.Close['2012']
SBUX12012
Target
TGT1 <- getSymbols( "TGT", auto.assign = FALSE )
TGT12012 <- TGT1$TGT.Close['2012']
TGT12012
Dunkin Donuts
DNKN1 <- getSymbols( "DNKN", auto.assign = FALSE )
DNKN12012 <- DNKN1$DNKN.Close['2012']
DNKN12012