|
Hello All,
I am relatively new to R and I am still not very comfortable with syntactic and libraries. Is there are any nice way to calculate and plot total returns for stocks which I would define as change in price and paid dividends? I made a code to do that but the loop that constructs prices+dividends looks ugly(see code below). Any suggestions to do it more efficiently? Thanks. Sergey ############## CODE ################################## library(quantmod) library(PerformanceAnalytics) #Time frame dt.end = "2010-01-01" dt.start = "2007-01-01" tickers = c('SPY', 'XLY', 'XLP', 'XLE', 'XLF', 'XLV', 'XLI', 'XLB', 'XLK', 'XLU') tickers.desc = c('SNP500', 'ConsumerCyclicals', 'ConsumerStaples', 'Energy', 'Financials', 'HealthCare', 'Industrials', 'Materials', 'Technology', 'Utilities') ############ Get prices ############################### setDefaults(getSymbols, warnings=FALSE,auto.assign=FALSE) fnPx <- function(i) { return(Ad(getSymbols(tickers[i], from=dt.start,to=dt.end))) } ts = lapply(1:length(tickers), fnPx) ########################################################################### ############ Get Dividends ################################ fnDiv<- function(i) { return(getDividends(tickers[i], from=dt.start,to=dt.end,auto.assign=FALSE)) } div = lapply(1:length(tickers), fnDiv) ########################################################################### ########### Create Prices + Dividends (UGLY !!!!) ##################### fnTotPx <- function(i) { ret = ts[[i]] for(j in 1:length(div[[i]])) { row = div[[i]][j,] tm = time(row) val = as.double(row[1,1]) iFwd = paste(tm,"::",sep='') iBk = paste("::",tm-1,sep='') unch = ret[iBk] chg = ret[iFwd]+val ret = rbind(unch,chg) } return(ret) } totPx = lapply(1:length(tickers), fnTotPx) ############################################################################ ################ Calc Total Returns ########################## fnRet <- function(i) { return(periodReturn(totPx[[i]],period='daily')) } ts.ret = lapply(1:length(tickers), fnRet) ################ Plot Total Returns ########################## ts.ret.df = as.data.frame(ts.ret) colnames(ts.ret.df)=tickers.desc chart.CumReturns(ts.ret.df, main="Cumulative Returns",geometric=FALSE,legend.loc="bottomleft") ############################################################################ _______________________________________________ [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. |
|
I think you're over-thinking this: if you have adjusted prices, they
already incorporate splits+dividends --- so the return in adjusted price *is* the total return. (Up to some fuzziness in how that adjustment should be done) Michael On Sat, Feb 18, 2012 at 5:20 PM, SW <[hidden email]> wrote: > Hello All, > > > I am relatively new to R and I am still not very comfortable with syntactic and libraries. Is there are any nice way to calculate and plot total returns for stocks which I would define as change in price and paid dividends? I made a code to do that but the loop that constructs prices+dividends looks ugly(see code below). Any suggestions to do it more efficiently? Thanks. Sergey > > ############## CODE ################################## > library(quantmod) > library(PerformanceAnalytics) > > #Time frame > dt.end = "2010-01-01" > dt.start = "2007-01-01" > > tickers = c('SPY', > 'XLY', > 'XLP', > 'XLE', > 'XLF', > 'XLV', > 'XLI', > 'XLB', > 'XLK', > 'XLU') > tickers.desc = c('SNP500', > 'ConsumerCyclicals', > 'ConsumerStaples', > 'Energy', > 'Financials', > 'HealthCare', > 'Industrials', > 'Materials', > 'Technology', > 'Utilities') > > ############ Get prices ############################### > setDefaults(getSymbols, warnings=FALSE,auto.assign=FALSE) > fnPx <- function(i) { return(Ad(getSymbols(tickers[i], from=dt.start,to=dt.end))) } > ts = lapply(1:length(tickers), fnPx) > ########################################################################### > > ############ Get Dividends ################################ > fnDiv<- function(i) { return(getDividends(tickers[i], from=dt.start,to=dt.end,auto.assign=FALSE)) } > div = lapply(1:length(tickers), fnDiv) > ########################################################################### > > ########### Create Prices + Dividends (UGLY !!!!) ##################### > fnTotPx <- function(i) > { > ret = ts[[i]] > for(j in 1:length(div[[i]])) > { > row = div[[i]][j,] > tm = time(row) > val = as.double(row[1,1]) > iFwd = paste(tm,"::",sep='') > iBk = paste("::",tm-1,sep='') > unch = ret[iBk] > chg = ret[iFwd]+val > ret = rbind(unch,chg) > } > return(ret) > } > totPx = lapply(1:length(tickers), fnTotPx) > ############################################################################ > > > ################ Calc Total Returns ########################## > fnRet <- function(i) { return(periodReturn(totPx[[i]],period='daily')) } > ts.ret = lapply(1:length(tickers), fnRet) > > ################ Plot Total Returns ########################## > ts.ret.df = as.data.frame(ts.ret) > colnames(ts.ret.df)=tickers.desc > chart.CumReturns(ts.ret.df, main="Cumulative Returns",geometric=FALSE,legend.loc="bottomleft") > ############################################################################ > > _______________________________________________ > [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. |
|
Hi Michael,
Thanks a lot! You are right. The adjusted prices will give me the correct numbers for total returns. I kind of overlooked it. Best regards, Sergey --- On Sat, 2/18/12, R. Michael Weylandt <[hidden email]> wrote: > From: R. Michael Weylandt <[hidden email]> > Subject: Re: [R-SIG-Finance] Stock Total Returns? > To: "SW" <[hidden email]> > Cc: [hidden email] > Date: Saturday, February 18, 2012, 5:27 PM > I think you're over-thinking this: if > you have adjusted prices, they > already incorporate splits+dividends --- so the return in > adjusted > price *is* the total return. (Up to some fuzziness in how > that > adjustment should be done) > > Michael > > On Sat, Feb 18, 2012 at 5:20 PM, SW <[hidden email]> > wrote: > > Hello All, > > > > > > I am relatively new to R and I am still not very > comfortable with syntactic and libraries. Is there are any > nice way to calculate and plot total returns for stocks > which I would define as change in price and paid dividends? > I made a code to do that but the loop that constructs > prices+dividends looks ugly(see code below). Any suggestions > to do it more efficiently? Thanks. Sergey > > > > ############## CODE > ################################## > > library(quantmod) > > library(PerformanceAnalytics) > > > > #Time frame > > dt.end = "2010-01-01" > > dt.start = "2007-01-01" > > > > tickers = c('SPY', > > 'XLY', > > 'XLP', > > 'XLE', > > 'XLF', > > 'XLV', > > 'XLI', > > 'XLB', > > 'XLK', > > 'XLU') > > tickers.desc = c('SNP500', > > 'ConsumerCyclicals', > > 'ConsumerStaples', > > 'Energy', > > 'Financials', > > 'HealthCare', > > 'Industrials', > > 'Materials', > > 'Technology', > > 'Utilities') > > > > ############ Get prices > ############################### > > setDefaults(getSymbols, > warnings=FALSE,auto.assign=FALSE) > > fnPx <- function(i) { > return(Ad(getSymbols(tickers[i], from=dt.start,to=dt.end))) > } > > ts = lapply(1:length(tickers), fnPx) > > > ########################################################################### > > > > ############ Get Dividends > ################################ > > fnDiv<- function(i) { > return(getDividends(tickers[i], > from=dt.start,to=dt.end,auto.assign=FALSE)) } > > div = lapply(1:length(tickers), fnDiv) > > > ########################################################################### > > > > ########### Create Prices + Dividends (UGLY !!!!) > ##################### > > fnTotPx <- function(i) > > { > > ret = ts[[i]] > > for(j in 1:length(div[[i]])) > > { > > row = div[[i]][j,] > > tm = time(row) > > val = as.double(row[1,1]) > > iFwd = paste(tm,"::",sep='') > > iBk = paste("::",tm-1,sep='') > > unch = ret[iBk] > > chg = ret[iFwd]+val > > ret = rbind(unch,chg) > > } > > return(ret) > > } > > totPx = lapply(1:length(tickers), fnTotPx) > > > ############################################################################ > > > > > > ################ Calc Total Returns > ########################## > > fnRet <- function(i) { > return(periodReturn(totPx[[i]],period='daily')) } > > ts.ret = lapply(1:length(tickers), fnRet) > > > > ################ Plot Total Returns > ########################## > > ts.ret.df = as.data.frame(ts.ret) > > colnames(ts.ret.df)=tickers.desc > > chart.CumReturns(ts.ret.df, main="Cumulative > Returns",geometric=FALSE,legend.loc="bottomleft") > > > ############################################################################ > > > > _______________________________________________ > > [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. |
|
There is also quantmod::adjustOHLC, which will provide better adjusted
OHL prices than using the Close / Adjusted Close ratio. -- Joshua Ulrich | FOSS Trading: www.fosstrading.com R/Finance 2012: Applied Finance with R www.RinFinance.com On Sat, Feb 18, 2012 at 4:44 PM, SW <[hidden email]> wrote: > Hi Michael, > > Thanks a lot! You are right. The adjusted prices will give me the correct numbers for total returns. I kind of overlooked it. > > Best regards, > Sergey > > > > > --- On Sat, 2/18/12, R. Michael Weylandt <[hidden email]> wrote: > >> From: R. Michael Weylandt <[hidden email]> >> Subject: Re: [R-SIG-Finance] Stock Total Returns? >> To: "SW" <[hidden email]> >> Cc: [hidden email] >> Date: Saturday, February 18, 2012, 5:27 PM >> I think you're over-thinking this: if >> you have adjusted prices, they >> already incorporate splits+dividends --- so the return in >> adjusted >> price *is* the total return. (Up to some fuzziness in how >> that >> adjustment should be done) >> >> Michael >> >> On Sat, Feb 18, 2012 at 5:20 PM, SW <[hidden email]> >> wrote: >> > Hello All, >> > >> > >> > I am relatively new to R and I am still not very >> comfortable with syntactic and libraries. Is there are any >> nice way to calculate and plot total returns for stocks >> which I would define as change in price and paid dividends? >> I made a code to do that but the loop that constructs >> prices+dividends looks ugly(see code below). Any suggestions >> to do it more efficiently? Thanks. Sergey >> > >> > ############## CODE >> ################################## >> > library(quantmod) >> > library(PerformanceAnalytics) >> > >> > #Time frame >> > dt.end = "2010-01-01" >> > dt.start = "2007-01-01" >> > >> > tickers = c('SPY', >> > 'XLY', >> > 'XLP', >> > 'XLE', >> > 'XLF', >> > 'XLV', >> > 'XLI', >> > 'XLB', >> > 'XLK', >> > 'XLU') >> > tickers.desc = c('SNP500', >> > 'ConsumerCyclicals', >> > 'ConsumerStaples', >> > 'Energy', >> > 'Financials', >> > 'HealthCare', >> > 'Industrials', >> > 'Materials', >> > 'Technology', >> > 'Utilities') >> > >> > ############ Get prices >> ############################### >> > setDefaults(getSymbols, >> warnings=FALSE,auto.assign=FALSE) >> > fnPx <- function(i) { >> return(Ad(getSymbols(tickers[i], from=dt.start,to=dt.end))) >> } >> > ts = lapply(1:length(tickers), fnPx) >> > >> ########################################################################### >> > >> > ############ Get Dividends >> ################################ >> > fnDiv<- function(i) { >> return(getDividends(tickers[i], >> from=dt.start,to=dt.end,auto.assign=FALSE)) } >> > div = lapply(1:length(tickers), fnDiv) >> > >> ########################################################################### >> > >> > ########### Create Prices + Dividends (UGLY !!!!) >> ##################### >> > fnTotPx <- function(i) >> > { >> > ret = ts[[i]] >> > for(j in 1:length(div[[i]])) >> > { >> > row = div[[i]][j,] >> > tm = time(row) >> > val = as.double(row[1,1]) >> > iFwd = paste(tm,"::",sep='') >> > iBk = paste("::",tm-1,sep='') >> > unch = ret[iBk] >> > chg = ret[iFwd]+val >> > ret = rbind(unch,chg) >> > } >> > return(ret) >> > } >> > totPx = lapply(1:length(tickers), fnTotPx) >> > >> ############################################################################ >> > >> > >> > ################ Calc Total Returns >> ########################## >> > fnRet <- function(i) { >> return(periodReturn(totPx[[i]],period='daily')) } >> > ts.ret = lapply(1:length(tickers), fnRet) >> > >> > ################ Plot Total Returns >> ########################## >> > ts.ret.df = as.data.frame(ts.ret) >> > colnames(ts.ret.df)=tickers.desc >> > chart.CumReturns(ts.ret.df, main="Cumulative >> Returns",geometric=FALSE,legend.loc="bottomleft") >> > >> ############################################################################ >> > >> > _______________________________________________ >> > [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. _______________________________________________ [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. |
|
In reply to this post by SW
Joshua,
Thank you for taking the trouble to recalculate adjustment factors based on actual dividends and splits data in quantmod::adjustOHLC function. I guess that the real meat of this function hidden in TTR::adjRatios (Is it native(C/C++) call?) My original question was based on my incomplete perception of the adjustment process. I always thought that the adjustment is done in order to keep historical prices continues to avoid ex-dividend date drop. To my surprise it also works to produce the correct total returns series. The bigger surprise was that it looks like that it gives the correct calculations not only for simple returns but also for logarithmic returns. I never thought about that before Michael's remark. Best Regards, Sergey ====================== .~. /v\ Sergey W. Andreyev // \\ kryp33 at yahoo.com /( )\ ^`~'^ ====================== ====================== --- On Mon, 2/20/12, [hidden email] <[hidden email]> wrote: > From: [hidden email] <[hidden email]> > Subject: R-SIG-Finance Digest, Vol 93, Issue 18 > To: [hidden email] > Date: Monday, February 20, 2012, 6:00 AM > Send R-SIG-Finance mailing list > submissions to > [hidden email] > > To subscribe or unsubscribe via the World Wide Web, visit > https://stat.ethz.ch/mailman/listinfo/r-sig-finance > or, via email, send a message with subject or body 'help' > to > [hidden email] > > You can reach the person managing the list at > [hidden email] > > When replying, please edit your Subject line so it is more > specific > than "Re: Contents of R-SIG-Finance digest..." > > > Today's Topics: > > 1. Re: Stock Total Returns? (Joshua > Ulrich) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Sun, 19 Feb 2012 17:22:01 -0600 > From: Joshua Ulrich <[hidden email]> > To: SW <[hidden email]> > Cc: [hidden email] > Subject: Re: [R-SIG-Finance] Stock Total Returns? > Message-ID: > <[hidden email]> > Content-Type: text/plain; charset=ISO-8859-1 > > There is also quantmod::adjustOHLC, which will provide > better adjusted > OHL prices than using the Close / Adjusted Close ratio. > -- > Joshua Ulrich ?| ?FOSS Trading: www.fosstrading.com > > R/Finance 2012: Applied Finance with R > www.RinFinance.com > > > > On Sat, Feb 18, 2012 at 4:44 PM, SW <[hidden email]> > wrote: > > Hi Michael, > > > > Thanks a lot! You are right. The adjusted prices will > give me the correct numbers for total returns. I kind of > overlooked it. > > > > Best regards, > > Sergey > > > > > > > > > > --- On Sat, 2/18/12, R. Michael Weylandt <[hidden email]> > wrote: > > > >> From: R. Michael Weylandt <[hidden email]> > >> Subject: Re: [R-SIG-Finance] Stock Total Returns? > >> To: "SW" <[hidden email]> > >> Cc: [hidden email] > >> Date: Saturday, February 18, 2012, 5:27 PM > >> I think you're over-thinking this: if > >> you have adjusted prices, they > >> already incorporate splits+dividends --- so the > return in > >> adjusted > >> price *is* the total return. (Up to some fuzziness > in how > >> that > >> adjustment should be done) > >> > >> Michael > >> > >> On Sat, Feb 18, 2012 at 5:20 PM, SW <[hidden email]> > >> wrote: > >> > Hello All, > >> > > >> > > >> > I am relatively new to R and I am still not > very > >> comfortable with syntactic and libraries. Is there > are any > >> nice way to calculate and plot total returns for > stocks > >> which I would define as change in price and paid > dividends? > >> I made a code to do that but the loop that > constructs > >> prices+dividends looks ugly(see code below). Any > suggestions > >> to do it more efficiently? Thanks. Sergey > >> > > >> > ############## ?CODE > >> ?################################## > >> > library(quantmod) > >> > library(PerformanceAnalytics) > >> > > >> > #Time frame > >> > dt.end = "2010-01-01" > >> > dt.start = ?"2007-01-01" > >> > > >> > tickers = c('SPY', > >> > ? ? ? ? ? ?'XLY', > >> > ? ? ? ? ? ?'XLP', > >> > ? ? ? ? ? ?'XLE', > >> > ? ? ? ? ? ?'XLF', > >> > ? ? ? ? ? ?'XLV', > >> > ? ? ? ? ? ?'XLI', > >> > ? ? ? ? ? ?'XLB', > >> > ? ? ? ? ? ?'XLK', > >> > ? ? ? ? ? ?'XLU') > >> > tickers.desc = c('SNP500', > >> > ? ? ? ? ? ? ? ? 'ConsumerCyclicals', > >> > ? ? ? ? ? ? ? ? 'ConsumerStaples', > >> > ? ? ? ? ? ? ? ? 'Energy', > >> > ? ? ? ? ? ? ? ? 'Financials', > >> > ? ? ? ? ? ? ? ? 'HealthCare', > >> > ? ? ? ? ? ? ? ? 'Industrials', > >> > ? ? ? ? ? ? ? ? 'Materials', > >> > ? ? ? ? ? ? ? ? 'Technology', > >> > ? ? ? ? ? ? ? ? 'Utilities') > >> > > >> > ############ ? ? ? ? ? ? ?Get prices > >> ?############################### > >> > setDefaults(getSymbols, > >> warnings=FALSE,auto.assign=FALSE) > >> > fnPx <- function(i) { > >> return(Ad(getSymbols(tickers[i], > from=dt.start,to=dt.end))) > >> } > >> > ts = lapply(1:length(tickers), fnPx) > >> > > >> > ########################################################################### > >> > > >> > ############ ? ? ? ? ? ? ?Get Dividends > >> ################################ > >> > fnDiv<- function(i) { > >> return(getDividends(tickers[i], > >> from=dt.start,to=dt.end,auto.assign=FALSE)) } > >> > div = lapply(1:length(tickers), fnDiv) > >> > > >> > ########################################################################### > >> > > >> > ########### ? ?Create Prices + Dividends (UGLY > !!!!) > >> ?##################### > >> > fnTotPx <- function(i) > >> > ?{ > >> > ? ?ret = ts[[i]] > >> > ? ?for(j in 1:length(div[[i]])) > >> > ? ?{ > >> > ? ? ?row ?= div[[i]][j,] > >> > ? ? ?tm ? = time(row) > >> > ? ? ?val ?= as.double(row[1,1]) > >> > ? ? ?iFwd = paste(tm,"::",sep='') > >> > ? ? ?iBk ?= paste("::",tm-1,sep='') > >> > ? ? ?unch = ret[iBk] > >> > ? ? ?chg ?= ret[iFwd]+val > >> > ? ? ?ret = rbind(unch,chg) > >> > ? ?} > >> > ? ?return(ret) > >> > ?} > >> > totPx = lapply(1:length(tickers), fnTotPx) > >> > > >> > ############################################################################ > >> > > >> > > >> > ################ ? ? ? ? ?Calc Total Returns > >> ########################## > >> > fnRet <- function(i) { > >> return(periodReturn(totPx[[i]],period='daily')) } > >> > ts.ret = lapply(1:length(tickers), fnRet) > >> > > >> > ################ ? ? ? ? ?Plot Total Returns > >> ########################## > >> > ts.ret.df = as.data.frame(ts.ret) > >> > colnames(ts.ret.df)=tickers.desc > >> > chart.CumReturns(ts.ret.df, main="Cumulative > >> Returns",geometric=FALSE,legend.loc="bottomleft") > >> > > >> > ############################################################################ > >> > > >> > > _______________________________________________ > >> > [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. > > > > ------------------------------ > > _______________________________________________ > R-SIG-Finance mailing list > [hidden email] > https://stat.ethz.ch/mailman/listinfo/r-sig-finance > > > End of R-SIG-Finance Digest, Vol 93, Issue 18 > ********************************************* > _______________________________________________ [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. |
| Powered by Nabble | Edit this page |
