Brokerages
Laying the Foundation
IBrokerageFactory | |
---|---|
Primary Role | Create and initialize a brokerage instance. |
Interface | IBrokerageFactory.cs |
Example | BitfinexBrokerageFactory.cs |
Target Location | Lean.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:
- In the Lean / Launcher / config.json file, add a few key-value pairs with your brokerage configuration information.
- In the Lean.Brokerages.<brokerageName> / QuantConnect.<brokerageName>Brokerage / <brokerageName>Factory.cs file, update the
BrokerageData
member so it uses theConfig
class to load all the required configuration settings from the Lean / Launcher / config.json file. - In the Lean / Common / Brokerages folder, create a <brokerageName>BrokerageModel.cs file with a stub implementation that inherits from the DefaultBrokerageModel.
- In the Lean.Brokerages.<BrokerageName> / QuantConnect.<brokerageName>Brokerage / <brokerageName>BrokerageFactory.cs file, define
GetBrokerageModel
to return an instance of your new brokerage model. - If your brokerage uses websockets to send data, in the Lean.Brokerages.<brokerageName> / QuantConnect.<brokerageName> / <brokerageName>Brokerage.cs file, replace the
Brokerage
base class forBaseWebsocketsBrokerage
. - In the Lean.Brokerages.<brokerageName> / QuantConnect.<brokerageName>Brokerage / <brokerageName>Brokerage.cs file, update the constructor to save required authentication data to private variables.
- 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. - In the Lean / Launcher / config.json file, add a
live-<brokerageName>
key. - In the Lean / Launcher / config.json file, set the
environment
value to the your new brokerage environment. - Build the solution.
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.
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.
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.
public override IBrokerageModel GetBrokerageModel(IOrderProvider orderProvider) { return new BrokerageNameBrokerageModel(); }
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.
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.
For example, "live-brokerage-name"
.
Running the solution won't work, but the stub implementation should still build.