<div dir="ltr">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)</div><div class="gmail_extra"><br clear="all"><div><div dir="ltr">
--<div>sm</div><div>Stergios Marinopoulos</div></div></div>
<br><br><div class="gmail_quote">On Fri, Aug 22, 2014 at 2:53 PM, Ilya Kipnis <span dir="ltr"><<a href="mailto:ilya.kipnis@gmail.com" target="_blank">ilya.kipnis@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Errr, in my demo, you need to put symbols <- "SPY" somewhere, as well<br>
as remove the initEq argument. (Was copying a bit too fast from my<br>
usual demos.) But yeah, the logic in my demo works.<br>
<br>
On Fri, Aug 22, 2014 at 5:19 PM, stergios marinopoulos<br>
<div class="HOEnZb"><div class="h5"><<a href="mailto:stergenator@gmail.com">stergenator@gmail.com</a>> wrote:<br>
> (I mistakenly did not include <a href="mailto:r-sig-finance@r-project.org">r-sig-finance@r-project.org</a> on the response.)<br>
><br>
><br>
> Hi Ilya,<br>
><br>
> I switched the orderqty as you suggested, but nothing changed.  That's<br>
> usually something I try when I question the total position quantity and in<br>
> my experience even if I had the wrong sign, it would still change the<br>
> quantity.  But in this case, nothing changed.<br>
><br>
> I do have the macd.R demo from the source working, which has a<br>
> commented-out stoptrailing chaining rule.  I'll check out your example<br>
> closely, and since it uses a limit order hopefully it should apply more<br>
> directly to my case.<br>
><br>
> Thanks for the ideas,<br>
><br>
> BTW, I was reading <a href="http://quantstrattrader.wordpress.com/" target="_blank">http://quantstrattrader.wordpress.com/</a> earlier in day<br>
> looking for clues to my problem.  That's a nice site.  Good work.<br>
><br>
><br>
><br>
><br>
> --<br>
> sm<br>
> Stergios Marinopoulos<br>
><br>
><br>
> On Fri, Aug 22, 2014 at 1:38 PM, Ilya Kipnis <<a href="mailto:ilya.kipnis@gmail.com">ilya.kipnis@gmail.com</a>> wrote:<br>
><br>
>> Stergios,<br>
>><br>
>> Can you rerun your code with orderqty set to -1 for your chain rule<br>
>> and see if that solves it?<br>
>><br>
>> -Ilya<br>
>><br>
>> On Fri, Aug 22, 2014 at 4:08 PM, stergios marinopoulos<br>
>> <<a href="mailto:stergenator@gmail.com">stergenator@gmail.com</a>> wrote:<br>
>> > Hi, I am having trouble getting a simple profit taking order to work via<br>
>> a<br>
>> > chaining rule.  In the contrived example below, I force a long entry of 2<br>
>> > shares.  Then I expect 1 share to be sold after a 1 point move (i.e. the<br>
>> > "profit taker"), and then I expect the final share to be sold when price<br>
>> > falls below the SMA50.<br>
>> ><br>
>> > While I do not see the profit taker order get executed, the price falling<br>
>> > below the SMA50 works as expected for all remaining shares.<br>
>> ><br>
>> > I would appreciate it if someone could point out what I am doing wrong<br>
>> with<br>
>> > the chaining rule.<br>
>> ><br>
>> > Thank you,<br>
>> > --<br>
>> > sm<br>
>> > Stergios Marinopoulos<br>
>> ><br>
>> ><br>
>> > library(quantstrat)<br>
>> ><br>
>> > # Boiler Plate<br>
>> > tickerSymbol   = "GE"<br>
>> > strategyStr  = 'TwoUnits'<br>
>> > GE  = getSymbols(tickerSymbol, from = "2012-01-01", to = "2012-11-15",<br>
>> > auto.assign = FALSE)<br>
>> > GE$SMA50  = SMA(Cl(GE), n = 50)<br>
>> > GE$CrossBack  = Cl(GE) - GE$SMA50<br>
>> > GE = GE["2012-07-15/"]<br>
>> > magicGoLongDay = "2012-07-23"<br>
>> ><br>
>> > currency("USD")<br>
>> > stock(tickerSymbol, currency="USD", multiplier=1)<br>
>> > rm.strat(strategyStr)<br>
>> > initDate = index(GE[1]) - 1<br>
>> > initPortf(name=strategyStr, symbols=tickerSymbol, initDate=initDate,<br>
>> > currency="USD")<br>
>> > initAcct(name=strategyStr, portfolios=strategyStr, initDate=initDate,<br>
>> > initEq=1e4, currency="USD")<br>
>> > initOrders(portfolio=strategyStr, initDate=initDate)<br>
>> > strategy(strategyStr, store=TRUE)<br>
>> ><br>
>> > zeros = xts(rep(0,nrow(GE)), <a href="http://order.by" target="_blank">order.by</a>=index(GE))<br>
>> > chartSeries(GE, TA="addTA(GE$SMA50, on=1, col=6);addTA(GE$CrossBack,<br>
>> > col=6);addTA(zeros, on=2, col=7);")<br>
>> ><br>
>> > # The indicator function.  Force a TRUE value on 6/5/2012<br>
>> > myIndicator <- function(n=2)<br>
>> > {<br>
>> >   indicator = xts(x=rep(0, nrow(GE)), <a href="http://order.by" target="_blank">order.by</a>=index(GE) )<br>
>> >   names(indicator) = "indValue"<br>
>> >   indicator[magicGoLongDay, "indValue"] = 1<br>
>> >   return( indicator[, "indValue"] )<br>
>> > }<br>
>> ><br>
>> > add.indicator(strategy=strategyStr, name="myIndicator",<br>
>> > arguments=list(n=2), label="indLabel")<br>
>> ><br>
>> > add.signal(<br>
>> >   strategy=strategyStr,<br>
>> >   name="sigThreshold",<br>
>> >   arguments=list(<br>
>> >     column       = "CrossBack",<br>
>> >     relationship = "lt",<br>
>> >     threshold    = 0,<br>
>> >     cross        = TRUE<br>
>> >   ),<br>
>> >   label="sig.price.lt.sma50"<br>
>> > )<br>
>> ><br>
>> > add.signal(<br>
>> >   strategy=strategyStr,<br>
>> >   name="sigThreshold",<br>
>> >   arguments=list(<br>
>> >     column       = "indValue.indLabel",<br>
>> >     relationship = "eq",<br>
>> >     threshold    = 1,<br>
>> >     cross        = TRUE<br>
>> >   ),<br>
>> >   label="goLong"<br>
>> > )<br>
>> ><br>
>> ><br>
>> > # Exit remaining when price crosses below on SMA50<br>
>> > add.rule(strategy=strategyStr, name='ruleSignal',<br>
>> >   arguments = list(<br>
>> >     sigcol      = "sig.price.lt.sma50",<br>
>> >     sigval      = TRUE,<br>
>> >     replace     = FALSE,<br>
>> >     orderside   = 'long',<br>
>> >     ordertype   = 'market',<br>
>> >     orderqty    = 'all',<br>
>> >     prefer      = 'Open'<br>
>> >   ),<br>
>> >   type    = 'exit',<br>
>> >   label   = 'ExitPriceLTSMA50'<br>
>> > )<br>
>> ><br>
>> > # Exit 1 unit as an initial profit target<br>
>> > add.rule(strategy=strategyStr, name='ruleSignal',<br>
>> >   arguments = list(<br>
>> >     sigcol    = 'goLong',<br>
>> >     sigval    = TRUE,<br>
>> >     replace   = FALSE,<br>
>> >     orderside = 'long',<br>
>> >     ordertype = 'limit',<br>
>> > #   ruletype  = 'exit',   # Is this order ambiguous?<br>
>> >     tmult     = FALSE,<br>
>> >     threshold = 1.00,<br>
>> >     orderqty  = 1,<br>
>> >     prefer    = 'Open'<br>
>> >   ),<br>
>> >   type    = 'chain',<br>
>> >   parent  = 'EnterLong',<br>
>> >   label   = 'TakeProfit'<br>
>> > )<br>
>> ><br>
>> > # Go long 2 shares when we have long signal<br>
>> > add.rule(strategy=strategyStr, name = 'ruleSignal',<br>
>> >   arguments = list(<br>
>> >     sigcol    = 'goLong',<br>
>> >     sigval    = TRUE,<br>
>> >     orderside = 'long' ,<br>
>> >     ordertype = 'market',<br>
>> >     orderqty  = 2,<br>
>> >     prefer    = 'Open'<br>
>> >   ),<br>
>> >   type  = 'enter',<br>
>> >   label = 'EnterLong'<br>
>> > )<br>
>> ><br>
>> > out = applyStrategy(strategy=strategyStr, portfolios=strategyStr,<br>
>> > verbose=TRUE, debug=TRUE)<br>
>> ><br>
>> > # Calculate P&L and resulting equity with blotter<br>
>> > dateRange=paste(<br>
>> >            as.character(index(first(GE))-1),<br>
>> >            '::',<br>
>> >            as.character(index(last(GE))+1),<br>
>> >            sep='')<br>
>> ><br>
>> > updatePortf(strategyStr, Dates = dateRange)<br>
>> > updateAcct(strategyStr,  Dates = dateRange)<br>
>> > updateEndEq(strategyStr, Dates = dateRange)<br>
>> ><br>
>> > obook        = getOrderBook(portfolio=strategyStr)<br>
>> > transactions = getTxns(Portfolio=strategyStr, Symbol=tickerSymbol)<br>
>> ><br>
>> >         [[alternative HTML version deleted]]<br>
>> ><br>
>> > _______________________________________________<br>
>> > <a href="mailto:R-SIG-Finance@r-project.org">R-SIG-Finance@r-project.org</a> mailing list<br>
>> > <a href="https://stat.ethz.ch/mailman/listinfo/r-sig-finance" target="_blank">https://stat.ethz.ch/mailman/listinfo/r-sig-finance</a><br>
>> > -- Subscriber-posting only. If you want to post, subscribe first.<br>
>> > -- Also note that this is not the r-help list where general R questions<br>
>> should go.<br>
>><br>
><br>
>         [[alternative HTML version deleted]]<br>
><br>
> _______________________________________________<br>
> <a href="mailto:R-SIG-Finance@r-project.org">R-SIG-Finance@r-project.org</a> mailing list<br>
> <a href="https://stat.ethz.ch/mailman/listinfo/r-sig-finance" target="_blank">https://stat.ethz.ch/mailman/listinfo/r-sig-finance</a><br>
> -- Subscriber-posting only. If you want to post, subscribe first.<br>
> -- Also note that this is not the r-help list where general R questions should go.<br>
</div></div></blockquote></div><br></div>