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.
Triggerstriggers method on the exit order request with the order to trigger.OrderFactory.OneCancelsOtherorder_factory.one_cancels_other method.Triggerstriggers method of the entry order request.Orderorder method.// 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.
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)))
+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:
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 @@request.Triggers(children)request.triggers(children)request.Bracket(takeProfitPrice, stopLossPrice)request.bracket(take_profit_price, stop_loss_price)OrderFactory.OneCancelsOther(requests)order_factory.one_cancels_other(requests)OrderFactory.OneUpdatesOther(requests)order_factory.one_updates_other(requests)