-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apress
committed
Oct 24, 2016
0 parents
commit 0f3cd8c
Showing
110 changed files
with
5,255 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Freeware License, some rights reserved | ||
|
||
Copyright (c) 2016 Christopher Conlan | ||
|
||
Permission is hereby granted, free of charge, to anyone obtaining a copy | ||
of this software and associated documentation files (the "Software"), | ||
to work with the Software within the limits of freeware distribution and fair use. | ||
This includes the rights to use, copy, and modify the Software for personal use. | ||
Users are also allowed and encouraged to submit corrections and modifications | ||
to the Software for the benefit of other users. | ||
|
||
It is not allowed to reuse, modify, or redistribute the Software for | ||
commercial use in any way, or for a user�s educational materials such as books | ||
or blog articles without prior permission from the copyright holder. | ||
|
||
The above copyright notice and this permission notice need to be included | ||
in all copies or substantial portions of the software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Declare global variables a and b | ||
a <- 2 | ||
b <- 3 | ||
|
||
# Declare functions | ||
f <- function(){ | ||
a | ||
} | ||
|
||
g <-function(){ | ||
f() + b | ||
} | ||
|
||
h <- function(b){ | ||
f() + b | ||
} | ||
|
||
|
||
# a = 2 throughout. | ||
# b = 3 when not supplied as a parameter. | ||
f() # f() = 2 | ||
g() # g() = 5 | ||
h(5) # h(5) = 7 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
mcTimeSeries <- function( data, tsfunc, byColumn, windowSize, workers ){ | ||
|
||
SERIES <- foreach( i = 1:workers, .combine = rbind ) %dopar% { | ||
|
||
jRange <- delegate( i = i, n = nrow(data), k = windowSize, p = workers) | ||
|
||
rollapply(data[jRange,], | ||
width = windowSize, | ||
FUN = tsfunc, | ||
align = "right", | ||
by.column = byColumn) | ||
|
||
} | ||
|
||
names(SERIES) <- gsub("\\..+", "", names(SERIES)) | ||
|
||
if( windowSize > 1){ | ||
PAD <- zoo(matrix(nrow = windowSize-1, ncol = ncol(SERIES), NA), | ||
order.by = index(data)[1:(windowSize-1)]) | ||
names(PAD) <- names(SERIES) | ||
SERIES <- rbind(PAD, SERIES) | ||
} | ||
|
||
if(is.null(names(SERIES))){ | ||
names(SERIES) <- gsub("\\..+", "", names(data)[1:ncol(SERIES)]) | ||
} | ||
|
||
return(SERIES) | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
exitfunc <- function(v) { | ||
# Body of developer's new exit function | ||
} | ||
|
||
evaluate(...) <- function(...){ | ||
|
||
# Body of the evaluate function | ||
|
||
EXIT <- mcTimeSeries(CLOSE, exitfunc, TRUE, 20, workers) | ||
|
||
# Remainder of the evaluate function | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Declare parameter alpha as function parameter | ||
exitfunc <- function(v, alpha) { | ||
# Body of developer's new exit function | ||
} | ||
|
||
# Declare function object exitfunc as | ||
# function parameter to evaluator | ||
evaluate <- function(... , exitfunc){ | ||
|
||
# Body of the evaluate function | ||
|
||
# alpha exists in the function scope | ||
# of the evaluator | ||
alpha <- 0.5 | ||
|
||
# Dynamically declare function object in | ||
# mcTimeSeries. Pass exitfunc and alpha | ||
# in the ellipses of the call because | ||
# the second argument depends on them. | ||
EXIT <- mcTimeSeries(CLOSE, | ||
function(v) exitfunc(v, alpha), | ||
TRUE, 20, workers, | ||
exitfunc, alpha) | ||
|
||
# Remainder of the evaluate function | ||
|
||
} | ||
|
||
|
||
optimize <- function(... , exitfunc){ | ||
|
||
# Alter all calls to evaluate to include | ||
# new function object parameter exitfunc | ||
|
||
# Body of the optimzer | ||
|
||
evaluate( ... , exitfunc ) | ||
|
||
# Body of the optimzer | ||
|
||
evaluate( ... , exitfunc ) | ||
|
||
# And so on. There are typically many calls | ||
# to evaluate() within the optimizer. | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
mcTimeSeries <- function( data, tsfunc, byColumn, windowSize, workers ){ | ||
|
||
SERIES <- foreach( i = 1:workers, .combine = rbind ) %dopar% { | ||
|
||
jRange <- delegate( i = i, n = nrow(data), k = windowSize, p = workers) | ||
|
||
rollapply(data[jRange,], | ||
width = windowSize, | ||
FUN = tsfunc, | ||
align = "right", | ||
by.column = byColumn) | ||
|
||
} | ||
|
||
names(SERIES) <- gsub("\\..+", "", names(SERIES)) | ||
|
||
if( windowSize > 1){ | ||
PAD <- zoo(matrix(nrow = windowSize-1, ncol = ncol(SERIES), NA), | ||
order.by = index(data)[1:(windowSize-1)]) | ||
names(PAD) <- names(SERIES) | ||
SERIES <- rbind(PAD, SERIES) | ||
} | ||
|
||
if(is.null(names(SERIES))){ | ||
names(SERIES) <- gsub("\\..+", "", names(data)[1:ncol(SERIES)]) | ||
} | ||
|
||
return(SERIES) | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Checks if quantmod is installed, installs it if unavailable, | ||
# loads it and turns off needless warning messages | ||
if(!("quantmod" %in% as.character(installed.packages()[,1]))) | ||
{ install.packages("quantmod") } | ||
library(quantmod) | ||
options("getSymbols.warning4.0"=FALSE, | ||
"getSymbols.auto.assign"=FALSE) | ||
|
||
# Loads S&P 500 ETF data, stores closing prices as a vector | ||
SPY <- suppressWarnings( | ||
getSymbols(c("SPY"),from = "2012-01-01")) | ||
SPY <- as.numeric(SPY$SPY.Close)[1:987] | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Create linearized equity curve and run regression | ||
y <- Et / Vt | ||
model <- lm(y ~ t) | ||
|
||
# Compute PPS by pulling "r.squared" value from summary function | ||
PPS <- ((Et[length(Et)] - Vt[1]) / Vt[1]) * summary(model)$r.squared |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Set Random Seed | ||
set.seed(123) | ||
|
||
# Create Time Index | ||
t <- 1:(length(SPY)-1) | ||
|
||
# Tradable Capital Vector | ||
Vt <- c(rep(10000, length(t))) | ||
|
||
# Benchmark Return Series | ||
Rb <- rep(NA, length(t)) | ||
for(i in 2:length(t)) { Rb[i] <- (SPY[i] / SPY[i - 1]) - 1 } | ||
|
||
# Benchmark Equity Curve | ||
Eb <- rep(NA, length(t)) | ||
Eb[1] <- Vt[1] | ||
for(i in 2:length(t)) { Eb[i] <- Eb[i-1] * (1 + Rb[i]) } | ||
|
||
# Randomly Simulated Return Series 1 | ||
Rt <- rep(NA, length(t)) | ||
for(i in 2:length(t)){ | ||
Rt[i] <- Rb[i] + rnorm(n = 1, | ||
mean = 0.24/length(t), | ||
sd = 2.5 * sd(Rb, na.rm = TRUE)) | ||
} | ||
|
||
# Randomly Simulated Return Series 2 | ||
Rt2 <- rep(NA, length(t)) | ||
for(i in 2:length(t)){ | ||
Rt2[i] <- Rb[i] + rnorm(n = 1, | ||
mean = 0.02/length(t), | ||
sd = .75 * sd(Rb, na.rm = TRUE)) | ||
} | ||
|
||
# Randomly Simulated Equity Curve 1 | ||
Et <- rep(NA, length(t)) | ||
Et <- Vt[1] | ||
for(i in 2:length(t)) { Et[i] <- Et[i-1] * (1 + Rt[i]) } | ||
|
||
# Randomly Simulated Equity Curve 2 | ||
Et2 <- rep(NA, length(t)) | ||
Et2 <- Vt[1] | ||
for(i in 2:length(t)) { Et2[i] <- Et2[i-1] * (1 + Rt2[i]) } | ||
|
||
# Plot of Et1 against the SPY Portfolio | ||
plot(y = Et, x = t, type = "l", col = 1, | ||
xlab = "Time", | ||
ylab= "Equity ($)", | ||
main = "Figure 1.3: Randomly Generated Equity Curves") | ||
grid() | ||
abline(h = 10000) | ||
lines(y = Et2, x = t, col = 2) | ||
lines(y = Eb, x = t, col = 8) | ||
legend(x = "topleft", col = c(1,2,8), lwd = 2, legend = c("Curve 1", | ||
"Curve 2", | ||
"SPY")) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Use na.rm = TRUE to ignore NA's at position 1 in return series | ||
SR <- mean(Rt, na.rm = TRUE) / sd(Rt, na.rm = TRUE) | ||
SR2 <- mean(Rt2, na.rm = TRUE) / sd(Rt2, na.rm = TRUE) | ||
SRb <- mean(Rb, na.rm = TRUE) / sd(Rb, na.rm = TRUE) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
plot(y = Et, x = t, type = "l", col = 1, | ||
xlab = "", | ||
ylab= "Equity ($)", | ||
main = "Figure 1.4: Sharpe Ratios") | ||
grid() | ||
abline(h = 10000) | ||
lines(y = Et2, x = t, col = 2) | ||
lines(y = Eb, x = t, col = 8) | ||
legend(x = "topleft", col = c(1,2,8), lwd = 2, | ||
legend = c(paste0("SR = ", round(SR, 3)), | ||
paste0("SR = ", round(SR2, 3)), | ||
paste0("SR = ", round(SRb, 3)))) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MD <- function(curve, n = 1){ | ||
|
||
time <- length(curve) | ||
v <- rep(NA, (time * (time - 1)) / 2) | ||
k <- 1 | ||
for(i in 1:(length(curve)-1)){ | ||
for(j in (i+1):length(curve)){ | ||
v[k] <- curve[i] - curve[j] | ||
k <- k + 1 | ||
} | ||
} | ||
|
||
m <- rep(NA, length(n)) | ||
for(i in 1:n){ | ||
m[i] <- max(v) | ||
v[which.max(v)] <- -Inf | ||
} | ||
|
||
return(m) | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
NPMD <- (Et[length(Et)] - Vt[1]) / MD(Et) | ||
|
||
Burke <- (Et[length(Et)] - Vt[1]) / | ||
sqrt((1/length(Et)) * sum(MD(Et, n = round(length(Et) / 20))^2)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
PM <- function(Rt, upper = FALSE, n = 2, Rb = 0){ | ||
if(n != 0){ | ||
if(!upper) return(mean(pmax(Rb - Rt, 0, na.rm = TRUE)^n)) | ||
if(upper) return(mean(pmax(Rt - Rb, 0, na.rm = TRUE)^n)) | ||
} else { | ||
if(!upper) return(mean(Rb >= Rt)) | ||
if(upper) return(mean(Rt > Rb)) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Omega <- mean(Rt, na.rm = TRUE) / PM(Rt)^0.5 | ||
UPR <- PM(Rt, upper = TRUE)^0.5 / PM(Rt)^0.5 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Scatterplot of Rt against Rb | ||
plot(y = Rt, x = Rb, | ||
pch = 20, | ||
cex = 0.5, | ||
xlab = "SPY Returns", | ||
ylab= "Return Series 1", | ||
main = "Figure 1.7: Return Series 1 vs. SPY") | ||
grid() | ||
abline(h = 0) | ||
abline(v = 0) | ||
|
||
# Compute and store the regression model | ||
model <- lm(Rt ~ Rb) | ||
|
||
# Plot the regression line | ||
abline(model, col = 2) | ||
|
||
# Display alpha and beta | ||
legend(x = "topleft", col = c(0,2), lwd = 2, | ||
legend = c("Alpha Beta R^2", | ||
paste0(round(model$coefficients[1], 4), " ", | ||
round(model$coefficients[2], 2), " ", | ||
round(summary(model)$r.squared, 2)))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Warning: These are not to be run concurrently | ||
|
||
# UPDATE Job | ||
source("~/Platform/update.R") | ||
|
||
# PLAN Job | ||
source("~/Platform/plan.R") | ||
|
||
# TRADE Job | ||
source("~/Platform/trade.R") | ||
|
||
# MODEL Job | ||
source("~/Platform/model.R") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 19 * * 1-5 ~/Platform/plan.sh |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
set path= %path%;C:\Program Files\R\R-3.3.0\bin\x64 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Rscript C:\Platform\plan.R |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
cd C:\Platform\errorlog | ||
Rscript C:\Platform\plan.R > planlog.txt 2>&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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
set path= %path%;C:\Program Files\R\R-3.2.3\bin | ||
cd C:\Platform\errorlog\ | ||
Rscript C:\Platform\plan.R > planlog.txt 2>&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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
schtasks /create /tn PLAN /sc weekly /d mon,tue,wed,thu,fri /mo 1 /st 19:00 | ||
/tr "C:\Platform\plan.bat" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Delete a task | ||
schtasks /delete /tn PLAN | ||
|
||
# Run a task | ||
schtasks /run /tn PLAN | ||
|
||
# End a currently run task, does not affect scheduling | ||
schtasks /end /tn PLAN | ||
|
||
# Get info on a task | ||
schtasks /query /tn PLAN | ||
|
||
# Modify a task (this example removes Wednesday from PLAN) | ||
schtasks /change /tn PLAN /d mon,tue,thu,fri | ||
|
||
# Disable a task, cancel scheduling | ||
schtasks /change /tn PLAN /disable | ||
|
||
# Enable an inactive task, resume scheduling | ||
schtasks /change /tn PLAN /enable |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Edit CRON jobs | ||
crontab -e | ||
|
||
# Delete all user-specified CRON jobs | ||
crontab -r |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
cd ~/Platform/errorlog | ||
Rscript ~/Platform/plan.R > planlog.txt 2>&1 |
Oops, something went wrong.