Stop Loss and Take Profit realization with One Cancels the other Order (OCO) #1700
-
I would like to implement a strategy on futures with fixed stop loss and take profit orders. In a trading software, these functions are usually combined by OCO, such that, SL is reached the TP order is canceled, and vice versa. I have found OCO in the documentation at In my code, I tried to use the order list: but how can I tell the engine to combine them as OCO? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @Felix99 Thanks for reaching out! This certainly needs more documentation, which we're working to improve. I suggest you use the You can access it from a strategy with |
Beta Was this translation helpful? Give feedback.
-
Hi @Felix99 , Insert this method in your strategy and just update entry / profit / stop prices - everything else should stay the same. def fire_bracket_order(self):
if self.portfolio.is_flat(self.config.instrument.id):
entry_price = self.last_price
profit_price = entry_price + (self.config.instrument.price_increment * 20)
stop_price = entry_price - (self.config.instrument.price_increment * 20)
bracket_order = self.order_factory.bracket(
instrument_id=self.config.instrument.id,
order_side=OrderSide.BUY,
quantity=self.config.instrument.make_qty(1),
entry_price=self.config.instrument.make_price(entry_price),
entry_order_type=OrderType.LIMIT,
tp_trigger_price=self.config.instrument.make_price(entry_price),
tp_price=self.config.instrument.make_price(profit_price),
tp_order_type=OrderType.LIMIT,
sl_trigger_price=self.config.instrument.make_price(stop_price), # There is only a sl_trigger_price because only StopMarket orders are supported as the SL
time_in_force=TimeInForce.GTC,
tp_post_only=False,
)
self.submit_order_list(bracket_order)
self.log.info(f"Submitted bracket order: entry @ market, TP @ {profit_price}, SL @ {stop_price}") This code is tested and working. |
Beta Was this translation helpful? Give feedback.
Hi @Felix99
Thanks for reaching out!
This certainly needs more documentation, which we're working to improve.
I suggest you use the
OrderFactory
which will construct an entry with bracketing SL and TP orders:https://github.com/nautechsystems/nautilus_trader/blob/develop/nautilus_trader/common/factories.pyx#L1141
You can access it from a strategy with
self.order_factory.bracket(...)
.