



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]]

