forked from dcdillon/dependable-development
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_first_pass.R
80 lines (59 loc) · 2.14 KB
/
01_first_pass.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
library(xts)
symbols <- c('SPY', 'XLB', 'XLE', 'XLF', 'XLI', 'XLK', 'XLP', 'XLU', 'XLV', 'XLY', 'XME')
dates <- c('2017-01-03', '2017-01-04', '2017-01-05', '2017-01-06', '2017-01-09', '2017-01-10', '2017-01-11', '2017-01-12', '2017-01-13', '2017-01-17', '2017-01-18', '2017-01-19', '2017-01-20')
all_data <- NULL
for (d in dates) {
data <- NULL
for (symbol in symbols) {
filename <- paste0("../data/", symbol, "/", d, ".csv")
a <- read.csv(filename, header=TRUE)
alpha <- .05
a$datetime <- as.POSIXct(a$datetime, tz="America/NewYork")
a <- xts(a[, 'close'], order.by=a$datetime)
start <- as.POSIXct(paste0(d, ' ', '09:31:00'), tz="America/NewYork")
end <- as.POSIXct(paste0(d, ' ', '16:00:00'), tz="America/NewYork")
idx <- seq(from=start, to=end, by=60)
idx <- xts(rep(NA, length(idx)), order.by=idx)
a <- na.locf(merge(idx, a))[index(idx)]
colnames(a) <- c('dummy', 'close')
a <- cbind(a, NA)
ema_col <- paste0('ema.', symbol)
colnames(a)[ncol(a)] <- ema_col
ema <- NULL
for (i in 1:dim(a)[1]) {
if (is.null(ema)) {
ema <- as.numeric(a[i, 'close'])
} else {
ema <- as.numeric((1 - alpha) * ema + alpha * a[i, 'close'])
}
a[i, ema_col] <- ema
}
a <- cbind(a, NA)
forward_col <- paste0('forward.', symbol)
colnames(a)[ncol(a)] <- forward_col
for (i in 1:dim(a)[1]) {
if (i + 10 <= dim(a)[1]) {
a[i, forward_col] <- as.numeric(a[i + 10, 'close'])
}
}
if (is.null(data)) {
data <- a[, -1]
colnames(data) <- c(paste0('close.', symbol), ema_col, forward_col)
} else {
colnames(a) <- c('dummy', paste0('close.', symbol), ema_col, forward_col)
data <- merge(data, a[, -1])
}
}
if (is.null(all_data)) {
all_data <- data
} else {
all_data <- rbind(all_data, data)
}
}
formula <- 'I(forward.SPY - close.SPY) ~ '
for (symbol in symbols[1:length(symbols)]) {
formula <- paste0(formula, 'I(close.', symbol, ' - ema.', symbol, ') + ')
}
formula <- paste0(formula, ' - 1')
result <- lm(formula, data=all_data)
print(summary(result))