Lean
$LEAN_TAG$
|
Represents the base Brokerage implementation. This provides logging on brokerage events. More...
Public Member Functions | |
abstract bool | PlaceOrder (Order order) |
Places a new order and assigns a new broker ID to the order More... | |
abstract bool | UpdateOrder (Order order) |
Updates the order with the same id More... | |
abstract bool | CancelOrder (Order order) |
Cancels the order with the specified ID More... | |
abstract void | Connect () |
Connects the client to the broker's remote servers More... | |
abstract void | Disconnect () |
Disconnects the client from the broker's remote servers More... | |
virtual void | Dispose () |
Dispose of the brokerage instance More... | |
abstract List< Order > | GetOpenOrders () |
Gets all open orders on the account. NOTE: The order objects returned do not have QC order IDs. More... | |
abstract List< Holding > | GetAccountHoldings () |
Gets all holdings for the account More... | |
abstract List< CashAmount > | GetCashBalance () |
Gets the current cash balance for each currency held in the brokerage account More... | |
virtual IEnumerable< BaseData > | GetHistory (HistoryRequest request) |
Gets the history for the requested security More... | |
virtual bool | ShouldPerformCashSync (DateTime currentTimeUtc) |
Returns whether the brokerage should perform the cash synchronization More... | |
virtual bool | PerformCashSync (IAlgorithm algorithm, DateTime currentTimeUtc, Func< TimeSpan > getTimeSinceLastFill) |
Synchronizes the cashbook with the brokerage account More... | |
Public Attributes | |
virtual bool | AccountInstantlyUpdated => false |
Specifies whether the brokerage will instantly update account balances More... | |
DateTime | LastSyncDateTimeUtc => new DateTime(Interlocked.Read(ref _lastSyncTimeTicks)) |
Gets the datetime of the last sync (UTC) More... | |
Protected Member Functions | |
Brokerage (string name) | |
Creates a new Brokerage instance with the specified name More... | |
virtual void | OnOrderEvents (List< OrderEvent > orderEvents) |
Event invocator for the OrderFilled event More... | |
virtual void | OnOrderEvent (OrderEvent e) |
Event invocator for the OrderFilled event More... | |
virtual void | OnOrderUpdated (OrderUpdateEvent e) |
Event invocator for the OrderUpdated event More... | |
virtual void | OnOrderIdChangedEvent (BrokerageOrderIdChangedEvent e) |
Event invocator for the OrderIdChanged event More... | |
virtual void | OnOptionPositionAssigned (OrderEvent e) |
Event invocator for the OptionPositionAssigned event More... | |
virtual void | OnOptionNotification (OptionNotificationEventArgs e) |
Event invocator for the OptionNotification event More... | |
virtual void | OnNewBrokerageOrderNotification (NewBrokerageOrderNotificationEventArgs e) |
Event invocator for the NewBrokerageOrderNotification event More... | |
virtual void | OnDelistingNotification (DelistingNotificationEventArgs e) |
Event invocator for the DelistingNotification event More... | |
virtual void | OnAccountChanged (AccountEvent e) |
Event invocator for the AccountChanged event More... | |
virtual void | OnMessage (BrokerageMessageEvent e) |
Event invocator for the Message event More... | |
virtual List< Holding > | GetAccountHoldings (Dictionary< string, string > brokerageData, IEnumerable< Security > securities) |
Helper method that will try to get the live holdings from the provided brokerage data collection else will default to the algorithm state More... | |
virtual List< CashAmount > | GetCashBalance (Dictionary< string, string > brokerageData, CashBook cashBook) |
Helper method that will try to get the live cash balance from the provided brokerage data collection else will default to the algorithm state More... | |
virtual CrossZeroOrderResponse | PlaceCrossZeroOrder (CrossZeroFirstOrderRequest crossZeroOrderRequest, bool isPlaceOrderWithLeanEvent=true) |
Places an order that crosses zero (transitions from a short position to a long position or vice versa) and returns the response. This method should be overridden in a derived class to implement brokerage-specific logic for placing such orders. More... | |
bool? | TryCrossZeroPositionOrder (Order order, decimal holdingQuantity) |
Attempts to place an order that may cross the zero position. If the order needs to be split into two parts due to crossing zero, this method handles the split and placement accordingly. More... | |
bool | TryGetUpdateCrossZeroOrderQuantity (Order leanOrder, out decimal quantity) |
Determines whether the given Lean order crosses zero quantity based on the initial order quantity. More... | |
bool | TryGetOrRemoveCrossZeroOrder (string brokerageOrderId, OrderStatus leanOrderStatus, out Order leanOrder) |
Attempts to retrieve or remove a cross-zero order based on the brokerage order ID and its filled status. More... | |
bool | TryHandleRemainingCrossZeroOrder (Order leanOrder, OrderEvent orderEvent) |
Attempts to handle any remaining orders that cross the zero boundary. More... | |
Static Protected Member Functions | |
static OrderPosition | GetOrderPosition (OrderDirection orderDirection, decimal holdingsQuantity) |
Gets the position that might result given the specified order direction and the current holdings quantity. This is useful for brokerages that require more specific direction information than provided by the OrderDirection enum (e.g. Tradier differentiates Buy/Sell and BuyToOpen/BuyToCover/SellShort/SellToClose) More... | |
Protected Attributes | |
DateTime | LastSyncDate => LastSyncDateTimeUtc.ConvertFromUtc(TimeZones.NewYork).Date |
Gets the date of the last sync (New York time zone) More... | |
Properties | |
string | Name [get] |
Gets the name of the brokerage More... | |
abstract bool | IsConnected [get] |
Returns true if we're currently connected to the broker More... | |
virtual string | AccountBaseCurrency [get, protected set] |
Returns the brokerage account's base currency More... | |
ConcurrentDictionary< string, Order > | LeanOrderByZeroCrossBrokerageOrderId = new() [get] |
A thread-safe dictionary that maps brokerage order IDs to their corresponding Order objects. More... | |
Properties inherited from QuantConnect.Interfaces.IBrokerage | |
string | Name [get] |
Gets the name of the brokerage More... | |
bool | IsConnected [get] |
Returns true if we're currently connected to the broker More... | |
bool | AccountInstantlyUpdated [get] |
Specifies whether the brokerage will instantly update account balances More... | |
string | AccountBaseCurrency [get] |
Returns the brokerage account's base currency More... | |
Properties inherited from QuantConnect.Interfaces.IBrokerageCashSynchronizer | |
DateTime | LastSyncDateTimeUtc [get] |
Gets the datetime of the last sync (UTC) More... | |
Events | |
EventHandler< BrokerageOrderIdChangedEvent > | OrderIdChanged |
Event that fires each time the brokerage order id changes More... | |
EventHandler< List< OrderEvent > > | OrdersStatusChanged |
Event that fires each time the status for a list of orders change More... | |
EventHandler< OrderUpdateEvent > | OrderUpdated |
Event that fires each time an order is updated in the brokerage side More... | |
EventHandler< OrderEvent > | OptionPositionAssigned |
Event that fires each time a short option position is assigned More... | |
EventHandler< OptionNotificationEventArgs > | OptionNotification |
Event that fires each time an option position has changed More... | |
EventHandler< NewBrokerageOrderNotificationEventArgs > | NewBrokerageOrderNotification |
Event that fires each time there's a brokerage side generated order More... | |
EventHandler< DelistingNotificationEventArgs > | DelistingNotification |
Event that fires each time a delisting occurs More... | |
EventHandler< AccountEvent > | AccountChanged |
Event that fires each time a user's brokerage account is changed More... | |
EventHandler< BrokerageMessageEvent > | Message |
Event that fires when an error is encountered in the brokerage More... | |
Events inherited from QuantConnect.Interfaces.IBrokerage | |
EventHandler< BrokerageOrderIdChangedEvent > | OrderIdChanged |
Event that fires each time the brokerage order id changes More... | |
EventHandler< List< OrderEvent > > | OrdersStatusChanged |
Event that fires each time the status for a list of orders change More... | |
EventHandler< OrderUpdateEvent > | OrderUpdated |
Event that fires each time an order is updated in the brokerage side More... | |
EventHandler< OrderEvent > | OptionPositionAssigned |
Event that fires each time a short option position is assigned More... | |
EventHandler< OptionNotificationEventArgs > | OptionNotification |
Event that fires each time an option position has changed More... | |
EventHandler< NewBrokerageOrderNotificationEventArgs > | NewBrokerageOrderNotification |
Event that fires each time there's a brokerage side generated order More... | |
EventHandler< DelistingNotificationEventArgs > | DelistingNotification |
Event that fires each time a delisting occurs More... | |
EventHandler< AccountEvent > | AccountChanged |
Event that fires each time a user's brokerage account is changed More... | |
EventHandler< BrokerageMessageEvent > | Message |
Event that fires when a message is received from the brokerage More... | |
Represents the base Brokerage implementation. This provides logging on brokerage events.
Definition at line 36 of file Brokerage.cs.
|
protected |
Creates a new Brokerage instance with the specified name
name | The name of the brokerage |
Definition at line 107 of file Brokerage.cs.
|
pure virtual |
Places a new order and assigns a new broker ID to the order
order | The order to be placed |
Implements QuantConnect.Interfaces.IBrokerage.
Implemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.
|
pure virtual |
Updates the order with the same id
order | The new order information |
Implements QuantConnect.Interfaces.IBrokerage.
Implemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.
|
pure virtual |
Cancels the order with the specified ID
order | The order to cancel |
Implements QuantConnect.Interfaces.IBrokerage.
Implemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.
|
pure virtual |
Connects the client to the broker's remote servers
Implements QuantConnect.Interfaces.IBrokerage.
Implemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage, and QuantConnect.Brokerages.BaseWebsocketsBrokerage.
|
pure virtual |
Disconnects the client from the broker's remote servers
Implements QuantConnect.Interfaces.IBrokerage.
Implemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.
|
virtual |
Dispose of the brokerage instance
Definition at line 146 of file Brokerage.cs.
|
protectedvirtual |
Event invocator for the OrderFilled event
orderEvents | The list of order events |
Reimplemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.
Definition at line 155 of file Brokerage.cs.
|
protectedvirtual |
Event invocator for the OrderFilled event
e | The order event |
Definition at line 171 of file Brokerage.cs.
|
protectedvirtual |
Event invocator for the OrderUpdated event
e | The update event |
Definition at line 180 of file Brokerage.cs.
|
protectedvirtual |
Event invocator for the OrderIdChanged event
e | The BrokerageOrderIdChangedEvent |
Definition at line 196 of file Brokerage.cs.
|
protectedvirtual |
Event invocator for the OptionPositionAssigned event
e | The OrderEvent |
Definition at line 212 of file Brokerage.cs.
|
protectedvirtual |
Event invocator for the OptionNotification event
e | The OptionNotification event arguments |
Definition at line 230 of file Brokerage.cs.
|
protectedvirtual |
Event invocator for the NewBrokerageOrderNotification event
e | The NewBrokerageOrderNotification event arguments |
Definition at line 248 of file Brokerage.cs.
|
protectedvirtual |
Event invocator for the DelistingNotification event
e | The DelistingNotification event arguments |
Definition at line 266 of file Brokerage.cs.
|
protectedvirtual |
Event invocator for the AccountChanged event
e | The AccountEvent |
Definition at line 284 of file Brokerage.cs.
|
protectedvirtual |
Event invocator for the Message event
e | The error |
Definition at line 302 of file Brokerage.cs.
|
protectedvirtual |
Helper method that will try to get the live holdings from the provided brokerage data collection else will default to the algorithm state
Holdings will removed from the provided collection on the first call, since this method is expected to be called only once on initialize, after which the algorithm should use Lean accounting
Definition at line 328 of file Brokerage.cs.
|
protectedvirtual |
Helper method that will try to get the live cash balance from the provided brokerage data collection else will default to the algorithm state
Cash balance will removed from the provided collection on the first call, since this method is expected to be called only once on initialize, after which the algorithm should use Lean accounting
Definition at line 357 of file Brokerage.cs.
|
pure virtual |
Gets all open orders on the account. NOTE: The order objects returned do not have QC order IDs.
Implements QuantConnect.Interfaces.IBrokerage.
Implemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.
|
pure virtual |
Gets all holdings for the account
Implements QuantConnect.Interfaces.IBrokerage.
Implemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage, and QuantConnect.Brokerages.Paper.PaperBrokerage.
|
pure virtual |
Gets the current cash balance for each currency held in the brokerage account
Implements QuantConnect.Interfaces.IBrokerage.
Implemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage, and QuantConnect.Brokerages.Paper.PaperBrokerage.
|
virtual |
Gets the history for the requested security
request | The historical data request |
Implements QuantConnect.Interfaces.IBrokerage.
Definition at line 413 of file Brokerage.cs.
|
staticprotected |
Gets the position that might result given the specified order direction and the current holdings quantity. This is useful for brokerages that require more specific direction information than provided by the OrderDirection enum (e.g. Tradier differentiates Buy/Sell and BuyToOpen/BuyToCover/SellShort/SellToClose)
orderDirection | The order direction |
holdingsQuantity | The current holdings quantity |
Definition at line 426 of file Brokerage.cs.
|
virtual |
Returns whether the brokerage should perform the cash synchronization
currentTimeUtc | The current time (UTC) |
Implements QuantConnect.Interfaces.IBrokerageCashSynchronizer.
Definition at line 453 of file Brokerage.cs.
|
virtual |
Synchronizes the cashbook with the brokerage account
algorithm | The algorithm instance |
currentTimeUtc | The current time (UTC) |
getTimeSinceLastFill | A function which returns the time elapsed since the last fill |
Implements QuantConnect.Interfaces.IBrokerageCashSynchronizer.
Definition at line 472 of file Brokerage.cs.
|
protectedvirtual |
Places an order that crosses zero (transitions from a short position to a long position or vice versa) and returns the response. This method should be overridden in a derived class to implement brokerage-specific logic for placing such orders.
crossZeroOrderRequest | The request object containing details of the cross zero order to be placed. |
isPlaceOrderWithLeanEvent | A boolean indicating whether the order should be placed with triggering a Lean event. Default is true , meaning Lean events will be triggered. |
NotImplementedException | Thrown if the method is not overridden in a derived class. |
Definition at line 610 of file Brokerage.cs.
|
protected |
Attempts to place an order that may cross the zero position. If the order needs to be split into two parts due to crossing zero, this method handles the split and placement accordingly.
order | The order to be placed. Must not be null . |
holdingQuantity | The current holding quantity of the order's symbol. |
true
if the order crosses zero and the first part was successfully placed;false
if the first part of the order could not be placed;
null
if the order does not cross zero.
ArgumentNullException | Thrown if order is null . |
Definition at line 630 of file Brokerage.cs.
|
protected |
Determines whether the given Lean order crosses zero quantity based on the initial order quantity.
leanOrder | The Lean order to check. |
quantity | The quantity to be updated based on whether the order crosses zero. |
true
if the Lean order does not cross zero quantity; otherwise, false
. ArgumentNullException | Thrown when the leanOrder is null. |
Definition at line 697 of file Brokerage.cs.
|
protected |
Attempts to retrieve or remove a cross-zero order based on the brokerage order ID and its filled status.
brokerageOrderId | The unique identifier of the brokerage order. |
leanOrderStatus | The updated status of the order received from the brokerage |
leanOrder | When this method returns, contains the Order object associated with the given brokerage order ID, if the operation was successful; otherwise, null. This parameter is passed uninitialized. |
true
if the method successfully retrieves or removes the order; otherwise, false
. The method locks on a private object to ensure thread safety while accessing the collection of orders. If the order is filled, it is removed from the collection. If the order is partially filled, it is retrieved but not removed. If the order is not found, the method returns false
.
Definition at line 741 of file Brokerage.cs.
|
protected |
Attempts to handle any remaining orders that cross the zero boundary.
leanOrder | The order object that needs to be processed. |
orderEvent | The event object containing order event details. |
Definition at line 767 of file Brokerage.cs.
virtual bool QuantConnect.Brokerages.Brokerage.AccountInstantlyUpdated => false |
Specifies whether the brokerage will instantly update account balances
Definition at line 401 of file Brokerage.cs.
|
protected |
Gets the date of the last sync (New York time zone)
Definition at line 441 of file Brokerage.cs.
DateTime QuantConnect.Brokerages.Brokerage.LastSyncDateTimeUtc => new DateTime(Interlocked.Read(ref _lastSyncTimeTicks)) |
Gets the datetime of the last sync (UTC)
Definition at line 446 of file Brokerage.cs.
|
get |
Gets the name of the brokerage
Definition at line 96 of file Brokerage.cs.
|
get |
Returns true if we're currently connected to the broker
Definition at line 101 of file Brokerage.cs.
|
getprotected set |
Returns the brokerage account's base currency
Definition at line 406 of file Brokerage.cs.
|
getprotected |
A thread-safe dictionary that maps brokerage order IDs to their corresponding Order objects.
This ConcurrentDictionary is used to maintain a mapping between Zero Cross brokerage order IDs and Lean Order objects. The dictionary is protected and read-only, ensuring that it can only be modified by the class that declares it and cannot be assigned a new instance after initialization.
Definition at line 593 of file Brokerage.cs.
EventHandler<BrokerageOrderIdChangedEvent> QuantConnect.Brokerages.Brokerage.OrderIdChanged |
Event that fires each time the brokerage order id changes
Definition at line 48 of file Brokerage.cs.
EventHandler<List<OrderEvent> > QuantConnect.Brokerages.Brokerage.OrdersStatusChanged |
Event that fires each time the status for a list of orders change
Definition at line 53 of file Brokerage.cs.
EventHandler<OrderUpdateEvent> QuantConnect.Brokerages.Brokerage.OrderUpdated |
Event that fires each time an order is updated in the brokerage side
These are not status changes but mainly price changes, like the stop price of a trailing stop order
Definition at line 61 of file Brokerage.cs.
EventHandler<OrderEvent> QuantConnect.Brokerages.Brokerage.OptionPositionAssigned |
Event that fires each time a short option position is assigned
Definition at line 66 of file Brokerage.cs.
EventHandler<OptionNotificationEventArgs> QuantConnect.Brokerages.Brokerage.OptionNotification |
Event that fires each time an option position has changed
Definition at line 71 of file Brokerage.cs.
EventHandler<NewBrokerageOrderNotificationEventArgs> QuantConnect.Brokerages.Brokerage.NewBrokerageOrderNotification |
Event that fires each time there's a brokerage side generated order
Definition at line 76 of file Brokerage.cs.
EventHandler<DelistingNotificationEventArgs> QuantConnect.Brokerages.Brokerage.DelistingNotification |
Event that fires each time a delisting occurs
Definition at line 81 of file Brokerage.cs.
EventHandler<AccountEvent> QuantConnect.Brokerages.Brokerage.AccountChanged |
Event that fires each time a user's brokerage account is changed
Definition at line 86 of file Brokerage.cs.
EventHandler<BrokerageMessageEvent> QuantConnect.Brokerages.Brokerage.Message |
Event that fires when an error is encountered in the brokerage
Definition at line 91 of file Brokerage.cs.