[R-SIG-Finance] quantstrat - macross demo problem
Aleksandr Rudnev
alex.rudnev at gmail.com
Wed Jan 26 04:47:22 CET 2011
On Tue, Jan 25, 2011 at 3:54 PM, Stephen Choularton
<stephen at organicfoodmarkets.com.au> wrote:
>
> ...
>
> The rules will be executed by type, in the order listed above.
> ...
> Has anyone got this problem and solved it?
I have seen similar thing, but never had time to trace it before today...
It seems like a defect in applyRules (if we take mentioned note as a
spec) or in add.rule, or in ruleOrderProc, depending on how you look
at it, but I guess authors will coment on that.
Basically, what's happening is that "rules" list in strategy is
populated with lists of rules of different type, each of such lists
("order", "entry", "exit", etc) is created and added to "rules" when
add.rule is invoked first time for such type (lazy initialization). In
applyRules "rules" are iterated in order corresponding to how
different types of rules were added. In your case you start with
"enter" type, then add "exit".
Workaround would to first add "exit" rule(s), then "enter" rule(s).
To get better understanding you can do the following:
options(warn = 1)
trace(addOrder, quote(cat("addOrder(", qty, side,
as.character(timestamp), ")\n")), print = FALSE)
Then, when you run it, you get the following output, that shows that
"short enter" order is added first, followed by "long exit" order on
2008-01-22, which causes ruleOrderProc to skip that second order that
is supposed to be on long side, but has qty = -100 at the time when
current posQty = 0 after "short entry" order:
...
addOrder( 100 long 2003-06-12 )
[1] "2003-06-12 AXJO 100 @ 3084.8"
addOrder( -100 short 2008-01-22 )
addOrder( -100 long 2008-01-22 )
[1] "2008-01-22 AXJO -100 @ 5186.8"
Warning in ruleOrderProc(portfolio = portfolio, symbol = symbol,
mktdata = mktdata, :
orderQty of-100would cross through zero, reducing qty to0
Warning in ruleOrderProc(portfolio = portfolio, symbol = symbol,
mktdata = mktdata, :
orderQty of-100would cross through zero, reducing qty to0
addOrder( 100 long 2009-06-17 )
Warning in ruleOrderProc(portfolio = portfolio, symbol = symbol,
mktdata = mktdata, :
orderQty of-100would cross through zero, reducing qty to0
...
If you revert order of first two add.rule lines to have it as the following:
stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal',
arguments = list(sigcol="shortMA.lt.longMA",sigval=TRUE,
orderqty=-100, ordertype='market', orderside='long', threshold=NULL,
replace=FALSE ),type='exit')
stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal',
arguments = list(sigcol="shortMA.gt.longMA",sigval=TRUE, orderqty=100,
ordertype='market', orderside='long', threshold=NULL, replace=FALSE
),type='enter')
You most probably get what you expected:
...
addOrder( 100 long 2003-06-12 )
[1] "2003-06-12 AXJO 100 @ 3084.8"
addOrder( -100 long 2008-01-22 )
addOrder( -100 short 2008-01-22 )
[1] "2008-01-22 AXJO -100 @ 5186.8"
[1] "2008-01-22 AXJO -100 @ 5186.8"
addOrder( 100 short 2009-06-17 )
addOrder( 100 long 2009-06-17 )
[1] "2009-06-17 AXJO 100 @ 3904.1"
[1] "2009-06-17 AXJO 100 @ 3904.1"
...
Also, you probably want to init currency, as "AUD" is not default one
and causes additional warning and probably some errors in
calculations.
I guess in any case logic in ruleOrderProc that prevents crossing
through zero should be refined.
Regards,
--
Alexander Rudnev
Cell: 1 (202) 904 7569
E-Mail: alex.rudnev at gmail.com
More information about the R-SIG-Finance
mailing list