Skip to content
All library documents

Modeling Bracket Orders as Linked One-Cancels-the-Other Orders

Article Quant Q&A · Author: Femto Trader

Summary

The document asks how to represent bracket orders in a Python backtester. A bracket consists of an entry order linked to child exit orders, typically a stop loss and a take profit, with one exit canceling the other after execution.

The response recommends modeling the exits as a grouped one-cancels-the-other order set attached to the entry. It points to an existing implementation in the R Quantstrat and Blotter ecosystem as a reference, suggesting that its order-handling logic and supporting infrastructure could be adapted for Python. This is implementation guidance rather than a complete specification: the discussion does not detail order activation timing, partial fills, gaps, or execution priority, all of which can affect backtest results. It also offers an opinion about framework support without presenting a systematic comparison.

Key ideas

  • A bracket order links an entry with stop-loss and take-profit child orders.
  • The two exit orders can be modeled as a one-cancels-the-other group.
  • An existing R backtesting implementation is suggested as a reference for a Python port.
  • Order activation, fills, gaps, and execution priority need further specification for realistic backtests.

Tags

Full text
# How to model bracket orders?


# How to model bracket orders?












I'm looking for a way to make my own Python backtester (like zipline or pyalgotrade) or improve one of these backtesters.

One major lack of these backtesters is the lack of support of bracket orders ("child" stoploss and takeprofit orders linked to a "parent" order)

I wonder how I should model bracket orders ?

## Answer by Kyle Balkissoon (score 3)

https://quant.stackexchange.com/a/16077

You will need an entry and then a "Grouped" stop loss and take profit (one cancels other). An implementation of this exists in quantstrat in R called ordersets.

Documentation and source code can be found here:

https://r-forge.r-project.org/scm/viewvc.php/pkg/quantstrat/R/orders.R?view=markup&root=blotter

You will unfortunately need to port this and other infrastructure over to python.

In my opinion the Blotter/Quantstrat backtesting framework has the most support for various order types it also reconciles with the brokerage statements of one of the contributors.

Here is an example of a Q/A solving this in S/O. https://stackoverflow.com/questions/10445936/r-quantstrat-orders-cancel-each-other

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.