---
title: "Laying the Foundation"
canonical: https://www.quantconnect.com/docs/v2/lean-engine/contributions/brokerages/laying-the-foundation
markdown: https://www.quantconnect.com/docs/v2/lean-engine/contributions/brokerages/laying-the-foundation.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/06%20LEAN%20Engine/02%20Contributions/02%20Brokerages/02%20Laying%20the%20Foundation
modified: 2026-11-10
index: https://www.quantconnect.com/llms.txt
---

# Laying the Foundation

[Documentation](https://www.quantconnect.com/docs/v2.md) > [LEAN Engine](https://www.quantconnect.com/docs/v2/lean-engine.md) > Contributions > [Brokerages](https://www.quantconnect.com/docs/v2/lean-engine/contributions/brokerages.md) > Laying the Foundation

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

| IBrokerageFactory |  |
| --- | --- |
| Primary Role | Create and initialize a brokerage instance. |
| Interface | [IBrokerageFactory.cs](https://github.com/QuantConnect/Lean/blob/master/Common/Interfaces/IBrokerageFactory.cs) |
| Example | [BitfinexBrokerageFactory.cs](https://github.com/QuantConnect/Lean.Brokerages.Bitfinex/blob/master/QuantConnect.BitfinexBrokerage/BitfinexBrokerageFactory.cs) |
| Target Location | `Lean.Brokerages.<brokerageName> / QuantConnect.<brokerageName>Brokerage /` |

## Introduction

The [IBrokerageFactory](https://github.com/QuantConnect/Lean/blob/master/Common/Interfaces/IBrokerageFactory.cs) creates brokerage instances and configures LEAN with a [Job Packet](https://github.com/QuantConnect/Lean/blob/master/Common/Packets/LiveNodePacket.cs#L37). To create the right `BrokerageFactory` type, LEAN uses the brokerage name in the job packet. To set the brokerage name, LEAN uses the `live-mode-brokerage` value in the [configuration file](https://github.com/QuantConnect/Lean/blob/master/Launcher/config.json).

## Prerequisites

You need to [set up your environment](https://www.quantconnect.com/docs/v2/lean-engine/contributions/brokerages/setting-up-your-environment.md) before you can lay the foundation for a new brokerage.

## Lay the Foundation

Follow these steps to stub out the implementation and initialize a brokerage instance:

1. In the `Lean / Launcher / config.json` file, add a few key-value pairs with your brokerage configuration information.
   For example, `oanda-access-token` and `oanda-account-id` keys. These key-value pairs will be used for most local debugging and testing as the default. LEAN automatically copies these pairs to the [BrokerageData](https://github.com/QuantConnect/Lean/blob/master/Common/Packets/LiveNodePacket.cs#L43) member of the job packet as a dictionary of `<string,string>` pairs.
2. In the `Lean.Brokerages.<brokerageName> / QuantConnect.<brokerageName>Brokerage / <brokerageName>Factory.cs` file, update the `BrokerageData` member so it uses the `Config` class to load all the required configuration settings from the `Lean / Launcher / config.json` file.
   For instance, `Config.Get("oanda-access-token")` returns the `"oanda-access-token"` value from the configuration file. For a full example, see the [BrokerageData member](https://github.com/QuantConnect/Lean.Brokerages.Bitfinex/blob/master/QuantConnect.BitfinexBrokerage/BitfinexBrokerageFactory.cs#L49) in the `BitfinexBrokerageFactory`.
   In the `IBrokerageFactory` examples, you'll see code like `Composer.Instance.AddPart<IDataQueueHandler>(dataQueueHandler)`, which adds parts to the **Composer**. The Composer is a system in LEAN for dynamically loading types. In this case, it's adding an instance of the `DataQueueHandler` for the brokerage to the composer. You can think of the Composer as a library and adding parts is like adding books to its collection.
3. In the `Lean / Common / Brokerages` folder, create a `<brokerageName>BrokerageModel.cs` file with a stub implementation that inherits from the [DefaultBrokerageModel](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/brokerages/supported-models/quantconnect-paper-trading.md).
   Brokerage models tell LEAN what order types a brokerage supports, whether we're allowed to update an order, and what [reality models](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/key-concepts.md) to use. Use the following stub implementation for now:
   where `BrokerageName` is the name of your brokerage. For example, if the brokerage name is XYZ, then `BrokerageNameBrokerageModel` should be `XYZBrokerageModel`. You'll extend this implementation later.
4. In the `Lean.Brokerages.<BrokerageName> / QuantConnect.<brokerageName>Brokerage / <brokerageName>BrokerageFactory.cs` file, define `GetBrokerageModel` to return an instance of your new brokerage model.
5. If your brokerage uses websockets to send data, in the `Lean.Brokerages.<brokerageName> / QuantConnect.<brokerageName> / <brokerageName>Brokerage.cs` file, replace the `Brokerage` base class for `BaseWebsocketsBrokerage`.
6. In the `Lean.Brokerages.<brokerageName> / QuantConnect.<brokerageName>Brokerage / <brokerageName>Brokerage.cs` file, update the constructor to save required authentication data to private variables.
7. In the `Lean.Brokerages.<brokerageName> / QuantConnect.<brokerageName>Brokerage / <brokerageName>BrokerageFactory.cs` file, define the `CreateBrokerage` method to create and return an instance of your new brokerage model without connecting to the brokerage.
   The Brokerage Factory uses a job packet to create an initialized brokerage instance in the `CreateBrokerage` method. Assume the `job` argument has the best source of data, not the `BrokerageData` property. The `BrokerageData` property in the factory are the starting default values from the configuration file, which can be overridden by a runtime job.
8. In the `Lean / Launcher / config.json` file, add a `live-<brokerageName>` key.
   These `live-<brokerageName>` keys group configuration flags together and override the root configuration values. Use the following key-value pair as a starting point:
   ```
   // defines the 'live-brokerage-name' environment
   "live-brokerage-name": {
     "live-mode": true,

     "live-mode-brokerage": "BrokerageName",

     "setup-handler": "QuantConnect.Lean.Engine.Setup.BrokerageSetupHandler",
     "result-handler": "QuantConnect.Lean.Engine.Results.LiveTradingResultHandler",
     "data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed",
     "data-queue-handler": [ "QuantConnect.Lean.Engine.DataFeeds.Queues.LiveDataQueue" ],
     "real-time-handler": "QuantConnect.Lean.Engine.RealTime.LiveTradingRealTimeHandler",
     "transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BacktestingTransactionHandler"
   },
   ```
   where `brokerage-name` and `"BrokerageName"` are placeholders for your brokerage name.
9. In the `Lean / Launcher / config.json` file, set the `environment` value to the your new brokerage environment.
   For example, `"live-brokerage-name"`.
10. Build the solution.
   Running the solution won't work, but the stub implementation should still build.
