2

I am attempting to write a formula that will return a stocks single day return, but I believe im having trouble with the data type of the periodReturn subset field

periodReturn(ticker,period='daily',subset='20161010::20161010') 

works but

dayReturn <- function(ticker,date) {

ticker <- c(MSFT)
date <- c(20161010)
dayreturn <- periodReturn(ticker,period='daily',paste("subset='",date,"::",date,"'"))
dayreturn
}

gives error

dayReturn(msft,20161010)

 daily.returns
Warning messages:
1: In as_numeric(YYYY) : NAs introduced by coercion
2: In as_numeric(MM) : NAs introduced by coercion
3: In as_numeric(DD) : NAs introduced by coercion
>

Thanks in advance for any advice!

FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67
Frank Drin
  • 1,613
  • 2
  • 13
  • 18

1 Answers1

1

You have a couple of syntax errors going on here inside your dayReturn function.

Here is reproducible code extracted from inside your function that will work:

library(quantmod)
getSymbols("MSFT")

ticker <- c(MSFT)
date <- c("20161010")
dayreturn <- periodReturn(ticker,period='daily',subset = paste0(date,"::",date,"'"))

Your errors:

  1. date wants to be a string, not a numeric number.
  2. Your string for the dates you want to subset over is incorrect. you want to use subset = "YYYYMMDD::YYYYMMDD" or (subset = "YYYY-MM-DD::YYYY-MM-DD") in side periodReturn.

Your function would work more correctly like this:

dayReturn <- function(ticker, date1 , date2) {
    dayreturn <- periodReturn(ticker,period='daily',subset = paste0(date1,"::",date2,"'"))
    dayreturn
}

dayReturn(MSFT, "20161010", "20161012")
# daily.returns
# 2016-10-10   0.004152284
# 2016-10-11  -0.014645107
# 2016-10-12  -0.001398811
FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67
  • Thank you so much, this fixed the issue. Really appreciate the time and assistance - – Frank Drin Dec 14 '16 at 16:57
  • @FrankDrin No worries. if you feel this answers your question (along with all the other questions you've asked SO), you might like to consider accepting the answer, and other people's answers to your other questions (the green tick). – FXQuantTrader Dec 16 '16 at 09:22
  • As you can see FXQuantTrader... still new to this :) Done, thanks for the heads up ! – Frank Drin Dec 16 '16 at 15:36