Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 311 lines (281 sloc) 10.579 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
###############################################################################
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
# Collection of routines to examine and compare proxies and data
# Copyright (C) 2013 Michael Kapler
#
# For more information please visit my blog at www.SystematicInvestor.wordpress.com
# or drop me a line at TheSystematicInvestor at gmail
###############################################################################


###############################################################################
#' Normilize all timeseries to start at one
#'
#' @param x \code{\link{xts}} time series
#'
#' @return scaled \code{\link{xts}} time series, so that each timeseries starts at one
#'
#' @examples
#' \dontrun{
#' plota.matplot(scale.one(data$prices))
#' }
#' @export
###############################################################################
# scale.one <- function(x) x / rep.row(as.numeric(x[1,]), nrow(x))
scale.one <- function
(
x,
overlay = F,
main.index = which(!is.na(x[1,]))[1]
)
{
index = 1:nrow(x)
if( overlay )
x / rep.row(apply(x, 2,
function(v) {
i = index[!is.na(v)][1]
v[i] / as.double(x[i,main.index])
}
), nrow(x))
else
x / rep.row(apply(x, 2, function(v) v[index[!is.na(v)][1]]), nrow(x))
}

###############################################################################
#' Create stock like \code{\link{xts}} object from one column time series
#'
#' @param out \code{\link{xts}} time series
#' @param column column index to use, \strong{defaults to 1}
#'
#' @return stock like \code{\link{xts}} object
#'
#' @export
###############################################################################
make.stock.xts <- function(out, column=1, ...) {
out = out[,column]
colnames(out) = 'Close'
out$Adjusted = out$Open = out$High = out$Low = out$Close
out$Volume = 0
return(out[,spl('Open,High,Low,Close,Volume,Adjusted')])
}

###############################################################################
#' Compute correlations
#'
#' @param data matrix with data
#' @param method method used to compute correlations, please see \code{\link{cor}} for more details
#'
#' @return correlation matrix
#'
#' @export
###############################################################################
compute.cor <- function
(
data, # matrix with data
method = c("pearson", "kendall", "spearman")
)
{
cor(data, use='complete.obs',method=method[1])
}

###############################################################################
#' Plot proxies and create summary table over common period
#'
#' @param data.all list or enviroment that holds proxy time series
#' @param names names or indexs of time series, \strong{defaults to all time series}
#' @param price.fn name of price function, \strong{defaults to Ad}
#'
#' @return nothing
#'
#' @examples
#' \dontrun{
#' tickers = spl('HYG,VWEHX')
#' data = new.env()
#' getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)
#'
#' proxy.test(data)
#' }
#' @export
###############################################################################
proxy.test <- function(data.all, names = ls(data.all), price.fn=Ad)
{
#*****************************************************************
# Prepare data
#******************************************************************
data = new.env()
data$symbolnames = names
for(n in data$symbolnames)
data[[n]] = make.stock.xts( price.fn( data.all[[n]] ) )
bt.prep(data, align='remove.na')

#*****************************************************************
# Prepare data
#******************************************************************
prices = data$prices

# Plot side by side
layout(1:2, heights=c(4,1))
plota.matplot(scale.one(prices))

rets = (prices/mlag(prices)-1)[-1,]

# compute correlations
temp = cor(rets, use='complete.obs', method='pearson')
diag(temp) = NA
temp[lower.tri(temp)] = NA
#temp = temp[-nrow(temp),-1,drop=F]
temp = temp[-nrow(temp),,drop=F]
temp[] = plota.format(100 * temp, 0, '', '%')
out = temp

# compute stats
temp = compute.stats( as.list(rets),
list(
Mean=function(x) 252*mean(x,na.rm=T),
StDev=function(x) sqrt(252)*sd(x,na.rm=T)
)
)
temp[] = plota.format(100 * temp, 1, '', '%')

# plot
out = rbind(out,NA,temp)
plot.table(out)
}

###############################################################################
#' Plot all proxies overlaying the longest one
#'
#' @param data.all list or enviroment that holds proxy time series
#' @param names names or indexs of time series, \strong{defaults to all time series}
#' @param price.fn name of price function, \strong{defaults to Ad}
#'
#' @return nothing
#'
#' @examples
#' \dontrun{
#' tickers = spl('HYG,VWEHX')
#' data = new.env()
#' getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)
#'
#' proxy.overlay.plot(data)
#' }
#' @export
###############################################################################
proxy.overlay.plot <- function(data.all, names = ls(data.all), price.fn=Ad)
{
#*****************************************************************
# Prepare data
#******************************************************************
data = new.env()
data$symbolnames = names
for(n in data$symbolnames)
data[[n]] = make.stock.xts( price.fn( data.all[[n]] ) )

bt.prep(data, align='keep.all')

#*****************************************************************
# Prepare data
#******************************************************************
prices = data$prices
prices = scale.one(prices, T)

# Plot side by side
layout(1)
plota.matplot(prices)
}

###############################################################################
#' Plot complete history for each index for Close and Adjusted, and create summary table
#'
#' @param data list or enviroment that holds proxy time series
#' @param names names or indexs of time series, \strong{defaults to all time series}
#'
#' @return nothing
#'
#' @examples
#' \dontrun{
#' tickers = spl('HYG,VWEHX')
#' data = new.env()
#' getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)
#'
#' proxy.prices(data)
#' }
#' @export
###############################################################################
proxy.prices <- function(data, names = ls(data)) {
n.names = len(names)
temp = list()

layout(1:(n.names+1))
for(n in names) {
plota.matplot(cbind(Cl(data[[n]]),Ad(data[[n]])),main=n)
temp[[ paste(n, 'Price') ]] = Cl(data[[n]])
temp[[ paste(n, 'Total') ]] = Ad(data[[n]])
}

# compute stats
temp = compute.stats( lapply(temp, function(x) ifna(x/mlag(x) -1,NA)),
list(
Mean=function(x) 252*mean(x,na.rm=T),
StDev=function(x) sqrt(252)*sd(x,na.rm=T)
)
)

# plot
temp[] = plota.format(100 * temp, 1, '', '%')
plot.table(temp)
}



proxy.example.test <- function() {
    #*****************************************************************
    # Load historical data
    #******************************************************************
    load.packages('quantmod')
    
    tickers = spl('GSG,DBC')
    data = new.env()
    getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)
        for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)
      
    # "TRJ_CRB" file was downloaded from the http://www.jefferies.com/Commodities/2cc/389
    # for "TRJ/CRB Index-Total Return"
    temp = extract.table.from.webpage( join(readLines("TRJ_CRB")), 'EODValue' )
    temp = join( apply(temp, 1, join, ','), '\n' )
    data$CRB_1 = make.stock.xts( read.xts(temp, format='%m/%d/%y' ) )
     
    # "prfmdata.csv" file was downloaded from the http://www.crbequityindexes.com/indexdata-form.php
    # for "TR/J CRB Global Commodity Equity Index", "Total Return", "All Dates"
    data$CRB_2 = make.stock.xts( read.xts("prfmdata.csv", format='%m/%d/%Y' ) )
                 
    #*****************************************************************
    # Compare
    #******************************************************************
jpeg(filename = 'plot1.jpg', width = 500, height = 500, units = 'px', pointsize = 12)
    proxy.test(data)
dev.off()
jpeg(filename = 'plot2.jpg', width = 500, height = 500, units = 'px', pointsize = 12)
    proxy.overlay.plot(data)
dev.off()

    #*****************************************************************
    # Load historical data
    #******************************************************************
    load.packages('quantmod')
    
    tickers = spl('IYR,VGSIX,RWO')
    data = new.env()
    getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)
        for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)
                 
    #*****************************************************************
    # Compare
    #******************************************************************
jpeg(filename = 'plot3.jpg', width = 500, height = 500, units = 'px', pointsize = 12)
    proxy.test(data)
dev.off()
jpeg(filename = 'plot4.jpg', width = 500, height = 500, units = 'px', pointsize = 12)
    proxy.overlay.plot(data)
dev.off()

# VGSIX,VEIEX,VBMFX,VWEHX,PEBIX,VIPSX,VTSMX,VGTSX,VFISX,VUSTX
#
# Equity Market
# Vanguard Total Stock Mkt (VTSMX)
# Vanguard Total Intl Stock (VGTSX)
# Vanguard 500 Index (VFINX)
# Vanguard Emerging Mkts (VEIEX)
# Fixed Income Market
# Vanguard Short-Term Treasury (VFISX)
# Vanguard Long-Term Treasury (VUSTX)
# Vanguard Total Bond Market (VBMFX)
# Vanguard High-Yield Corporate (VWEHX)
# PIMCO Emerging Markets Bond (PEBIX)
# Vanguard Inflation-Protected (VIPSX)
# PIMCO Total Return (PTTRX)
# Vanguard REIT (VGSIX)
#
}
Something went wrong with that request. Please try again.