Home > Blockchain >  How Can I select dates when extracting Stock data from Yahoo Finance on R
How Can I select dates when extracting Stock data from Yahoo Finance on R

Time:11-16

I hope everyone is doing great! I'm currently learning how to use R. I'm trying to download Stock DATA from Yahoo Finance. In this case, I'm trying just to get closing prices from these securities. However, I'd like to extract only daily data from let's day September 1st 2022 to November 13rd 2022. How could I do that?

So far I got this:

symbols <- c("SPY", "JNK", "EEM", "EMB", "TLT", "USO")

getSymbols(symbols)
ClosePrices <- do.call(merge, lapply(symbols, function(x) Cl(get(x))))
head(ClosePrices)

Thank you in advance for your help! Eduardo

CodePudding user response:

Here is one way:

library(quantmod)

symbols <- c("SPY", "JNK", "EEM", "EMB", "TLT", "USO")
getSymbols(symbols)

ClosePrices <- do.call(merge, lapply(symbols, function(x)  Cl(get(x))[seq.Date(
  from = as.Date("2022-09-01"), 
  to   = as.Date("2022-11-13"), 
  by   = "days")]))

head(ClosePrices)
#>            SPY.Close JNK.Close EEM.Close EMB.Close TLT.Close USO.Close
#> 2022-09-01    396.42     91.56     39.12     84.78    109.60     70.95
#> 2022-09-02    392.24     91.59     38.76     85.08    110.22     71.43
#> 2022-09-06    390.76     91.38     38.30     84.30    107.49     71.42
#> 2022-09-07    397.78     92.57     38.64     85.58    109.19     67.62
#> 2022-09-08    400.38     92.95     38.43     85.61    108.07     68.09
#> 2022-09-09    406.60     93.35     39.00     86.13    108.31     71.10

Created on 2022-11-14 with reprex v2.0.2

CodePudding user response:

Here's how I do it:

symbols <- c("SPY", "JNK", "EEM", "EMB", "TLT", "USO")

# Environment to hold data
myData <- new.env()

# Tell getSymbols() to load the data into 'myData'
getSymbols(symbols, env = myData)

# Combine all the close prices into one xts object
ClosePrices <- do.call(merge, lapply(myData, Cl))

# Now use xts-style subset to get the date range you want
ClosePriceSubset <- ClosePrices["2022-09-01/2022-11-13"]

head(ClosePriceSubset)
##            TLT.Close EEM.Close USO.Close EMB.Close JNK.Close SPY.Close
## 2022-09-01    109.60     39.12     70.95     84.78     91.56    396.42
## 2022-09-02    110.22     38.76     71.43     85.08     91.59    392.24
## 2022-09-06    107.49     38.30     71.42     84.30     91.38    390.76
## 2022-09-07    109.19     38.64     67.62     85.58     92.57    397.78
## 2022-09-08    108.07     38.43     68.09     85.61     92.95    400.38
## 2022-09-09    108.31     39.00     71.10     86.13     93.35    406.60

tail(ClosePriceSubset)
##            TLT.Close EEM.Close USO.Close EMB.Close JNK.Close SPY.Close
## 2022-11-04     94.22     36.20     76.82     80.41     89.33    376.35
## 2022-11-07     93.28     36.22     76.56     80.38     89.33    379.95
## 2022-11-08     94.30     36.48     74.47     80.94     89.15    382.00
## 2022-11-09     94.61     35.85     71.67     79.85     88.09    374.13
## 2022-11-10     98.25     37.15     72.24     83.52     90.84    394.69
## 2022-11-11     97.89     38.16     74.38     83.50     91.16    398.51
  • Related