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:

 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')

 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')

 # if you want a long/short Stops and Reverse MA cross strategy, you'd add two more rules for the short side:
 
 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')
 
 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')
 
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:
getOrderBook(portfolio.st)
$macross
$macross$AXJO
           Order.Qty Order.Price Order.Type Order.Side Order.Threshold
1999-12-31 "0"       NA          "init"     "long"     "0"           
2003-06-12 "100"     "3084.8"    "market"   "long"     NA            
2008-01-22 "-100"    "5186.8"    "market"   "short"    NA            
2008-01-22 "-100"    "5186.8"    "market"   "long"     NA            
2009-06-17 "100"     "3904.1"    "market"   "long"     NA            
2010-06-07 "-100"    "4325.9"    "market"   "short"    NA            
2010-06-07 "-100"    "4325.9"    "market"   "long"     NA            
2010-11-02 "100"     "4701.4"    "market"   "long"     NA            
           Order.Status Order.StatusTime Order.Set Txn.Fees
1999-12-31 "closed"     "1999-12-31"     "1"       "0"    
2003-06-12 "closed"     "2003-06-13"     NA        "0"    
2008-01-22 "closed"     "2008-01-23"     NA        "0"    
2008-01-22 "closed"     "2010-06-07"     NA        "0"    
2009-06-17 "closed"     "2009-06-18"     NA        "0"    
2010-06-07 "closed"     "2010-06-08"     NA        "0"    
2010-06-07 "closed"     "2011-01-24"     NA        "0"    
2010-11-02 "closed"     "2010-11-03"     NA        "0"    


attr(,"class")
[1] "order_book"
>
> print('transactions')
[1] "transactions"
>
>
> getTxns(Portfolio=portfolio.st, Symbol=stock.str)
           Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost
1999-12-31       0       0.0        0         0          0.0
2003-06-12     100    3084.8        0    308480       3084.8
2008-01-22    -100    5186.8        0   -518680       5186.8
2008-01-22    -100    5186.8        0   -518680       5186.8
2009-06-17     100    3904.1        0    390410       3904.1
2010-06-07    -100    4325.9        0   -432590       4325.9
2010-06-07    -100    4325.9        0   -432590       4325.9
2010-11-02     100    4701.4        0    470140       4701.4
           Net.Txn.Realized.PL
1999-12-31                   0
2003-06-12                   0
2008-01-22              210200
2008-01-22                   0
2009-06-17                   0
2010-06-07               42180
2010-06-07                   0
2010-11-02                   0

I have done a manual calculation that shows the results should be:

my calculation










06/12/03 100 3084.8 0 308480
01/22/08 -100 5186.8 0 -518680 210200
01/22/08 -100 5186.8 0 -518680
06/17/09 100 3904.1 0 390410 128270
06/17/09 100 3904.1 0 390410
06/07/10 -100 4325.9 0 -432590 42180
06/07/10 -100 4325.9 0 -432590
11/02/10 100 4701.4 0 470140 -37550
11/02/10 100 4701.4 0 470140
close -100 4801.5
-480150 10010











353110

and I can run the r code twice (on the long, then short side) and do this:

 long
           Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost
1999-12-31       0       0.0        0         0          0.0
2003-06-12     100    3084.8        0    308480       3084.8
2008-01-22    -100    5186.8        0   -518680       5186.8
2009-06-17     100    3904.1        0    390410       3904.1
2010-06-07    -100    4325.9        0   -432590       4325.9
2010-11-02     100    4701.4        0    470140       4701.4
           Net.Txn.Realized.PL
1999-12-31                   0
2003-06-12                   0
2008-01-22              210200
2009-06-17                   0
2010-06-07               42180
2010-11-02                   0
> short
           Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost
1999-12-31       0       0.0        0         0          0.0
2008-01-22    -100    5186.8        0   -518680       5186.8
2009-06-17     100    3904.1        0    390410       3904.1
2010-06-07    -100    4325.9        0   -432590       4325.9
2010-11-02     100    4701.4        0    470140       4701.4
           Net.Txn.Realized.PL
1999-12-31                   0
2008-01-22                   0
2009-06-17              128270
2010-06-07                   0
2010-11-02              -37550
> global <- short + long
> global
           Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost
1999-12-31       0       0.0        0         0          0.0
2008-01-22    -200   10373.6        0  -1037360      10373.6
2009-06-17     200    7808.2        0    780820       7808.2
2010-06-07    -200    8651.8        0   -865180       8651.8
2010-11-02     200    9402.8        0    940280       9402.8
           Net.Txn.Realized.PL
1999-12-31                   0
2008-01-22              210200
2009-06-17              128270
2010-06-07               42180
2010-11-02              -37550
>

which confirms the code can calculate the correct figures.

the notes on add.rule say  the various types are dealt with:

All rules have a type. These may be any of:

risk
rules that check and react to risk of positions, may stop all other rule execution temporarily or permanently
order
rules for order processing of any open orders at time t, always path-dependent
rebalance
rules executed specifically in a portfolio context, unnecessary in univariate strategies
exit
rules to determine whether to exit a position
enter
rules to determine whether to enter or increase a position
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.

Has anyone got this problem and solved it?
 
#########################################################################################################################################################################
 #A simple moving average strategy to evaluate trade efficiency
 #checks on SMA of 50 days and SMA of 200 days
 #Author: R. Raghuraman("raghu"), Brian Peterson
 
#########################################################################################################################################################################
 
 require(quantstrat)

 try(rm("order_book.macross",pos=.strategy),silent=TRUE)
 #get("order_book.macross",pos=.strategy)

 try(rm("account.macross","portfolio.macross",pos=.blotter),silent=TRUE)

 
try(rm("account.st","portfolio.st","stock.str","stratMACROSS","initDate","initEq",'start_t','end_t'),silent=TRUE)
 
 
 initDate='1999-12-31'
 
 initEq=1000000
 
 stock.str='^AXJO' # what are we trying it on
 stock.str=getSymbols(stock.str,from=as.Date(initDate)+1) 
 
 # changeable short & long MA's
 
 # shorter periods did NOT work, eg 10/20 20/60
 shortMA <- 50
 longMA <- 200

 stock(stock.str,currency='AUD',multiplier=1)

 

 portfolio.st='macross'

 account.st='macross'

 
 initPortf(portfolio.st,symbols=stock.str, currency='AUD', initDate=initDate)
 #initPortf(portfolio.st,symbols=stock.str, initDate=initDate)

 initAcct(account.st,portfolios='macross',  currency='AUD', initDate=initDate)
#initAcct(account.st,portfolios=portfolio.st, initDate=initDate)

 initOrders(portfolio=portfolio.st,initDate=initDate)

 stratMACROSS<- strategy(portfolio.st)

 # indicators
 
 stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", arguments 
= list(x=quote(Cl(mktdata)), n=shortMA),label= "shortMA" )
 
 
 stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", arguments 
= list(x=quote(Cl(mktdata)), n=longMA),label= "longMA")

 # signals
 

 # long side
 

 stratMACROSS <- add.signal(strategy = 
stratMACROSS,name="sigCrossover",arguments = 
list(columns=c("shortMA","longMA"),relationship="gt"),label="shortMA.gt.longMA")

 stratMACROSS <- add.signal(strategy = 
stratMACROSS,name="sigCrossover",arguments = 
list(column=c("shortMA","longMA"),relationship="lt"),label="shortMA.lt.longMA")
 
 # short side
 

 stratMACROSS <- add.signal(strategy = 
stratMACROSS,name="sigCrossover",arguments = 
list(columns=c("shortMA","longMA"),relationship="lt"),label="shortMA.lt.longMA")
 
 stratMACROSS <- add.signal(strategy = 
stratMACROSS,name="sigCrossover",arguments = 
list(column=c("shortMA","longMA"),relationship="gt"),label="shortMA.gt.longMA")

 # rules
 
 # txnExp = ???
 

 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')

 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')

 # if you want a long/short Stops and Reverse MA cross strategy, you'd add two 
more rules for the short side: 
 
 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')
 
 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')
 


#getSymbols('^AXJO',currency='AUD', from=initDate)

 start_t<-Sys.time()

 out<-try(applyStrategy(strategy=stratMACROSS , 
portfolios=portfolio.st,verbose=TRUE))
 

 end_t<-Sys.time()

 print(end_t-start_t)

 start_t<-Sys.time()

 updatePortf(Portfolio='macross',Dates=paste('::',as.Date(Sys.time()),sep=''))
 end_t<-Sys.time()

 print("trade blotter portfolio update:")

 print(end_t-start_t)

 chart.Posn(Portfolio=portfolio.st,Symbol=stock.str)

 add_SMA(n=shortMA , on=1,col='blue')

 add_SMA(n=longMA , on=1)
 
#get/save some data

write.table(mktdata, 'c:/r/logs/mktdata.tbl', col.name=TRUE)

print ('order book')


getOrderBook(portfolio.st)

print('transactions')


getTxns(Portfolio=portfolio.st, Symbol=stock.str)

print('account')
  
getAccount(portfolio.st)



 ###############################################################################
 # R (http://r-project.org/) Quantitative Strategy Model Framework
 #
 # Copyright (c) 2009-2010
 # Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and Joshua 
Ulrich 
 #
 # This library is distributed under the terms of the GNU Public License (GPL)
 # for full details see the file COPYING
 #
 # $Id: maCross.R 374 2010-08-17 18:43:35Z braverock $
 #
 ###############################################################################


_______________________________________________
[email protected] 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.

Reply via email to