0
$\begingroup$

I downloaded adjusted closing price using quantmod for a set of securities. I want to calculate daily/weekly/monthly return for all securities. Usual dailyReturn, weeklyReturn etc not working. What do I need to do? Here is my code.

tickers <- c('FB','MMM')
data_env <- new.env()
getSymbols(Symbols = tickers, env = data_env)
tempPort <- do.call(merge, eapply(data_env, Ad))

head(tempPort )
           MMM.Adjusted FB.Adjusted
2007-01-03     57.00983          NA
2007-01-04     56.78401          NA
2007-01-05     56.39790          NA
2007-01-08     56.52174          NA
2007-01-09     56.58731          NA
2007-01-10     56.71116          NA

head(weeklyReturn(tempPort,  type = 'log', leading=TRUE))
           weekly.returns
2012-05-18   -0.010791856
2012-05-25    0.015093078
2012-06-01   -0.023027534
2012-06-08    0.037315263
2012-06-15    0.016605617
2012-06-22   -0.007000966

It returned garbage. How to handle this?

$\endgroup$
0
$\begingroup$

weeklyReturnis for univariate series.

Try this

tail(do.call("merge",apply(tempPort,2,weeklyReturn,type = 'log', leading=TRUE)))

#             weekly.returns weekly.returns.1
# 2019-04-18    0.006692682      -0.00458900
# 2019-04-26   -0.132748543       0.07148027
# 2019-05-03   -0.034230826       0.02057131
# 2019-05-10   -0.051856081      -0.03715810
# 2019-05-17   -0.039257136      -0.01627267
# 2019-05-24   -0.009381505      -0.02314769
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.