Daily dividend yield

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Daily dividend yield

FJ M

I'm trying to calculate the current yield for MSFT using the most recent dividend and daily closing price of MSFT. I've tried a couple of approaches that don't work, any suggestions would be appreciated.
 
1) I have downloaded both the dividends and the OHLCVAdjC data successfully. When I merge the data with fill=0.0 and export the merged data with write.csv, the date is replaced with the index from 1 to 52, first few lines:
 














MSFT.Open
MSFT.High
MSFT.Low
MSFT.Close
MSFT.Volume
MSFT.Adjusted
Divs

1
29.59
29.65
29.21
29.41
31721800
29.22
0

2
29.21
29.53
28.97
29.19
39520500
29
0
 
2) Divs contains the dividend date and the 1 dividend 0.20. Stock contains theOHLVAdjC data for Microsoft. I can print the dates using Divs[1,0] and Stock[1,0] but a comparison of those dates always results in an error:
 
>
> for (j in 1:d) {
+ for (i in 1:n) {
+ print(c(Stock[i,0],Divs[j,0]))
+ if ((Stock[i,0]) == (Divs[j,0]))  Current_Div <- Divs[j,1]
+ Stock$MSFT.Adjusted[i]<-Current_Div * Divs_Per_Year
+ }
+ }
2012-08-01
2012-08-14
Error in if ((Stock[i, 0]) == (Divs[j, 0])) Current_Div <- Divs[j, 1] :
  argument is of length zero
Execution halted

Code follows:
 
library(quantmod)
## Div frequency
Divs_Per_Year<-4
ticker<-"MSFT"
startDate=as.Date("2012-08-01")
endDate=as.Date("2012-10-13")
ticker
startDate
endDate
Divs<-getDividends(ticker, from = startDate, to = endDate)
d<-nrow(Divs)
d
Divs
Stock<-getSymbols.yahoo(ticker, from = startDate, to = endDate, src = "yahoo", verbose=TRUE, auto.assign=FALSE)
n<-nrow(Stock)
n
Stock_Divs<-merge(Stock,Divs,fill=0.0)
Stock_Divs
Divs_Per_Year
Current_Div<-Divs[1,1]
Current_Div
d<-nrow(Divs)
d
write.csv(Stock_Divs, file = "Stock_Divs.csv", append = FALSE, quote = TRUE, sep = " ")
for (j in 1:d) {
for (i in 1:n) {
print(c(Stock[i,0],Divs[j,0]))
if ((Stock[i,0]) == (Divs[j,0]))  Current_Div <- Divs[j,1]
Stock$MSFT.Adjusted[i]<-Current_Div * Divs_Per_Year
}
}
 
 
Frank
Chicago, IL    
        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Re: Daily dividend yield

gsee
Hi Frank,

> 1) I have downloaded both the dividends and the OHLCVAdjC data successfully. When I merge the data with fill=0.0 and export the merged data with write.csv, the date is replaced with the index from 1 to 52, first few lines:

I suggest using write.zoo().  But, you can also consult ?write.table
and the R Data Import/Export manual
(http://cran.r-project.org/doc/manuals/R-data.html)

> 2) Divs contains the dividend date and the 1 dividend 0.20. Stock contains theOHLVAdjC data for Microsoft. I can print the dates using Divs[1,0] and Stock[1,0] but a comparison of those dates always results in an error:

What looks like Dates to you is actually a zero-width xts object.

This is the line that's giving you trouble

    if ((Stock[i,0]) == (Divs[j,0]))  Current_Div <- Divs[j,1]

If you are trying to compare indexes, maybe something along these
lines would work better.

    index(Stock[i]) == index(Divs[j])

#----
> if (Stock[i, 0] == Divs[1, 0]) print("equal")
Error in if (Stock[i, 0] == Divs[1, 0]) print("equal") :
  argument is of length zero
> Divs[1, 0]

2012-08-14
> length(Divs[1, 0])
[1] 0
> index(Divs[1])
[1] "2012-08-14"
> if (index(Stock[i]) == index(Divs[1])) print("equal") # no error
#----

That said, I'm fairly confident that you should not use a (double) for
loop for this.  I can't quite tell from your code what you're trying
to do, but if you provide your desired output, I'm sure we can come up
with a vectorized solution.

For example, if you want to multiply the last quarterly dividend
amount by 4 and use that value every day until the next dividend, you
can do the following:

    # Setup based on original question
    Divs_Per_Year<-4
    startDate=as.Date("2012-05-01")
    endDate=as.Date("2012-10-13")
    Divs<-getDividends(ticker, from = startDate, to = endDate)
    Stock <- getSymbols(ticker, from=startDate, to=endDate, src='yahoo',
                                   auto.assign=FALSE)
    # End setup

    na.locf(merge(Stock, Divs * Divs_Per_Year, retside=c(FALSE, TRUE)))

Or, if you want to do basically the same thing, but divide the current
estimate for the annual dividend amount by the most recent price in
order to calculate the current yield, you can do this:

    with(na.locf(merge(Stock, AnnDiv=Divs * Divs_Per_Year)), AnnDiv /
MSFT.Adjusted)

HTH,
Garrett

On Sat, Oct 13, 2012 at 10:29 PM, FJ M <[hidden email]> wrote:
>
> I'm trying to calculate the current yield for MSFT using the most recent dividend and daily closing price of MSFT. I've tried a couple of approaches that don't work, any suggestions would be appreciated.

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Re: Daily dividend yield

FJ M

Hey Garrett, Nice! Thanks, much appreciated and very helpful.  I'm using the first file to calculate 1-day returns adjusted for the dividend where the dividend is on the ex-dividend date, otherwise 0. It is the Yahoo Finance data plus one extra column. The second file adds the annualized dividend to the standard Yahoo file and the third file adds the current yield to the second file. The somewhat tightened up code follows. Warmest Regards, Frank
 library(quantmod)##
## Merge Date OHLC with dividend on ex-dividend date
##
ticker<-"MSFT"
Divs_Per_Year<-4
Divs<-getDividends(ticker)
Stock<-getSymbols.yahoo(ticker, from = start(Divs), src = "yahoo", verbose=TRUE, auto.assign=FALSE)
Stock_Divs<-merge(Stock,Divs,fill=0.0)
write.zoo(Stock_Divs, file = "Stock_Divs.csv", append = FALSE, quote = TRUE, sep = ",")##
## Calcuate current annual dividend and write it out for each date to a file
##
Annual_Div<-Divs * Divs_Per_Year
Stock_Annual_Dividend<-na.locf(merge(Stock, Annual_Div, retside=c(TRUE, TRUE)))
write.zoo(Stock_Annual_Dividend, file = "Stock_Annual_Dividend.csv", append = FALSE, quote = TRUE, sep = ",")##
## Calcuate current annual dividend yield for each date and write it out to a file
##
Current_Yield<-with(na.locf(merge(Stock, Annual_Div=Divs * Divs_Per_Year)), Annual_Div / MSFT.Adjusted)
Stock_Current_Yield<-na.locf((merge(Stock_Annual_Dividend, Current_Yield, retside=c(TRUE, TRUE))))
write.zoo(Stock_Current_Yield, file = "Stock_Current_Yield.csv", append = FALSE, quote = TRUE, sep = ",")
quit()

 > Date: Sun, 14 Oct 2012 21:39:26 -0500

> Subject: Re: [R-SIG-Finance] Daily dividend yield
> From: [hidden email]
> To: [hidden email]
> CC: [hidden email]
>
> Hi Frank,
>
> > 1) I have downloaded both the dividends and the OHLCVAdjC data successfully. When I merge the data with fill=0.0 and export the merged data with write.csv, the date is replaced with the index from 1 to 52, first few lines:
>
> I suggest using write.zoo().  But, you can also consult ?write.table
> and the R Data Import/Export manual
> (http://cran.r-project.org/doc/manuals/R-data.html)
>
> > 2) Divs contains the dividend date and the 1 dividend 0.20. Stock contains theOHLVAdjC data for Microsoft. I can print the dates using Divs[1,0] and Stock[1,0] but a comparison of those dates always results in an error:
>
> What looks like Dates to you is actually a zero-width xts object.
>
> This is the line that's giving you trouble
>
>     if ((Stock[i,0]) == (Divs[j,0]))  Current_Div <- Divs[j,1]
>
> If you are trying to compare indexes, maybe something along these
> lines would work better.
>
>     index(Stock[i]) == index(Divs[j])
>
> #----
> > if (Stock[i, 0] == Divs[1, 0]) print("equal")
> Error in if (Stock[i, 0] == Divs[1, 0]) print("equal") :
>   argument is of length zero
> > Divs[1, 0]
>
> 2012-08-14
> > length(Divs[1, 0])
> [1] 0
> > index(Divs[1])
> [1] "2012-08-14"
> > if (index(Stock[i]) == index(Divs[1])) print("equal") # no error
> #----
>
> That said, I'm fairly confident that you should not use a (double) for
> loop for this.  I can't quite tell from your code what you're trying
> to do, but if you provide your desired output, I'm sure we can come up
> with a vectorized solution.
>
> For example, if you want to multiply the last quarterly dividend
> amount by 4 and use that value every day until the next dividend, you
> can do the following:
>
>     # Setup based on original question
>     Divs_Per_Year<-4
>     startDate=as.Date("2012-05-01")
>     endDate=as.Date("2012-10-13")
>     Divs<-getDividends(ticker, from = startDate, to = endDate)
>     Stock <- getSymbols(ticker, from=startDate, to=endDate, src='yahoo',
>                                    auto.assign=FALSE)
>     # End setup
>
>     na.locf(merge(Stock, Divs * Divs_Per_Year, retside=c(FALSE, TRUE)))
>
> Or, if you want to do basically the same thing, but divide the current
> estimate for the annual dividend amount by the most recent price in
> order to calculate the current yield, you can do this:
>
>     with(na.locf(merge(Stock, AnnDiv=Divs * Divs_Per_Year)), AnnDiv /
> MSFT.Adjusted)
>
> HTH,
> Garrett
>
> On Sat, Oct 13, 2012 at 10:29 PM, FJ M <[hidden email]> wrote:
> >
> > I'm trying to calculate the current yield for MSFT using the most recent dividend and daily closing price of MSFT. I've tried a couple of approaches that don't work, any suggestions would be appreciated.
     
        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.