---
title: "Key Concepts"
description: "Brokerages provide you with a connection to the market so you can fill trades. Brokerage models simulate the live behavior of a real brokerage."
canonical: https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/brokerages/key-concepts
markdown: https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/brokerages/key-concepts.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/03%20Writing%20Algorithms/24%20Reality%20Modeling/05%20Brokerages/01%20Key%20Concepts
modified: 2026-10-31
index: https://www.quantconnect.com/llms.txt
---

# Key Concepts

[Documentation](https://www.quantconnect.com/docs/v2.md) > [Writing Algorithms](https://www.quantconnect.com/docs/v2/writing-algorithms.md) > Reality Modeling > Brokerages > Key Concepts

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

## Introduction

Brokerages provide you with a connection to the market so you can fill trades. Brokerage models simulate the live behavior of a real brokerage. To avoid sending invalid orders for execution in live trading, the brokerage model validates your orders before LEAN sends them to the real brokerage. Brokerage models combine together all of the models relevant for a brokerage. If you set the appropriate brokerage model, the supported order types, default markets, and some of the [security-level models](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/key-concepts.md#02-Security-Level-Models) are appropriately set in your algorithm.

## Set Models

To set a brokerage model, in the `initialize` method, call the `set_brokerage_model` method with a `BrokerageName` and an `AccountType`. If you set a brokerage model, it overrides any security level models you manually set in your algorithm.

```python
def initialize(self) -> None:
	# Set the brokerage model to backtest with the most realistic scenario to handle the order validity, margin rate and transaction fee
	self.set_brokerage_model(BrokerageName.OANDA_BROKERAGE) # Defaults to margin account
	self.set_brokerage_model(BrokerageName.BITFINEX, AccountType.MARGIN) # Overrides the default account type, which is AccountType.CASH
```

In live trading, LEAN doesn't ignore your `set_brokerage_model` method calls. LEAN uses some of the brokerage model rules to catch invalid orders before they reach your real brokerage.

To view all the pre-built brokerage models, see [Supported Models](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/brokerages/supported-models.md).

## Default Behavior

The default brokerage model is the `DefaultBrokerageModel`, but LEAN has many other brokerage models you can use in your algorithms. For more information about the `DefaultBrokerageModel`, see [QuantConnect Paper Trading](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/brokerages/supported-models/quantconnect-paper-trading.md).

## Modeled Properties

Brokerage models model that following properties:

- Transaction fees
- Supported asset classes
- Account types
- Support for extended market hours
- Leverage for assets
- Trade settlement rules
- Order properties and updates

## Model Structure

Brokerage models should extend the `DefaultBrokerageModel` class. Extensions of the `DefaultBrokerageModel` class should implement the following methods:

```python
class CustomBrokerageModelExampleAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        # In the Initialize method, set the custom brokerage model
        self.set_brokerage_model(MyBrokerageModel())

# Define the custom brokerage model outside of the algorithm
class MyBrokerageModel(DefaultBrokerageModel):
    default_markets = {SecurityType.EQUITY: Market.USA}
    required_free_buying_power_percent = 0

    def __init__(self, accountType: account_type = account_type.margin):
        self.account_type = accountType

    def can_submit_order(self, security: Security, order: Order,
         message: BrokerageMessageEvent) -> bool:
        return super().can_submit_order(security, order, message)

    def can_update_order(self, security: Security, order: Order,
         request: UpdateOrderRequest, message: BrokerageMessageEvent) -> bool:
        return super().can_update_order(security, order, request, message)

    def can_execute_order(self, security: Security, order: Order) -> bool:
        return super().can_execute_order(security, order)

    def apply_split(self, tickets: List[OrderTicket], split: Split) -> None:
        super().apply_split(tickets, split)

    def get_leverage(self, security: Security) -> float:
        return super().get_leverage(security)

    def get_benchmark(self, securities: SecurityManager) -> IBenchmark:
        return super().get_benchmark(securities)

    def get_fill_model(self, security: Security) -> IFillModel:
        return super().get_fill_model(security)

    def get_fee_model(self, security: Security) -> IFeeModel:
        return super().get_fee_model(security)

    def get_slippage_model(self, security: Security) -> ISlippageModel:
        return super().get_slippage_model(security)

    def get_settlement_model(self, security: Security) -> ISettlementModel:
        return super().get_settlement_model(security)

    def get_buying_power_model(self, security: Security) -> IBuyingPowerModel:
        return super().get_buying_power_model(security)

    def get_margin_interest_rate_model(self, security: Security) -> IMarginInterestRateModel:
        return super().get_margin_interest_rate_model(security)

    def get_shortable_provider(self, security: Security) -> IShortableProvider:
        return super().get_shortable_provider(security)
```

Custom brokerage models give you enormous control over your algorithm behavior and allow you to model virtually any brokerage in the world.

## Live Trading Considerations

LEAN doesn't infer account type, leverage, or margin requirements from your brokerage account. For example, if you have a cash account, and your algorithm doesn't set `AccountType.``CASH`, LEAN will not automatically enforce cash account restrictions such as leverage of 1x or immediate settlement. You must manually configure your algorithm to adhere to these rules. If you set the leverage to 10x and you have a leverage of 2x, LEAN will place orders that can be rejected by your brokerage.

The default leverage of U.S. Equities is 2x. However, your brokerage can reduce the leverage of some securities based on market conditions. For example, leveraged ETFs such as TQQQ might have lower leverage during periods of high volatility. If your algorithm doesn't account for this, LEAN doesn't account for it and your orders can be rejected by your brokerage.

## Examples

The following examples demonstrate common practices for implementing a custom brokerage model.

### Example 1: Default Model Wrapper

The following algorithm defines a subclass of the `DefaultBrokerageModel` and overrides the built-in members so you can easily customize the parts you need.

```python
class CustomBrokerageModelAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 9, 1)
        self.set_end_date(2024, 12, 31)
        self.set_cash(100000)
        self.set_brokerage_model(MyBrokerageModel())
        self.add_equity('SPY', Resolution.DAILY)

    def on_data(self, data: Slice) -> None:
        if not self.portfolio.invested:
            self.set_holdings('SPY', 1)

class MyBrokerageModel(DefaultBrokerageModel):
    default_markets = {SecurityType.EQUITY: Market.USA}
    required_free_buying_power_percent = 0

    def __init__(self, account_type: AccountType = AccountType.MARGIN):
        self.account_type = account_type

    def can_submit_order(self, security: Security, order: Order,
         message: BrokerageMessageEvent) -> bool:
        return super().can_submit_order(security, order, message)

    def can_update_order(self, security: Security, order: Order,
         request: UpdateOrderRequest, message: BrokerageMessageEvent) -> bool:
        return super().can_update_order(security, order, request, message)

    def can_execute_order(self, security: Security, order: Order) -> bool:
        return super().can_execute_order(security, order)

    def apply_split(self, tickets: List[OrderTicket], split: Split) -> None:
        super().apply_split(tickets, split)

    def get_leverage(self, security: Security) -> float:
        return super().get_leverage(security)

    def get_benchmark(self, securities: SecurityManager) -> IBenchmark:
        return super().get_benchmark(securities)

    def get_fill_model(self, security: Security) -> IFillModel:
        return super().get_fill_model(security)

    def get_fee_model(self, security: Security) -> IFeeModel:
        return super().get_fee_model(security)

    def get_slippage_model(self, security: Security) -> ISlippageModel:
        return super().get_slippage_model(security)

    def get_settlement_model(self, security: Security) -> ISettlementModel:
        return super().get_settlement_model(security)

    def get_buying_power_model(self, security: Security) -> IBuyingPowerModel:
        return super().get_buying_power_model(security)

    def get_margin_interest_rate_model(self, security: Security) -> IMarginInterestRateModel:
        return super().get_margin_interest_rate_model(security)

    def get_shortable_provider(self, security: Security) -> IShortableProvider:
        return super().get_shortable_provider(security)
```

### Other Examples

For more examples, see the following algorithms:

**Demonstration Algorithms**

- [BrokerageModelAlgorithm.py](https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/BrokerageModelAlgorithm.py) (Python)
