<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
    <title></title>
  </head>
  <body text="#000000" bgcolor="#ffffff">
    Thanks - I tried most everything else but I guess I still haven't
    got all of this in my mind.   I suppose debugging R is better than
    javascript but on marginally.<br>
    <br>
    I've attached the working version if anyone needs it, but I still
    have to add costs and, I guess a stop loss , and so on.<br>
    <br>
    <div class="moz-signature">Stephen Choularton Ph.D., FIoD<br>
      <br>
      9999 2226<br>
      0413 545 182<br>
      <br>
      <img src="cid:part1.06040608.04010302@organicfoodmarkets.com.au"
        alt="" width="300" align="" border="0" height="188"><br>
      for insurance go to <a href="http://www.netinsure.com.au">www.netinsure.com.au</a><br>
      for markets go to <a href="http://www.organicfoodmarkets.com.au">www.organicfoodmarkets.com.au</a><br>
    </div>
    <br>
    On 26/01/2011 2:47 PM, Aleksandr Rudnev wrote:
    <blockquote
      cite="mid:AANLkTiksDUVnMPQEvyE6v6c8aAqeOS4Q6SMmh48gsw5=@mail.gmail.com"
      type="cite">
      <pre wrap="">On Tue, Jan 25, 2011 at 3:54 PM, Stephen Choularton
<a class="moz-txt-link-rfc2396E" href="mailto:stephen@organicfoodmarkets.com.au"><stephen@organicfoodmarkets.com.au></a> wrote:
</pre>
      <blockquote type="cite">
        <pre wrap="">
...

The rules will be executed by type, in the order listed above.
...
Has anyone got this problem and solved it?
</pre>
      </blockquote>
      <pre wrap="">
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: <a class="moz-txt-link-abbreviated" href="mailto:alex.rudnev@gmail.com">alex.rudnev@gmail.com</a>

_______________________________________________
<a class="moz-txt-link-abbreviated" href="mailto:R-SIG-Finance@r-project.org">R-SIG-Finance@r-project.org</a> mailing list
<a class="moz-txt-link-freetext" href="https://stat.ethz.ch/mailman/listinfo/r-sig-finance">https://stat.ethz.ch/mailman/listinfo/r-sig-finance</a>
-- 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.




-----
No virus found in this message.
Checked by AVG - <a class="moz-txt-link-abbreviated" href="http://www.avg.com">www.avg.com</a>




</pre>
    </blockquote>
  </body>
</html>