Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R-Forge #5028] Caching instrument data to prevent addTxns calling getInstrument each time #37

Closed
joshuaulrich opened this issue May 13, 2016 · 1 comment

Comments

@joshuaulrich
Copy link
Collaborator

Submitted by: Ilya Kipnis
Assigned to: Nobody
R-Forge link

So thanks to help from Josh Ulrich, I was able to get to the bottom of why my relatively simple strategy was taking an obnoxiously long time (or at least one of the reasons), in that addTxns calls getInstrument on each call if ConMult is not passed in as an argument to applyStrategy. Since applyRules already calls getInstrument, I'm hoping there's a way to cache each instrument for the duration of the strategy with only one call to getInstrument for each instrument?

Because for the futures contract I'm testing on, it takes 1.5 seconds for a call to getInstrument to finish execution on it, and when that call is made ~20,000 times over the course of the backtest, that alone takes up more than 8 hours.

Here's the offending chunk of code, in addTxns (near the beginning of the function):

if (is.null(ConMult) | !hasArg(ConMult)) {
tmp_instr <- try(getInstrument(Symbol), silent = TRUE)
if (inherits(tmp_instr, 'try-error') | !is.instrument(tmp_instr)) {
warning(paste('Instrument', Symbol, ' not found, using contract multiplier of 1'))
ConMult <- 1
}
else {
ConMult <- tmp_instr$multiplier
}
}

Calling getInstrument thousands of times IMO is highly excessive, and there is probably a workaround somewhere that reduces this operation to O(k), where k is the number of instruments, rather than O(N), where N is the number of transactions.

@braverock
Copy link
Owner

addTxns was replaced with C code long ago. closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants