Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>The order factory creates order requests without submitting them. Use order requests to estimate fees and check buying power before you place orders, to submit several orders together, and to build <a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/contingent-orders'>contingent orders</a>. When you submit an order request, you get an <a href='/docs/v2/writing-algorithms/trading-and-orders/order-management/order-tickets'>order ticket</a> to manage the order.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<p>To create an order request, call one of the methods of the <code class="csharp">OrderFactory</code><code class="python">order_factory</code> member. Each method returns a <code>SubmitOrderRequest</code> object.</p>

<div class="section-example-container">
<pre class="csharp">var request = OrderFactory.LimitOrder(_symbol, 100, limitPrice, tag: "Entry");</pre>
<pre class="python">request = self.order_factory.limit_order(self._symbol, 100, limit_price, tag="Entry")</pre>
</div>

<p>The order factory has a method for each order type. The following table shows the required arguments of each method:</p>

<table class="table qc-table">
<thead>
<tr>
<th>Method</th>
<th>Order Type</th>
</tr>
</thead>
<tbody>
<tr>
<td><code class="csharp">MarketOrder(symbol, quantity)</code><code class="python">market_order(symbol, quantity)</code></td>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/market-orders'>Market</a></td>
</tr>
<tr>
<td><code class="csharp">LimitOrder(symbol, quantity, limitPrice)</code><code class="python">limit_order(symbol, quantity, limit_price)</code></td>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/limit-orders'>Limit</a></td>
</tr>
<tr>
<td><code class="csharp">LimitIfTouchedOrder(symbol, quantity, triggerPrice, limitPrice)</code><code class="python">limit_if_touched_order(symbol, quantity, trigger_price, limit_price)</code></td>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/limit-if-touched-orders'>Limit if touched</a></td>
</tr>
<tr>
<td><code class="csharp">StopMarketOrder(symbol, quantity, stopPrice)</code><code class="python">stop_market_order(symbol, quantity, stop_price)</code></td>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/stop-market-orders'>Stop market</a></td>
</tr>
<tr>
<td><code class="csharp">StopLimitOrder(symbol, quantity, stopPrice, limitPrice)</code><code class="python">stop_limit_order(symbol, quantity, stop_price, limit_price)</code></td>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/stop-limit-orders'>Stop limit</a></td>
</tr>
<tr>
<td><code class="csharp">TrailingStopOrder(symbol, quantity, trailingAmount, trailingAsPercentage)</code><code class="python">trailing_stop_order(symbol, quantity, trailing_amount, trailing_as_percentage)</code></td>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/trailing-stop-orders'>Trailing stop</a></td>
</tr>
<tr>
<td><code class="csharp">TrailingStopOrder(symbol, quantity, stopPrice, trailingAmount, trailingAsPercentage)</code><code class="python">trailing_stop_order(symbol, quantity, stop_price, trailing_amount, trailing_as_percentage)</code></td>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/trailing-stop-orders'>Trailing stop with an initial stop price</a></td>
</tr>
<tr>
<td><code class="csharp">MarketOnOpenOrder(symbol, quantity)</code><code class="python">market_on_open_order(symbol, quantity)</code></td>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/market-on-open-orders'>Market on open</a></td>
</tr>
<tr>
<td><code class="csharp">MarketOnCloseOrder(symbol, quantity)</code><code class="python">market_on_close_order(symbol, quantity)</code></td>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/market-on-close-orders'>Market on close</a></td>
</tr>
<tr>
<td><code class="csharp">ComboMarketOrder(legs, quantity)</code><code class="python">combo_market_order(legs, quantity)</code></td>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/combo-market-orders'>Combo market</a></td>
</tr>
<tr>
<td><code class="csharp">ComboLimitOrder(legs, quantity, limitPrice)</code><code class="python">combo_limit_order(legs, quantity, limit_price)</code></td>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/combo-limit-orders'>Combo limit</a></td>
</tr>
<tr>
<td><code class="csharp">ComboLegLimitOrder(legs, quantity)</code><code class="python">combo_leg_limit_order(legs, quantity)</code></td>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/combo-leg-limit-orders'>Combo leg limit</a></td>
</tr>
<tr>
<td><code class="csharp">OptionStrategyOrder(strategy, quantity)</code><code class="python">option_strategy_order(strategy, quantity)</code></td>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/option-strategies'>Option strategy</a></td>
</tr>
<tr>
<td><code class="csharp">ExerciseOption(optionSymbol, quantity)</code><code class="python">exercise_option(option_symbol, quantity)</code></td>
<td><a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/option-exercise-orders'>Option exercise</a></td>
</tr>
</tbody>
</table>

<p>Every method also accepts the optional <code>asynchronous</code>, <code>tag</code>, and <code class="csharp">orderProperties</code><code class="python">order_properties</code> arguments.</p>

<p>The combo and Option strategy methods return a list with one order request for each leg. The order request stores the current time and a copy of the <a href='/docs/v2/writing-algorithms/trading-and-orders/order-properties'>default order properties</a>, unless you pass order properties. If the ticker of the security changed, the order request uses the current <code>Symbol</code>.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<p>To submit order requests, pass a single request or a list of requests to the <code class="csharp">Order</code><code class="python">order</code> method. The method returns a list with the order ticket of each order. For a combo order, include all the legs.</p>

<div class="section-example-container">
<pre class="csharp">var tickets = Order(new List&lt;SubmitOrderRequest&gt;
{
OrderFactory.MarketOrder(_spy, 10),
OrderFactory.LimitOrder(_aapl, -20, aaplLimitPrice)
});</pre>
<pre class="python">tickets = self.order([
self.order_factory.market_order(self._spy, 10),
self.order_factory.limit_order(self._aapl, -20, aapl_limit_price)
])</pre>
</div>

<p>LEAN runs the <a href='/docs/v2/writing-algorithms/trading-and-orders/order-errors'>pre-order checks</a> on every request before it submits any of them. If a request fails the checks, LEAN submits none of the orders and the method returns a list with a single invalid order ticket. LEAN checks the <a href='/docs/v2/writing-algorithms/reality-modeling/buying-power'>buying power</a> of each order after you submit it.</p>

<p>LEAN applies the usual order adjustments when you submit the request. For example, LEAN converts a market order into a market on open order if the market is closed. LEAN also sets the initial stop price of a trailing stop order from the market price at that time.</p>

<p>You can submit each order request once. After you submit a request, its <code class="csharp">OrderId</code><code class="python">order_id</code> property holds the order Id. To get the order ticket of a request, <a href='/docs/v2/writing-algorithms/trading-and-orders/order-management/transaction-manager#02-Get-a-Single-Order-Ticket'>get it from the transaction manager</a>.</p>

<div class="section-example-container">
<pre class="csharp">var ticket = Transactions.GetOrderTicket(request.OrderId);</pre>
<pre class="python">ticket = self.transactions.get_order_ticket(request.order_id)</pre>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?
$linkOrderFactory = false;
include(DOCS_RESOURCES."/trading-and-orders/estimate-order-fees.php");
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?
$linkOrderFactory = false;
include(DOCS_RESOURCES."/trading-and-orders/check-buying-power.php");
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<p>The following examples demonstrate common practices for using the order factory.</p>

<h4>Example 1: Skip Expensive Rebalance Orders</h4>
<p>The following algorithm rebalances an equal-weight portfolio every month. For each security, it creates a market order request for the difference between the target and current quantities and estimates the fee. The algorithm drops the orders whose fee exceeds 0.1% of the order value and submits the rest together, with the sell orders first.</p>
<div class="section-example-container">
<pre class="csharp">public class OrderFactoryExampleAlgorithm : QCAlgorithm
{
private List&lt;Symbol&gt; _symbols;

public override void Initialize()
{
SetStartDate(2024, 1, 1);
SetEndDate(2024, 12, 31);
SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin);
_symbols = new[] { "SPY", "TLT", "GLD" }.Select(ticker =&gt; AddEquity(ticker, Resolution.Daily).Symbol).ToList();
Schedule.On(DateRules.MonthStart(_symbols[0]), TimeRules.AfterMarketOpen(_symbols[0], 30), Rebalance);
}

private void Rebalance()
{
var requests = new List&lt;SubmitOrderRequest&gt;();
foreach (var symbol in _symbols)
{
var quantity = CalculateOrderQuantity(symbol, 1m / _symbols.Count);
if (quantity == 0) continue;
var security = Securities[symbol];
var request = OrderFactory.MarketOrder(symbol, quantity);
// Estimate the fee without submitting the order.
var fee = security.FeeModel.GetOrderFee(new OrderFeeParameters(security, QuantConnect.Orders.Order.CreateOrder(request))).Value.Amount;
if (fee &lt;= 0.001m * Math.Abs(quantity) * security.Price)
{
requests.Add(request);
}
}
// Place the sell orders first to free up buying power.
Order(requests.OrderBy(request =&gt; request.Quantity));
}
}</pre>
<pre class="python">class OrderFactoryExampleAlgorithm(QCAlgorithm):

def initialize(self) -&gt; None:
self.set_start_date(2024, 1, 1)
self.set_end_date(2024, 12, 31)
self.set_brokerage_model(BrokerageName.INTERACTIVE_BROKERS_BROKERAGE, AccountType.MARGIN)
self._symbols = [self.add_equity(ticker, Resolution.DAILY).symbol for ticker in ["SPY", "TLT", "GLD"]]
self.schedule.on(self.date_rules.month_start(self._symbols[0]), self.time_rules.after_market_open(self._symbols[0], 30), self._rebalance)

def _rebalance(self) -&gt; None:
requests = []
for symbol in self._symbols:
quantity = self.calculate_order_quantity(symbol, 1 / len(self._symbols))
if not quantity:
continue
security = self.securities[symbol]
request = self.order_factory.market_order(symbol, quantity)
# Estimate the fee without submitting the order.
fee = security.fee_model.get_order_fee(OrderFeeParameters(security, Order.create_order(request))).value.amount
if fee &lt;= 0.001 * abs(quantity) * security.price:
requests.append(request)
# Place the sell orders first to free up buying power.
self.order(sorted(requests, key=lambda request: request.quantity))</pre>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "metadata",
"values": {
"description": "The order factory creates order requests without submitting them, so you can estimate their fees, submit them together, or build contingent orders.",
"keywords": "order factory, order requests, SubmitOrderRequest, submit orders together, estimate order fees, contingent orders",
"og:description": "The order factory creates order requests without submitting them, so you can estimate their fees, submit them together, or build contingent orders.",
"og:title": "Order Factory - Documentation QuantConnect.com",
"og:type": "website",
"og:site_name": "Order Factory - QuantConnect.com",
"og:image": "https://cdn.quantconnect.com/docs/i/writing-algorithms/trading-and-orders/order-management/order-factory.png"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<div class="sub-heading-content">
<div class="sub-heading-landing">
<div class="internal-content">
<p>Contingent orders are sets of orders with related lifecycles. When an order in the set fills or you cancel it, the brokerage triggers, cancels, or resizes the other orders in the set. The brokerage manages the whole set, so a fill and a cancel request can't race each other and leave a position without its exit orders. LEAN supports the following contingency types:</p>

<ul>
<li>One Cancels Other (OCO) orders cancel the rest of the set when one of them fills, even partially.</li>
<li>One Triggers Other (OTO) orders hold the child orders until the parent order completely fills.</li>
<li>One Updates Other (OUO) orders reduce the remaining quantity of the rest of the set when one of them partially fills.</li>
</ul>

<p>You can combine these types. For example, a bracket order is an entry order that triggers a take-profit order and a stop-loss order, which cancel each other. The members of a set can be <a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/combo-market-orders'>combo orders</a>.</p>
</div>
<div class="docs-landing-links">
<div class="col-md-6 col-sm-12 docs-landing-link-wrap">
<div class="content clickable" onclick="window.location.href = '/docs/v2/writing-algorithms/trading-and-orders/order-types/contingent-orders/bracket-orders'">
<div class="docs-internal-link">
<h4>
Bracket Orders
</h4>
</div>
<code class='csharp'>BracketOrder</code><code class='python'>self.bracket_order</code>
</div>
</div>
<div class="col-md-6 col-sm-12 docs-landing-link-wrap">
<div class="content clickable" onclick="window.location.href = '/docs/v2/writing-algorithms/trading-and-orders/order-types/contingent-orders/one-cancels-other-orders'">
<div class="docs-internal-link">
<h4>
One Cancels Other Orders
</h4>
</div>
<code class='csharp'>OneCancelsOtherOrder</code><code class='python'>self.one_cancels_other_order</code>
</div>
</div>
<div class="col-md-6 col-sm-12 docs-landing-link-wrap">
<div class="content clickable" onclick="window.location.href = '/docs/v2/writing-algorithms/trading-and-orders/order-types/contingent-orders/one-triggers-other-orders'">
<div class="docs-internal-link">
<h4>
One Triggers Other Orders
</h4>
</div>
<code class='csharp'>OneTriggersOtherOrder</code><code class='python'>self.one_triggers_other_order</code>
</div>
</div>
<div class="col-md-6 col-sm-12 docs-landing-link-wrap">
<div class="content clickable" onclick="window.location.href = '/docs/v2/writing-algorithms/trading-and-orders/order-types/contingent-orders/one-updates-other-orders'">
<div class="docs-internal-link">
<h4>
One Updates Other Orders
</h4>
</div>
<code class='csharp'>OneUpdatesOtherOrder</code><code class='python'>self.one_updates_other_order</code>
</div>
</div>
<div class="col-md-6 col-sm-12 docs-landing-link-wrap">
<div class="content clickable" onclick="window.location.href = '/docs/v2/writing-algorithms/trading-and-orders/order-types/contingent-orders/custom-contingent-orders'">
<div class="docs-internal-link">
<h4>
Custom Contingent Orders
</h4>
</div>
<code class='csharp'>OrderFactory</code><code class='python'>self.order_factory</code>
</div>
</div>
</div>
<div class="see-also">
<h3>See Also</h3>
<a href="/docs/v2/writing-algorithms/trading-and-orders/order-management/order-factory">Order Factory</a><br>
<a href="/docs/v2/writing-algorithms/trading-and-orders/order-types/combo-market-orders">Combo Market Orders</a><br>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>A bracket order is an entry order that triggers a take-profit order and a stop-loss order when it completely fills. The take-profit order and the stop-loss order are <a href='/docs/v2/writing-algorithms/trading-and-orders/order-types/contingent-orders/one-cancels-other-orders'>one cancels other</a> orders, so when one of them fills, LEAN cancels the other one. Bracket orders set an upper and a lower bound on the exit price of a trade.</p>
Loading