Live Trading

Brokerages

Introduction

Brokerages provide you with a connection to the market so you can fill trades. To avoid placing invalid orders for execution in live trading, LEAN validates your orders before sending them to the real brokerage. To view all of the integrated brokerages, see Brokerages.

Portfolio

In live trading, LEAN populates the Portfolio object with your account holdings and the Transactions object with your open positions. If you don't manually subscribe to the assets in your account, LEAN subscribes to them with the lowest resolution of the subscriptions in your algorithm. For example, say you hold AAPL shares in your account and create the following subscriptions in your algorithm:

AddEquity("SPY", Resolution.Hour);
AddEquity("MSFT", Resolution.Second);
self.AddEquity("SPY", Resolution.Hour)
self.AddEquity("MSFT", Resolution.Second)

In this case, LEAN subscribes to second-resolution data for AAPL since the lowest resolution in your algorithm is Resolution.Second.

Deposits and Withdraws

You can deposit and withdraw cash from your brokerage account while you run an algorithm that's connected to the account. We sync the algorithm's cash holdings with the cash holdings in your brokerage account every day at 7:45 AM Eastern Time (ET).

Monitor the Brokerage Connection

We notify your algorithm when your brokerage connection disconnects and reconnects.

Lost Connections

If the brokerage connection breaks, we notify your algorithm through the OnBrokerageDisconnect event handler.

public override void OnBrokerageDisconnect() 
{
    Debug("Brokerage connection lost");
}
def OnBrokerageDisconnect(self) -> None:
    self.Debug("Brokerage connection lost")

Restored Connections

When the brokerage connection restores after it disconnects, we notify your algorithm through the OnBrokerageReconnect event handler.

public override void OnBrokerageReconnect() 
{
    Debug("Brokerage connection restored");
}
def OnBrokerageReconnect(self) -> None:
    self.Debug("Brokerage connection restored")

When LEAN reconnects with your brokerage, it synchronizes the state of your live orders. For example, say the brokerage fills your limit order during the period of disconnection. When the connection restores, LEAN updates the state of your portfolio and orders to reflect the filled order, but you won't receive an order event.

Example Algorithm

For a full example algorithm that implements the OnBrokerageDisconnect and OnBrokerageReconnect methods, see the BrokerageActivityEventHandlingAlgorithmBrokerageActivityEventHandlingAlgorithm in the LEAN GitHub repository.

Monitor Brokerage Messages

When your brokerage sends you a message, we notify your algorithm through the OnBrokerageMessage event handler.

public override void OnBrokerageMessage(BrokerageMessageEvent messageEvent) 
{
    Debug(f"Brokerage message received: {messageEvent.Message}");
}
def OnBrokerageMessage(self, message_event: BrokerageMessageEvent) -> None:
    self.Debug(f"Brokerage message received: {message_event.Message}")

BrokerageMessageEvent objects have the following attributes:

For a full example algorithm that implements the OnBrokerageMessage method, see the BrokerageActivityEventHandlingAlgorithmBrokerageActivityEventHandlingAlgorithm in the LEAN GitHub repository.

To handle brokerage messages outside of your algorithm class, create and set a BrokerageMessageHandler. For a full example, see the CustomBrokerageMessageHandlerAlgorithm in the LEAN GitHub repository.

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: