Skip to content

quantstrat - trailingStop offsets?

4 messages · Mark Knecht, Brian G. Peterson

#
Good morning,
   Base question: Are there any quantstrat examples showing how to set
a minimum profit target before a trailing stop kicks in?

   For example, for a risk stop of 5% and a trailing stop of 3%, I
don't want to the trailing stop to be executed until I have met a
minimum profit of at least 2% resulting in a maximum approximate loss
of 1% if the trailing stop executes.

   I don't see anything in addOrder to do that but I suspect there's
someway to handle it.

Thanks,
Mark
#
On Mon, Oct 13, 2014 at 7:29 AM, Mark Knecht <markknecht at gmail.com> wrote:
I think this is possibly intended to be done in the order sizing
function by setting the size to zero until the minimum gain is met.

<SNIP>
      orderqty <- osFUN(strategy = strategy, data = mktdata,
                        timestamp = timestamp, orderqty = orderqty,
ordertype = ordertype,
                        orderside = orderside, portfolio = portfolio,
                        symbol = symbol, ... = ..., ruletype = ruletype,
                        orderprice = as.numeric(orderprice))
    }
    if (!is.null(orderqty) && orderqty != 0 && length(orderprice)) {
      addOrder(portfolio = portfolio, symbol = symbol,
               timestamp = timestamp, qty = orderqty, price =
as.numeric(orderprice),
               ordertype = ordertype, side = orderside, orderset = orderset,
               threshold = threshold, status = "open", replace = replace,
               delay = delay, tmult = tmult, ... = ..., prefer = prefer,
               TxnFees = TxnFees, label = label, time.in.force = time.in.force)
    }
<SNIP>

If there's a more canned way to do it I'd appreciate understanding it
but for now I'll proceed in that direction.

Sorry for the noise,
Mark
#
On 10/13/2014 09:29 AM, Mark Knecht wrote:
If your risk stop is 5% and your trailing stop is 3%, there's no reason 
not to just enter the trailing stop at 3%, thus limiting your loss to 3% 
or less.  The risk stop would never be triggered.

Or am I missing what you're asking to do?

There are a number of ways to trigger orders in quantstrat.

Simple signals trigger a rule.  This is how your entry most likely works.

'chain' rule orders are triggered by a fill.  for example, your entry 
order is filled, and the chain rule for that entry will trigger. You can 
see examples of this in the luxor scripts where stop loss and take 
profit orders are entered after a position is initiated.

order sets create an OCO relationship between related orders.  in the 
example above, your stop and profit taking order would be an orderset. 
if one is filled, the other is canceled.

in the case of 'I only want to enter an order if my existing position 
has moved x% in my favor', I guess I'd ask 'why?' and 'how do you plan 
to do this in production?'.  I see very little difference in your 
example between 'enter my trailing stop at -3% after I've gone up 2%, 
but until then, use a 5% stop' and the much simpler 'enter a trailing 
stop at 3% when I get my entry fill'.

All that said, you could do what I think you say you want by triggering 
a signal for 2% above your entry signal.  The rule for that signal would 
cancel the 5% risk stop and add the 3% trailing stop.

Regards,

Brian
#
On Mon, Oct 13, 2014 at 7:52 AM, Brian G. Peterson <brian at braverock.com> wrote:
If the entry is 'efficient' to the extent that the entry price within
3% of the low
before the market starts moving in my direction then, yes, you are right. There
wouldn't be any advantage.

On the other hand, if the entry is not efficient and I'm in too early
then I want
to give it more breathing room (5%) before it starts moving. In this example
I define 'moving' as 2% in my favor.

Granted, position size for a fixed dollar risk is smaller.
No, I think you do understand based on the last idea at the end.
OK, Joshua pointed me at order sets in luxor.5.strategy.ordersets.R so
I'm looking there.
Maybe I'm driven too much by TradeStation history but many traders
there look for some sort of a 'break even' exit if the trade doesn't meet
a profit target. I.e - I'm targeting a 5% win. I'l accept a 5% loss until I get
to a 3% profit at which time I will expect to break even worst case.

(That's different than what I proposed at the outset where I would accept a
1% loss if I make it to 2% gain max before it falls back, but I am pretty sure
you see the idea.)
So I'll need to figure out what you mean by 'cancel' but the wording
seems like what I'm looking for.

General code idea?

If (MaxGain < 2%){
   enable.rule('luxor','chain','StopLoss',enabled=TRUE)
   enable.rule('luxor','chain','StopTrailing',enabled=FALSE)
}else{
  enable.rule('luxor','chain','StopLoss',enabled=FALSE)
   enable.rule('luxor','chain','StopTrailing',enabled=TRUE)
}

Does quantstrat allow the state of a rule be dynamic like that?
Much thanks!

Cheers,
Mark