1

我正在尝试计算股票的每日股息收益率(sum of all dividends in the past year / current stock price)。我有季度股息金额:

Date            Dividend Amount($)
1/1/2001             .56
4/2/2001             .56
7/1/2001             .57
10/3/2001            .57
...                  ....

和收盘价:

Date              Closing Price($)
    1/1/2001             100.54
    1/2/2001             101.01
    1/3/2001             100.44
    ...                   ...

问题是股息日期是季度,股票日期是工作日。

我想结束这样的事情:

Date              Closing Price($)    Dividend Amount($)
    1/1/2001             100.54                2.25
    1/2/2001             101.01                2.25
    1/3/2001             100.44                2.25
    ...                   ...                  ...

有没有可以处理这个问题的包,或者我最好使用 ifelse / loop 结构?

4

2 回答 2

1

这是@JoshuaUlrich 建议的解决方案示例。

require("quantmod") 
s <- "SPY"
# get Closing prices
p <- Cl(getSymbols(s, src='yahoo', auto.assign=FALSE, from='2010-01-01', 
                   to='2012-06-20'))
# get dividends
div <- getDividends(s, from=start(p), to=end(p))
ydiv <- runSum(div, n=4) # rolling sum of last 4 quarters
out <- na.locf(merge(p, ydiv, all=TRUE)) # merge and fill in NAs with previous values
out$yld <- out[, 2] / out[, 1] # "current yield"
tail(out)
#           SPY.Close  ydiv        yld
#2012-06-13    132.07 2.637 0.01996668
#2012-06-14    133.47 2.637 0.01975725
#2012-06-15    134.14 2.697 0.02010586
#2012-06-18    134.40 2.697 0.02006696
#2012-06-19    135.70 2.697 0.01987472
#2012-06-20    135.48 2.697 0.01990700
于 2012-07-02T00:23:28.227 回答
0

如果股息支付不是严格按季度支付,则以下脚本可以计算股息收益率的大致历史记录。该脚本假设在一个季度内,每日股息支付是恒定的。由于一个季度内的股息支付日期可能会有所不同,因此开始日期和截止日期需要是季度的开始/结束。

EGAS 过去按季度支付股息,后来改为按月分配。

# script calculates and plots history of the 
# dividend yield of a given stock symbol

require(zoo)
require(quantmod)

sym <- "EGAS"
from <- "1999-01-01"
to <- "2013-06-30"

# get price, dividends and splits from finance.yahoo.com
TS <- getSymbols(sym, from=from, to=to, auto.assign=FALSE)
div <- getDividends(sym, from=from, to=to)
splits <- getSplits(sym, from=from, to=to)

# adjust for splits: code copied and adapted from quantmod::adjustOHLC
if (is.xts(splits) && is.xts(div) && nrow(splits) > 0 && nrow(div) > 0) {
  div <- div * 1/adjRatios(splits = merge(splits, index(div)))[, 1]
  colnames(div) <- "Dividend"
}
ratios <- adjRatios(splits, div, Cl(TS))
Adjusted <- Cl(TS) * ratios[,1]
TS <- structure(cbind((ratios[,1] * (Op(TS) - Cl(TS)) + Adjusted), 
                      (ratios[,1] * (Hi(TS) - Cl(TS)) + Adjusted), 
                      (ratios[,1] * (Lo(TS) - Cl(TS)) + Adjusted), 
                      Adjusted, 
                      if (has.Vo(TS)) Vo(TS)
                        else NULL, 
                      if (has.Ad(TS)) Ad(TS)
                        else NULL), 
                .Dimnames = list(NULL, colnames(TS)))

# assume the daily dividend "payment" to be constant within a quarter
TSComb <- merge(Cl(TS), div, fill=0)
DailyDiv <- structure(apply.quarterly(TSComb[,2], mean), 
                      .Dimnames = list(NULL, "DailyDividend"))
TSComb <- na.locf(merge(TSComb, DailyDiv), fromLast=TRUE)
TSComb <- merge(TSComb, structure(runSum(TSComb[,"DailyDividend"], n=250), 
                      .Dimnames = list(NULL, "YearlyDividend")))
TSComb <- merge(TSComb, structure(TSComb[,"YearlyDividend"]/TSComb[,1]*100, 
                      .Dimnames = list(NULL, "Yield")))

plot(as.zoo(cbind(TSComb[,"Yield"], div)),
       main=paste(sym, "Dividend Yield"),
       heights=c(2,1),
       ylim=list(c(0,max(TSComb[,"Yield"], na.rm=TRUE)), 
                 c(0,max(div))),
       xlab=NULL, ylab=c("Yield", "Dividend"),
       col="blue",
       las=1, type=c("l", "p"), yax.flip=1)
# end of script

该股票一直在分配大量股息: 股息收益率和支付随时间变化的图

于 2013-09-17T22:02:33.323 回答