I have the following question about modeling in quantstrat multiple exits
for a given transaction. How do you model a firm stop loss exit rule in
addition to a trailing-style exit for a given transaction? For example, how
do you add an exit rule for a given transaction which says exit when the
price drops below "Close - (2) ATR" of the security, where "Close -  (2)
ATR" is calculated at the time of entry for the transaction and stays
constant for the life of the transaction?  I've included the "maCross" demo
from quantstrat to use as an example. It has an exit of "ma50 less than
ma200" rule. To this, I'd like to add the "when closing price less than (2)
ATR, calculated at the time of entry, exit" rule but need help with how to
code it in quantstrat.

Thanks much.
jnoble

#########################################################################################################################################################################
#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)
suppressWarnings(rm("order_book.macross",pos=.strategy))
suppressWarnings(rm("account.macross","portfolio.macross",pos=.blotter))
suppressWarnings(rm("account.st","portfolio.st
","stock.str","stratMACROSS","initDate","initEq",'start_t','end_t'))
stock.str='AAPL' # what are we trying it on
currency('USD')
stock(stock.str,currency='USD',multiplier=1)
initDate='1999-12-31'
initEq=1000000
portfolio.st='macross'
account.st='macross'
initPortf(portfolio.st,symbols=stock.str, initDate=initDate)
initAcct(account.st,portfolios=portfolio.st, initDate=initDate)
initOrders(portfolio=portfolio.st,initDate=initDate)
stratMACROSS<- strategy(portfolio.st)
stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA",
arguments = list(x=quote(Cl(mktdata)), n=50),label= "ma50" )
stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA",
arguments = list(x=quote(Cl(mktdata)), n=200),label= "ma200")
stratMACROSS <- add.signal(strategy =
stratMACROSS,name="sigCrossover",arguments =
list(columns=c("ma50","ma200"), relationship="gte"),label="ma50.gt.ma200")
stratMACROSS <- add.signal(strategy =
stratMACROSS,name="sigCrossover",arguments =
list(column=c("ma50","ma200"),relationship="lt"),label="ma50.lt.ma200")
stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal',
arguments = list(sigcol="ma50.gt.ma200",sigval=TRUE, orderqty=100,
ordertype='market', orderside='long'),type='enter')
stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal',
arguments = list(sigcol="ma50.lt.ma200",sigval=TRUE, orderqty=-100,
ordertype='market', orderside='long'),type='exit')

#####################################################################
#
# This is where I would want to add the stop loss exit based on Close -
 (2) ATR calculated at time of entry and stays constant for the transaction
to go
#
######################################################################

getSymbols(stock.str,from=initDate)
for(i in stock.str)
assign(i, adjustOHLC(get(i),use.Adjusted=TRUE))
start_t<-Sys.time()
out<-try(applyStrategy(strategy=stratMACROSS , portfolios=portfolio.st))
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='macross',Symbol=stock.str)
plot(add_SMA(n=50 , on=1,col='blue'))
plot(add_SMA(n=200, on=1))

	[[alternative HTML version deleted]]

