-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathhfreq_aggregation.R
More file actions
executable file
·128 lines (105 loc) · 3.7 KB
/
Copy pathhfreq_aggregation.R
File metadata and controls
executable file
·128 lines (105 loc) · 3.7 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
################################################
### file contains scripts for high frequency data tasks:
###
### 1. estimating variance, skewness, and kurtosis,
###
### 2. running simple trading strategies,
###
### 3. plotting using chart_Series,
###
################################################
# plotting
chart_Series(get(symbol)[rangev], name=symbol)
chart_Series(volat, name=paste(symbol, "vol"))
# plotting skew with custom y-axis range
# extract and modify plot object to reduce y-axis range
chobj <- chart_Series(x=skew, plot=FALSE)
# extract and modify ylim using accessor and setter functions
ylim <- chobj$get_ylim()
ylim[[2]] <- structure(c(-1, 1), fixed=TRUE)
chobj$set_ylim(ylim)
# render the plot
plot(chobj)
chart_xts(skew)
chart_xts(skew, ylim=c(-1, 1))
### simple trading strategy for daily aggs
returns <- get(symbol)[index(skew), 4]
returns <- diff(log(returns))
colnames(returns) <- paste(symbol, "Ret", sep=".")
# correlation
blah <- na.omit(merge(skew, returns))
colnames(blah) <- c(colnames(blah)[1], "SPY.Ret")
# scatterplot of skew and returns
plot(coredata(blah["2008-09/2009-05"]))
cor(coredata(blah["2008-09/2009-05"]))
cor.test(coredata(blah["2008-09/2009-05"])[, 1], coredata(blah["2008-09/2009-05"])[, 2])
# run strategy out-of-sample
blah <- -sign(lag(skew))
colnames(blah) <- paste(symbol, "Posit", sep=".")
blah <- na.omit(merge(blah, returns))
# scatterplot of skew and returns
plot(coredata(blah["2008-09/2009-05"]))
cor(coredata(blah["2008-09/2009-05"]))
cor.test(coredata(blah["2008-09/2009-05"])[, 1], coredata(blah["2008-09/2009-05"])[, 2])
blah <- cumsum(blah[, 1]*blah[, 2])
plot(blah)
### simple trading strategy for rolling minute aggs
# signal threshold trading level
threshold <- 0.5
# score <- sign(skew-threshold)
score <- NA*numeric(nrow(skew))
score <- ifelse(skew>threshold, -1, score)
score <- ifelse(skew<(-threshold), 1, score)
score <- ifelse((skew*lag(skew))<0, 0, score)
# score <- xts(x=score, order.by=index(skew))
# lag the signal by one
# score <- lag(score)
score <- c(0, score)
score <- score[-length(score)]
score <- na.locf(score)
score <- merge(skew, score)
colnames(score)[2] <- "positions"
# number of bars in long and short positions
sum(score[, 2]>0)
sum(score[, 2]<0)
chart_Series(score["2013-10-12/2013-11-13"],
name=paste(symbol, "skew"))
# chart_xts(score["2013-10-12/2013-11-13"])
# plotting
rangev <- "2013-10-12/2013-11-13"
# extract and modify plot object to reduce y-axis range
chobj <- chart_Series(x=score[rangev, 1],
name=paste(colnames(score[rangev, 1]), "/", date()),
plot=FALSE)
# extract and modify ylim using accessor and setter functions
ylim <- chobj$get_ylim()
ylim[[2]] <- structure(c(-1, 1), fixed=TRUE)
chobj$set_ylim(ylim)
# render the plot
plot(chobj)
add_TA(score[rangev, 2]>0, on=-1, col="lightgreen", border=NA)
add_TA(score[rangev, 2]<0, on=-1, col="lightpink", border=NA)
add_TA(score[rangev, 2]==0, on=-1, col="lightgrey", border=NA)
# cumulative PnL
blah <- cumsum(score[, 2]*returns[, 1])
plot(blah, format.labels="%Y-%m")
# number of position flips
blahh <- diff(score[, 2])
blahh[1,] <- 0
head(blahh)
tail(blahh)
sum(abs(blahh))
plotSeries <- function(datav, name.plot, indeks=NULL) {
stopifnot(inherits(datav, "xts"))
theme <- chart_theme()
# if(name=='Signals') theme$col$line.col = 'red'
invisible(chart_Series(datav, name=name.plot))
if (!is.null(indeks))
{
invisible(add_TA(indeks$positions>0, on=-1, col="lightgreen", border=NA))
invisible(add_TA(indeks$positions<0, on=-1, col="lightgrey", border=NA))
invisible(add_TA(indeks$stop.loss>0, on=-1, col="red", border=NA))
}
# Plot the charts and suppress warnings
suppressWarnings(.chob)
} # end plotSeries