-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathLinearStrategy.R
More file actions
219 lines (218 loc) · 6.5 KB
/
LinearStrategy.R
File metadata and controls
219 lines (218 loc) · 6.5 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# ###################################################
## LINEAR MODEL STRATEGY :
## BUY SIGNAL (at t)
## * E[ FPC (t)] >= 0.2
##
## SELL SIGNAL (at t)
## * E[ FPC (t)] <= -0.2
##
## if signal hits , buy or sell maximum position
# ###################################################
source (file = ' GetMainContract.R')
source (file = ' BuildLinearData.R')
LinearStrategy <-
function (data ,
coefs ,
lags ,
strategy = 'A',
threshold = 0.2 ,
morning = T,
open.int = F,
trade.at.mid = F,
functions = NULL) {
## get all the market data ( this would be a data - stream in a real - time system )
TR.COST <- 2.5 * 1e - 5
value <-
BuildLinearData (
data ,
morning = morning ,
open.int = open.int ,
delay = 0,
lags = lags
)
main.data <- value$data
n <- nrow (main.data)
mid.price <- value$mid.price
spread <- value$spread
time.secs <- value$time.secs
ind.open <- value$ind.open
ind.close <- value$ind.close
own <- F
pos <- 0
strat <- rep (0, n)
realized.pnl <- rep(NA , n)
total.trade.pnl <- c()
returns <- c()
pnl <- 0
trade.costs <- 0
buy.price <- 0
sell.price <- 0
entry <- 0
trade.volume <- 0
sharpes <- c()
# get the vector of bid/ ask prices ( this will be scalar in data stream )
ask <- if (trade.at.mid)
mid.price
else
main.data$AskPrice1
bid <- if (trade.at.mid)
mid.price
else
main.data$BidPrice1
# Set the x- values to be used in prediction depending on strategy
# these would be scalar in a data stream
VOI <- value$VOI
OIR <- value$OIR
MPB <- value$MPB
identity <- function (x)
x
f.VOI <-
if (is.null (functions [['VOI ']]))
identity
else
functions [['VOI ']]
f.OIR <-
if (is.null (functions [['OIR ']]))
identity
else
functions [['OIR ']]
x <- cbind (rep (1, n))
if (strategy == 'A') {
x <- cbind (x, f.VOI (VOI))
} else if (strategy == 'B') {
x <-
cbind (x, f.VOI (VOI) / spread , f.OIR(OIR) / spread , MPB [, 1] / spread)
} else {
stop (paste ('Missing Linear Strategy :', strategy))
}
# this is where we assume we get a data stream instead of looping through the
dataset
# multiply the coefficients with the factors and check if it 's above / below
threshold
# and trade if the signal is good
# in an actual trading system , the decision would be calculated by a strategy
engine
# having the real - time data fed into the engine via a data stream
# but in this simulation , we just assume we have the full dataset and the
# strategy engine is the coefficient multiplication on the next line
efpc.vec <-
rowSums (x * matrix (rep (coefs , n), byrow = T, nrow = n))
# each k = 500 ms;
for (k in trade.ind) {
efpc <- efpc.vec [k]
## check if we are within trading hours
if (k >= ind.open &
k < ind.close & own == F & efpc >= threshold) {
## BUY to OPEN
strat [k] <- 1
own = T
pos <- 1
buy.price <- ask[k]
entry <- k
tc <- buy.price * TR.COST
trade.costs <- trade.costs + tc
trade.volume <- trade.volume + 1
} else if (k >= ind.open &
k < ind.close & own == F & efpc <= -threshold) {
## SELL to OPEN
strat [k] <- -1
own = T
pos <- -1
sell.price <- bid[k]
entry <- k
tc <- sell.price * TR.COST
trade.costs <- trade.costs + tc
trade.volume <- trade.volume + 1
} else if (own == T & pos == 1 & efpc <= -threshold) {
## SELL to CLOSE
strat [k] <- -1
own <- F
pos <- 0
sell.price <- bid[k]
tc <- tc + sell.price * TR.COST
trade.costs <- trade.costs + tc
trade.pnl <- sell.price - buy.price - tc
pnl <- pnl + trade.pnl
trade.volume <- trade.volume + 1
total.trade.pnl <- c(total.trade.pnl , trade.pnl)
if (k >= ind.open & k < ind.close) {
## SELL to OPEN
strat [k] <- -2
own <- T
pos <- -1
sell.price <- bid[k]
entry <- k
tc <- sell.price * TR.COST
trade.costs <- trade.costs + tc
trade.volume <- trade.volume + 1
}
} else if (own == T & pos == -1 & efpc >= threshold) {
## BUY to CLOSE
strat [k] <- 1
own = F
pos <- 0
buy.price <- ask[k]
tc <- tc + buy.price * TR.COST
trade.costs <- trade.costs + tc
trade.pnl <- sell.price - buy.price - tc
pnl <- pnl + trade.pnl
trade.volume <- trade.volume + 1
total.trade.pnl <- c(total.trade.pnl , trade.pnl)
if (k >= ind.open & k < ind.close) {
## BUY to OPEN
strat [k] <- 2
own <- T
pos <- 1
buy.price <- ask[k]
entry <- k
tc <- buy.price * TR.COST
trade.costs <- trade.costs + tc
trade.volume <- trade.volume + 1
}
}
realized.pnl [k] <- pnl
}
# check if we have a left - over position at end -of - day and close it
if (sum (strat) == 1) {
if (strat [n] == 1) {
strat [n] <- 0
trade.volume <- trade.volume - 1
} else {
strat [n] <- -1
sell.price <- bid[n]
tc <- tc + sell.price * TR.COST
trade.costs <- trade.costs + tc
trade.pnl <- sell.price - buy.price - tc
pnl <- pnl + trade.pnl
realized.pnl [n] <- pnl
total.trade.pnl <- c(total.trade.pnl , trade.pnl)
trade.volume <- trade.volume + 1
}
} else if (sum (strat) == -1) {
if (strat [n] == -1) {
strat [n] <- 0
trade.volume <- trade.volume - 1
} else {
strat [n] <- 1
buy.price <- ask[n]
tc <- tc + buy.price * TR.COST
trade.costs <- trade.costs + tc
trade.pnl <- (sell.price - buy.price) - tc
pnl <- pnl + trade.pnl
realized.pnl [n] <- pnl
total.trade.pnl <- c(total.trade.pnl , trade.pnl)
trade.volume <- trade.volume + 1
}
}
# return stats
realized.pnl <- na.locf (c(0, realized.pnl))[-1]
value <- {
}
value$time <- time.secs
value$pnl <- realized.pnl
value$strategy <- strat
value$trade.volume <- trade.volume
value$trade.pnl <- total.trade.pnl
value$trade.costs <- trade.costs
return (value)
}