Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use endpoints function to get start points instead?

Tags:

r

xts

I have an xts object called Daily_Quotes that contains stock quotes. I'm using endpoints to get monthly stock quotes that I retrieved using getSymbols (from the quantmod package). I noticed that the endpoints function creates an index of the row that contains the last trading day for the particular month and assigns it to the new object for the specified date range. Is there anyway to get first trading day of the month instead?

# My code    
Monthly_Quotes <- Daily_Quotes[endpoints(Daily_Quotes,'months')]

What I tried doing was:

# This gave me the next day or 1st day of the next month
# or next row for the object.
endpoints(Daily_Quotes,'months') + 1

# So I applied this and it gave me 
# Error in `[.xts`(Daily_Quotes, endpoints(Daily_Quotes, "months") + 1) :
# subscript out of bounds
Monthly_Quotes <- Daily_Quotes[endpoints(Daily_Quotes,'months') + 1]

How do I attempt to solve this ?

like image 667
NewComer Avatar asked Nov 17 '14 21:11

NewComer


1 Answers

You can create a startpoints function like this

startpoints <- function (x, on = "months", k = 1) {
  head(endpoints(x, on, k) + 1, -1)
}
like image 147
GSee Avatar answered Nov 03 '22 02:11

GSee