-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmarket_making.R
More file actions
executable file
·414 lines (348 loc) · 15 KB
/
Copy pathmarket_making.R
File metadata and controls
executable file
·414 lines (348 loc) · 15 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# make_market_loop() Function for market making strategy - loop version
make_market <- function(ohlc, ohlc_lag=rutils::lagit(ohlc),
buy_spread, sell_spread, lambda, invent_limit, warm_up) {
nrows <- NROW(ohlc)
openp <- ohlc[, 1]
highp <- ohlc[, 2]
lowp <- ohlc[, 3]
closep <- ohlc[, 4]
# lookb <- 111
# weightv <- exp(-lambda*1:lookb)
# weightv <- weightv/sum(weightv)
ew_ma <- numeric(nrows)
ew_ma[1] <- ohlc[1, 6]
# ew_ma <- drop(ew_ma)
# bia_s is the spread for biasing the limit price, depending on the ew_ma
# bia_s <- ifelse(ohlc_lag[, 4] > ew_ma, 0.25, -0.25)
bia_s <- numeric(nrows)
# buylimit <- numeric(nrows)
# sell_limit <- numeric(nrows)
n_buys <- numeric(nrows)
n_sells <- numeric(nrows)
# inventory
inv_ent <- numeric(nrows)
buy_s <- numeric(nrows)
sell_s <- numeric(nrows)
re_al <- numeric(nrows)
un_real <- numeric(nrows)
pnls <- numeric(nrows)
for (it in 1:(nrows-1)) {
# cat("iteration:", it, "\n")
# b_spread <- buy_spread
# s_spread <- sell_spread
# Spread widening rules to limit inventory risk
# limit prices are the low and high prices of the lagged bar, plus the spreads
# doesn't work well
# if (n_sells[it-1] > n_buys[it-1])
# s_spread <- sell_spread + ((n_sells[it-1] - n_buys[it-1]) %/% 25) / 4
# else
# b_spread <- buy_spread + ((n_buys[it-1] - n_sells[it-1]) %/% 25) / 4
# Or
# if (n_sells[it-1] > n_buys[it-1])
# b_spread <- buy_spread - ((n_sells[it-1] - n_buys[it-1]) %/% 25) / 4
# else
# s_spread <- sell_spread - ((n_buys[it-1] - n_sells[it-1]) %/% 25) / 4
# Or
# if ((n_sells[it-1]-n_buys[it-1]) > 20)
# s_spread <- sell_spread + 5
# if ((n_buys[it-1]-n_sells[it-1]) > 20)
# b_spread <- buy_spread + 5
ew_ma[it] <- lambda*ohlc[it, 6] + (1-lambda)*ew_ma[max(it-1, 1)]
if (it > warm_up) {
# bia_s[it] <- (if (ohlc_lag[it, 4] > ew_ma[it]) 0.25 else -0.25)
buylimit <- (ohlc_lag[it, 3] - buy_spread + bia_s[it])
# buylimit should be no greater than previous close price
buylimit <- min(closep[it], buylimit)
sell_limit <- (ohlc_lag[it, 2] + sell_spread + bia_s[it])
# sell_limit should be no less than previous close price
sell_limit <- max(closep[it], sell_limit)
# Trade in the next period - but don't trade if inventory exceeds limit
buy_ind <- (lowp[it+1] < buylimit) & (inv_ent[it] < invent_limit)
sell_ind <- (highp[it+1] > sell_limit) & (inv_ent[it] > -invent_limit)
n_buys[it+1] <- n_buys[it] + buy_ind
n_sells[it+1] <- n_sells[it] + sell_ind
inv_ent[it+1] <- (n_buys[it+1] - n_sells[it+1])
buy_s[it+1] <- buy_s[it] + buy_ind*buylimit
sell_s[it+1] <- sell_s[it] + sell_ind*sell_limit
# Realized and unrealized pnls
mark_to_market <- (-closep[it+1]*inv_ent[it+1])
# This part takes long to run, so commented out
# past_buys <- buy_s[match(n_sells[it], n_buys)]
# past_sells <- sell_s[match(n_buys[it], n_sells)]
#
# re_al[it] <- ifelse(n_buys[it] > n_sells[it],
# sell_s[it] - past_buys,
# past_sells - buy_s[it])
# un_real[it] <- ifelse(n_buys[it] > n_sells[it],
# past_buys - buy_s[it] - mark_to_market,
# sell_s[it] - past_sells - mark_to_market)
# Pnls equal to difference between filled sell minus buy prices, minus mark_to_market
pnls[it+1] <- ((sell_s[it+1] - buy_s[it+1]) - mark_to_market)
} # end for
} # end if
pnls <- cbind(ohlc[, 1:4], pnls, inv_ent, re_al, un_real, ew_ma)
colnames(pnls)[5:9] <- c("Strategy PnL", "Inventory", "Realized PnL", "Unrealized PnL", "EWMA")
pnls
} # end make_market
# make_market_vec() Function for market making strategy - vectorized version
make_market_vec <- function(ohlc, ohlc_lag=rutils::lagit(ohlc),
# stdev,
buy_spread, sell_spread, lambda, invent_limit) {
openp <- ohlc[, 1]
highp <- ohlc[, 2]
lowp <- ohlc[, 3]
closep <- ohlc[, 4]
lookb <- 111
weightv <- exp(-lambda*1:lookb)
weightv <- weightv/sum(weightv)
ew_ma <- HighFreq::roll_wsum(vectorv=rutils::lagit(ohlc)[, 4], weightv=rev(weightv))
ew_ma <- drop(ew_ma)
# bia_s is the spread for biasing the limit price, depending on the ew_ma
# bia_s <- ifelse(ohlc_lag[, 4] > ew_ma, 0.25, -0.25)
# Run the trading model (strategy):
# bia_s <- numeric(nrows)
bia_s <- ifelse(ohlc_lag[, 4] > ew_ma, 0.25, -0.25)
# limit prices are the low and high prices of the lagged bar, plus the spreads
buylimit <- (ohlc_lag[, 3] - buy_spread + bia_s)
sell_limit <- (ohlc_lag[, 2] + sell_spread + bia_s)
# lag1 <- rutils::lagit(ohlc)
# buylimit <- (pmin(ohlc_lag[, 3], lag1[, 3]) - buy_spread)
# sell_limit <- (pmax(ohlc_lag[, 2], lag1[, 2]) + sell_spread)
# Buylimit should be no greater than open price openp
buylimit <- pmin(openp, buylimit)
# sell_limit should be no less than open price openp
sell_limit <- pmax(openp, sell_limit)
# Indicators of whether the orders were filled
buy_ind <- (lowp < buylimit)
sell_ind <- (highp > sell_limit)
# Cumulative numbers of filled orders
n_buys <- cumsum(buy_ind)
n_sells <- cumsum(sell_ind)
# Prices of the filled orders
nrows <- NROW(ohlc)
buy_s <- numeric(nrows)
buy_s[buy_ind] <- buylimit[buy_ind]
buy_s <- cumsum(buy_s)
sell_s <- numeric(nrows)
sell_s[sell_ind] <- sell_limit[sell_ind]
sell_s <- cumsum(sell_s)
# Realized and unrealized pnls
mark_to_market <- closep*(n_sells - n_buys)
past_buys <- buy_s[match(n_sells, n_buys)]
past_sells <- sell_s[match(n_buys, n_sells)]
re_al <- ifelse(n_buys > n_sells,
sell_s - past_buys,
past_sells - buy_s)
un_real <- ifelse(n_buys > n_sells,
past_buys - buy_s - mark_to_market,
sell_s - past_sells - mark_to_market)
# Pnls equal to difference between filled sell minus buy prices and mark_to_market
pnls <- ((sell_s-buy_s) - mark_to_market)
pnls <- cbind(ohlc[, 1:4], pnls, n_buys-n_sells, re_al, un_real, ew_ma)
colnames(pnls)[5:9] <- c("Strategy PnL", "Inventory", "Realized PnL", "Unrealized PnL", "EWMA")
pnls
} # end make_market_vec
###
# trade_median() Function for market making strategy - vectorized version
trade_median <- function(returns, ohlc, ohlc_lag=rutils::lagit(ohlc),
lookb, threshold, buy_spread, sell_spread, lambda, invent_limit) {
nrows <- NROW(ohlc)
openp <- ohlc[, 1]
highp <- ohlc[, 2]
lowp <- ohlc[, 3]
closep <- ohlc[, 4]
# weightv <- exp(-lambda*1:lookb)
# weightv <- weightv/sum(weightv)
# ew_ma <- HighFreq::roll_wsum(vectorv=rutils::lagit(ohlc)[, 4], weightv=rev(weightv))
# ew_ma <- drop(ew_ma)
# bia_s is the spread for biasing the limit price, depending on the ew_ma
# bia_s <- ifelse(ohlc_lag[, 4] > ew_ma, 0.25, -0.25)
mean_roll <- TTR::runMean(x=returns, n=lookb)
median_roll <- TTR::runMedian(x=returns, n=lookb)
mad_roll <- TTR::runMAD(x=returns, n=lookb)
indic <- rutils::lagit((mean_roll - median_roll)/mad_roll)
indic <- zoo::na.locf(indic, na.rm=FALSE)
indic <- zoo::na.locf(indic, na.rm=FALSE, fromLast=TRUE)
# indic <- (indic > threshold)
# Run the trading model (strategy):
# bia_s <- numeric(nrows)
# limit prices are the low and high prices of the lagged bar, plus the spreads
buylimit <- rep(NA_integer_, nrows)
# buylimit <- ifelse((abs(rutils::diffit(indic > threshold)) > 0), ohlc_lag[, 3] - buy_spread, 0)
buylimit <- ifelse((abs(rutils::diffit(indic < (-threshold))) > 0), ohlc_lag[, 4] - buy_spread, 0)
buylimit <- zoo::na.locf(buylimit, na.rm=FALSE)
buylimit <- zoo::na.locf(buylimit, na.rm=FALSE, fromLast=TRUE)
sell_limit <- rep(NA_integer_, nrows)
# sell_limit <- ifelse((abs(rutils::diffit(indic < (-threshold))) > 0), ohlc_lag[, 2] + sell_spread, ohlc_lag[, 2] + 1e5)
sell_limit <- ifelse((abs(rutils::diffit(indic > threshold)) > 0), ohlc_lag[, 4] + sell_spread, ohlc_lag[, 2] + 1e5)
sell_limit <- zoo::na.locf(sell_limit, na.rm=FALSE)
sell_limit <- zoo::na.locf(sell_limit, na.rm=FALSE, fromLast=TRUE)
# lag1 <- rutils::lagit(ohlc)
# buylimit <- (pmin(ohlc_lag[, 3], lag1[, 3]) - buy_spread)
# sell_limit <- (pmax(ohlc_lag[, 2], lag1[, 2]) + sell_spread)
# Buylimit should be no greater than open price openp
buylimit <- pmin(openp, buylimit)
# sell_limit should be no less than open price openp
sell_limit <- pmax(openp, sell_limit)
# Indicators of whether the orders were filled
buy_ind <- (lowp < buylimit)
sell_ind <- (highp > sell_limit)
# Cumulative numbers of filled orders
n_buys <- cumsum(buy_ind)
n_sells <- cumsum(sell_ind)
# Prices of the filled orders
buy_s <- numeric(nrows)
buy_s[buy_ind] <- buylimit[buy_ind]
buy_s <- cumsum(buy_s)
sell_s <- numeric(nrows)
sell_s[sell_ind] <- sell_limit[sell_ind]
sell_s <- cumsum(sell_s)
# Realized and unrealized pnls
mark_to_market <- closep*(n_sells - n_buys)
past_buys <- buy_s[match(n_sells, n_buys)]
past_sells <- sell_s[match(n_buys, n_sells)]
re_al <- ifelse(n_buys > n_sells,
sell_s - past_buys,
past_sells - buy_s)
un_real <- ifelse(n_buys > n_sells,
past_buys - buy_s - mark_to_market,
sell_s - past_sells - mark_to_market)
# Pnls equal to difference between filled sell minus buy prices and mark_to_market
pnls <- ((sell_s-buy_s) - mark_to_market)
# pnls <- cumsum(sign(indic)*returns)
pnls <- cbind(ohlc[, 1:4], pnls, n_buys-n_sells, re_al, un_real)
colnames(pnls)[5:8] <- c("Strategy PnL", "Inventory", "Realized PnL", "Unrealized PnL")
pnls
} # end trade_median
###
# make_market_ewma() Function for EWMA crossover strategy
make_market_ewma <- function(ohlc, ohlc_lag=rutils::lagit(ohlc), lagg,
# stdev,
buy_spread, sell_spread, lambda, invent_limit,
lookb=100, warm_up=100) {
# lookb <- 111
# weightv <- exp(-lambda*1:lookb)
# weightv <- weightv/sum(weightv)
# ew_ma <- HighFreq::roll_wsum(vectorv=ohlc[, 4], weightv=rev(weightv))
# ew_ma <- drop(ew_ma)
# Run the trading model (strategy):
## Vector code
# bia_s <- numeric(nrows)
# bia_s <- rutils::lagit(ifelse(ohlc[, 4] > ew_ma, -1, 1), lagg=lagg)
# Pnls equal to sum
# pnls <- cumsum(bia_s*rutils::diffit(ohlc[, 4]))
# pnls <- cbind(ohlc[, 1:4], pnls, ew_ma)
# colnames(pnls)[5:6] <- c("Strategy PnL", "EWMA")
## Loop code
nrows <- NROW(ohlc)
openp <- ohlc[, 1]
highp <- ohlc[, 2]
lowp <- ohlc[, 3]
closep <- ohlc[, 4]
# lookb <- 111
# weightv <- exp(-lambda*1:lookb)
# weightv <- weightv/sum(weightv)
ew_ma <- numeric(nrows)
ew_ma[1] <- ohlc[1, 6]
z_score <- numeric(nrows)
# ew_ma <- drop(ew_ma)
# bia_s is the spread for biasing the limit price, depending on the ew_ma
# bia_s <- ifelse(ohlc_lag[, 4] > ew_ma, 0.25, -0.25)
# buylimit <- numeric(nrows)
# sell_limit <- numeric(nrows)
n_buys <- numeric(nrows)
n_sells <- numeric(nrows)
# inventory
inv_ent <- numeric(nrows)
buy_s <- numeric(nrows)
sell_s <- numeric(nrows)
re_al <- numeric(nrows)
un_real <- numeric(nrows)
pnls <- numeric(nrows)
for (it in 1:(nrows-1)) {
# cat("iteration:", it, "\n")
# b_spread <- buy_spread
# s_spread <- sell_spread
# Spread widening rules to limit inventory risk
# limit prices are the low and high prices of the lagged bar, plus the spreads
# doesn't work well
# if (n_sells[it-1] > n_buys[it-1])
# s_spread <- sell_spread + ((n_sells[it-1] - n_buys[it-1]) %/% 25) / 4
# else
# b_spread <- buy_spread + ((n_buys[it-1] - n_sells[it-1]) %/% 25) / 4
# Or
# if (n_sells[it-1] > n_buys[it-1])
# b_spread <- buy_spread - ((n_sells[it-1] - n_buys[it-1]) %/% 25) / 4
# else
# s_spread <- sell_spread - ((n_buys[it-1] - n_sells[it-1]) %/% 25) / 4
# Or
# if ((n_sells[it-1]-n_buys[it-1]) > 20)
# s_spread <- sell_spread + 5
# if ((n_buys[it-1]-n_sells[it-1]) > 20)
# b_spread <- buy_spread + 5
ew_ma[it] <- lambda*ohlc[it, 6] + (1-lambda)*ew_ma[max(it-1, 1)]
# z_score[it] <- calc_zscore(val_ue=closep[it],
# tseries=closep[max(it-warm_up, 1):max(it-1, 1)],
# design, design_inv, design2, oo_s, oos_t, deg_free)
if (it > warm_up) {
sell_limit <- 0
buylimit <- 0
# if (closep[it-lagg] > ew_ma[it-lagg]) {
if (closep[it-lagg] > ew_ma[it-lagg]) {
# for limit order
sell_limit <- (highp[it] + sell_spread)
sell_ind <- (highp[it+1] > sell_limit) & (inv_ent[it] > -invent_limit)
# for market order
# sell_limit <- openp[it+1]
# sell_ind <- 1
n_sold <- sell_ind*max(1+inv_ent[it], 0)
n_bot <- 0
} else {
# for limit order
buylimit <- (lowp[it] - buy_spread)
buy_ind <- (lowp[it+1] < buylimit) & (inv_ent[it] < invent_limit)
# for market order
# buylimit <- openp[it+1]
# buy_ind <- 1
n_bot <- buy_ind*max(1-inv_ent[it], 0)
n_sold <- 0
} # end if
n_sells[it+1] <- n_sells[it] + n_sold
sell_s[it+1] <- sell_s[it] + sell_limit*n_sold
n_buys[it+1] <- n_buys[it] + n_bot
buy_s[it+1] <- buy_s[it] + buylimit*n_bot
# Trade in the next period - but don't trade if inventory exceeds limit
inv_ent[it+1] <- (n_buys[it+1] - n_sells[it+1])
# Realized and unrealized pnls
mark_to_market <- (-closep[it+1]*inv_ent[it+1])
# This part takes long to run, so commented out
# past_buys <- buy_s[match(n_sells[it], n_buys)]
# past_sells <- sell_s[match(n_buys[it], n_sells)]
#
# re_al[it] <- ifelse(n_buys[it] > n_sells[it],
# sell_s[it] - past_buys,
# past_sells - buy_s[it])
# un_real[it] <- ifelse(n_buys[it] > n_sells[it],
# past_buys - buy_s[it] - mark_to_market,
# sell_s[it] - past_sells - mark_to_market)
# Pnls equal to difference between filled sell minus buy prices, minus mark_to_market
pnls[it+1] <- ((sell_s[it+1] - buy_s[it+1]) - mark_to_market)
} # end for
} # end if
ew_ma[nrows] <- lambda*ohlc[nrows, 6] + (1-lambda)*ew_ma[nrows-1]
pnls <- cbind(ohlc[, 1:4], pnls, inv_ent, re_al, un_real, ew_ma)
colnames(pnls)[5:9] <- c("Strategy PnL", "Inventory", "Realized PnL", "Unrealized PnL", "EWMA")
pnls
} # end make_market_ewma
###
# calc_zscore() Function calculates z-scores of out-of-sample values relative to their predictions
calc_zscore <- function(val_ue, tseries, design, design_inv, design2, oo_s, oos_t, deg_free) {
if (NROW(tseries) < deg_free) return(0)
betas <- design_inv %*% tseries
fit_ted <- drop(design %*% betas)
residuals <- (tseries - fit_ted)
r_ss <- sqrt(sum(residuals^2)/deg_free)
predic_tions <- cbind(predicted=drop(oo_s %*% betas),
stddev=diag(r_ss*sqrt(oo_s %*% design2 %*% oos_t)))
(val_ue-predic_tions[1])/predic_tions[2]
} # end calc_zscore