**R and Trading**, and kindly contributed to R-bloggers)

library(quantmod)

library(PerformanceAnalytics)

library(tseries)

# initial value

stockRatio <- .5

interRatio <- .15

# rebalancing period

# prd <- ‘yearly’

prd <- ‘monthly’

# prd <- ‘daily’

# symbols used

# VUSTX govvies

# VWESX IG bonds

# VTSMX stocks

# VWEHX junk

# VGTSX international stocks ex US

# VTRIX international value

# get the data

getSymbols(c(‘VTSMX’,’VWESX’, ‘VUSTX’, ‘VWEHX’, ‘VGTSX’), from=’1900-07-28′)

# ———this section is the base portfolio analysis————

m=merge(Ad(VTSMX),Ad(VUSTX), all=F)

s=periodReturn(m[,1], period = prd)

b=periodReturn(m[,2], period = prd)

# create the portfolios

p1 <- stockRatio * s + (1-stockRatio) * b

# first plot

plot(cumprod(1+p1), main= ‘Total Returns, Base Port, Yearly Rebal’, log=’y’)

grid(lwd=3, equilogs = F)

# ———this section is the IG corporate portfolio analysis————

m=merge(Ad(VTSMX),Ad(VWESX), all=F)

s=periodReturn(m[,1], period = prd)

b=periodReturn(m[,2], period = prd)

# create the portfolios

p1 <- stockRatio * s + (1-stockRatio) * b

# first plot

plot(cumprod(1+p1), main= ‘Total Returns, IG Corp Port, Yearly Rebal’, log=’y’)

# plot last year

plot(tail(cumprod(1+p1),255), main= ‘Total Returns, Base Port, Yearly Rebal’, log=’y’)

# ———this section is the high yield analysis————

m=merge(Ad(VTSMX),Ad(VWEHX),Ad(VUSTX), all=F)

s=periodReturn(m[,1], period = prd)

j=periodReturn(m[,2], period = prd)

b=periodReturn(m[,3], period = prd)

# create the portfolios

p1 <- stockRatio * s + (1-stockRatio) * b

p2 <- stockRatio * s + (1-stockRatio) * j

# first plot

plot(cumprod(1+s), main= ‘Total Returns’)

lines(cumprod(1+j), col=’red’)

lines(cumprod(1+b), col=’green’)

grid(lwd = 3)

legend(‘topleft’, c(‘Stocks’,’High Yield’,’Gov Bonds’), fill = c(‘black’,’red’,’green’))

# second plot

plot(cumprod(1+p1), ylim=c(1,10), log=’y’, main=’Portfolio Growth of $1′)

lines(cumprod(1+p2), col=’red’)

grid(lwd = 3, equilogs = F)

legend(‘topleft’, c(‘Govern Bonds’,’High Yield’), fill = c(‘black’,’red’))

# get the drawdowns, use daily data

prd <- ‘daily’

s=periodReturn(m[,1], period = prd)

j=periodReturn(m[,2], period = prd)

b=periodReturn(m[,3], period = prd)

# create the portfolios

p1 <- stockRatio * s + (1-stockRatio) * b

p2 <- stockRatio * s + (1-stockRatio) * j

table.Drawdowns(p1)

table.Drawdowns(p2)

# not used here

# pDum <- portfolio.optim(mm, pm=.07)

# pdum$pw

# ———this section is the international stock analysis————

m <- merge(Ad(VTSMX), Ad(VGTSX), Ad(VUSTX), all=F)

# p3 is US stocks/bonds since 1996

# p4 is US + internat / bonds

s <- periodReturn(m[,1])

i <- periodReturn(m[,2])

b <- periodReturn(m[,3])

p3 <- (s + b) /2

p4 <- (.35 *s + .15 * i + .5 * b)

plot(cumprod(1+p3), log=’y’, main = ‘With/Without International Stocks’)

lines(cumprod(1+p4), col=’red’)

grid(lwd = 3)

legend(‘topleft’, c(‘US Stocks only’,’Incl Intern Stocks’), fill = c(‘black’,’red’), bty = ‘n’,cex = 2)

**leave a comment**for the author, please follow the link and comment on their blog:

**R and Trading**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...