Contributing Brokerages

Laying the Foundation

IBrokerageFactory
Primary RoleCreate and initialize a brokerage instance.
InterfaceIBrokerageFactory.cs
ExampleBitfinexBrokerageFactory.cs
Target LocationLean.Brokerages.<brokerageName> / QuantConnect.<brokerageName>Brokerage /

Introduction

The IBrokerageFactory creates brokerage instances and configures LEAN with a Job Packet. 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.

Prerequisites

You need to set up your environment 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.
  2. 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 member of the job packet as a dictionary of <string,string> pairs.

  3. 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.
  4. 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 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.

  5. In the Lean / Common / Brokerages folder, create a <brokerageName>.cs file with a stub implementation that inherits from the DefaultBrokerageModel.
  6. Brokerage models tell LEAN what order types a brokerage supports, whether we're allowed to update an order, and what reality models to use. Use the following stub implementation for now:

    namespace QuantConnect.Brokerages
    {
        public class BrokerageNameBrokerageModel : DefaultBrokerageModel
        {
            
        }
    }

    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.

  7. In the Lean.Brokerages.<BrokerageName> / QuantConnect.<brokerageName>Brokerage / <brokerageName>BrokerageFactory.cs file, define GetBrokerageModel to return an instance of your new brokerage model.
  8. public override IBrokerageModel GetBrokerageModel(IOrderProvider orderProvider)
    {
        return new BrokerageNameBrokerageModel();
    }
  9. 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.
  10. In the Lean.Brokerages.<brokerageName> / QuantConnect.<brokerageName>Brokerage / <brokerageName>Brokerage.cs file, update the constructor to save required authentication data to private variables.
  11. 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.
  12. 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.

  13. In the Lean / Launcher / config.json file, add a live-<brokerageName> key.
  14. 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.

  15. In the Lean / Launcher / config.json file, set the environment value to the your new brokerage environment.
  16. For example, "live-brokerage-name".

  17. Build the solution.
  18. Running the solution won't work, but the stub implementation should still build.

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: