如何解决R图表RollingPerformance股票价格无法输出正确的y轴
这是我读取一堆ETF的代码。我尝试使用chart.RollingPerformance返回ETF,但似乎无法输出具有正确y轴的显示?
我用这些读取数据:
mystocks <- new.env(hash=TRUE)
getSymbols(c("QQQ","XBI","VYM","VOO"),env=mystocks,from ="2016-01-04",to ="2020-10-22")
etf <- do.call(cbind,eapply(mystocks,Cl))
str(etf)
head(etf)
An ‘xts’ object on 2016-01-04/2020-10-21 containing:
Data: num [1:1210,1:4] 184 185 182 178 176 ...
- attr(*,"dimnames")=List of 2
..$ : NULL
..$ : chr [1:4] "VOO.Close" "QQQ.Close" "XBI.Close" "VYM.Close"
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
$ src : chr "yahoo"
$ updated: POSIXct[1:1],format: "2020-10-23 15:38:16"
VOO.Close QQQ.Close XBI.Close VYM.Close
2016-01-04 184.31 109.50 67.83 65.95
2016-01-05 184.64 109.31 67.22 66.28
2016-01-06 182.30 108.26 64.35 65.41
2016-01-07 177.86 104.87 61.82 64.00
2016-01-08 175.97 104.01 60.51 63.28
2016-01-11 175.99 104.33 57.14 63.35
我用以下代码进行绘图。我已附上图片以供参考。
etf_returns_discrete = Return.calculate(etf,method = c("discrete"))
etf_returns_log = Return.calculate(etf,method = c("log"))
charts.RollingPerformance(etf_returns_discrete,Rf=.03/12,main="Rolling 12-Month Performance",legend.loc="topleft")
提前谢谢!
解决方法
之所以会这样,是因为chart.RollingPerformance中width的默认值= 12,并且您使用的是每日收益。由于仅用12天就可以对收益进行年度化计算,因此您有时会获得> 100%的年度化收益。如果您使用大约一年的时间(252个工作日),则会获得理想的结果。只需更改对图表的调用即可。RollingPerformance为此:
charts.RollingPerformance(R = etf_returns_discrete,width = 252,Rf = 0,"Rolling 12-Month Performance")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。