Skip to content

Instantly share code, notes, and snippets.

View timelyportfolio's full-sized avatar

timelyportfolio timelyportfolio

View GitHub Profile
@timelyportfolio
timelyportfolio / construction of horizon plot in r.r
Created August 1, 2012 20:20
construction of horizon plot
#look at steps in constructing a horizon plot version
#of http://www.mebanefaber.com/timing-model/
#do horizon of percent above or below 10 month / 200 day moving average
require(lattice)
require(latticeExtra)
require(quantmod)
#since we are focused on the horizon plot, let's just look at one stock
@timelyportfolio
timelyportfolio / add_RSIhorizon.r
Created August 2, 2012 16:18
horizon in quantmod
#code almost entirely from addRSI from quantmod package
# add_RSI {{{
add_RSI.test <- function (n=14, maType="EMA", ..., RSIup=70, RSIdn=30, horizonscale=25) {
require(RColorBrewer)
lenv <- new.env()
lenv$plot_rsi <- function(x, n, maType, ...) {
xdata <- x$Env$xdata
xsubset <- x$Env$xsubset
rsi <- RSI(Cl(xdata),n=n,maType=maType)[xsubset] - 50
@timelyportfolio
timelyportfolio / horizon in base.r
Created August 3, 2012 13:17
horizon in base graphics
require(RColorBrewer)
require(quantmod)
require(PerformanceAnalytics)
data(managers)
#let's do managers from 2002 to 2004 to get positive and negative
x <- cumprod(1+managers["2002::2004"])[,1] - 1
#get some decent colors from RColorBrewer
@timelyportfolio
timelyportfolio / horizon plot of french 48 industry portfolio.r
Created August 7, 2012 19:22
horizon plot of french 48 industry portfolio
#get very helpful Ken French data
#for this project we will look at Industry Portfolios
#http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/48_Industry_Portfolios_daily.zip
require(latticeExtra)
require(PerformanceAnalytics)
require(quantmod)
#my.url will be the location of the zip file with the data
my.url="http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/48_Industry_Portfolios_daily.zip"
@timelyportfolio
timelyportfolio / why trend is not your friend on french industries.r
Created August 8, 2012 20:20
why trend is not your friend on french industries
#get very helpful Ken French data
#for this project we will look at Industry Portfolios
#http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/48_Industry_Portfolios_daily.zip
require(latticeExtra)
require(PerformanceAnalytics)
require(quantmod)
#my.url will be the location of the zip file with the data
my.url="http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/48_Industry_Portfolios_daily.zip"
@timelyportfolio
timelyportfolio / horizon plot of 48 industries with dendrogram order.r
Created August 9, 2012 21:43
horizon plot of 48 industries with dendrogram order
require(fAssets)
require(latticeExtra)
require(quantmod)
require(PerformanceAnalytics)
#my.url will be the location of the zip file with the data
my.url="http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/48_Industry_Portfolios_daily.zip"
#this will be the temp file set up for the zip file
my.tempfile<-paste(tempdir(),"\\frenchindustry.zip",sep="")
#my.usefile is the name of the txt file with the data
@timelyportfolio
timelyportfolio / french industry animated correlation gif.r
Created August 10, 2012 20:11
french industry animated gif of correlation
#get very helpful Ken French data
#for this project we will look at Industry Portfolios
#http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/48_Industry_Portfolios_daily.zip
require(latticeExtra)
require(animation)
require(PerformanceAnalytics)
require(quantmod)
#my.url will be the location of the zip file with the data
@timelyportfolio
timelyportfolio / plotxts version of chartTimeSeries.r
Created August 15, 2012 17:18
chart.* functions with plot.xts
#work on porting chart.TimeSeries to plot.xts
#believe all these initial issues have been resolved
#issues: no log scale y axis option so will have to handle
# no legend functionality yet
# shading of blocks not reaching bottom (sent to M Weylandt)
# main does not automatically choose first column like chart.TimeSeries
require(PerformanceAnalytics)
@timelyportfolio
timelyportfolio / plotxts horizon example.r
Created August 15, 2012 18:50
plot.xts horizon plot example
#plot.xts with horizons
require(PerformanceAnalytics)
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
require(RColorBrewer)
col.brew <- brewer.pal(name="RdBu",n=10)
#ease this reference later
n=NROW(x)
@timelyportfolio
timelyportfolio / chartTimeSeries and chartsPerformanceSummary.r
Last active August 23, 2018 00:43
plot.xts replacements of PerformanceAnalytics chart.TimeSeries and charts.PerformanceSummary
#install.packages("xtsExtra", repos="http://R-Forge.R-project.org")
require(PerformanceAnalytics)
require(xtsExtra) #if you get error, please install xtsExtra from r-forge as shown in the top line
require(RColorBrewer)
#function add alpha or transparency to colors
addalpha <- function(cols,alpha=180) {
rgbcomp <- col2rgb(cols)
rgbcomp[4] <- alpha