[R-SIG-Finance] Fwd: quantstrat chain rule type

stergios marinopoulos stergenator at gmail.com
Mon Aug 25 19:39:32 CEST 2014


Wow, that's subtle.  Even though all the examples show setting the
timezone, I somehow still missed it.  Thanks for your help.

The complete, working example is attached for anyone else following along,
(now or in the future)

--
sm
Stergios Marinopoulos


On Sat, Aug 23, 2014 at 8:38 AM, Joshua Ulrich <josh.m.ulrich at gmail.com>
wrote:

> Stergios,
>
> In addition to Ilya's correction of orderqty = -1, you need to
> explicitly set the timezone of your R session using:
> Sys.setenv(TZ="UTC").
>
> If you do not do this, then the blotter portfolio xts object will be
> initialized with your current system's timezone, but the timezone on
> the GE data is "UTC" because it has a "Date" class index.  That causes
> the chain rule's parent order price lookup to fail (because the
> timestamps do not match).
>
> Best,
> --
> Joshua Ulrich  |  about.me/joshuaulrich
> FOSS Trading  |  www.fosstrading.com
>
>
> On Fri, Aug 22, 2014 at 4:19 PM, stergios marinopoulos
> <stergenator at gmail.com> wrote:
> > (I mistakenly did not include r-sig-finance at r-project.org on the
> response.)
> >
> >
> > Hi Ilya,
> >
> > I switched the orderqty as you suggested, but nothing changed.  That's
> > usually something I try when I question the total position quantity and
> in
> > my experience even if I had the wrong sign, it would still change the
> > quantity.  But in this case, nothing changed.
> >
> > I do have the macd.R demo from the source working, which has a
> > commented-out stoptrailing chaining rule.  I'll check out your example
> > closely, and since it uses a limit order hopefully it should apply more
> > directly to my case.
> >
> > Thanks for the ideas,
> >
> > BTW, I was reading http://quantstrattrader.wordpress.com/ earlier in day
> > looking for clues to my problem.  That's a nice site.  Good work.
> >
> >
> >
> >
> > --
> > sm
> > Stergios Marinopoulos
> >
> >
> > On Fri, Aug 22, 2014 at 1:38 PM, Ilya Kipnis <ilya.kipnis at gmail.com>
> wrote:
> >
> >> Stergios,
> >>
> >> Can you rerun your code with orderqty set to -1 for your chain rule
> >> and see if that solves it?
> >>
> >> -Ilya
> >>
> >> On Fri, Aug 22, 2014 at 4:08 PM, stergios marinopoulos
> >> <stergenator at gmail.com> wrote:
> >> > Hi, I am having trouble getting a simple profit taking order to work
> via
> >> a
> >> > chaining rule.  In the contrived example below, I force a long entry
> of 2
> >> > shares.  Then I expect 1 share to be sold after a 1 point move (i.e.
> the
> >> > "profit taker"), and then I expect the final share to be sold when
> price
> >> > falls below the SMA50.
> >> >
> >> > While I do not see the profit taker order get executed, the price
> falling
> >> > below the SMA50 works as expected for all remaining shares.
> >> >
> >> > I would appreciate it if someone could point out what I am doing wrong
> >> with
> >> > the chaining rule.
> >> >
> >> > Thank you,
> >> > --
> >> > sm
> >> > Stergios Marinopoulos
> >> >
> >> >
> >> > library(quantstrat)
> >> >
> >> > # Boiler Plate
> >> > tickerSymbol   = "GE"
> >> > strategyStr  = 'TwoUnits'
> >> > GE  = getSymbols(tickerSymbol, from = "2012-01-01", to = "2012-11-15",
> >> > auto.assign = FALSE)
> >> > GE$SMA50  = SMA(Cl(GE), n = 50)
> >> > GE$CrossBack  = Cl(GE) - GE$SMA50
> >> > GE = GE["2012-07-15/"]
> >> > magicGoLongDay = "2012-07-23"
> >> >
> >> > currency("USD")
> >> > stock(tickerSymbol, currency="USD", multiplier=1)
> >> > rm.strat(strategyStr)
> >> > initDate = index(GE[1]) - 1
> >> > initPortf(name=strategyStr, symbols=tickerSymbol, initDate=initDate,
> >> > currency="USD")
> >> > initAcct(name=strategyStr, portfolios=strategyStr, initDate=initDate,
> >> > initEq=1e4, currency="USD")
> >> > initOrders(portfolio=strategyStr, initDate=initDate)
> >> > strategy(strategyStr, store=TRUE)
> >> >
> >> > zeros = xts(rep(0,nrow(GE)), order.by=index(GE))
> >> > chartSeries(GE, TA="addTA(GE$SMA50, on=1, col=6);addTA(GE$CrossBack,
> >> > col=6);addTA(zeros, on=2, col=7);")
> >> >
> >> > # The indicator function.  Force a TRUE value on 6/5/2012
> >> > myIndicator <- function(n=2)
> >> > {
> >> >   indicator = xts(x=rep(0, nrow(GE)), order.by=index(GE) )
> >> >   names(indicator) = "indValue"
> >> >   indicator[magicGoLongDay, "indValue"] = 1
> >> >   return( indicator[, "indValue"] )
> >> > }
> >> >
> >> > add.indicator(strategy=strategyStr, name="myIndicator",
> >> > arguments=list(n=2), label="indLabel")
> >> >
> >> > add.signal(
> >> >   strategy=strategyStr,
> >> >   name="sigThreshold",
> >> >   arguments=list(
> >> >     column       = "CrossBack",
> >> >     relationship = "lt",
> >> >     threshold    = 0,
> >> >     cross        = TRUE
> >> >   ),
> >> >   label="sig.price.lt.sma50"
> >> > )
> >> >
> >> > add.signal(
> >> >   strategy=strategyStr,
> >> >   name="sigThreshold",
> >> >   arguments=list(
> >> >     column       = "indValue.indLabel",
> >> >     relationship = "eq",
> >> >     threshold    = 1,
> >> >     cross        = TRUE
> >> >   ),
> >> >   label="goLong"
> >> > )
> >> >
> >> >
> >> > # Exit remaining when price crosses below on SMA50
> >> > add.rule(strategy=strategyStr, name='ruleSignal',
> >> >   arguments = list(
> >> >     sigcol      = "sig.price.lt.sma50",
> >> >     sigval      = TRUE,
> >> >     replace     = FALSE,
> >> >     orderside   = 'long',
> >> >     ordertype   = 'market',
> >> >     orderqty    = 'all',
> >> >     prefer      = 'Open'
> >> >   ),
> >> >   type    = 'exit',
> >> >   label   = 'ExitPriceLTSMA50'
> >> > )
> >> >
> >> > # Exit 1 unit as an initial profit target
> >> > add.rule(strategy=strategyStr, name='ruleSignal',
> >> >   arguments = list(
> >> >     sigcol    = 'goLong',
> >> >     sigval    = TRUE,
> >> >     replace   = FALSE,
> >> >     orderside = 'long',
> >> >     ordertype = 'limit',
> >> > #   ruletype  = 'exit',   # Is this order ambiguous?
> >> >     tmult     = FALSE,
> >> >     threshold = 1.00,
> >> >     orderqty  = 1,
> >> >     prefer    = 'Open'
> >> >   ),
> >> >   type    = 'chain',
> >> >   parent  = 'EnterLong',
> >> >   label   = 'TakeProfit'
> >> > )
> >> >
> >> > # Go long 2 shares when we have long signal
> >> > add.rule(strategy=strategyStr, name = 'ruleSignal',
> >> >   arguments = list(
> >> >     sigcol    = 'goLong',
> >> >     sigval    = TRUE,
> >> >     orderside = 'long' ,
> >> >     ordertype = 'market',
> >> >     orderqty  = 2,
> >> >     prefer    = 'Open'
> >> >   ),
> >> >   type  = 'enter',
> >> >   label = 'EnterLong'
> >> > )
> >> >
> >> > out = applyStrategy(strategy=strategyStr, portfolios=strategyStr,
> >> > verbose=TRUE, debug=TRUE)
> >> >
> >> > # Calculate P&L and resulting equity with blotter
> >> > dateRange=paste(
> >> >            as.character(index(first(GE))-1),
> >> >            '::',
> >> >            as.character(index(last(GE))+1),
> >> >            sep='')
> >> >
> >> > updatePortf(strategyStr, Dates = dateRange)
> >> > updateAcct(strategyStr,  Dates = dateRange)
> >> > updateEndEq(strategyStr, Dates = dateRange)
> >> >
> >> > obook        = getOrderBook(portfolio=strategyStr)
> >> > transactions = getTxns(Portfolio=strategyStr, Symbol=tickerSymbol)
> >> >
> >> >         [[alternative HTML version deleted]]
> >> >
> >> > _______________________________________________
> >> > R-SIG-Finance at r-project.org 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.
> >>
> >
> >         [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-SIG-Finance at r-project.org 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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20140825/329e737f/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: qsChainingSellLimit.R
Type: application/octet-stream
Size: 5131 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20140825/329e737f/attachment.obj>


More information about the R-SIG-Finance mailing list