[R-SIG-Finance] quantstrat - macross demo problem

Joshua Ulrich josh.m.ulrich at gmail.com
Thu Jan 27 16:55:24 CET 2011

On Wed, Jan 26, 2011 at 6:42 AM, Brian G. Peterson <brian at braverock.com> wrote:
> On 01/25/2011 09:47 PM, Aleksandr Rudnev wrote:
>> 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 comment 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).
> This is correct.  Thank You for the detailed examination of the code.
I second that.  Aleksandr did most of the work, which made it very
easy to patch.

> The lines in question in applyRules() are a loop around a switch:
> l506 and following:
> for ( type in names(strategy$rules)){
>        switch( type ,
> I'll need to think about an alternate formulation in applyRules() that would
> work the rule types in order as described in the documentation (which was
> our intent).
> Any input on fixes gladly accepted.
I've committed the patch below.  Rather than looping over
names(strategy$rules), I create a factor that will sort in the same
order as described in the documentation.  Obviously, if a new new rule
type is created, it must be added to this list.

types <- sort(factor(names(strategy$rules),
for ( type in types ) {
  switch( type ,

The maCross demo now works correctly when the short rules are uncommented.

Joshua Ulrich  |  FOSS Trading: www.fosstrading.com

> Regards,
>  - Brian
> --
> Brian G. Peterson
> http://braverock.com/brian/
> Ph: 773-459-4973
> IM: bgpbraverock
> _______________________________________________
> 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