问题
I've been encountering an issue when optimizing a strategy using the apply.paramset function in quantstrat.
The issue I am having appears to be the same as the one here: Quantstrat: apply.paramset fails due to combine error for certain paramater distributions, but not others
The optimization works well if all of the parameter combinations return at least one transaction, however, if one of the combinations doesn't return a transaction then the results for all of the combinations are lost/NULL, typically with the following error:
"simpleError in match.names(clabs, names(xi)): names do not match previous names"
I have provided a short example below where it produces one successful parameter combination that produces a number of transactions (.nlist = 10 and .sdlist = 1) and one combination which doesn't produce any transactions (.nlist = 10 and .sdlist = 20).
I would like to be able to extract the tradeStats for this optimization from the "results" environment, but due to the error the results become NULL.
What would be the best way to solve this problem?
# Demo from https://github.com/braverock/quantstrat/blob/master/demo/bbandParameters.R
require(foreach,quietly=TRUE)
require(iterators)
require(quantstrat)
demo('bbands',ask=FALSE)
strategy.st='bbands'
# Here I have chosen only a single parameter for the first distribution and two for the second, 1 will work but 20 will fail
.nlist = 10
.sdlist = c(1, 20)
# Here are parameters that will successfully produce the results and tradeStats
#.nlist = c(10,12)
#.sdlist = 1
# number of random samples of the parameter distribution to use for random run
.nsamples = 2
add.distribution(strategy.st,
paramset.label = 'BBparams',
component.type = 'indicator',
component.label = 'BBands',
variable = list(n = .nlist),
label = 'nFAST'
)
add.distribution(strategy.st,
paramset.label = 'BBparams',
component.type = 'indicator',
component.label = 'BBands',
variable = list(sd = .sdlist),
label = 'nSLOW'
)
results <- apply.paramset(strategy.st,
paramset.label='BBparams',
portfolio.st=portfolio.st,
account.st=account.st,
nsamples=.nsamples,
verbose=TRUE)
Hopefully I have provided enough information, if not please let me know and I'll try to elaborate.
Thanks in advance
Edit: The obvious answer to the question would be to not choose parameters which have a range which is too large, in other words don't cast the net too wide, but even if the parameters didn't span such large ranges this issue could still arise if you were applying a small parameter optimisation on a large number of assets/datasets. For example, applying the parameters: ".nlist = c(10,12)" and ".sdlist = 1" (which are successful in the above example) to a number of different stocks in this case, It may not work for all of them and will therefore encounter this problem too.
Any input would be greatly appreciated.
回答1:
The 20 standard deviation indicator is resulting in no trades, and the resulting rbind() of the tradeStats from the different param.combo strategy results is throwing an error. We should handle these errors more gracefully, and at least inform the user and allow the code to continue executing. I have created an issue here - https://github.com/braverock/quantstrat/issues/121.
Your code will work fine with a more realistic standard deviation, say .sdlist = c(1, 2).
来源:https://stackoverflow.com/questions/61987166/no-results-from-apply-paramset-if-one-parameter-combination-returns-nothing