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
portfolio
object with your account holdings and the Transactions
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.add_equity("SPY", Resolution.HOUR) self.add_equity("MSFT", Resolution.SECOND)
In this case, LEAN subscribes to second-resolution data for AAPL since the lowest resolution in your algorithm is
Resolution.Second
Resolution.SECOND
.
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
on_brokerage_disconnect
event handler.
public override void OnBrokerageDisconnect() { Debug("Brokerage connection lost"); }
def on_brokerage_disconnect(self) -> None: self.debug("Brokerage connection lost")
Restored Connections
When the brokerage connection restores after it disconnects, we notify your algorithm through the OnBrokerageReconnect
on_brokerage_reconnect
event handler.
public override void OnBrokerageReconnect() { Debug("Brokerage connection restored"); }
def on_brokerage_reconnect(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
on_brokerage_disconnect
and OnBrokerageReconnect
on_brokerage_reconnect
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
on_brokerage_message
event handler.
public override void OnBrokerageMessage(BrokerageMessageEvent messageEvent) { Debug(f"Brokerage message received: {messageEvent.Message}"); }
def on_brokerage_message(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
on_brokerage_message
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 more information example, see the Reality Modeling > Brokerage Message Handler.
Brokerage Models
Brokerage features and limitations are realistically modelled with Brokerage Models. These reality models set the supported order and asset types for the brokerage. To learn how to set your specific brokerage, see Supported Models.