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.

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.

Stephen Choularton Ph.D., FIoD

9999 2226
0413 545 182


for insurance go to www.netinsure.com.au
for markets go to www.organicfoodmarkets.com.au

On 26/01/2011 2:47 PM, Aleksandr Rudnev wrote:
On Tue, Jan 25, 2011 at 3:54 PM, Stephen Choularton
<[email protected]> 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: [email protected]

_______________________________________________
[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.




-----
No virus found in this message.
Checked by AVG - www.avg.com




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


 # 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