<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#ffffff">
I've been working on the problem of getting the macross demo to work
on a long + short basis but I have a problem with 'order confusion'
by ruleOrderProc. I attach the full code below but the critical
section appears to be:<br>
<br>
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')<br>
<br>
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')<br>
<br>
# if you want a long/short Stops and Reverse MA cross strategy,
you'd add two more rules for the short side: <br>
<br>
stratMACROSS <- add.rule(strategy =
stratMACROSS,name='ruleSignal', arguments =
list(sigcol="shortMA.lt.longMA",sigval=TRUE, orderqty=-100,
ordertype='market', orderside='short', threshold=NULL, replace=FALSE
),type='enter')<br>
<br>
stratMACROSS <- add.rule(strategy =
stratMACROSS,name='ruleSignal', arguments =
list(sigcol="shortMA.gt.longMA",sigval=TRUE, orderqty=100,
ordertype='market', orderside='short', threshold=NULL, replace=FALSE
),type='exit')<br>
<br>
As you can see I use type='enter' and type='exit' which appears
correct, but when I run it I get some crossing zero warnings and the
code does not pick up short closes, viz:<br>
getOrderBook(portfolio.st)<br>
$macross<br>
$macross$AXJO<br>
Order.Qty Order.Price Order.Type Order.Side
Order.Threshold<br>
1999-12-31 "0" NA "init" "long"
"0" <br>
2003-06-12 "100" "3084.8" "market" "long"
NA <br>
2008-01-22 "-100" "5186.8" "market" "short"
NA <br>
2008-01-22 "-100" "5186.8" "market" "long"
NA <br>
2009-06-17 "100" "3904.1" "market" "long"
NA <br>
2010-06-07 "-100" "4325.9" "market" "short"
NA <br>
2010-06-07 "-100" "4325.9" "market" "long"
NA <br>
2010-11-02 "100" "4701.4" "market" "long"
NA <br>
Order.Status Order.StatusTime Order.Set Txn.Fees<br>
1999-12-31 "closed" "1999-12-31" "1" "0" <br>
2003-06-12 "closed" "2003-06-13" NA "0" <br>
2008-01-22 "closed" "2008-01-23" NA "0" <br>
2008-01-22 "closed" "2010-06-07" NA "0" <br>
2009-06-17 "closed" "2009-06-18" NA "0" <br>
2010-06-07 "closed" "2010-06-08" NA "0" <br>
2010-06-07 "closed" "2011-01-24" NA "0" <br>
2010-11-02 "closed" "2010-11-03" NA "0" <br>
<br>
<br>
attr(,"class")<br>
[1] "order_book"<br>
> <br>
> print('transactions')<br>
[1] "transactions"<br>
> <br>
> <br>
> getTxns(Portfolio=portfolio.st, Symbol=stock.str)<br>
Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost<br>
1999-12-31 0 0.0 0 0 0.0<br>
2003-06-12 100 3084.8 0 308480 3084.8<br>
2008-01-22 -100 5186.8 0 -518680 5186.8<br>
2008-01-22 -100 5186.8 0 -518680 5186.8<br>
2009-06-17 100 3904.1 0 390410 3904.1<br>
2010-06-07 -100 4325.9 0 -432590 4325.9<br>
2010-06-07 -100 4325.9 0 -432590 4325.9<br>
2010-11-02 100 4701.4 0 470140 4701.4<br>
Net.Txn.Realized.PL<br>
1999-12-31 0<br>
2003-06-12 0<br>
2008-01-22 210200<br>
2008-01-22 0<br>
2009-06-17 0<br>
2010-06-07 42180<br>
2010-06-07 0<br>
2010-11-02 0<br>
<br>
I have done a manual calculation that shows the results should be: <br>
<br>
<table border="0" cellspacing="0" cols="6" frame="VOID" rules="NONE">
<colgroup><col width="86"><col width="86"><col width="86"><col
width="86"><col width="86"><col width="86"></colgroup> <tbody>
<tr>
<td width="86" align="LEFT" height="17"><font face="Times New
Roman">my calculation</font></td>
<td width="86" align="LEFT"><br>
</td>
<td width="86" align="LEFT"><br>
</td>
<td width="86" align="LEFT"><br>
</td>
<td width="86" align="LEFT"><br>
</td>
<td width="86" align="LEFT"><br>
</td>
</tr>
<tr>
<td align="LEFT" height="17"><font face="Times New Roman"><br>
</font></td>
<td align="LEFT"><br>
</td>
<td align="LEFT"><br>
</td>
<td align="LEFT"><br>
</td>
<td align="LEFT"><br>
</td>
<td align="LEFT"><br>
</td>
</tr>
<tr>
<td align="RIGHT" height="17"><font face="Times New Roman">06/12/03</font></td>
<td align="RIGHT"><font face="Times New Roman">100</font></td>
<td align="RIGHT"><font face="Times New Roman">3084.8</font></td>
<td align="RIGHT"><font face="Times New Roman">0</font></td>
<td align="RIGHT"><font face="Times New Roman">308480</font></td>
<td align="LEFT"><font face="Times New Roman"><br>
</font></td>
</tr>
<tr>
<td align="RIGHT" height="17"><font face="Times New Roman">01/22/08</font></td>
<td align="RIGHT"><font face="Times New Roman">-100</font></td>
<td align="RIGHT"><font face="Times New Roman">5186.8</font></td>
<td align="RIGHT"><font face="Times New Roman">0</font></td>
<td align="RIGHT"><font face="Times New Roman">-518680</font></td>
<td align="RIGHT"><font face="Times New Roman">210200</font></td>
</tr>
<tr>
<td align="RIGHT" height="17"><font face="Times New Roman">01/22/08</font></td>
<td align="RIGHT"><font face="Times New Roman">-100</font></td>
<td align="RIGHT"><font face="Times New Roman">5186.8</font></td>
<td align="RIGHT"><font face="Times New Roman">0</font></td>
<td align="RIGHT"><font face="Times New Roman">-518680</font></td>
<td align="LEFT"><font face="Times New Roman"><br>
</font></td>
</tr>
<tr>
<td align="RIGHT" height="17"><font face="Times New Roman">06/17/09</font></td>
<td align="RIGHT"><font face="Times New Roman">100</font></td>
<td align="RIGHT"><font face="Times New Roman">3904.1</font></td>
<td align="RIGHT"><font face="Times New Roman">0</font></td>
<td align="RIGHT"><font face="Times New Roman">390410</font></td>
<td align="RIGHT"><font face="Times New Roman">128270</font></td>
</tr>
<tr>
<td align="RIGHT" height="17"><font face="Times New Roman">06/17/09</font></td>
<td align="RIGHT"><font face="Times New Roman">100</font></td>
<td align="RIGHT"><font face="Times New Roman">3904.1</font></td>
<td align="RIGHT"><font face="Times New Roman">0</font></td>
<td align="RIGHT"><font face="Times New Roman">390410</font></td>
<td align="LEFT"><font face="Times New Roman"><br>
</font></td>
</tr>
<tr>
<td align="RIGHT" height="17"><font face="Times New Roman">06/07/10</font></td>
<td align="RIGHT"><font face="Times New Roman">-100</font></td>
<td align="RIGHT"><font face="Times New Roman">4325.9</font></td>
<td align="RIGHT"><font face="Times New Roman">0</font></td>
<td align="RIGHT"><font face="Times New Roman">-432590</font></td>
<td align="RIGHT"><font face="Times New Roman">42180</font></td>
</tr>
<tr>
<td align="RIGHT" height="17"><font face="Times New Roman">06/07/10</font></td>
<td align="RIGHT"><font face="Times New Roman">-100</font></td>
<td align="RIGHT"><font face="Times New Roman">4325.9</font></td>
<td align="RIGHT"><font face="Times New Roman">0</font></td>
<td align="RIGHT"><font face="Times New Roman">-432590</font></td>
<td align="LEFT"><font face="Times New Roman"><br>
</font></td>
</tr>
<tr>
<td align="RIGHT" height="17"><font face="Times New Roman">11/02/10</font></td>
<td align="RIGHT"><font face="Times New Roman">100</font></td>
<td align="RIGHT"><font face="Times New Roman">4701.4</font></td>
<td align="RIGHT"><font face="Times New Roman">0</font></td>
<td align="RIGHT"><font face="Times New Roman">470140</font></td>
<td align="RIGHT"><font face="Times New Roman">-37550</font></td>
</tr>
<tr>
<td align="RIGHT" height="17"><font face="Times New Roman">11/02/10</font></td>
<td align="RIGHT"><font face="Times New Roman">100</font></td>
<td align="RIGHT"><font face="Times New Roman">4701.4</font></td>
<td align="RIGHT"><font face="Times New Roman">0</font></td>
<td align="RIGHT"><font face="Times New Roman">470140</font></td>
<td align="LEFT"><font face="Times New Roman"><br>
</font></td>
</tr>
<tr>
<td align="LEFT" height="17"><font face="Times New Roman">close</font></td>
<td align="RIGHT"><font face="Times New Roman">-100</font></td>
<td align="RIGHT"><font face="Times New Roman">4801.5</font></td>
<td align="LEFT"><font face="Times New Roman"><br>
</font></td>
<td align="RIGHT"><font face="Times New Roman">-480150</font></td>
<td align="RIGHT"><font face="Times New Roman">10010</font></td>
</tr>
<tr>
<td align="LEFT" height="17"><font face="Times New Roman"><br>
</font></td>
<td align="LEFT"><font face="Times New Roman"><br>
</font></td>
<td align="LEFT"><font face="Times New Roman"><br>
</font></td>
<td align="LEFT"><font face="Times New Roman"><br>
</font></td>
<td align="LEFT"><font face="Times New Roman"><br>
</font></td>
<td align="LEFT"><font face="Times New Roman"><br>
</font></td>
</tr>
<tr>
<td align="LEFT" height="17"><font face="Times New Roman"><br>
</font></td>
<td align="LEFT"><font face="Times New Roman"><br>
</font></td>
<td align="LEFT"><font face="Times New Roman"><br>
</font></td>
<td align="LEFT"><font face="Times New Roman"><br>
</font></td>
<td align="LEFT"><font face="Times New Roman"><br>
</font></td>
<td align="RIGHT"><font face="Times New Roman">353110</font></td>
</tr>
</tbody>
</table>
<br>
and I can run the r code twice (on the long, then short side) and do
this:<br>
<br>
long
<br>
Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost
<br>
1999-12-31 0 0.0 0 0 0.0
<br>
2003-06-12 100 3084.8 0 308480 3084.8
<br>
2008-01-22 -100 5186.8 0 -518680 5186.8
<br>
2009-06-17 100 3904.1 0 390410 3904.1
<br>
2010-06-07 -100 4325.9 0 -432590 4325.9
<br>
2010-11-02 100 4701.4 0 470140 4701.4
<br>
Net.Txn.Realized.PL
<br>
1999-12-31 0
<br>
2003-06-12 0
<br>
2008-01-22 210200
<br>
2009-06-17 0
<br>
2010-06-07 42180
<br>
2010-11-02 0
<br>
> short
<br>
Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost
<br>
1999-12-31 0 0.0 0 0 0.0
<br>
2008-01-22 -100 5186.8 0 -518680 5186.8
<br>
2009-06-17 100 3904.1 0 390410 3904.1
<br>
2010-06-07 -100 4325.9 0 -432590 4325.9
<br>
2010-11-02 100 4701.4 0 470140 4701.4
<br>
Net.Txn.Realized.PL
<br>
1999-12-31 0
<br>
2008-01-22 0
<br>
2009-06-17 128270
<br>
2010-06-07 0
<br>
2010-11-02 -37550
<br>
> global <- short + long
<br>
> global
<br>
Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost
<br>
1999-12-31 0 0.0 0 0 0.0
<br>
2008-01-22 -200 10373.6 0 -1037360 10373.6
<br>
2009-06-17 200 7808.2 0 780820 7808.2
<br>
2010-06-07 -200 8651.8 0 -865180 8651.8
<br>
2010-11-02 200 9402.8 0 940280 9402.8
<br>
Net.Txn.Realized.PL
<br>
1999-12-31 0
<br>
2008-01-22 210200
<br>
2009-06-17 128270
<br>
2010-06-07 42180
<br>
2010-11-02 -37550
<br>
><br>
<br>
which confirms the code can calculate the correct figures.<br>
<br>
the notes on add.rule say the various types are dealt with:<br>
<br>
<p>
All rules have a <code>type</code>. These may be any of:
</p>
<dl>
<dt>risk</dt>
<dd> rules that check and react to risk of positions, may stop all
other rule execution temporarily or permanently</dd>
<dt>order</dt>
<dd> rules for order processing of any open orders at time t,
always path-dependent</dd>
<dt>rebalance</dt>
<dd> rules executed specifically in a portfolio context,
unnecessary in univariate strategies</dd>
<dt>exit</dt>
<dd> rules to determine whether to exit a position</dd>
<dt>enter</dt>
<dd> rules to determine whether to enter or increase a position</dd>
</dl>
The rules will be executed by type, in the order listed above.
Multiple rules of each type may be defined, as with signals and
indicators, they will be executed in order by index number with any
other rules sharing the same type.
I've tried messing about with indexnum but no luck.<br>
<br>
Has anyone got this problem and solved it? <br>
</body>
</html>