-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.R
403 lines (328 loc) · 12.8 KB
/
utils.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
must.have.args <- function(supplied.args, mandatory.args)
{
msg <- ': argument(s) missing in call to function '
missing.args <- NULL
for(arg in mandatory.args)
{
if(length(grep(paste('^',arg,'$',sep=''), names(as.list(supplied.args)))) == 0)
{
if(is.null(missing.args))
missing.args <- arg
else
missing.args <- paste(missing.args, ', ', arg)
}
}
if(length(missing.args) > 0)
{
funcname <- as.character(sys.call(-1)[[1]])
stop(paste(missing.args, msg, funcname, sep=''))
}
}
must.be.environment <- function(e)
{
if(!is.environment(e))
stop(paste(e, ': not an environment', sep=''))
}
must.be.strategy <- function(strategy)
{
if(!is.strategy(strategy))
{
strategy<-try(getStrategy(strategy))
if(inherits(strategy,"try-error"))
stop(paste(strategy, ': not a strategy'))
}
return(strategy)
}
must.be.portfolio <- function(portfolio)
{
if(!is.portfolio(portfolio))
{
portfolio<-try(.getPortfolio(portfolio))
if(inherits(portfolio,"try-error"))
stop(paste(portfolio, ': not a portfolio'))
}
}
modify.args <- function(formals, arglist, ..., dots=FALSE)
{
# avoid evaluating '...' to make things faster
dots.names <- eval(substitute(alist(...)))
if(missing(arglist))
arglist <- NULL
arglist <- c(arglist, dots.names)
# see 'S Programming' p. 67 for this matching
# nothing to do if arglist is empty; return formals
if(!length(arglist))
return(formals)
argnames <- names(arglist)
if(!is.list(arglist) && !is.null(argnames) && !any(argnames == ""))
stop("'arglist' must be a *named* list, with no names == \"\"")
.formals <- formals
onames <- names(.formals)
pm <- pmatch(argnames, onames, nomatch = 0L)
#if(any(pm == 0L))
# message(paste("some arguments stored for", fun, "do not match"))
names(arglist[pm > 0L]) <- onames[pm]
.formals[pm] <- arglist[pm > 0L]
# include all elements from arglist if function formals contain '...'
if(dots && !is.null(.formals$...)) {
dotnames <- names(arglist[pm == 0L])
.formals[dotnames] <- arglist[dotnames]
#.formals$... <- NULL # should we assume we matched them all?
}
.formals
}
clone.portfolio <- function(portfolio.st, cloned.portfolio.st, strip.history=TRUE)
{
#must.have.args(match.call(), c('portfolio.st', 'cloned.portfolio.st'))
portfolio <- .getPortfolio(portfolio.st)
if(strip.history==TRUE)
{
for(symbol in ls(portfolio$symbols))
{
portfolio$symbols[[symbol]]$txn <- portfolio$symbols[[symbol]]$txn[1,]
xts.tables <- grep('(^posPL|txn)',names(portfolio$symbols[[symbol]]), value=TRUE)
for(xts.table in xts.tables)
portfolio$symbols[[symbol]][[xts.table]] <- portfolio$symbols[[symbol]][[xts.table]][1,]
}
portfolio$summary <- portfolio$summary[1,]
}
put.portfolio(as.character(cloned.portfolio.st), portfolio)
return(cloned.portfolio.st)
}
# creates a copy of an orderbook, stripping all orders
clone.orderbook <- function(portfolio.st, cloned.portfolio.st, strip.history=TRUE)
{
#must.have.args(match.call(), c('portfolio.st', 'cloned.portfolio.st'))
orderbook <- getOrderBook(portfolio.st)
i <- 1 # TODO: find index number by name
names(orderbook)[i] <- cloned.portfolio.st
if(strip.history == TRUE)
{
for(symbol in names(orderbook[[portfolio.st]]))
orderbook[[portfolio.st]][[symbol]] <- orderbook[[portfolio.st]][[symbol]][1,]
}
put.orderbook(cloned.portfolio.st, orderbook)
}
### local functions ############################################################
must.be.paramset <- function(strategy, paramset)
{
if(!(paramset %in% names(strategy$paramsets)))
stop(paste(paramset, ': no such paramset in strategy', strategy$name))
}
create.paramset <- function(strategy, paramset.label)
{
strategy$paramsets[[paramset.label]] <- list()
strategy$paramsets[[paramset.label]]$distributions <- list()
strategy$paramsets[[paramset.label]]$constraints <- list()
strategy
}
expand.distributions <- function(distributions)
{
param.values <- list()
for(distribution.name in names(distributions))
{
variable.name <- names(distributions[[distribution.name]]$variable)
param.values[[distribution.name]] <-
distributions[[distribution.name]]$variable[[variable.name]]
}
expand.grid(param.values)
}
apply.constraints <- function(constraints, distributions, param.combos)
{
for(constraint in constraints)
{
operator <- constraint$operator
distribution.name.1 <- constraint$distributions[[1]]
distribution.name.2 <- constraint$distributions[[2]]
variable.name.1 <- names(distributions[[distribution.name.1]]$variable)
variable.name.2 <- names(distributions[[distribution.name.2]]$variable)
result <- do.call(operator, list(param.combos[,distribution.name.1], param.combos[,distribution.name.2]))
param.combos <- param.combos[which(result),]
}
param.combos
}
select.samples <- function(nsamples, param.combos)
{
nsamples <- min(nsamples, nrow(param.combos))
param.combos <- param.combos[sample(nrow(param.combos), size=nsamples),,drop=FALSE]
if(NCOL(param.combos) == 1)
param.combos <- param.combos[order(param.combos),,drop=FALSE]
else
param.combos <- param.combos[with(param.combos,order(param.combos[,1],param.combos[,2])),]
param.combos
}
install.param.combo <- function(strategy, param.combo, paramset.label)
{
if (is.null(dim(param.combo))) {
stop("'param.combo' must have a dim attribute")
}
for(param.label in colnames(param.combo))
{
distribution <- strategy$paramsets[[paramset.label]]$distributions[[param.label]]
component.type <- distribution$component.type
component.label <- distribution$component.label
variable.name <- names(distribution$variable)
found <- FALSE
switch(component.type,
indicator =,
signal =
{
# indicator and signal slots in strategy list use plural name for some reason:
components.type <- paste(component.type,'s',sep='')
n <- length(strategy[[components.type]])
for(index in 1:n)
{
if(strategy[[components.type]][[index]]$label == component.label)
{
strategy[[components.type]][[index]]$arguments[[variable.name]] <- param.combo[,param.label]
found <- TRUE
break
}
}
},
order =,
enter =,
exit =,
chain =
{
n <- length(strategy$rules[[component.type]])
for(index in 1:n)
{
if(strategy$rules[[component.type]][[index]]$label == component.label)
{
if(variable.name %in% c('timespan'))
strategy$rules[[component.type]][[index]][[variable.name]] <- as.character(param.combo[,param.label])
else
strategy$rules[[component.type]][[index]]$arguments[[variable.name]] <- param.combo[,param.label]
found <- TRUE
break
}
}
}
)
if(!found) stop(paste(component.label, ': no such ', component.type, ' rule in strategy ', strategy$name, sep=''))
}
return(strategy)
}
### exported functions ############################################################
#' Delete a paramset from a strategy
#'
#' Delete a paramset from a strategy, including its distributions and constraints.
#'
#' @param strategy the name of the strategy object
#' @param paramset.label a label uniquely identifying the paramset within the strategy
#' @param store indicates whether to store the strategy in the .strategy environment
#'
#' @author Jan Humme
#' @export
#' @seealso \code{\link{add.distribution}}, \code{\link{add.distribution.constraint}}, \code{\link{apply.paramset}}
delete.paramset <- function(strategy, paramset.label, store=TRUE)
{
must.have.args(match.call(), c('strategy', 'paramset.label'))
if(!is.strategy(strategy))
{
strategy <- must.be.strategy(strategy)
store <- TRUE
}
if(!is.null(strategy$paramsets[[paramset.label]])) {
strategy$paramsets[[paramset.label]] <- NULL
} else {
warning("strategy ", sQuote(strategy$name), " does not have a paramset ",
sQuote(paramset.label), " to delete. Aborting.", immediate.=TRUE)
}
if(store)
{
put.strategy(strategy)
return(strategy$name)
}
return(strategy)
}
#' Adds a distribution to a paramset in a strategy
#'
#' Creates a distribution in paramset, where a distribution consists of the name of a variable in
#' a strategy component plus a range of values for this variable.
#'
#' @param strategy the name of the strategy object to add the distribution to
#' @param paramset.label a label uniquely identifying the paramset within the strategy
#' @param component.type one of c('indicator', 'signal', 'order', 'enter', 'exit', 'chain')
#' @param component.label a label identifying the component. must be unique per component type
#' @param variable the name of the variable in the component
#' @param label a label uniquely identifying the distribution within the paramset
#' @param weight vector
#' @param store indicates whether to store the strategy in the .strategy environment
#'
#' @author Jan Humme
#' @export
#' @seealso \code{\link{add.distribution.constraint}}, \code{\link{delete.paramset}}, \code{\link{apply.paramset}}
add.distribution <- function(strategy, paramset.label, component.type, component.label, variable, weight=NULL, label, store=TRUE)
{
must.have.args(match.call(), c('strategy', 'paramset.label', 'component.type', 'component.label', 'variable', 'label'))
if(!is.strategy(strategy))
{
strategy <- must.be.strategy(strategy)
store <- TRUE
}
new_distribution <- list()
new_distribution$component.type <- component.type
new_distribution$component.label <- component.label
new_distribution$variable <- variable
new_distribution$weight <- weight
if(!(paramset.label %in% names(strategy$paramsets)))
strategy <- create.paramset(strategy, paramset.label)
if(label %in% names(strategy$paramsets[[paramset.label]]$distributions)) {
fmt <- paste("add.distribution replacing previously defined",
"distribution %s in paramset %s for strategy %s.")
msg <- sprintf(fmt, sQuote(label), sQuote(paramset.label), sQuote(strategy$name))
warning(msg, immediate.=TRUE, call.=FALSE)
}
strategy$paramsets[[paramset.label]]$distributions[[label]] <- new_distribution
if(store)
{
put.strategy(strategy)
return(strategy$name)
}
return(strategy)
}
#' Adds a constraint on 2 distributions within a paramset
#'
#' Creates a constraint on 2 distributions in a paramset, i.e. a restriction limiting the allowed
#' combinations from the ranges for distribution 1 and distribution 2.
#'
#' @param strategy the name of the strategy object to add the constraint to
#' @param paramset.label a label uniquely identifying the paramset within the strategy
#' @param distribution.label.1 a label identifying the first distribution
#' @param distribution.label.2 a label identifying the second distribution
#' @param operator an operator specifying the relational constraint between the 2 distributions
#' @param label a label uniquely identifying the constraint within the paramset
#' @param store indicates whether to store the strategy in the .strategy environment
#'
#' @author Jan Humme
#' @export
#' @seealso \code{\link{add.distribution}}, \code{\link{delete.paramset}}, \code{\link{apply.paramset}}
add.distribution.constraint <- function(strategy, paramset.label, distribution.label.1, distribution.label.2, operator, label, store=TRUE)
{
must.have.args(match.call(), c('strategy', 'paramset.label', 'distribution.label.1', 'distribution.label.2', 'operator', 'label'))
if(!is.strategy(strategy))
{
strategy <- must.be.strategy(strategy)
store <- TRUE
}
new_constraint <- list()
new_constraint$distributions <- list(distribution.label.1, distribution.label.2)
new_constraint$operator <- operator
if(!(paramset.label %in% names(strategy$paramsets)))
strategy <- create.paramset(strategy, paramset.label)
if(label %in% names(strategy$paramsets[[paramset.label]]$constraints)) {
fmt <- paste("add.distribution.constraint replacing previously defined",
"constraint %s in paramset %s for strategy %s.")
msg <- sprintf(fmt, sQuote(label), sQuote(paramset.label), sQuote(strategy$name))
warning(msg, immediate.=TRUE, call.=FALSE)
}
strategy$paramsets[[paramset.label]]$constraints[[label]] <- new_constraint
if(store)
{
put.strategy(strategy)
return(strategy$name)
}
return(strategy)
}