quantstrat - trailingStop offsets?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

quantstrat - trailingStop offsets?

Mark Knecht
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

_______________________________________________
[hidden email] 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.
markknecht@gmail.com
Reply | Threaded
Open this post in threaded view
|

Re: quantstrat - trailingStop offsets?

Mark Knecht
On Mon, Oct 13, 2014 at 7:29 AM, Mark Knecht <[hidden email]> wrote:

> 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

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

_______________________________________________
[hidden email] 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.
markknecht@gmail.com
Reply | Threaded
Open this post in threaded view
|

Re: quantstrat - trailingStop offsets?

braverock
In reply to this post by Mark Knecht
On 10/13/2014 09:29 AM, Mark Knecht wrote:
> 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.

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

--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: quantstrat - trailingStop offsets?

Mark Knecht
On Mon, Oct 13, 2014 at 7:52 AM, Brian G. Peterson <[hidden email]> wrote:

> On 10/13/2014 09:29 AM, Mark Knecht wrote:
>>
>> 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.
>
>
> 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.
>

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.


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

No, I think you do understand based on the last idea at the end.

> 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.
>

OK, Joshua pointed me at order sets in luxor.5.strategy.ordersets.R so
I'm looking there.

> 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'.
>

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.)

> 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.
>

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?

> Regards,
>
> Brian
>

Much thanks!

Cheers,
Mark

_______________________________________________
[hidden email] 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.
markknecht@gmail.com