-
Notifications
You must be signed in to change notification settings - Fork 16
/
quantstrat-parameters.R
458 lines (372 loc) · 15.8 KB
/
quantstrat-parameters.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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# getParameterTable<-function (strategy) #,staticSwitch)
# {
#
# tmp_paramTable<-list()
# nofi=0
# indexnum=0
# for (indicator in strategy$indicators ){
# # .formals <- formals(fun) #yc here get the prameters needed for that function.
# # print(.formals)
# nofi=nofi+1
# indexnum=indexnum+1
# #
# fun<-match.fun(indicator$name) #yc here get the function of the indicator
# tmp_paramTable[[nofi]]<-list()
# #tmp_paramTable[[nofi]]<-formals(fun)
# tmp_paramTable[[nofi]]$paramType<-'indicator'
# tmp_paramTable[[nofi]]$paramEnabled<-indicator$enabled
# tmp_paramTable[[nofi]]$indexnum=indexnum
# tmp_paramTable[[nofi]]$label<-indicator$label
# tmp_paramTable[[nofi]]$args<-formals(fun)
#
# }
#
# indexnum=0
# for (signal in strategy$signals ){
#
#
# nofi=nofi+1
# indexnum=indexnum+1
#
# fun<-match.fun(signal$name)
# tmp_paramTable[[nofi]]<-list()
#
# tmp_paramTable[[nofi]]$paramType<-'signal'
# tmp_paramTable[[nofi]]$paramEnabled<-signal$enabled
# tmp_paramTable[[nofi]]$indexnum=indexnum
# tmp_paramTable[[nofi]]$label<-signal$label
# tmp_paramTable[[nofi]]$args<-formals(fun)
#
# }
#
# for (rule in strategy$rules ){
# indexnum=0
# for (trule in rule){
#
#
#
# nofi=nofi+1
# indexnum=indexnum+1
#
# fun<-match.fun(trule$name)
# tmp_paramTable[[nofi]]<-list()
#
# tmp_paramTable[[nofi]]$paramType<-trule$type
# tmp_paramTable[[nofi]]$paramEnabled<-trule$enabled
# tmp_paramTable[[nofi]]$indexnum=indexnum
# tmp_paramTable[[nofi]]$label<-trule$label
# tmp_paramTable[[nofi]]$args<-formals(fun)
# tmp_paramTable[[nofi]]$timespan<-trule$timespan
#
# }
#
# }
# #data.frame(c(paramStructure[[6]][1:4],param.name.=names(paramStructure[[6]]$args)))
# paramPack<-list()
# for (i in 1:length(tmp_paramTable)){
#
# paramPack$paramNameList[[i]]<-data.frame(c(tmp_paramTable[[i]][1:4],param.=names(tmp_paramTable[[i]]$args)))
#
# }
#
# #tmp_paramTable$strategyName<-strategy$name
# paramPack$strategyName<-strategy$name
# paramPack$structure<-tmp_paramTable
#
# return(paramPack)
#
# }
# setParameterDistribution<-function(paramDist=NULL,type=NULL,indexnum=0,distribution=NULL,weight,label,psindex=NULL){#All is needed, set to illegal values
#
# if(!hasArg(paramDist)||!exists(as.character(substitute(paramDist))) ){
# paramDist<-list()
# print('Object for parameter distribution initialized...')
# }
# # else{
#
# if (!is.list(distribution)|length(distribution)!=1) stop("distribution must be passed as a named list of length 1")
# if (!type %in% c("indicator","signal","enter","exit","order")) stop("Type must be a string in: indicator, signal, enter, exit, order")
#
# tmp_paramDist<-list()
# tmp_paramDist$type<-type
# tmp_paramDist$indexnum<-indexnum
# tmp_paramDist$distribution<-distribution
#
# if (missing(label)) {
# tmp_paramDist$label<-paste('Param',type,indexnum,names(distribution),sep='.')
# }
# else {tmp_paramDist$label<-label}
#
#
# if(!hasArg(weight)) weight<-sample(1/length(distribution[[1]]),length(distribution[[1]]),replace=TRUE)
#
# tmp_paramDist$weight<-weight
#
# if(!hasArg(psindex) | (hasArg(psindex) & is.null(psindex))) psindex = length(paramDist)+1
# #class(tmp_paramDist)<-'parameter_distribution'
#
# #TODO put an check to see if the type/indexnum exist already.
# paramDist[[psindex]]<-tmp_paramDist
# # }
# return(paramDist)
# }
applyParameter<-function(strategy,portfolios,parameterPool,parameterConstraints,method,sampleSize,verbose=FALSE,...){
#need to create combination of distribution values in each slot of the parameterPool
initialPortf<-getPortfolio(portfolios)
symbols<-names(initialPortf$symbols)
initDate<-time(first(initialPortf$symbols[[1]]$posPL))
limits<-list()
for(symbol in names(initialPortf$symbols))
limits[[symbol]]<-initialPortf$symbols[[symbol]]$PosLimit
tmp_strategy<-strategy
results<-list()
results$stats<-NULL
if (!is.strategy(tmp_strategy)) {
tmp_strategy<-try(getStrategy(tmp_strategy))
if(inherits(tmp_strategy,"try-error"))
stop ("You must supply an object of type 'strategy'.")
}
out<-list()
paramdist<-list()
paramweight<-list()
paramLabel<-list()
lvmatch<-list()
for (i in 1:length(parameterPool)){
distr<-parameterPool[[i]]
#paramdist[[i]]<-distr$distribution[[1]]
paramdist[[paste('Param',distr$type,distr$indexnum,names(distr$distribution),sep='.')]]<-distr$distribution[[1]]
paramweight[[paste('ParamWt',distr$type,distr$indexnum,names(distr$distribution),sep='.')]]<-distr$weight
#paramdist[[paste(i)]]<-distr$distribution[[1]]
#Build label<->var name match.
lvmatch$label[i]<-distr$label
lvmatch$varName[i]<-paste('Param',distr$type,distr$indexnum,names(distr$distribution),sep='.')
}
paramLabel<-data.frame(lvmatch,stringsAsFactors=FALSE)
#TODO make it take sample size etc.
if (method=='expand')
{
paramTable<-expand.grid(paramdist, stringsAsFactors=FALSE)
}
else if (method=='random')
{
if (missing(sampleSize)) {stop ("sampleSize is needed")}
#paramTable<-data.frame()
#genSample update the paramTable with more sample rows.
genSample<-function(iparamTable,paramdist,tsampleSize,remainSize)
{
if (missing(remainSize) ) remainSize=tsampleSize
tparamTable<-data.frame()
for( i in 1:length(paramdist))
{
ireplace<-(length(paramdist[i])<tsampleSize)
if (nrow(tparamTable)==0)
{
tparamTable<-data.frame(sample(paramdist[[i]],remainSize,prob=paramweight[[i]],replace=ireplace),stringsAsFactors=FALSE)
}
else{
tparamTable<-cbind(tparamTable,data.frame(sample(paramdist[[i]],remainSize,prob=paramweight[[i]],replace=ireplace),stringsAsFactors=FALSE))
}
}
names(tparamTable)<-names(paramdist)
# put constraint test on tparamTable, before rbind
for (k in 1:length(parameterConstraints))
{
constrintfill<-paramConstraint(label=parameterConstraints[[k]]$constraintLabel,
data=tparamTable,
columns=merge(paramLabel,data.frame(parameterConstraints[[k]]$paramList),by="label")$varName, #has to keep the order.
relationship=parameterConstraints[[k]]$relationship)
#only keep the samples fulfill the constraints.
tparamTable<-tparamTable[which(constrintfill==TRUE),]
}
iparamTable<-rbind(iparamTable,tparamTable)
iparamTable<-unique(iparamTable)
# if(verbose >=1) print("nnnnnnnnnnnnnnnnnnnnnnn")
# if(verbose >=1) print(nrow(iparamTable))
if (nrow(iparamTable)<tsampleSize)
{
iparamTable<-genSample(iparamTable,paramdist,tsampleSize,remainSize=tsampleSize-nrow(iparamTable))
}
names(iparamTable)<-names(paramdist)
return(iparamTable)
} #end define function
paramTable<-NULL
paramTable<-genSample(paramTable,paramdist,sampleSize)
}
strategyList<-list()
if(verbose >=1) print("ParamTable generated")
instruments<-as.list(FinancialInstrument:::.instrument)
getSymbols<-as.list(.getSymbols)
blotter<-as.list(.blotter)
#Pack all symbols downloaded in .GlobalEnv
symbols<-names(.getSymbols)
testPackListPRL<-foreach (i = 1:nrow(paramTable), .export=c('instruments',symbols,'getSymbols','blotter','tmp_strategy'),.verbose=TRUE,...=...) %dopar%
{
#if(verbose)
print(paste('===> now starting parameter test', i))
require(quantstrat, quietly=TRUE)
# loops must be run with an empty .blotter environment each, or .blotter appears to accumulate portfolios and accounts
# and passes them from one loop to the next on each CPU - JH July 2012
if (getDoParRegistered() && getDoParWorkers()>1)
{
rm(list=ls(pos=.blotter), pos=.blotter)
gc(verbose=verbose)
}
testPack<-list()
#Pass environments needed.
loadInstruments(instruments)
.getSymbols<-as.environment(getSymbols)
#Unpack symbols to worker. change later.
#seems need to go through assign, rather than just .export the names...
for (sym in symbols) {
assign(sym, eval(as.name(sym)), .GlobalEnv)
}
#Create a copy of strategy object, so not to lock up on the sameone.
PLtmp_strategy<-tmp_strategy
#Extract parameter from table and construct PLtmp_strategy.
for (j in 1:ncol(paramTable))
{
set.param.values <- function(param.list, new.values)
{
pnamepos<-pmatch(names(param.list),names(new.values),nomatch=0L)
if( any(pnamepos>0))
{
#FIXME: any matching args will be set to 1st param
param.list[which(pnamepos>0)]<-new.values[1]
}
else
{
param.list<-append(param.list, new.values)
}
param.list
}
tmp_arg<-parameterPool[[j]]$distribution[1] #Just get the list form with name
tmp_arg[[1]]<-paramTable[i,j]
tmp_index<-parameterPool[[j]]$indexnum
switch(parameterPool[[j]]$type,
'indicator'=
{
PLtmp_strategy$indicators[[tmp_index]] = set.param.values(PLtmp_strategy$indicators[[tmp_index]], tmp_arg)
PLtmp_strategy$indicators[[tmp_index]]$arguments = set.param.values(PLtmp_strategy$indicators[[tmp_index]]$arguments, tmp_arg)
},
'signal'=
{
PLtmp_strategy$signals[[tmp_index]] = set.param.values(PLtmp_strategy$signals[[tmp_index]], tmp_arg)
PLtmp_strategy$signals[[tmp_index]]$arguments = set.param.values(PLtmp_strategy$signals[[tmp_index]]$arguments, tmp_arg)
},
'order'=
{
PLtmp_strategy$rules$order[[tmp_index]] = set.param.values(PLtmp_strategy$rules$order[[tmp_index]], tmp_arg)
PLtmp_strategy$rules$order[[tmp_index]]$arguments = set.param.values(PLtmp_strategy$rules$order[[tmp_index]]$arguments, tmp_arg)
},
'enter'=
{
PLtmp_strategy$rules$enter[[tmp_index]] = set.param.values(PLtmp_strategy$rules$enter[[tmp_index]], tmp_arg)
PLtmp_strategy$rules$enter[[tmp_index]]$arguments = set.param.values(PLtmp_strategy$rules$enter[[tmp_index]]$arguments, tmp_arg)
},
'exit'=
{
PLtmp_strategy$rules$exit[[tmp_index]] = set.param.values(PLtmp_strategy$rules$exit[[tmp_index]], tmp_arg)
PLtmp_strategy$rules$exit[[tmp_index]]$arguments = set.param.values(PLtmp_strategy$rules$exit[[tmp_index]]$arguments, tmp_arg)
}
)
} #loop j
#Initial portfolio for each test
#######################################################################################
testPack$portfolio.st<-paste(portfolios,'p',i,sep='.')
testPack$account.st<-paste(portfolios,'p',i,sep='.')
rmpstr<-paste('portfolio',testPack$portfolio.st,sep=".")
rmastr<-paste('account',testPack$account.st,sep=".")
try(rm(list = rmpstr, pos = .blotter),silent=FALSE)
try(rm(list = rmastr, pos = .blotter),silent=FALSE)
try(rm(list=paste("order_book",testPack$account.st,sep="."),pos=.strategy),silent=FALSE)
if(verbose >=1) print('Initial portf')
# Decide not to remove the main obj from .blotter, incase of non-parallel run.
# try(rm(list=paste("order_book",portfolios,sep='.'),pos=.strategy),silent=TRUE)
## try(rm(paste("account",portfolio.st,sep='.'),paste("portfolio",portfolio.st,sep='.'),pos=.blotter),silent=TRUE)
# try(rm(list=paste("account",portfolios,sep='.'),pos=.blotter))
# try(rm(list=paste("portfolio",portfolios,sep='.'),pos=.blotter))
try({initPortf(testPack$portfolio.st,symbols=symbols, initDate=initDate)})
try({initAcct(testPack$account.st,testPack$portfolio.st, initDate=initDate)})
try({initOrders(portfolio=testPack$portfolio.st,initDate=initDate)})
for(symbol in names(limits))
addPosLimit(portfolio=testPack$portfolio.st, symbol=symbol, timestamp=initDate, maxpos=limits[[symbol]]$MaxPos[[1]])
# Apply strategy ######################################################################################
if(verbose >=1) print("Apply strategy...")
try(rm("PLtmp_strategy",pos=.strategy),silent=TRUE)
if(verbose >=1) print(PLtmp_strategy$signals[[2]])
assign("PLtmp_strategy1",PLtmp_strategy,envir=as.environment(.strategy))
testPack$out<-try(applyStrategy(strategy=PLtmp_strategy , portfolios=testPack$portfolio.st ),...=...)
testPack$strategy<-PLtmp_strategy
# Update portfolio ######################################################################################
#out<-try(applyStrategy(strategy=stratBBands , portfolios=portfolios ))
# try({
# updatePortf(testPack$portfolio.st,Date=initDate)
# updateAcct(testPack$account.st,Date=initDate)
# updateOrders(portfolio=testPack$portfolio.st)
# })
#try(updatePortf(Portfolio=testPack$portfolio.st,Dates=paste('::',as.Date(Sys.time()),sep='')))
updatePortf(Portfolio=testPack$portfolio.st,Dates=paste('::',as.Date(Sys.time()),sep=''))
#no need to update account.
#updateAcct(account.st,Dates=paste(startDate,endDate,sep="::"))
#updateEndEq(account.st,Dates=paste(startDate,endDate,sep="::"))
#getEndEq(account.st,Sys.time())
testPack$parameters<-paramTable[i,]
testPack$stats<-tradeStats(Portfolios=testPack$portfolio.st)
testPack$blotterl<-as.list(.blotter)
# testPack$blotter<-as.environment(.blotter)
# testPack$blotterr<-.blotter
return(testPack)
} # Loop i
gc(verbose=verbose)
for (k in 1: nrow(paramTable)){
results$statsTable<-rbind(results$stats,cbind(testPackListPRL[[k]]$parameters,testPackListPRL[[k]]$stats))
if(verbose >=1) print(names(testPackListPRL[[k]]$blotterl))
for(nn in 1:length(testPackListPRL[[k]]$blotterl)){
# if(verbose >=1) print(paste(names(testPackListPRL[[k]]$blotterl)[nn],'nnp',nn,sep='.'))
assign(names(testPackListPRL[[k]]$blotterl[nn]),testPackListPRL[[k]]$blotterl[[nn]],envir=as.environment(.blotter))
}
names(testPackListPRL)[k]<-testPackListPRL[[k]]$portfolio.st
}
results$eachRun<-testPackListPRL
results$paramTable<-paramTable
results$paramConstrainTable<-data.frame(parameterConstraints)
results$parameterDistribution<-parameterPool
results$parameterConstraints<-parameterConstraints
return(results)
}
assignInNamespace("applyParameter", applyParameter, ns="quantstrat")
paramConstraint <- function(label,data=mktdata, columns, relationship=c("gt","lt","eq","gte","lte")) {
relationship=relationship[1] #only use the first one
# if(verbose >=1) print(columns)
if (length(columns)==2){
ret_sig=NULL
if (relationship=='op'){
# (How) can this support "Close"? --jmu
if(columns[1] %in% c("Close","Cl","close"))
stop("Close not supported with relationship=='op'")
switch(columns[1],
Low =,
low =,
bid = { relationship = 'lt' },
Hi =,
High=,
high=,
ask = {relationship = 'gt'}
)
}
colNums <- match.names(columns,colnames(data))
opr <- switch( relationship,
gt = , '>' = '>',
lt =, '<' = '<',
eq =, "==" =, "=" = "==",
gte =, gteq =, ge =, ">=" = ">=",
lte =, lteq =, le =, "<=" = "<="
)
ret_sig$tname <- do.call( opr, list(data[,colNums[1]], data[,colNums[2]]))
} else {
stop("comparison of more than two columns not supported, see sigFormula")
}
names(ret_sig)<-label
return(data.frame(ret_sig))
}
assignInNamespace("paramConstraint", paramConstraint, ns="quantstrat")