Skip to content

Instantly share code, notes, and snippets.

View zachmayer's full-sized avatar

Zach Deane-Mayer zachmayer

View GitHub Profile
@zachmayer
zachmayer / Linear Model.R
Created August 26, 2011 14:52
Sheep and Temperature
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.18471 0.12130 9.767 1.08e-12 ***
Sheep -0.71154 0.08546 -8.326 1.16e-10 ***
@zachmayer
zachmayer / 1. Functions.R
Created September 16, 2011 17:36
Backtesting a Simple Stock Trading Strategy 2
rm(list = ls(all = TRUE))
daysSinceHigh <- function(x, n){
apply(embed(x, n), 1, which.max)-1
}
myStrat <- function(x, nHold=100, nHigh=200) {
position <- ifelse(daysSinceHigh(x, nHigh)<=nHold,1,0)
c(rep(0,nHigh-1),position)
}
@zachmayer
zachmayer / Naive.R
Created September 20, 2011 18:31
Recessions III
#Load current know recessions
library(xts)
ModRec <- read.csv("http://dl.dropbox.com/u/7428423/Modified%20NBER%20Recession%20Data.csv")
ModRec$DATE <- as.Date(ModRec$DATE)
ModRec <- xts(ModRec[,-1],order.by=ModRec[,1])
#Load actual recessions
library(quantmod)
getSymbols('USREC',src='FRED')
@zachmayer
zachmayer / 1. Setup.R
Created October 17, 2011 14:44
Backtesting a Simple Stock Trading Strategy 3
rm(list = ls(all = TRUE))
memory.limit(size = 4000)
#Get Data
library(quantmod)
getSymbols('^GSPC',from='1900-01-01')
myStock <- Cl(GSPC)
bmkReturns <- dailyReturn(myStock, type = "arithmetic")
#Apply our strategy to tomorrows returns
@zachmayer
zachmayer / Random Strategies.R
Created October 20, 2011 20:19
Random Strategies
#Setup
rm(list = ls(all = TRUE))
set.seed(1)
nsims <- 1000
library(quantmod)
library(PerformanceAnalytics)
#Load Data
getSymbols('^GSPC',from='1900-01-01')
spyReturns <- dailyReturn(GSPC$GSPC.Adjusted, type = "arithmetic")
@zachmayer
zachmayer / cv.ts.R
Created December 12, 2011 16:26
Time series cross-validation 3
#Function to cross-validate a time series.
cv.ts <- function(x, FUN, tsControl, xreg=NULL, ...) {
#Load required packages
stopifnot(is.ts(x))
stopifnot(is.data.frame(xreg) | is.matrix(xreg) | is.null(xreg))
stopifnot(require(forecast))
stopifnot(require(foreach))
stopifnot(require(plyr))
@zachmayer
zachmayer / InstallPackages.R
Created June 19, 2012 14:33
Install Packages
install.packages(c('caret', 'doParallel', 'ggplot2', 'data.table', 'caTools', 'XML', 'quantmod'), dependencies=TRUE)
@zachmayer
zachmayer / Backtesting Part 5.R
Created June 25, 2012 22:19
Backtesting Part 5
rm(list = ls(all = TRUE))
memory.limit(size = 4095)
#Get Data
library(quantmod)
getSymbols('^GSPC',from='1900-01-01')
myStock <- Cl(GSPC)
bmkReturns <- dailyReturn(myStock, type = "arithmetic")
@zachmayer
zachmayer / 1. Functions.R
Created December 29, 2011 18:05
Forecasting S&P 500
#Function to cross-validate a time series.
cv.ts <- function(x, FUN, tsControl, xreg=NULL, ...) {
#Load required packages
stopifnot(is.ts(x))
stopifnot(is.data.frame(xreg) | is.matrix(xreg) | is.null(xreg))
stopifnot(require(forecast))
stopifnot(require(foreach))
stopifnot(require(plyr))
rm(list = ls(all = TRUE))
#http://etfprophet.com/days-since-200-day-highs/
require(quantmod)
getSymbols('^GSPC',from='1900-01-01')
daysSinceHigh <- function(x, n){
apply(embed(x, n), 1, which.max)-1
}