Cluracan
Cluracan

Reputation: 1

Using quant package with R to pull daily stock returns using a For Loop

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

Google

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

Facebook

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

Upvotes: 0

Views: 498

Answers (1)

Ogustari
Ogustari

Reputation: 159

I can propose this solution, but here you do not really need a for loop.

I took as example Microsoft

#zoo and xts are need to allows quantmod library to work (I got an error message from R...
library(zoo)
library(xts)
library(quantmod)

MFSFT <- NULL
    for(i in 2012:2017){
      MSFT1 <- getSymbols( "MSFT", auto.assign = FALSE )
      MFSFT2012 <- MSFT1$MSFT.Close[as.character(i),]
      MFSFT <- rbind(MFSFT, MFSFT2012) 
    }

Your result will looks like this

head(MFSFT)
                MSFT.Close
2012-01-03      26.77
2012-01-04      27.40
2012-01-05      27.68
2012-01-06      28.11
2012-01-09      27.74
2012-01-10      27.84

However if you want a for cycle to go directly into all the stock in few line, you can do it this way

Stocks <- c("MSFT", "GOOG", "AAPL", "FB", "X", "SBUX", "TGT", "DNKN")
Daily_Stock <- NULL
for(i in Stocks){
  df1 <- getSymbols(i, auto.assign = FALSE)
  Close <- df1[as.character(2012:2017),4]
  Daily_Stock <- cbind(Daily_Stock, Close)
}

And you will obtain something like this

head(Daily_Stock)

               MSFT.Close GOOG.Close AAPL.Close FB.Close X.Close SBUX.Close TGT.Close DNKN.Close
2012-01-03      26.77   331.4626   58.74714       NA   28.17     22.645     51.12      24.74
2012-01-04      27.40   332.8922   59.06286       NA   28.44     23.085     50.00      24.73
2012-01-05      27.68   328.2745   59.71857       NA   27.79     23.180     48.51      25.17
2012-01-06      28.11   323.7963   60.34286       NA   27.30     23.360     48.95      25.47
2012-01-09      27.74   310.0678   60.24714       NA   26.78     23.295     48.57      25.21
2012-01-10      27.84   310.4065   60.46286       NA   27.29     23.410     48.79      25.42

Upvotes: 2

Related Questions