-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtechnical_patterns_back-testing.R
299 lines (259 loc) · 9.58 KB
/
technical_patterns_back-testing.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
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
require(quantmod)
require(techchart)
require(beepr)
require(quantstrat)
################# order sizing function ################################
osFixedDollar <- function(timestamp,orderqty, portfolio, symbol,
ruletype, tradeSize, ...){
px <- as.numeric(Cl(mktdata[timestamp,]))
orderqty <- round(tradeSize/px,2)*sign(orderqty)
return(orderqty)
}
################## download data and set up pattern search #############
x <- getSymbols("^GSPC", auto.assign = F)
n <- NROW(x)
k <- 200; l <- 1; carry <- 5
################## run the HS search ##########################
pattern.matches <- list()
for(i in k:n){
y <- x[(i-k+1):i]
tpattern <- find.pattern(y, pip.tolerance = c(1.5))
if(!is.null(tpattern)){
print(paste("pattern found on",as.Date(index(x[i]))))
pattern.matches[[l]] <- tpattern
l <- l+1
}
}
beep()
l <- NROW(pattern.matches)
sig <- as.xts(rep(0,n),as.Date(index(x)))
colnames(sig) <- c("sig")
for(i in 1:l){
match_date <- pattern.matches[[i]]$matches[[1]]$date
match_idx <- match(match_date,index(sig))
sig[match_idx] <- 1
}
exit_sig <- as.xts(rep(1,n),as.Date(index(x)))
colnames(exit_sig) <- c("exit")
for(i in 1:l){
match_date <- pattern.matches[[i]]$matches[[1]]$date
match_idx <- match(match_date,index(sig))
exit_idx <- match_idx+carry
exit_sig[match_idx:exit_idx] <- 0
#if(sum(sig[(exit_idx-carry+1):exit_idx]) >0){
# exit_sig[exit_idx] <- 0
#}
}
sigs <- merge(sig, exit_sig)
x <- merge(OHLC(x),sigs)
#################### run the IHS search #######################
l <- 1; carry <- 5
# run the IHS search
pattern.matches <- list()
for(i in k:n){
y <- x[(i-k+1):i]
tpattern <- find.pattern(y,pattern = pattern.db("IHS")[[1]],
pip.tolerance = c(1.5))
if(!is.null(tpattern)){
print(paste("pattern found on",as.Date(index(x[i]))))
pattern.matches[[l]] <- tpattern
l <- l+1
}
}
beep()
l <- NROW(pattern.matches)
sig <- as.xts(rep(0,n),as.Date(index(x)))
colnames(sig) <- c("sig1")
for(i in 1:l){
match_date <- pattern.matches[[i]]$matches[[1]]$date
match_idx <- match(match_date,index(sig))
sig[match_idx] <- 1
}
exit_sig <- as.xts(rep(1,n),as.Date(index(x)))
colnames(exit_sig) <- c("exit1")
for(i in 1:l){
match_date <- pattern.matches[[i]]$matches[[1]]$date
match_idx <- match(match_date,index(sig))
exit_idx <- match_idx+carry
exit_sig[match_idx:exit_idx] <- 0
#if(sum(sig[(exit_idx-carry+1):exit_idx]) > 0){
# exit_sig[exit_idx] <- 0
#}
}
sigs <- merge(sig, exit_sig)
x <- merge(x,sigs)
########################### set up the back-tester ######################
# clean-up
if (!exists('.blotter')) .blotter <- new.env()
if (!exists('.strategy')) .strategy <- new.env()
suppressWarnings(rm(list = ls(envir = .blotter), envir = .blotter))
suppressWarnings(rm(list = ls(envir = .strategy), envir = .strategy))
# instrument setup
underlying <- "x"
currency("INR")
stock(underlying, currency = "INR", multiplier = 1)
# required quantstrat variables
initDate <- index(x)[1]
initEq <- 1e5
.txnfees = -0
.orderqty = 100
.tradeSize <- initEq
.maxSize <- 100000
.slow = 100
.fast = 50
short.threshold <-0.5
long.threshold <- 0.5
qs.account <- "pattern.act"
qs.portfolio <- "pattern.port"
qs.strategy <- "pattern.strat"
# initialize quantstrat
initPortf(name = qs.portfolio, symbols = underlying, initDate = initDate,
currency='INR')
initOrders(portfolio = qs.portfolio, initDate = initDate)
initAcct(name = qs.account, portfolios = qs.portfolio, initDate = initDate,
initEq = initEq, currency='INR')
#addPosLimit(portfolio=qs.portfolio, symbol=underlying, timestamp=initDate,
# maxpos=.orderqty)
# define strategy
strategy(name = qs.strategy, store = TRUE)
# add indicators
add.indicator(qs.strategy, name="SMA",
arguments=list(x=quote(Cl(mktdata)[,1]), n=.fast),
label="fstMA")
add.indicator(qs.strategy, name="SMA",
arguments=list(x=quote(Cl(mktdata)[,1]), n=.slow),
label="slwMA")
# add signals
# long signal from patterns
add.signal(strategy=qs.strategy, name="sigThreshold",
arguments=list(column="sig1", threshold=short.threshold,
relationship="gt", cross=TRUE),
label="longpattern")
# short signal from patterns
add.signal(strategy=qs.strategy, name="sigThreshold",
arguments=list(column="sig", threshold=short.threshold,
relationship="gt", cross=TRUE),
label="shortpattern")
# timed out exit from long and short positions
add.signal(strategy=qs.strategy, name="sigThreshold",
arguments=list(column="exit1", threshold=0,
relationship="gt", cross=FALSE),
label="timedoutlong")
add.signal(strategy=qs.strategy, name="sigThreshold",
arguments=list(column="exit", threshold=0,
relationship="gt", cross=FALSE),
label="timedoutshort")
# momentum signals
add.signal(strategy=qs.strategy, name="sigComparison",
arguments=list(column=c("slwMA","fstMA"),
relationship="gt", cross=FALSE),
label="momshort")
add.signal(strategy=qs.strategy, name="sigComparison",
arguments=list(column=c("slwMA","fstMA"),
relationship="lt", cross=FALSE),
label="momlong")
# entry to long and short positions
add.signal(strategy=qs.strategy, name="sigAND",
arguments=list(column=c("longpattern","longpattern"), cross=TRUE),
label="longEntry")
add.signal(strategy=qs.strategy, name="sigAND",
arguments=list(column=c("shortpattern","shortpattern"),
cross=TRUE),
label="shortEntry")
# exit from long and short positions
add.signal(strategy=qs.strategy, name="sigAND",
arguments=list(column=c("timedoutlong","momshort"), cross=TRUE),
label="longExit")
add.signal(strategy=qs.strategy, name="sigAND",
arguments=list(column=c("timedoutshort","momlong"),
cross=TRUE),
label="shortExit")
# add long entry rule
add.rule(strategy=qs.strategy, name='ruleSignal',
arguments=list(sigcol='longEntry' , sigval=TRUE,
orderside='long' ,
ordertype='market',
orderqty= .orderqty,
#tradeSize=.tradeSize,
#maxSize = .maxSize,
#osFUN=osMaxPos,
osFUN="osFixedDollar", tradeSize=.tradeSize,
orderset='ocolong',
replace=FALSE
),
type='enter',
label='EnterLONG'
)
# add short entry rule
add.rule(strategy=qs.strategy, name='ruleSignal',
arguments=list(sigcol='shortEntry' , sigval=TRUE,
orderside='short' ,
ordertype='market',
orderqty= -.orderqty,
#tradeSize=.tradeSize,
#maxSize = .maxSize,
#osFUN=osMaxPos,
osFUN="osFixedDollar", tradeSize=.tradeSize,
orderset='ocoshort',
replace=FALSE
),
type='enter',
label='EnterSHORT'
)
# add short exit rule
add.rule(strategy=qs.strategy, name='ruleSignal',
arguments=list(sigcol='longExit', sigval=TRUE,
orderside='long' ,
ordertype='market',
orderqty='all',
TxnFees=.txnfees,
orderset='ocolong',
replace=FALSE
),
type='exit',
label='longExit'
)
# add short exit rule
add.rule(strategy=qs.strategy, name='ruleSignal',
arguments=list(sigcol='shortExit', sigval=TRUE,
orderside='short' ,
ordertype='market',
orderqty='all',
TxnFees=.txnfees,
orderset='ocoshort',
replace=FALSE
),
type='exit',
label='shortExit'
)
# run back-test
applyStrategy(strategy = qs.strategy, portfolios = qs.portfolio)
updatePortf(Portfolio = qs.portfolio)
updateAcct(qs.account)
updateEndEq(qs.account)
# get trading data
book = getOrderBook(qs.portfolio)
stats = tradeStats(qs.portfolio, use = "trades", inclZeroDays = TRUE)
ptstats = perTradeStats(qs.portfolio)
txns = getTxns(qs.portfolio, underlying)
# analyze performance
equity.curve <- getAccount(qs.account)$summary$End.Eq
daily.returns <- Return.calculate(equity.curve$End.Eq, "discrete")
names(daily.returns) <- "return"
#hist(daily.returns)
# get annualized summary
table.AnnualizedReturns(daily.returns, scale = 252)
# chart performance
charts.PerformanceSummary(daily.returns, main = "Pattern Trading Performance")
# get some summary trade statistics
stats[,c("Symbol", "Num.Trades", "Percent.Positive", "Net.Trading.PL",
"Profit.Factor", "Max.Drawdown")]
# get table of monthly returns
monthly.returns <- Return.calculate(to.monthly(equity.curve)[, 4],"discrete")
names(monthly.returns) <- "Total"
table.CalendarReturns(monthly.returns)
# more diagnositics
chart.Posn(qs.portfolio, underlying,
TA='add_BBands(n=20);add_SMA(n=10);add_SMA(n=50)')
View(t(stats))
View(ptstats)