[R-SIG-Finance] Defining many future_series() at once in FinancialInstruments
G See
gsee000 at gmail.com
Wed Jan 18 07:23:16 CET 2012
Markus,
Thanks for the report.
The part that is slow is when it is searching the instrument
environment to see if the future_series you are trying to define has
already been defined. I managed to more than double the speed of that
search following a suggestion from Brian to use unlist and regular
expressions.
However, the real speed up is to skip that step. So, I added an
"overwrite" argument. Now, future_series will not check to see if
there is already a future_series defined unless you call it with
overwrite=FALSE.
Patched at Rev 913.
Timings of your test below.
## Before patch
> system.time(future_series(all_contracts[1:200]))
user system elapsed
2.600 0.000 2.595
> system.time(future_series(all_contracts[201:800]))
user system elapsed
21.330 0.000 21.351
> system.time(future_series(all_contracts[801:2000]))
user system elapsed
104.950 0.000 105.041
> ## Update 1: unlist and regexpr instead of for loop
> system.time(future_series(all_contracts[1:200]))
user system elapsed
1.160 0.000 1.158
> system.time(future_series(all_contracts[201:800]))
user system elapsed
8.810 0.000 8.816
> system.time(future_series(all_contracts[801:2000]))
user system elapsed
44.030 0.000 44.071
> ## New version
> system.time(future_series(all_contracts[1:200]))
user system elapsed
0.180 0.000 0.176
> system.time(future_series(all_contracts[201:800]))
user system elapsed
0.520 0.000 0.522
> system.time(future_series(all_contracts[801:2000]))
user system elapsed
1.050 0.000 1.054
Regards,
Garrett
On Tue, Jan 17, 2012 at 4:55 AM, varcovar <varcovar at live.com> wrote:
> Hi guys,
>
> I have 100 Futures symbols that are defined using the future() function from
> the FinancialInstrument package. I want to define their respective contracts
> using the future_series() function. I have about 15000 contracts to define.
> The problem here is that the time needed to define a contract with
> future_series() increases exponentially with the number of contracts defined
> (it is very fast for the first contract, and then goes slower and slower).
>
> Here is a reproducible example. I build a vector of 104 market symbols and
> then a vector of 19136 contracts (184 contracts for each market symbol).
> Then I calculate the time needed to define the first 200 contracts, and then
> 600 contracts. While it takes about 2.76 seconds on my desktop to define the
> first 200 contracts, it takes more than 23 seconds for the next 600
> contracts. At this increasing rate, it would take an eternity to define all
> those 19136 contracts.
>
> library(quantstrat)
> ## create a vector of 104 symbols
> symbols <- NULL
> for (i in 1:4)
> symbols <- c(symbols, paste(LETTERS[(2 * i):26], LETTERS[1:26], sep =
> ""))
> currency("USD")
> ## create a vector containing all contracts for the 104 symbols (thus 19136
> contracts)
> all_contracts <- NULL
> for (i in 1:length(symbols)) {
> ## define futures
> future(primary_id = symbols[i], currency = "USD", multiplier = 100,
> tick_size = 0.1, month_cycle = "F,G,H,J,M,Q,V,Z")
> ## create contracts series
> contracts <- build_series_symbols(
> roots = data.frame(
> primary_id = getInstrument(symbols[i])$primary_id,
> month_cycle = getInstrument(symbols[i])$month_cycle),
> yearlist = 1990:2012)
> ## format contracts symbols
> contracts <- format_id(contracts, format = "CYY")
> ## append new contracts to all contracts
> all_contracts <- c(all_contracts, contracts)
> }
> ## calculate time needed to define contracts
> system.time(future_series(all_contracts[1:200]))
> system.time(future_series(all_contracts[201:800]))
>
> Once all the future_series are defined, I expect to save them using the
> saveInstruments() function for later use. Any suggestion for making this
> process faster? I really appreciate your help! Thank you.
>
> Markus Douglas
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Defining-many-future-series-at-once-in-FinancialInstruments-tp4302922p4302922.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
More information about the R-SIG-Finance
mailing list