Bracket order with a Trailing Stop
-
Any input to how I smartest set a bracket order plus a trailing stop such that the 1) stop and the 2) limit in the bracket have a OCO (aka One Cancel Others) relationship with 3) the trailing stop?
if not self.position and self.myindicator > self.params.p: self.buy_bracket(limitprice=self.data.close[0]*self.params.lPerc, price=None, stopprice=self.data.close[0]*(1-self.params.sPerc)) self.tstop = self.sell(exectype=bt.Order.StopTrail, trailpercent=self.params.tPerc)
In other word
tstop
gets canceled immediately in the same next() loop if the stop or the limit order in the bracket is executed, cancelled or expires - AND visa versa. -
@søren-pallesen You can use trailing stops in
buy_bracket
with the latest release, but note that there might be a bug. See my posts regarding the bug.You would write something like this:
self.buy_bracket(...,stopexec=bt.Order.StopTrail,trailamount=trailamount,...)
-
@cheez can i have both a stop and a trailing stop in the same bracket order?
-
@søren-pallesen Ah, no I don't think that's possible.
-
Anyone? ...maybe @backtrader? thx in advance ...
-
What is the reason to have 2 stops at the same time? Only one will work anyway.
-
@ab_trader There are three live orders on the position: One is a stop (to limit loss) and the other is a trailing stop (to take profit) and the last is a limit (to take profit).
They all trigger on different conditions.
-
From two stops only one will work, the one which is closer to the current price. IMO it is useless to have two stops for single position.
-
A trailing stop and a (normal) stop works completly different. For example on position bought at $10 a stop at say $9 would protect against a loss. A trailing stop of 20% would ensure that you sold if the price drops 20% at any time of the life span on the position. So if the price increased to $20 and drops to $16 it would be sold with a profit of $6.
A normal stop is static protecting against a loss and a trailingstop dynamic and typically primarily used to lock in a profit while keeping the upside of further increases.
-
This worked fo me (it seems - still have more testing to do) in case anyone is interested ... :-)
mainside = self.buy(transmit=False) lowside = self.sell(price=self.data.close[0]*(1-self.params.sPerc), size=mainside.size, exectype=bt.Order.Stop, transmit=False, parent=mainside) highside = self.sell(price=self.data.close[0]*self.params.lPerc, size=mainside.size, exectype=bt.Order.Limit, transmit=False, parent=mainside) trailside = self.sell(size=mainside.size, exectype=bt.Order.StopTrail, trailpercent=self.params.tPerc, transmit=True, parent=mainside)
Incendently anyone know the differences in doing this with 'OCO' vs the 'Parent' property?
-
OCO orders are all active at the same time.
Bracket orders (the ones you submit with
parent
) have a different lifecycle:- parent is 1st alive. children are inactive
- If the parent is cancelled the children are simply discarded (they weren't active)
- If the parent is executed, the children are activated
- Both children behave now like OCO orders