---
title: "Trading and Orders"
description: "We send your live order requests to the API of your brokerage and wait for their response to update the state of your algorithm and portfolio."
canonical: https://www.quantconnect.com/docs/v2/writing-algorithms/live-trading/trading-and-orders
markdown: https://www.quantconnect.com/docs/v2/writing-algorithms/live-trading/trading-and-orders.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/03%20Writing%20Algorithms/40%20Live%20Trading/04%20Trading%20and%20Orders
modified: 2026-10-30
index: https://www.quantconnect.com/llms.txt
---

# Trading and Orders

[Documentation](https://www.quantconnect.com/docs/v2.md) > [Writing Algorithms](https://www.quantconnect.com/docs/v2/writing-algorithms.md) > [Live Trading](https://www.quantconnect.com/docs/v2/writing-algorithms/live-trading.md) > Trading and Orders

> Code examples are shown in Python. Append `?lang=csharp` to this URL for the C# version of this page.

## Introduction

LEAN has dozens of methods to create, update, and cancel orders. You can place orders automatically with [helper methods](https://www.quantconnect.com/docs/v2/writing-algorithms/trading-and-orders/position-sizing.md) or manually through methods on the algorithm API. You can fetch, update, and cancel manual orders with [order tickets](https://www.quantconnect.com/docs/v2/writing-algorithms/trading-and-orders/order-management/order-tickets.md). As the state of your orders change, LEAN creates [events](https://www.quantconnect.com/docs/v2/writing-algorithms/trading-and-orders/order-events.md) that notify your algorithm.

In backtesting, LEAN simulates order fills with historical data, but you can create your own fill, fee, slippage, and margin models via plugin points. You control how optimistic or pessimistic order fills are with transaction model classes. For more information about these types of models, see [Reality Modeling](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/key-concepts.md).

In live trading, orders fill asynchronously. We send your order to the API of your brokerage and wait for their response to update the state of your algorithm and portfolio. The timing of live order fills doesn't depend on the resolution of your security subscriptions. When your order fills, the fill price and fee is set by your brokerage. You can add event handlers to your algorithm to [monitor the brokerage connection](https://www.quantconnect.com/docs/v2/writing-algorithms/live-trading/brokerages.md#04-Monitor-the-Brokerage-Connection) and [brokerage messages](https://www.quantconnect.com/docs/v2/writing-algorithms/live-trading/brokerages.md#05-Monitor-Brokerage-Messages).

In backtesting, the trade fill timing depends on the resolution of your security subscription. For example, if you subscribe to a security with minute resolution data, the algorithm only receives data in one-minute [time slices](https://www.quantconnect.com/docs/v2/writing-algorithms/key-concepts/time-modeling/timeslices.md). As a result, the [fill model](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/trade-fills/key-concepts.md) can only evaluate if the order should fill on a minute-by-minute frequency.

## Order Life Cycle

When you call one of the order methods to place an order, LEAN performs [pre-order checks](https://www.quantconnect.com/docs/v2/writing-algorithms/trading-and-orders/pre-trade-risk-control.md) to ensure that the order meets some requirements and uses the last known price to check you have [enough capital](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/buying-power.md). To see the requirements of each order, see the **Requirements** section of [the documentation for the order types](https://www.quantconnect.com/docs/v2/writing-algorithms/trading-and-orders/key-concepts.md#03-Order-Types) and the **Orders** section of your [brokerage model](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/brokerages/supported-models.md). If you can place the order, LEAN creates `Order` and [OrderTicket](https://www.quantconnect.com/docs/v2/writing-algorithms/trading-and-orders/order-management/order-tickets.md) objects. The order ticket is sent to your brokerage. As the brokerage processes your order, it returns another order ticket and it's compared against the order to see if the order is satisfied. Orders fill asynchronously in live trading, so if you want to change an order, you must request it with the order ticket. Order changes are not guaranteed since your order may fill by the time brokerage receives the request.

![Flow of ordering and filling between algorithm and brokerage](https://cdn.quantconnect.com/i/tu/orders-and-tickets2.webp)

When you create an order ticket, LEAN validates it to avoid sending orders that will be rejected by your brokerage. If the order is invalid, it's status is set to `OrderStatus.INVALID`. Otherwise, LEAN sets the order status to `OrderStatus.NEW`, sends the order to your brokerage, and waits for the brokerage message. If your brokerage accepts the order, the order status becomes `OrderStatus.SUBMITTED`. Otherwise, it becomes `OrderStatus.CANCELED`.

You can update orders until they fill or the brokerage prevents modifications. If you place an order update request, LEAN validates it to avoid sending orders that will be rejected by you brokerage. If the order update is valid, LEAN sends the update request and waits for the brokerage message. If your brokerage accepts the update, the order status becomes `OrderStatus.UPDATE_SUBMITTED`. Otherwise, it becomes `OrderStatus.CANCELED`.

The brokerage notifies LEAN when you order fills (`OrderStatus.FILLED`), partially fills (`OrderStatus.PARTIALLY_FILLED`) or is canceled (`OrderStatus.CANCELED`). The brokerage can cancel your order before it completely fills. For example, if you place a [market on open](https://www.quantconnect.com/docs/v2/writing-algorithms/trading-and-orders/order-types/market-on-open-orders.md) and the asset price moves before the market open to where you can't afford the quantity of the order, the brokerage rejects your order. The brokerage can also cancel your market on open order after partial fills if there is no shares left to trade.

## Transaction Manager

LEAN doesn't save orders and order tickets across live deployments.

When you deploy your algorithm, LEAN fetches the open orders from your brokerage and populate the algorithm transactions manager (`SecurityTransactionManager`).

## Disable Buying Power

You can disable order margin checks and opt to let your brokerage decide to accept or reject the trades. This is helpful in live trading if you have a more permissive brokerage margin allowance that what LEAN models. The [default position group buying power models](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/buying-power.md#08-Default-Behavior) are helpful for Option trading strategies. However, it can be counterproductive if it's not a [supported Option strategy](https://www.quantconnect.com/docs/v2/writing-algorithms/trading-and-orders/option-strategies.md). To disable the validations of the default position group buying power model, use the `NullSecurityPositionGroupModel`. To set the `NullSecurityPositionGroupModel` for the portfolio, during [initialization](https://www.quantconnect.com/docs/v2/writing-algorithms/initialization.md), call the `set_positions` method with the `SecurityPositionGroupModel. Null NULL` argument.

```python
self.portfolio.set_positions(SecurityPositionGroupModel.NULL)
```

To disable the validations of the [default buying power model](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/buying-power.md#08-Default-Behavior), use the `NullBuyingPowerModel`. To set the `NullBuyingPowerModel` for a security subscription, call the `set_buying_power_model` method with the `BuyingPowerModel. Null NULL` argument.

```python
def initialize(self) -> None:
    equity = self.add_equity("SPY")
    # To disable any buying power of the selected security
    equity.set_buying_power_model(BuyingPowerModel.NULL)
    # Alias:
    # equity.set_margin_model(SecurityMarginModel.NULL)
```

You can also set the `NullBuyingPowerModel` in a [security initializer](https://www.quantconnect.com/docs/v2/writing-algorithms/initialization.md#07-Set-Security-Initializer). If your algorithm has a universe, use the security initializer technique. To set the buying power of securities in the security initializer, [set the brokerage model](https://www.quantconnect.com/docs/v2/writing-algorithms/initialization.md#05-Set-Brokerage-and-Cash-Model), [set the security initializer](https://www.quantconnect.com/docs/v2/writing-algorithms/initialization.md#07-Set-Security-Initializer), and then create security subscriptions.

```python
class AddSecurityInitializerExampleAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        # In the Initialize method, set the security initializer to set models of assets.
        self.add_security_initializer(self._custom_security_initializer)

    def _custom_security_initializer(self, security: Security) -> None:
        # Overwrite the security buying power
        security.set_buying_power_model(BuyingPowerModel.NULL)
```
