I'm continuing to work with Quantstrat to try to understand the syntax and functionality of the program. I've been experimenting with the prefer pricing options in add.rule and applyStrategy. The prefer pricing works in applyStrategy - but there is only one option, enter and exit on the same prefer choice, which is not what I'm looking for.

On the other hand, add.rule allows the choice of different price preferences for enter and exit. However, the results are not what I would expect. The order book shows the correct prices - High for enter and Low for exit but the transactions show Closing prices as shown below
#           Order.Qty Order.Price Txn.Qty Txn.Price IBM.High IBM.Low IBM.Close
#2007-02-26       100       97.86     100     96.91    97.86   96.46     96.91
#2007-03-20      -100       93.55    -100     94.50    94.53   93.55     94.50
#2007-04-03      -100       96.23    -100     96.10    96.23   95.33     96.10
#2007-04-12       100       94.53     100     95.67    95.82   94.53     95.67
#2007-04-17      -100       97.66    -100     97.12    97.66   96.02     97.12
#2007-04-18       100       94.40     100     94.80    95.91   94.40     94.80
#2007-04-24      -100       99.90    -100     98.49    99.90   97.23     98.49

I've tried putting the prefer settings in parameters and dots but that doesn't work. I've also tried debugging in ruleOrderProc and it appears that the prefer setting is not being passed to the getPrice function. However, the code is complex and I haven't been able to understand where the issue is.

The following shows the code based on the bbands demo example. Any help would be welcome.

Thanks
Dan
####  CODE #####################################################
require(quantstrat)
suppressWarnings(rm("order_book.bbands",pos=.strategy))
suppressWarnings(rm("account.bbands","portfolio.bbands",pos=.blotter))
suppressWarnings(rm("account.st","portfolio.st","stock.str","stratBBands","initDate","initEq",'start_t','end_t'))

# some things to set up here
stock.str='IBM' # what are we trying it on

# we'll pass these
SD = 2 # how many standard deviations, traditionally 2
N = 20 # how many periods for the moving average, traditionally 20

currency('USD')
stock(stock.str,currency='USD',multiplier=1)

initDate='2006-12-31'
initEq=1000000

portfolio.st='bbands'
account.st='bbands'

initPortf(portfolio.st,symbols=stock.str, initDate=initDate)
initAcct(account.st,portfolios='bbands', initDate=initDate)
initOrders(portfolio=portfolio.st,initDate=initDate)

stratBBands <- strategy("bbands")

#one indicator
stratBBands <- add.indicator(strategy = stratBBands, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), maType='SMA'))


#add signals:
stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("Close","up"),relationship="gt"),label="Cl.gt.UpperBand")
stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("Close","dn"),relationship="lt"),label="Cl.lt.LowerBand")
stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("High","Low","mavg"),relationship="op"),label="Cross.Mid")

# lets add some rules
#put prefer as an argument
stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
list(sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100, ordertype='market',
 orderside=NULL, threshold=NULL,prefer="High"),type='enter')

stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
 list(sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100, ordertype='market',
 orderside=NULL, threshold=NULL,prefer="High"),type='enter')

stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
 list(sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all', ordertype='market',
  orderside=NULL, threshold=NULL,prefer="Low"),type='exit')

stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
list(sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100, ordertype='market',
 orderside=NULL, threshold=NULL,prefer="High"),type='enter')

stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
 list(sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100, ordertype='market',
 orderside=NULL, threshold=NULL,prefer="High"),type='enter')

stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
 list(sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all', ordertype='market',
  orderside=NULL, threshold=NULL,prefer="Low"),type='exit')



#alternately, to exit at the opposite band, the rules would be...
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Lo.gt.UpperBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Hi.lt.LowerBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')

#TODO add thresholds and stop-entry and stop-exit handling to test
if(!exists(stock.str)){
getSymbols(stock.str,from=initDate)
}

start_t<-Sys.time()
out<-try(applyStrategy(strategy=stratBBands , portfolios='bbands',parameters=list(sd=SD,n=N)) )

# look at the order book
ob<-getOrderBook(portfolio.st)
closedOrders<-ob$bbands$IBM[ob$bbands$IBM[,6]=="closed",1:2]
#get the stock prices then put in the order book and transactions
IBM_HLC<-HLC(get(stock.str)) #get the prices for the stock
txns<-getTxns(portfolio.st,stock.str)  #transactions come from the portfolio

compare<-merge(closedOrders[index(txns)],txns[,1:2],IBM_HLC[index(txns),])
compare[2:8,]

#           Order.Qty Order.Price Txn.Qty Txn.Price IBM.High IBM.Low IBM.Close
#2007-02-26       100       97.86     100     96.91    97.86   96.46     96.91
#2007-03-20      -100       93.55    -100     94.50    94.53   93.55     94.50
#2007-04-03      -100       96.23    -100     96.10    96.23   95.33     96.10
#2007-04-12       100       94.53     100     95.67    95.82   94.53     95.67
#2007-04-17      -100       97.66    -100     97.12    97.66   96.02     97.12
#2007-04-18       100       94.40     100     94.80    95.91   94.40     94.80
#2007-04-24      -100       99.90    -100     98.49    99.90   97.23     98.49
#


start_t<-Sys.time()
updatePortf(Portfolio='bbands',Dates=paste('::',as.Date(Sys.time()),sep=''),Prices=tran_cl)
end_t<-Sys.time()
print("updatePortf execution time:")
print(end_t-start_t)

chart.Posn(Portfolio='bbands',Symbol=stock.str)
plot(add_BBands(on=1,sd=SD,n=N))

############### stuff that didn't work ######################################

#move the prefer to paramaters - doesn't work - "close" is returned for orders and txns
#try list(prefer=High)etc
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
#list(sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100, ordertype='market',
# orderside=NULL, threshold=NULL),type='enter',parameters=list("prefer=High"))
#
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
# list(sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100, ordertype='market',
# orderside=NULL, threshold=NULL),type='enter',parameters=list("prefer=High"))
#
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
# list(sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all', ordertype='market',
#  orderside=NULL, threshold=NULL),type='exit',parameters=list("prefer=Low"))
#
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
#list(sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100, ordertype='market',
# orderside=NULL, threshold=NULL),type='enter',parameters=list("prefer=High"))
#
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
# list(sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100, ordertype='market',
# orderside=NULL, threshold=NULL),type='enter',parameters=list("prefer=High"))
#
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
# list(sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all', ordertype='market',
#  orderside=NULL, threshold=NULL),type='exit',parameters=list("prefer=Low"))
#

#now try without parameters designation, presumably as part of dots
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
#list(sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100, ordertype='market',
# orderside=NULL, threshold=NULL),type='enter',prefer="High")
#
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
# list(sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100, ordertype='market',
# orderside=NULL, threshold=NULL),type='enter',prefer="High")
#
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
# list(sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all', ordertype='market',
#  orderside=NULL, threshold=NULL),type='exit',prefer="Low")
#
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
#list(sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100, ordertype='market',
# orderside=NULL, threshold=NULL),type='enter',prefer="High")
#
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
# list(sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100, ordertype='market',
# orderside=NULL, threshold=NULL),type='enter',prefer="High")
#
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
# list(sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all', ordertype='market',
#  orderside=NULL, threshold=NULL),type='exit',prefer="Low")
#

###############################################################################
# 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: bbands.R 621 2011-06-09 23:18:04Z gsee $
#

	[[alternative HTML version deleted]]

