Recursively retrieve Date OHLC Adj Close and dividends, adjust some dividends, then calculate current yield two problems

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

Recursively retrieve Date OHLC Adj Close and dividends, adjust some dividends, then calculate current yield two problems

FJ M




I successfully retrieve Date OHLCAdjC and dividends for two stocks, code at the end.  1) I am unable to replace the MSFT $3.08 dividend with $0.08 (the ongoing dividend without the $3 special dividend) using: if (ticker[i]=="MSFT") {print(Divs[4]); Divs[4] <= 0.08; print(Divs[4])} ## $3 special dividend, $0.08 regular dividend           [,1]
2004-11-15 3.08
           [,1]
2004-11-15 3.08 2) I am only able to calculate the current yield for the first ticker using explicity MSFT.Close which works, but when I load data for the next symbol, KMI, MSFT.Close does not exist. How do I refer to the properly refer to the close when looping through multiple symbols? Current_Yield<-with(na.locf(merge(Stock, Annual_Div=Divs * Divs_Per_Year)), Annual_Div / MSFT.Close) Thanks, FrankChicago library(quantmod)##
## Merge Date OHLC with dividend on ex-dividend date
##
ticker<-c("MSFT","KMI")
Divs_Per_Year<-4for (i in 1:2)
{
file_name<- paste0(ticker[i],"_Divs.csv")
Divs<-getDividends(ticker[i])
print(Divs)
Stock<-getSymbols.yahoo(ticker[i], from = start(Divs), src = "yahoo", verbose=TRUE, auto.assign=FALSE)
Stock_Divs<-merge(Stock,Divs,fill=0.0)
write.zoo(Stock_Divs, file = file_name, append = FALSE, quote = TRUE, sep = ",")##
## Calcuate current annual dividend and write it out for each date to a file
##if (ticker[i]=="MSFT") {print(Divs[4]); Divs[4] <= 0.08; print(Divs[4])} ## $3 special dividend, $0.08 regular dividend
if (ticker[i]=="KMI")  {print(Divs[1]); Divs[1] <= 0.29; print(Divs[1])} ## First dividend was 0.14 but based on a full quarter payout of 0.29
file_name<- paste0(ticker[i],"_Annual_Divs.csv")
print(file_name)
print(Divs)
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 = file_name, append = FALSE, quote = TRUE, sep = ",")
##
## Calcuate current annual dividend yield for each date and write it out to a file
##file_name<- paste0(ticker[i],"_Current_Yield.csv")
print(file_name)
Current_Yield<-with(na.locf(merge(Stock, Annual_Div=Divs * Divs_Per_Year)), Annual_Div / MSFT.Close)
Stock_Current_Yield<-na.locf((merge(Stock_Annual_Dividend, Current_Yield, retside=c(TRUE, TRUE))))
write.zoo(Stock_Current_Yield, file = file_name, append = FALSE, quote = TRUE, sep = ",")
}quit()
     
        [[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: Recursively retrieve Date OHLC Adj Close and dividends, adjust some dividends, then calculate current yield two problems

Joshua Ulrich
As it says in the posting guide and the general instructions to the
mailing lists, please don't send HTML mail.
http://www.r-project.org/posting-guide.html
http://www.r-project.org/mail.html#instructions

The mess below was produced in an attempt to "un-HTML" your email.
People are unlikely to spend time cleaning up your prose and code in
order to help.

Best,
--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com


On Wed, Oct 17, 2012 at 12:12 PM, FJ M <[hidden email]> wrote:

>
>
>
>
> I successfully retrieve Date OHLCAdjC and dividends for two stocks, code at the end.  1) I am unable to replace the MSFT $3.08 dividend with $0.08 (the ongoing dividend without the $3 special dividend) using: if (ticker[i]=="MSFT") {print(Divs[4]); Divs[4] <= 0.08; print(Divs[4])} ## $3 special dividend, $0.08 regular dividend           [,1]
> 2004-11-15 3.08
>            [,1]
> 2004-11-15 3.08 2) I am only able to calculate the current yield for the first ticker using explicity MSFT.Close which works, but when I load data for the next symbol, KMI, MSFT.Close does not exist. How do I refer to the properly refer to the close when looping through multiple symbols? Current_Yield<-with(na.locf(merge(Stock, Annual_Div=Divs * Divs_Per_Year)), Annual_Div / MSFT.Close) Thanks, FrankChicago library(quantmod)##
> ## Merge Date OHLC with dividend on ex-dividend date
> ##
> ticker<-c("MSFT","KMI")
> Divs_Per_Year<-4for (i in 1:2)
> {
> file_name<- paste0(ticker[i],"_Divs.csv")
> Divs<-getDividends(ticker[i])
> print(Divs)
> Stock<-getSymbols.yahoo(ticker[i], from = start(Divs), src = "yahoo", verbose=TRUE, auto.assign=FALSE)
> Stock_Divs<-merge(Stock,Divs,fill=0.0)
> write.zoo(Stock_Divs, file = file_name, append = FALSE, quote = TRUE, sep = ",")##
> ## Calcuate current annual dividend and write it out for each date to a file
> ##if (ticker[i]=="MSFT") {print(Divs[4]); Divs[4] <= 0.08; print(Divs[4])} ## $3 special dividend, $0.08 regular dividend
> if (ticker[i]=="KMI")  {print(Divs[1]); Divs[1] <= 0.29; print(Divs[1])} ## First dividend was 0.14 but based on a full quarter payout of 0.29
> file_name<- paste0(ticker[i],"_Annual_Divs.csv")
> print(file_name)
> print(Divs)
> 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 = file_name, append = FALSE, quote = TRUE, sep = ",")
> ##
> ## Calcuate current annual dividend yield for each date and write it out to a file
> ##file_name<- paste0(ticker[i],"_Current_Yield.csv")
> print(file_name)
> Current_Yield<-with(na.locf(merge(Stock, Annual_Div=Divs * Divs_Per_Year)), Annual_Div / MSFT.Close)
> Stock_Current_Yield<-na.locf((merge(Stock_Annual_Dividend, Current_Yield, retside=c(TRUE, TRUE))))
> write.zoo(Stock_Current_Yield, file = file_name, append = FALSE, quote = TRUE, sep = ",")
> }quit()
>
>         [[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.

_______________________________________________
[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.