Skip to content

Instantly share code, notes, and snippets.

View timelyportfolio's full-sized avatar

timelyportfolio timelyportfolio

View GitHub Profile
@timelyportfolio
timelyportfolio / horizonplot from plot-xts.r
Created August 17, 2012 14:10
plot.xts can create horizon charts
#plot.xts with horizons
#install.packages("xtsExtra", repos="http://R-Forge.R-project.org")
require(PerformanceAnalytics)
require(quantmod)
require(xtsExtra) #if you get error, install xtsExtra from r-forge
horizon.panel <- function(index,x,...) {
#get some decent colors from RColorBrewer
#we will use colors on the edges so 2:4 for red and 7:9 for blue
@timelyportfolio
timelyportfolio / garch in plot.xts panel.r
Created August 17, 2012 21:43
plot.xts and rugarch
#install.packages("xtsExtra", repos="http://R-Forge.R-project.org")
require(quantmod)
require(rugarch)
require(xtsExtra) #if you get an error, see first line and install from r-forge
getSymbols("DEXJPUS",src="FRED")
DEXJPUS<-1/to.weekly(DEXJPUS)
ugarch.panel <- function(index,x,type,cex,col,pch,...){
@timelyportfolio
timelyportfolio / plotxts moving average.r
Created August 20, 2012 20:23
plot.xts moving average panel
#install.packages("xtsExtra", repos="http://R-Forge.R-project.org")
require(quantmod)
require(RColorBrewer)
require(xtsExtra) #if you get an error, see first line and install from r-forge
#use Mebane Faber tickers VTI, VEU, IEF, VNQ, DBC
#as discussed in http://papers.ssrn.com/sol3/papers.cfm?abstract_id=962461
tckrs <- c("VTI", "VEU", "IEF", "VNQ", "DBC")
@timelyportfolio
timelyportfolio / bonds and buffett sharpe.r
Created August 23, 2012 19:52
bonds and buffett sharpe
require(quantmod)
require(PerformanceAnalytics)
require(xtsExtra)
require(RColorBrewer)
#unfortunately don't feel like fighting IP lawyers so I cannot share this index data
portfolio <- read.csv("file.csv",stringsAsFactors=FALSE)
portfolio <- portfolio[2:NROW(portfolio),2:NCOL(portfolio)]
portfolio <- portfolio[,c(1,3,5)]
@timelyportfolio
timelyportfolio / horizon plot in ggplot2.r
Created August 28, 2012 04:33
horizon plot in ggplot2
#first attempt at implementing horizon plots in ggplot2
#pleased with result but code sloppy and inflexible
#as always very open to improvements and forks
require(ggplot2)
require(reshape2)
require(quantmod)
require(PerformanceAnalytics)
require(xtsExtra)
@timelyportfolio
timelyportfolio / plotxts with ProspectRatio.r
Created August 30, 2012 14:20
plot.xts scattered smothered covered chunked and diced
#integrate 2 Google Summer of Code 2012 Projects
#plot.xts and PerformanceAnalytics both received very nice additions
#wish I knew the exact link but believe this section came from stackoverflow
#this allows you to work with the code straight from r-forge SVN
## If you want to source() a bunch of files, something like
## the following may be useful:
#path="C:\\Program Files\\R\\R-2.15.1\\sandbox\\svnsource\\returnanalytics\\pkg\\PerformanceAnalytics\\r"
#sourceDir <- function(path, trace = TRUE, ...) {
@timelyportfolio
timelyportfolio / fat tails to the upside.r
Last active January 20, 2021 03:54
exceptional case of the Russell 2000
#explore exceptional case of the Russell 2000
require(quantmod)
require(PerformanceAnalytics)
require(xtsExtra)
getSymbols("^RUT", from = "1900-01-01")
getSymbols("^GSPC", from = "1900-01-01")
#do initial exploration of distribution
require(PerformanceAnalytics)
require(quantmod)
getSymbols("^GSPC", from = "1900-01-01")
#get return series from closing price
ret.bh <- ROC(GSPC[,4],n = 1,type = "discrete")
#change first value from NA to 0
ret.bh[1,] <- 0
@timelyportfolio
timelyportfolio / obviousness of reits.r
Created September 20, 2012 20:20
obviousness of reits
#obviousness of reits?
#get NAREIT data
#I like NAREIT since I get back to 1971
#much easier though to get Wilshire REIT from FRED
#also it is daily instead of monthly
#getSymbols("WILLREITIND",src="FRED") will do this
require(gdata)
reitURL <- "http://returns.reit.com/returns/MonthlyHistoricalReturns.xls"
reitExcel <- read.xls(reitURL,sheet="Index Data",pattern="All REITs",stringsAsFactors=FALSE)
@timelyportfolio
timelyportfolio / russell 2000 when low vol.r
Created October 1, 2012 15:04
russell 2000 when low vol
require(quantmod)
require(PerformanceAnalytics)
getSymbols("^RUT",from = "1900-01-01")
getSymbols("^GSPC",from = "1900-01-01")
#get 1 day change for the Russell 2000 and S&P 500
roc <- na.omit(merge(ROC(RUT[,4],type="discrete",n=1),ROC(GSPC[,4],type="discrete",n=1)))
stdev <- rollapplyr(roc,FUN=sd,width=50)
#get relative strength of Russell 2000 versus S&P 500