首页 > 解决方案 > 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.

标签: rquantmod

解决方案


完全在 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 倍之间。


推荐阅读