r - Get stock return over a specific time period
问题描述
Does someone have a good idea how to get the return for a stock for a specific time period e.g. AAPL from 2000-01-01 to 2020-01-01. I know there is something like
periodReturn(AAPL,period='yearly',subset='2000::')
But this is giving me the yearly returns. I actually just want the whole return.
解决方案
完全在 quantmod 函数中:
library(quantmod)
aapl <- getSymbols("AAPL", from = "2000-01-01", auto.assign = F)
# first and last get the first and last entry in the timeseries.
# select the close values
# Delt calculates the percent difference
Delt(Cl(first(aapl)), Cl(last(aapl)))
Delt.0.arithmetic
2020-07-08 94.39573
或者简单的数学:
as.numeric(Cl(last(aapl))) / as.numeric(Cl(first(aapl))) - 1
[1] 94.39573
我正在取第一个条目的接近值。您可能会选择当天的开盘价、最高价或最低价。这对 2000 年从低点 3.63 到高点 4.01 的回报有一些影响。根据您的选择,回报将在您的起始资本的 104 到 93.9 倍之间。
推荐阅读
- python - 导入无法解决 Pylance 问题
- python - Python 3 urllib 请求代理隧道标头
- javascript - 为什么在“Hello World”下方显示“未定义”?
- css - 试图用 json "npm run sass" 中的 css 编译我的 sass
- controller - 无法使用 bsDatePicker 在 Angular 中编辑日历月视图
- c# - WPF 在后面的代码中设置 DataTemplate 网格大小(ResourceDictionary)
- javascript - 没有默认滚动行为的 MVC PHP 软件中的 JavaScript SCROLLING 平滑 - 像滚动行为“自动”这样的硬直接跳跃不起作用
- pycharm - 我在 pycharm Interpreter 中找不到安装包
- azure-devops - 如何创建数组 | 在 Azure Pipelines (.yml) 中动态列出
- reactjs - 如何更改我在 ReactJS 中动态状态的余额