[R-SIG-Finance] stoptrailing mechanics question in MACD example - Quantstrat

Derek Wong treydog999 at gmail.com
Sat Mar 12 06:44:34 CET 2016


Hi Joshua,

Thank you for that reply. I will take a look at that. But what you
explained seems to be the conditions I found by digging into the order
book and order update logic. The confusing part for that was using the
OHLC data and mixing the close price as a "filter" rather than just
highest high lowest low. Also for the fills as well, as a close below
the stop limit order is not really realistic. As you can be filled by
the low of the day and still have the close above (high of the day and
close below for a short), in reality you would have obtained a fill in
the market. This is assuming that the stoptrailing order is in the
market as it executes at the limit price. But in the back test you
would still be maintaining a position as the order would not be
filled. This leads to some disparity in the results as only winners
would not be filled and continue higher but any losers would be cut
off properly. When in reality both would have been closed. I believe
this backtesting behavior biases the returns upwards when using the
stoptrailing order.

Thanks again,

Derek

On Sat, Mar 12, 2016 at 11:26 AM, Joshua Ulrich <josh.m.ulrich at gmail.com> wrote:
> On Wed, Feb 17, 2016 at 2:22 AM, Derek Wong <treydog999 at gmail.com> wrote:
>> Hello All,
>>
>> I am using Quantstrat 0.9.1709 with Microsoft R Open 3.2.3 on Rstudio.
>>
>> I am having a problem understanding the trailing methodology of the
>> stoptrailing order type in Quantstrat. It seems to me that the
>> trailing stop is not replaced as frequently as it should. Only for
>> larger price changes (threshold value?) it moves but for smaller
>> increments it remains the same. Is this the intended result?
>>
>> I am using the MACD example and commenting out the original exit rule
>> and un-commenting the trailing exit.
>>
>> <snip>
>> #alternatives for risk stops:
>> # simple stoplimit order, with threshold multiplier
>> #add.rule(strat.st,name='ruleSignal', arguments =
>> list(sigcol="signal.gt.zero",sigval=TRUE, orderqty='all',
>> ordertype='stoplimit', orderside='long', threshold=-.05,tmult=TRUE,
>> orderset='exit2'),type='chain', parent='enter',
>> label='risk',storefun=FALSE)
>> # alternately, use a trailing order, also with a threshold multiplier
>> add.rule(strat.st,name='ruleSignal',
>>          arguments = list(sigcol="signal.gt.zero",
>>                           sigval=TRUE,
>>                           orderqty='all',
>>                           ordertype='stoptrailing',
>>                           orderside='long',
>>                           threshold=-1,
>>                           tmult=FALSE,
>>                           orderset='exit2'),
>>          type='chain',
>>          parent='enter',
>>          label='trailingexit')
>>
>> # # exit
>> # add.rule(strat.st,name='ruleSignal',
>> #          arguments = list(sigcol="signal.lt.zero",
>> #                           sigval=TRUE,
>> #                           orderqty='all',
>> #                           ordertype='market',
>> #                           orderside='long',
>> #                           threshold=NULL,
>> #                           orderset='exit2'),
>> #          type='exit',
>> #          label='exit'
>> # )
>>
>> <snip>
>>
>>
>> I am looking at the orderbook and comparing it to the portfolio and
>> price of AAPL. I can see that the trailing stop moves but not on every
>> new high created during the trade. The first trade is an example.
>>
>> <output>
>>> AAPL["2007-03-15/2007-03-26"]
>>            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
>> 2007-03-15     89.96     90.36    89.31      89.57   139874700      11.84997
>> 2007-03-16     89.54     89.99    89.32      89.59   142926000      11.85262
>> 2007-03-19     90.24     91.55    89.59      91.13   178240300      12.05636
>> 2007-03-20     91.35     91.84    91.06      91.48   122229100      12.10266
>> 2007-03-21     91.99     94.00    91.65      93.87   171724000      12.41886
>> 2007-03-22     93.73     94.36    93.00      93.96   140373100      12.43076
>> 2007-03-23     93.35     94.07    93.30      93.52   112721000      12.37255
>> 2007-03-26     93.99     95.90    93.30      95.85   216246800      12.68081
>>> obook$macd$AAPL["2007-03-15/2007-03-26"]
>>            Order.Qty Order.Price Order.Type     Order.Side
>> Order.Threshold Order.Status Order.StatusTime      Prefer
>> 2007-03-15 "100"     "89.570001" "market"       "long"     NA
>>     "closed"     "2007-03-16 00:00:00" ""
>> 2007-03-16 "all"     "88.590001" "stoptrailing" "long"     "-1"
>>     "replaced"   "2007-03-19 00:00:00" ""
>> 2007-03-19 "all"     "90.549999" "stoptrailing" "long"     "-1"
>>     "replaced"   "2007-03-21 00:00:00" ""
>> 2007-03-21 "all"     "93.000002" "stoptrailing" "long"     "-1"
>>     "replaced"   "2007-03-26 00:00:00" ""
>> 2007-03-26 "all"     "94.899999" "stoptrailing" "long"     "-1"
>>     "closed"     "2007-03-28 00:00:00" ""
>>            Order.Set Txn.Fees Rule           Time.In.Force
>> 2007-03-15 NA        "0"      "enter"        ""
>> 2007-03-16 "exit2"   "0"      "trailingexit" ""
>> 2007-03-19 "exit2"   "0"      "trailingexit" ""
>> 2007-03-21 "exit2"   "0"      "trailingexit" ""
>> 2007-03-26 "exit2"   "0"      "trailingexit" ""
>>
>> <output>
>>
>> You can see that for the date 2007-03-15 to 2007-03-21 we would have
>> what I would believe to be the expected behavior. However at
>> 2007-03-22 we have a new high of 94.36 so i thought the new
>> Order.Price would be at 93.36 given a threshold =-1 but the order is
>> not adjusted. The order is however adjusted to the correct value on
>> 2007-03-26 after a new AAPL.High at 95.90 and Order Price is 94.899999
>>
>> I am very curious if the threshold works not only as the distance
>> between price but also the threshold of a new high price change in
>> order for the new Order.Price to be changed. I was expecting every new
>> AAPL.high to generate a new Order.Price for a stoptrailing order type.
>>
>> If someone could explain this to me or tell me the expected behavior
>> and mechanics of the trailing methodology I would very much appreciate
>> it.
>>
> For OHLC data, the sell stoplimit order price will be based on the
> high price, but the order evaluation is based on the close price.
> That means the stoplimit will only be moved if the close is >= order
> price + threshold.  And when it is moved, the new order price will be
> high - threshold.
>
> For BBO data, a sell stoplimit order price *and* evaluation would be
> based on the offer price.
>
> I don't recall at the moment why they're different.  Perhaps someone
> else will remember.
>
>> Thank you
>>
>> Derek
>>
>> _______________________________________________
>> 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.
>
>
>
> --
> Joshua Ulrich  |  about.me/joshuaulrich
> FOSS Trading  |  www.fosstrading.com
> R/Finance 2016 | www.rinfinance.com



More information about the R-SIG-Finance mailing list