[R-SIG-Finance] stop and reverse strategy not behaving as I expected

Brian G. Peterson brian at braverock.com
Wed Jan 18 18:02:13 CET 2012


Check the order of your rules.

Exit rules will be processed before entries, per the documentation.

So, if you want to stop and reverse, you need to exit first, then enter
the new position, and not violate your max position on either side.

I'll take a look at your code when I get a chance, but hopefully this
helps.

  - Brian
 

On Wed, 2012-01-18 at 17:40 +0100, Samuel de Bousquet wrote:
> Yes, that's correct. But the signal I use to exit the long position is the
> same that the one I use to go short. Same thing when I go from short to
> long. That is not a typical Donchian breakout trading, but a "stop and
> reverse strategy".
> 
> Looking at the code, I have twice 2 rules looking at the same indicator.
> However, only one will trigger a trade the same day.
> 
> S.
> 
> 
> 
> On Wed, Jan 18, 2012 at 4:50 PM, Kent Hoxsey <khoxsey at gmail.com> wrote:
> 
> > Argh, forgot to include the list as well.
> > ---
> > Sam- the behavior you describe is precisely how channel-breakout
> > strategies are expected to operate, with a stable no-trade zone between the
> > upper and lower breakout levels (the "channel") to keep you from getting
> > whipsawed during consolidation periods.
> >
> > If you want a strategy that is always in the market, you might choose a
> > moving average or other index indicator rather than a channel indicator.
> >
> > Kent
> >
> >
> > On Jan 18, 2012, at 3:07 AM, Samuel de Bousquet wrote:
> >
> > > Hello,
> > >
> > > I am a little bit surprised by the behaviour of the below strategy, a
> > > simple 40 day Donchian Breakout (do not pay attention to the portfolio
> > > being named Faber).
> > >
> > > Passed the initialization, I would have expected the strategy to be
> > either
> > > long +1000 contracts or short 1000 contracts as it suppose to stop and
> > > reverse on the same rule. But is sometimes goes cash.
> > >
> > > Would you understand why ?
> > >
> > > Sam
> > >
> > > Code:
> > >
> > >
> > > require(quantstrat)
> > > require(PerformanceAnalytics)
> > >
> > >
> > >
> > > # Set initial values
> > > initDate <- "2010-04-30"
> > > endDate <- "2012-01-13"
> > > initEq <- 1000000
> > >
> > > # Pull Yahoo Finance data
> > > symbols <- c("IEF", "SPY")
> > > getSymbols(symbols, from=initDate, to=endDate,
> > > index.class=c("POSIXt","POSIXct"), src='yahoo')
> > >
> > > # Set up instruments with FinancialInstruments package
> > > currency("USD")
> > > for(symbol in symbols) {
> > >  stock(symbol, currency="USD", multiplier=1)
> > > }
> > >
> > > # Delete portfolio, account, and order book if they already exist
> > > suppressWarnings(rm("account.faber","portfolio.faber",pos=.blotter))
> > > suppressWarnings(rm("order_book.faber",pos=.strategy))
> > >
> > > # Initialize portfolio and account
> > > initPortf("faber", symbols=symbols, initDate=initDate)
> > > initAcct("faber", portfolios="faber", initDate=initDate, initEq=initEq)
> > > initOrders(portfolio="faber", initDate=initDate)
> > >
> > > # Initialize a strategy object
> > > stratFaber <- strategy("faber")
> > >
> > > # Add the 40-day Donchian indicator
> > > stratFaber <- add.indicator(strategy=stratFaber,
> > >                            name="DonchianChannel",
> > >
> > > arguments=list(HL=quote(cbind(Hi(mktdata),Lo(mktdata))), n=40),
> > >                            label="Donchian40")
> > >
> > > # There are two signals:
> > > # The first is when monthly price crosses over the 40-days Donchian
> > > stratFaber <- add.signal(stratFaber,
> > >                         name="sigComparison",
> > >
> > > arguments=list(columns=c("High","Donchian40.high"),relationship="gte"),
> > >                         label="Hi.gt.Donchian40")
> > > # The second is when the monthly price crosses under the 40-days Donchian
> > > stratFaber <- add.signal(stratFaber,
> > >                         name="sigComparison",
> > >                         arguments=list(columns=c("Low","Donchian40.low"),
> > > relationship="lte"),
> > >                         label="Lo.lt.Donchian40")
> > >
> > > # There are two rules:
> > > # The first is to buy when the price crosses above the Donchian
> > > stratFaber <- add.rule(stratFaber,
> > >                       name="ruleSignal",
> > >                       arguments=list(sigcol="Hi.gt.Donchian40",
> > >                                      sigval=TRUE,
> > >                                      orderqty=1000,
> > >                                      ordertype="market",
> > >                                      orderside="long",
> > >                                      pricemethod="market",
> > >                                      TxnFees=-5,
> > >                                      osFUN=osMaxPos),
> > >                       type="enter",
> > >                       path.dep=TRUE)
> > > # The second is to sell when the price crosses below the Donchian
> > > stratFaber <- add.rule(stratFaber,
> > >                       name="ruleSignal",
> > >                       arguments=list(sigcol="Lo.lt.Donchian40",
> > >                                      sigval=TRUE,
> > >                                      orderqty=-1000,
> > >                                      ordertype="market",
> > >                                      orderside="short",
> > >                                      pricemethod="market",
> > >                                      TxnFees=-5,
> > >                                      osFUN=osMaxPos),
> > >                       type="exit",
> > >                       path.dep=TRUE)
> > >
> > > stratFaber <- add.rule(stratFaber,
> > >                       name="ruleSignal",
> > >                       arguments=list(sigcol="Lo.lt.Donchian40",
> > >                                      sigval=TRUE,
> > >                                      orderqty=-1000,
> > >                                      ordertype="market",
> > >                                      orderside="short",
> > >                                      pricemethod="market",
> > >                                      TxnFees=-5,
> > >                                      osFUN=osMaxPos),
> > >                       type="enter",
> > >                       path.dep=TRUE)
> > >
> > > stratFaber <- add.rule(stratFaber,
> > >                       name="ruleSignal",
> > >                       arguments=list(sigcol="Hi.gt.Donchian40",
> > >                                      sigval=TRUE,
> > >                                      orderqty=1000,
> > >                                      ordertype="market",
> > >                                      orderside="long",
> > >                                      pricemethod="market",
> > >                                      TxnFees=-5,
> > >                                      osFUN=osMaxPos),
> > >                       type="exit",
> > >                       path.dep=TRUE)
> > >
> > >
> > >
> > > # Set position limits so we don't add to the position every month Close >
> > > Donchian40
> > > addPosLimit("faber", "SPY", timestamp=initDate, maxpos=1000,
> > minpos=-1000)
> > > addPosLimit("faber", "IEF", timestamp=initDate, maxpos=1000,
> > minpos=-1000)
> > >
> > > # Process the indicators and generate trades
> > > out <- try(applyStrategy(strategy=stratFaber, portfolios="faber"))
> > > updatePortf(Portfolio = "faber",
> > > Dates=paste('::',as.Date("2012-01-13"),sep=''))
> > >
> > > # Evaluate results
> > > portRet <- PortfReturns("faber")
> > > portRet$Total <- rowSums(portRet, na.rm=TRUE)
> > > charts.PerformanceSummary(portRet$Total)
> > > tradeStats("faber")[,c("Symbol","Num.Trades","Net.Trading.PL
> > > ","maxDrawdown")]
> > >
> > >
> > > themelist<-chart_theme()
> > > themelist$col$up.col<-'green'
> > > themelist$col$dn.col<-'pink'
> > > chart.Posn(Portfolio="faber",Symbol="SPY",theme=themelist,log=TRUE, TA =
> > > "Donchian40")
> > >
> > >       [[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list