[R-SIG-Finance] Fwd: quantstrat chain rule type
stergios marinopoulos
stergenator at gmail.com
Mon Aug 25 19:34:30 CEST 2014
Thanks for that Ilya. I have made the edits and attached a complete
working example to this message for any else following along. (now or in
the future)
--
sm
Stergios Marinopoulos
On Fri, Aug 22, 2014 at 2:53 PM, Ilya Kipnis <ilya.kipnis at gmail.com> wrote:
> Errr, in my demo, you need to put symbols <- "SPY" somewhere, as well
> as remove the initEq argument. (Was copying a bit too fast from my
> usual demos.) But yeah, the logic in my demo works.
>
> On Fri, Aug 22, 2014 at 5: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/975e535d/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ilya.R
Type: application/octet-stream
Size: 3048 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20140825/975e535d/attachment.obj>
More information about the R-SIG-Finance
mailing list