【问题标题】:Daily Arithmetic and daily Geometric Averages for each year (AAPL)每年的每日算术平均值和每日几何平均值 (AAPL)
【发布时间】:2017-09-20 14:15:48
【问题描述】:

我正在尝试使用 R 为 APPL 股票数据获取每年的每日算术和每日几何平均值。我对此的实现将是最后几行中的 periodReturn 函数,但它似乎不起作用, 并给出错误:“...”在不正确的上下文中使用。

如何修改我的代码以获得所需的输出?一些帮助将不胜感激。

# Get historical price data (daily)
getSymbols('AAPL', from = "2005-01-01")

AAPLdaily <- as.data.frame(AAPL)
head(AAPLdaily)

?to.period

# AAPLweekly <- to.weekly(to.weekly(AAPL, indexAt = 'endof'))
# AAPLweekly <- as.data.frame(AAPLweekly)
# Better to do it in one step like this:
AAPLweekly <- as.data.frame( to.weekly(AAPL, indexAt = 'endof') )
head(AAPLweekly)

AAPLmonthly <- as.data.frame( to.monthly(AAPL, indexAt = 'endof') )
head(AAPLmonthly)

AAPLyearly <- as.data.frame( to.yearly(AAPL, indexAt = 'endof') )
AAPLyearly

# Another way to do this
AAPLweekly1 <- as.data.frame(to.period(AAPL, period = 'weeks', indexAt = 'endof'))
head(AAPLweekly1)
AAPLmonthly1 <- as.data.frame(to.period(AAPL, period = 'months', indexAt = 'endof'))
head(AAPLmonthly1)
AAPLyearly1 <- as.data.frame(to.period(AAPL, period = 'years', indexAt = 'endof'))
head(AAPLyearly1)


########## Another possible method  ######### 

# Change to data.frames
AAPL = as.data.frame(AAPL)
head(AAPL)

# Get Dates
dates <- as.Date(row.names(AAPL))
head(dates)

# Create a cloumn in APPL data frame with the dates
AAPL$dates <- as.Date(row.names(AAPL))

?aggregate
?substr

# Last Day of month

lastDayofMonth <- aggregate(AAPL["dates"], list(month = substr(AAPL$dates, 1, 7)), max)
head(lastDayofMonth)
AAPLmonth <- AAPL[dates %in% lastDayofMonth$dates, ]
head(AAPLmonth)

# Last day of year

lastDayofYear <- aggregate(AAPL["dates"], list(month = substr(AAPL$dates, 1, 4)), max)
head(lastDayofYear)
AAPLyear <- AAPL[dates %in% lastDayofYear$dates, ]
AAPLmonth

AAPLdaily <- as.data.frame( to.daily(AAPL, indexAt = 'endof') )
AAPLdaily

dailyReturn(AAPLdaily)

periodReturn(AAPL,
             period='daily',
             subset=NULL,
             type='arithmetic',
             leading=TRUE,
             ...
             )

【问题讨论】:

  • yearlyReturn(AAPLdaily)这是你需要的吗?
  • @GeorgeSotiropoulos 是的,如果每年的输出是每个相应年份的每日回报,通过算术平均值计算。但是,这并没有给出每年的几何平均值。

标签: r return quantmod yahoo-finance stock


【解决方案1】:

如果您要求的是每年、每月、每周的算术/几何回报,您所要做的就是:

    getSymbols('AAPL',from= '2010-01-01')
    ROC(AAPL[endpoints(AAPL,on = 'years'),"AAPL.Adjusted"],type='discrete’)

2012-12-31    0.32566879
2013-12-31    0.08069493
2014-12-31    0.40622488
2015-12-31   -0.03013708
2016-12-30    0.12480425
2017-09-20    0.36428706

对于几何(对数)返回,将 ROC 参数更改为“连续”:

ROC(AAPL[endpoints(AAPL,on = 'years'),"AAPL.Adjusted"],type='continuous’)

2012-12-31    0.28191708
2013-12-31    0.07760429
2014-12-31    0.34090873
2015-12-31   -0.03060053
2016-12-30    0.11760902
2017-09-20    0.31063199

对于其他时间段,将 endpoints 参数更改为 monthsweeks

【讨论】:

  • 即使使用 quantmod,我也无法获得上述结果。以下是我收到的输出: > getSymbols('AAPL',from= '2010-01-01') [1] "AAPL" > ROC(AAPL[endpoints(AAPL,on = 'years'),"AAPL.已调整"],type='continuous') +
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 2018-07-01
  • 2021-07-30
  • 2019-03-07
  • 1970-01-01
  • 2020-06-04
  • 1970-01-01
相关资源
最近更新 更多