Stupid_Intern
Stupid_Intern

Reputation: 3450

How to return weekly data starting from Monday in getSymbols

If run this getSymbols function on Saturday it will return data for only completed weeks i.e. it will omit current week data and will only return data till 15th May 2021

 d <- as.data.frame(
      getSymbols(
        paste("WIPRO" , ".NS", sep = ""),
        from ="2000-01-01", 
        periodicity = "weekly",
        return.class = 'zoo',
        env = NULL
      )
    )

If I want this function to return data till today or whichever is latest, how do I modify this function?

If I add to = "2021-05-22" parameter, it returns the record but it is filled with NA.

Upvotes: 0

Views: 628

Answers (1)

phiver
phiver

Reputation: 23598

You can work around this by using the daily data and then rolling it up to weekly.

library(quantmod)

daily.x <- getSymbols(
  paste("WIPRO" , ".NS", sep = ""),
  from ="2000-01-01", 
  auto.assign = FALSE
)

weekly.x <- to.period(daily.x, period = "weeks")
tail(weekly.x)
           x.Open x.High  x.Low x.Close  x.Volume x.Adjusted
2021-04-16 450.00 473.65 412.60  469.20 171627114     469.20
2021-04-23 463.00 494.50 461.10  475.70 117757478     475.70
2021-04-30 479.40 511.80 477.00  492.75  62896349     492.75
2021-05-07 487.95 516.55 477.80  515.25  67638876     515.25
2021-05-14 517.60 528.50 492.75  498.45  34864957     498.45
2021-05-21 498.45 517.80 495.00  512.70  34113599     512.70

Upvotes: 2

Related Questions