0

Dear R coding community,

I am currently writing my master's theses but I struggle with combining multiple rolling annualized returns. To be more specific, I try to overlay 3 different portfolio's returns with the command 'chart.RollingPerformance'. The code for 1 portfolio is as follows:

chart.RollingPerformance(R=bt_benchmarkM_returns, width=6,
                         main='Rolling 6-month annualized return',
                         FUN="Return.annualized",legend.loc="bottomleft")

I tried the 'lines' command:

lines(pf_mad_returnmonthly, col="red")

and also

lines(chart.RollingPerformance(R=pf_bl_returnmonthly, width=6, 
                                 colorset=rich8equal,                              
                                 FUN="Return.annualized", 
                                 legend.loc="bottomleft",
                                 main="BLCOP - rolling 6-month annualized 
                                 return"))

but none of them yielded the result I desired.

I hope there is anybody helping me with this issue, as I am rather new to R, but still have little experience due to the coding already implemented.

KR Patrick

  • If your goal is to plot multiple lines in the same chart try using ggplot. Have a look at this post: https://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph – S. Elzwawi Aug 22 '18 at 12:06
  • Actually, it is my goal. I also read about the package 'ggplot2' several times, but I have not figured out yet how to use ggplot to add series to my 'chart.rollingperformance' command from 'PerformanceAnalytics' to add multiple lines (e.g. to reflect rolling return analysis of all portfolios in one single graph). This might sound stupid, but would you be so kind to show me how it is done? I am still not aware of such basics in R. Thank you KR Patrick – Bohnenjim Aug 22 '18 at 12:23
  • It would be useful to see a glimpse of how the data frame looks like (by using head(pf_bl_returnmonthly)) and to see the output (or error message) you are getting (if any). – S. Elzwawi Aug 22 '18 at 12:52

1 Answers1

0

Well, here's my data:

    > head(bt_benchmarkM_returns)
GMT
            EUROSTOXX50
2016-07-31  0.043990038
2016-08-31  0.010823293
2016-09-30 -0.006910022
2016-10-31  0.017656820
2016-11-30 -0.001191357
2016-12-31  0.078289790
> head(pf_bl_returnmonthly)
           portfolio.returns
2016-08-31       0.009311298
2016-09-30      -0.001002361
2016-10-31       0.015371851
2016-11-30      -0.012162073
2016-12-31       0.073918208
2017-01-31      -0.010528706
> head(pf_mad_returnmonthly)
           portfolio.returns
2016-08-31       0.025284203
2016-09-30       0.011681711
2016-10-31       0.008464807
2016-11-30       0.006796675
2016-12-31       0.058598377
2017-01-31       0.007101811

It consists of 2 time series of monthly returns obtained by portfolio optimizations I created previously. If i try the following code:

chart.RollingPerformance(R=bt_benchmarkM_returns, width=6,
                         main='Rolling 6-month annualized return',
                         FUN="Return.annualized",legend.loc="bottomleft")
  lines(pf_mad_returnmonthly, col="red")
  lines(pf_bl_returnmonthly, col="blue")
  legend("bottomleft", legend=c("BLCOP", "MAD", "Benchmark"),
           col=c("blue", "red", "black"), lty=1, cex=0.8)

then I will obtain a chart with three lines, but only the 'bt_benchmarkM_returns' line is of a rolling annualized return while the other two are not.

Further, I tried following code:

> chart.RollingPerformance(R=bt_benchmarkM_returns, width=6,
+                          main='Rolling 6-month annualized return',
+                          FUN="Return.annualized",legend.loc="bottomleft")
>   lines(chart.RollingPerformance(R=pf_mad_returnmonthly, width=6, colorset=rich8equal,
+                                FUN="Return.annualized", legend.loc="bottomleft"))
Error in as.double(y) : 
  cannot coerce type 'environment' to vector of type 'double'
>   lines(chart.RollingPerformance(R=pf_bl_returnmonthly, width=6, colorset=rich8equal,
+                                  FUN="Return.annualized", legend.loc="bottomleft"))
Error in as.double(y) : 
  cannot coerce type 'environment' to vector of type 'double'
>   legend("bottomleft", legend=c("BLCOP", "MAD", "Benchmark"),
+            col=c("blue", "red", "black"), lty=1, cex=0.8)

than there is only one line plotted on the graph.

The whole code was copy&paste from the console, therefore, only in the second case, there is an error.

I hope this will help you.

KR Patrick