This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################# | |
# Build a model | |
################################################# | |
#Choose Differencing window | |
#Hussman uses 6 months | |
Diff <- 6 | |
Data$PAYEMS <- (Data$PAYEMS-Lag(Data$PAYEMS,Diff))/Lag(Data$PAYEMS,Diff) | |
Data$UNRATE <- (Data$UNRATE-Lag(Data$UNRATE,Diff))/Lag(Data$UNRATE,Diff) | |
Data$SP500 <- (Data$SP500-Lag(Data$SP500,Diff))/Lag(Data$SP500,Diff) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Actual Recessions | |
getSymbols('USREC',src='FRED') | |
USREC <- USREC["1997-07-01::",] #Start from same period | |
#Compare recession now to warning last month | |
library(caret) | |
OneMonth <- Lag(P.Rec) | |
compare <- na.omit(merge(OneMonth,USREC)) | |
confusionMatrix(compare[,1],compare[,2],positive = '1') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cor(iris[-5]) | |
pairs(iris[-5], bg=iris$Species, pch=21) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 *** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Load Data | |
rm(list = ls(all = TRUE)) | |
library(quantmod) | |
library(PerformanceAnalytics) | |
symbols <- c('XLE','XLV','XLI','XLU','XLP','IYZ','XLK','XLY','XLF','XLB','GLD','SLV','EFA','EEM','FXA','FXE','FXY','HYG','LQD') | |
getSymbols(symbols,from='2007-01-01') | |
getSymbols('SPY',from='2007-01-01') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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") |