From 04a4e25ca73821bf13b48ac54147f88c550d57ec Mon Sep 17 00:00:00 2001 From: Alexandre Catarino Date: Fri, 25 Sep 2026 13:52:37 +0100 Subject: [PATCH] Add Place Related Orders to contingent order types Co-Authored-By: Claude Opus 5.5 (1M context) --- .../03 Place Related Orders.html | 27 ++++++++ ...nitor Orders.php => 04 Monitor Orders.php} | 0 ...Update Orders.php => 05 Update Orders.php} | 0 ...Cancel Orders.php => 06 Cancel Orders.php} | 0 ...cktest Fills.php => 07 Backtest Fills.php} | 0 ...7 Buying Power.php => 08 Buying Power.php} | 0 ...e Support.php => 09 Brokerage Support.php} | 0 .../01 Bracket Orders/99 Examples.html | 67 +++++++++++++++++++ .../03 Place Related Orders.html | 14 ++++ ...nitor Orders.php => 04 Monitor Orders.php} | 0 ...Update Orders.php => 05 Update Orders.php} | 0 ...Cancel Orders.php => 06 Cancel Orders.php} | 0 ...cktest Fills.php => 07 Backtest Fills.php} | 0 ...7 Buying Power.php => 08 Buying Power.php} | 0 ...e Support.php => 09 Brokerage Support.php} | 0 .../02 Place Orders.html | 13 ---- .../03 Place Related Orders.html | 12 ++++ ...nitor Orders.php => 04 Monitor Orders.php} | 0 ...Update Orders.php => 05 Update Orders.php} | 0 ...Cancel Orders.php => 06 Cancel Orders.php} | 0 ...cktest Fills.php => 07 Backtest Fills.php} | 0 ...7 Buying Power.php => 08 Buying Power.php} | 0 ...e Support.php => 09 Brokerage Support.php} | 0 .../03 Place Related Orders.html | 20 ++++++ ...nitor Orders.php => 04 Monitor Orders.php} | 0 ...Update Orders.php => 05 Update Orders.php} | 0 ...Cancel Orders.php => 06 Cancel Orders.php} | 0 ...cktest Fills.php => 07 Backtest Fills.php} | 0 ...7 Buying Power.php => 08 Buying Power.php} | 0 ...e Support.php => 09 Brokerage Support.php} | 0 .../02 Relate Order Requests.html | 5 ++ 31 files changed, 145 insertions(+), 13 deletions(-) create mode 100644 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/03 Place Related Orders.html rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/{03 Monitor Orders.php => 04 Monitor Orders.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/{04 Update Orders.php => 05 Update Orders.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/{05 Cancel Orders.php => 06 Cancel Orders.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/{06 Backtest Fills.php => 07 Backtest Fills.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/{07 Buying Power.php => 08 Buying Power.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/{08 Brokerage Support.php => 09 Brokerage Support.php} (100%) create mode 100644 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/03 Place Related Orders.html rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/{03 Monitor Orders.php => 04 Monitor Orders.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/{04 Update Orders.php => 05 Update Orders.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/{05 Cancel Orders.php => 06 Cancel Orders.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/{06 Backtest Fills.php => 07 Backtest Fills.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/{07 Buying Power.php => 08 Buying Power.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/{08 Brokerage Support.php => 09 Brokerage Support.php} (100%) create mode 100644 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/03 Place Related Orders.html rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/{03 Monitor Orders.php => 04 Monitor Orders.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/{04 Update Orders.php => 05 Update Orders.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/{05 Cancel Orders.php => 06 Cancel Orders.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/{06 Backtest Fills.php => 07 Backtest Fills.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/{07 Buying Power.php => 08 Buying Power.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/{08 Brokerage Support.php => 09 Brokerage Support.php} (100%) create mode 100644 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/03 Place Related Orders.html rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/{03 Monitor Orders.php => 04 Monitor Orders.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/{04 Update Orders.php => 05 Update Orders.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/{05 Cancel Orders.php => 06 Cancel Orders.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/{06 Backtest Fills.php => 07 Backtest Fills.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/{07 Buying Power.php => 08 Buying Power.php} (100%) rename 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/{08 Brokerage Support.php => 09 Brokerage Support.php} (100%) diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/03 Place Related Orders.html b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/03 Place Related Orders.html new file mode 100644 index 0000000000..d118a70500 --- /dev/null +++ b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/03 Place Related Orders.html @@ -0,0 +1,27 @@ +

The exit orders that the BracketOrderbracket_order and Bracketbracket methods create can't trigger other orders. To make an exit trigger another order, build the bracket from its parts with the order factory. For more information, see Custom Contingent Orders.

+ +
    +
  1. Call the Triggerstriggers method on the exit order request with the order to trigger.
  2. +
  3. Relate the exit order requests with the OrderFactory.OneCancelsOtherorder_factory.one_cancels_other method.
  4. +
  5. Pass the exit order requests to the Triggerstriggers method of the entry order request.
  6. +
  7. Pass the entry order request to the Orderorder method.
  8. +
+ +
+
// The take profit triggers a new entry at the entry price.
+var takeProfit = OrderFactory.LimitOrder(_symbol, -100, price * 1.02m)
+    .Triggers(OrderFactory.LimitOrder(_symbol, 100, price * 0.999m));
+var stopLoss = OrderFactory.StopMarketOrder(_symbol, -100, price * 0.99m);
+var entry = OrderFactory.LimitOrder(_symbol, 100, price * 0.999m)
+    .Triggers(OrderFactory.OneCancelsOther(takeProfit, stopLoss));
+var tickets = Order(entry);
+
# The take profit triggers a new entry at the entry price.
+take_profit = self.order_factory.limit_order(self._symbol, -100, price * 1.02).triggers(
+    self.order_factory.limit_order(self._symbol, 100, price * 0.999))
+stop_loss = self.order_factory.stop_market_order(self._symbol, -100, price * 0.99)
+entry = self.order_factory.limit_order(self._symbol, 100, price * 0.999).triggers(
+    self.order_factory.one_cancels_other(take_profit, stop_loss))
+tickets = self.order(entry)
+
+ +

The order that the exit triggers is a grandchild of the entry. The brokerage holds it until the exit completely fills. If the other exit fills first, LEAN cancels the exit and its grandchild. The Orderorder method returns the order tickets of the entry, the take profit, the order it triggers, and the stop loss, in that order.

diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/03 Monitor Orders.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/04 Monitor Orders.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/03 Monitor Orders.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/04 Monitor Orders.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/04 Update Orders.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/05 Update Orders.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/04 Update Orders.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/05 Update Orders.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/05 Cancel Orders.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/06 Cancel Orders.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/05 Cancel Orders.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/06 Cancel Orders.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/06 Backtest Fills.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/07 Backtest Fills.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/06 Backtest Fills.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/07 Backtest Fills.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/07 Buying Power.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/08 Buying Power.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/07 Buying Power.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/08 Buying Power.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/08 Brokerage Support.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/09 Brokerage Support.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/08 Brokerage Support.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/09 Brokerage Support.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/99 Examples.html b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/99 Examples.html index 6f99d1b1df..d27d768822 100644 --- a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/99 Examples.html +++ b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/01 Bracket Orders/99 Examples.html @@ -49,3 +49,70 @@

Example 1: Bracket Orders on New Highs

quantity = self.calculate_order_quantity(self._spy, 0.5) self.bracket_order(self._spy, quantity, take_profit_price=round(bar.close * 1.02, 2), stop_loss_price=round(bar.close * 0.99, 2)) + +

Example 2: Re-Enter After the Take Profit

+

The following algorithm buys SPY with a bracket order when it has no position. The entry is 1% below the close price. The take profit is 2% above the close price, and the stop loss is 2% below it. When the take profit fills, it triggers a new entry at the entry price. When the new entry fills, the algorithm places new exit orders as one cancels other orders.

+
+
public class BracketReEntryExampleAlgorithm : QCAlgorithm
+{
+    private Symbol _spy;
+
+    public override void Initialize()
+    {
+        SetStartDate(2024, 1, 1);
+        SetEndDate(2024, 12, 31);
+        _spy = AddEquity("SPY", Resolution.Daily).Symbol;
+    }
+
+    public override void OnData(Slice slice)
+    {
+        if (Transactions.GetOpenOrders(_spy).Count > 0 || !slice.Bars.TryGetValue(_spy, out var bar))
+        {
+            return;
+        }
+        var entryPrice = Math.Round(bar.Close * 0.99m, 2);
+        var takeProfitPrice = Math.Round(bar.Close * 1.02m, 2);
+        var stopLossPrice = Math.Round(bar.Close * 0.98m, 2);
+        if (Portfolio[_spy].Invested)
+        {
+            // Exit the position of the new entry.
+            OneCancelsOtherOrder(new List<SubmitOrderRequest>
+            {
+                OrderFactory.LimitOrder(_spy, -100, takeProfitPrice),
+                OrderFactory.StopMarketOrder(_spy, -100, stopLossPrice)
+            });
+            return;
+        }
+        var takeProfit = OrderFactory.LimitOrder(_spy, -100, takeProfitPrice)
+            .Triggers(OrderFactory.LimitOrder(_spy, 100, entryPrice));
+        var stopLoss = OrderFactory.StopMarketOrder(_spy, -100, stopLossPrice);
+        Order(OrderFactory.LimitOrder(_spy, 100, entryPrice).Triggers(OrderFactory.OneCancelsOther(takeProfit, stopLoss)));
+    }
+}
+
class BracketReEntryExampleAlgorithm(QCAlgorithm):
+
+    def initialize(self) -> None:
+        self.set_start_date(2024, 1, 1)
+        self.set_end_date(2024, 12, 31)
+        self._spy = self.add_equity("SPY", Resolution.DAILY).symbol
+
+    def on_data(self, slice: Slice) -> None:
+        bar = slice.bars.get(self._spy)
+        if self.transactions.get_open_orders(self._spy) or not bar:
+            return
+        entry_price = round(bar.close * 0.99, 2)
+        take_profit_price = round(bar.close * 1.02, 2)
+        stop_loss_price = round(bar.close * 0.98, 2)
+        if self.portfolio[self._spy].invested:
+            # Exit the position of the new entry.
+            self.one_cancels_other_order([
+                self.order_factory.limit_order(self._spy, -100, take_profit_price),
+                self.order_factory.stop_market_order(self._spy, -100, stop_loss_price)
+            ])
+            return
+        take_profit = self.order_factory.limit_order(self._spy, -100, take_profit_price).triggers(
+            self.order_factory.limit_order(self._spy, 100, entry_price))
+        stop_loss = self.order_factory.stop_market_order(self._spy, -100, stop_loss_price)
+        self.order(self.order_factory.limit_order(self._spy, 100, entry_price).triggers(
+            self.order_factory.one_cancels_other(take_profit, stop_loss)))
+
diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/03 Place Related Orders.html b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/03 Place Related Orders.html new file mode 100644 index 0000000000..f0bfe37708 --- /dev/null +++ b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/03 Place Related Orders.html @@ -0,0 +1,14 @@ +

Each order in an OCO set can trigger other orders. To trigger orders after an OCO order fills, call the Triggerstriggers or Bracketbracket method on its order request. Call the method before you relate the request with the other orders. The brokerage holds the triggered orders until their parent completely fills. If another order in the set fills first, LEAN cancels the parent and the orders it triggers.

+ +
+
// Two breakout entries that cancel each other, each with its own exit orders.
+var breakoutUp = OrderFactory.StopMarketOrder(_symbol, 100, high).Bracket(high + range, low);
+var breakoutDown = OrderFactory.StopMarketOrder(_symbol, -100, low).Bracket(low - range, high);
+var tickets = OneCancelsOtherOrder(new List<SubmitOrderRequest> { breakoutUp, breakoutDown });
+
# Two breakout entries that cancel each other, each with its own exit orders.
+breakout_up = self.order_factory.stop_market_order(self._symbol, 100, high).bracket(high + range_, low)
+breakout_down = self.order_factory.stop_market_order(self._symbol, -100, low).bracket(low - range_, high)
+tickets = self.one_cancels_other_order([breakout_up, breakout_down])
+
+ +

The method returns the order ticket of each OCO order followed by the order tickets of the orders it triggers. For a complete example, see Custom Contingent Orders.

diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/03 Monitor Orders.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/04 Monitor Orders.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/03 Monitor Orders.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/04 Monitor Orders.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/04 Update Orders.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/05 Update Orders.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/04 Update Orders.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/05 Update Orders.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/05 Cancel Orders.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/06 Cancel Orders.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/05 Cancel Orders.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/06 Cancel Orders.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/06 Backtest Fills.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/07 Backtest Fills.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/06 Backtest Fills.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/07 Backtest Fills.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/07 Buying Power.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/08 Buying Power.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/07 Buying Power.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/08 Buying Power.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/08 Brokerage Support.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/09 Brokerage Support.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/08 Brokerage Support.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/02 One Cancels Other Orders/09 Brokerage Support.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/02 Place Orders.html b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/02 Place Orders.html index 986ffab6aa..47d2120992 100644 --- a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/02 Place Orders.html +++ b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/02 Place Orders.html @@ -16,19 +16,6 @@ ]) -

The child orders are independent of each other and can be for different securities. To relate the child orders, pass them through the OrderFactory.OneCancelsOtherorder_factory.one_cancels_other or OrderFactory.OneUpdatesOtherorder_factory.one_updates_other method first, as described in Custom Contingent Orders. A child order can trigger its own child orders, which are grandchild orders of the parent. To create a chain, call the Triggerstriggers method on the child order request.

- -
-
// The IBM order triggers the BAC order once it fills.
-var child = OrderFactory.MarketOrder(_ibm, 10).Triggers(OrderFactory.MarketOrder(_bac, 20));
-var tickets = OneTriggersOtherOrder(parent, new List<SubmitOrderRequest> { child });
-
# The IBM order triggers the BAC order once it fills.
-child = self.order_factory.market_order(self._ibm, 10).triggers(self.order_factory.market_order(self._bac, 20))
-tickets = self.one_triggers_other_order(parent, [child])
-
- -

The brokerage holds the grandchild order until the child order completely fills. The method returns the order tickets in the order parent, child, and grandchild, with each parent before the orders it triggers. If you cancel the parent, LEAN cancels the child and the grandchild.

-

The following images show the two outcomes of an OTO set. In the first image, the parent order fills and triggers the child order, which fills later:

An OTO set has a parent buy limit order at 45,000 and a child sell limit order at 58,000. The brokerage holds the child order until the price falls to 45,000 and the parent fills. The price then rises to 58,000 and the child order fills. diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/03 Place Related Orders.html b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/03 Place Related Orders.html new file mode 100644 index 0000000000..8786a58c22 --- /dev/null +++ b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/03 Place Related Orders.html @@ -0,0 +1,12 @@ +

The child orders are independent of each other and can be for different securities. To relate the child orders, pass them through the OrderFactory.OneCancelsOtherorder_factory.one_cancels_other or OrderFactory.OneUpdatesOtherorder_factory.one_updates_other method first, as described in Custom Contingent Orders. A child order can trigger its own child orders, which are grandchild orders of the parent. To create a chain, call the Triggerstriggers method on the child order request.

+ +
+
// The IBM order triggers the BAC order once it fills.
+var child = OrderFactory.MarketOrder(_ibm, 10).Triggers(OrderFactory.MarketOrder(_bac, 20));
+var tickets = OneTriggersOtherOrder(parent, new List<SubmitOrderRequest> { child });
+
# The IBM order triggers the BAC order once it fills.
+child = self.order_factory.market_order(self._ibm, 10).triggers(self.order_factory.market_order(self._bac, 20))
+tickets = self.one_triggers_other_order(parent, [child])
+
+ +

The brokerage holds the grandchild order until the child order completely fills. The method returns the order tickets in the order parent, child, and grandchild, with each parent before the orders it triggers. If you cancel the parent, LEAN cancels the child and the grandchild.

diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/03 Monitor Orders.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/04 Monitor Orders.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/03 Monitor Orders.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/04 Monitor Orders.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/04 Update Orders.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/05 Update Orders.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/04 Update Orders.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/05 Update Orders.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/05 Cancel Orders.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/06 Cancel Orders.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/05 Cancel Orders.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/06 Cancel Orders.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/06 Backtest Fills.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/07 Backtest Fills.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/06 Backtest Fills.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/07 Backtest Fills.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/07 Buying Power.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/08 Buying Power.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/07 Buying Power.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/08 Buying Power.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/08 Brokerage Support.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/09 Brokerage Support.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/08 Brokerage Support.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/03 One Triggers Other Orders/09 Brokerage Support.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/03 Place Related Orders.html b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/03 Place Related Orders.html new file mode 100644 index 0000000000..82edae9721 --- /dev/null +++ b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/03 Place Related Orders.html @@ -0,0 +1,20 @@ +

Each order in an OUO set can trigger other orders. To trigger orders after an OUO order fills, call the Triggerstriggers method on its order request before you relate it with the other orders. The brokerage holds the triggered orders until their parent completely fills. If another order in the set completely fills, LEAN cancels the parent and the orders it triggers. This rule applies even if the parent partially filled.

+ +
+
// Reverse the long position when the stop loss fills.
+var stopLoss = OrderFactory.StopMarketOrder(_symbol, -100, price * 0.99m).Triggers(OrderFactory.MarketOrder(_symbol, -100));
+var tickets = OneUpdatesOtherOrder(new List<SubmitOrderRequest>
+{
+    OrderFactory.LimitOrder(_symbol, -100, price * 1.02m),
+    stopLoss
+});
+
# Reverse the long position when the stop loss fills.
+stop_loss = self.order_factory.stop_market_order(self._symbol, -100, price * 0.99).triggers(
+    self.order_factory.market_order(self._symbol, -100))
+tickets = self.one_updates_other_order([
+    self.order_factory.limit_order(self._symbol, -100, price * 1.02),
+    stop_loss
+])
+
+ +

The quantity of a triggered order doesn't change when LEAN reduces the quantity of its parent.

diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/03 Monitor Orders.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/04 Monitor Orders.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/03 Monitor Orders.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/04 Monitor Orders.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/04 Update Orders.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/05 Update Orders.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/04 Update Orders.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/05 Update Orders.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/05 Cancel Orders.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/06 Cancel Orders.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/05 Cancel Orders.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/06 Cancel Orders.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/06 Backtest Fills.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/07 Backtest Fills.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/06 Backtest Fills.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/07 Backtest Fills.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/07 Buying Power.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/08 Buying Power.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/07 Buying Power.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/08 Buying Power.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/08 Brokerage Support.php b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/09 Brokerage Support.php similarity index 100% rename from 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/08 Brokerage Support.php rename to 03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/04 One Updates Other Orders/09 Brokerage Support.php diff --git a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/05 Custom Contingent Orders/02 Relate Order Requests.html b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/05 Custom Contingent Orders/02 Relate Order Requests.html index 1fd846d56d..3249e8c4e0 100644 --- a/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/05 Custom Contingent Orders/02 Relate Order Requests.html +++ b/03 Writing Algorithms/22 Trading and Orders/03 Order Types/13 Contingent Orders/05 Custom Contingent Orders/02 Relate Order Requests.html @@ -5,24 +5,29 @@ Method Description + Examples request.Triggers(children)request.triggers(children) The request triggers the child orders when it completely fills (OTO). + Place Related Orders request.Bracket(takeProfitPrice, stopLossPrice)request.bracket(take_profit_price, stop_loss_price) The request triggers a take-profit order and a stop-loss order that cancel each other. + Place Related Orders OrderFactory.OneCancelsOther(requests)order_factory.one_cancels_other(requests) The first fill of a request cancels the others (OCO). + Place Related Orders OrderFactory.OneUpdatesOther(requests)order_factory.one_updates_other(requests) A partial fill of a request reduces the quantity of the others (OUO). + Place Related Orders