Lean  $LEAN_TAG$
QuantConnect.Brokerages.Brokerage Class Referenceabstract

Represents the base Brokerage implementation. This provides logging on brokerage events. More...

Inheritance diagram for QuantConnect.Brokerages.Brokerage:
[legend]

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< OrderGetOpenOrders ()
 Gets all open orders on the account. NOTE: The order objects returned do not have QC order IDs. More...
 
abstract List< HoldingGetAccountHoldings ()
 Gets all holdings for the account More...
 
abstract List< CashAmountGetCashBalance ()
 Gets the current cash balance for each currency held in the brokerage account More...
 
virtual IEnumerable< BaseDataGetHistory (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< HoldingGetAccountHoldings (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< CashAmountGetCashBalance (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...
 

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...
 
- 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< BrokerageOrderIdChangedEventOrderIdChanged
 Event that fires each time the brokerage order id changes More...
 
EventHandler< List< OrderEvent > > OrdersStatusChanged
 
EventHandler< OrderUpdateEventOrderUpdated
 Event that fires each time an order is updated in the brokerage side More...
 
EventHandler< OrderEventOptionPositionAssigned
 Event that fires each time a short option position is assigned More...
 
EventHandler< OptionNotificationEventArgsOptionNotification
 Event that fires each time an option position has changed More...
 
EventHandler< NewBrokerageOrderNotificationEventArgsNewBrokerageOrderNotification
 Event that fires each time there's a brokerage side generated order More...
 
EventHandler< DelistingNotificationEventArgsDelistingNotification
 Event that fires each time a delisting occurs More...
 
EventHandler< AccountEventAccountChanged
 Event that fires each time a user's brokerage account is changed More...
 
EventHandler< BrokerageMessageEventMessage
 Event that fires when an error is encountered in the brokerage More...
 
- Events inherited from QuantConnect.Interfaces.IBrokerage
EventHandler< BrokerageOrderIdChangedEventOrderIdChanged
 Event that fires each time the brokerage order id changes More...
 
EventHandler< List< OrderEvent > > OrdersStatusChanged
 
EventHandler< OrderUpdateEventOrderUpdated
 Event that fires each time an order is updated in the brokerage side More...
 
EventHandler< OrderEventOptionPositionAssigned
 Event that fires each time a short option position is assigned More...
 
EventHandler< OptionNotificationEventArgsOptionNotification
 Event that fires each time an option position has changed More...
 
EventHandler< NewBrokerageOrderNotificationEventArgsNewBrokerageOrderNotification
 Event that fires each time there's a brokerage side generated order More...
 
EventHandler< DelistingNotificationEventArgsDelistingNotification
 Event that fires each time a delisting occurs More...
 
EventHandler< AccountEventAccountChanged
 Event that fires each time a user's brokerage account is changed More...
 
EventHandler< BrokerageMessageEventMessage
 Event that fires when a message is received from the brokerage More...
 

Detailed Description

Represents the base Brokerage implementation. This provides logging on brokerage events.

Definition at line 33 of file Brokerage.cs.

Constructor & Destructor Documentation

◆ Brokerage()

QuantConnect.Brokerages.Brokerage.Brokerage ( string  name)
protected

Creates a new Brokerage instance with the specified name

Parameters
nameThe name of the brokerage

Definition at line 103 of file Brokerage.cs.

Member Function Documentation

◆ PlaceOrder()

abstract bool QuantConnect.Brokerages.Brokerage.PlaceOrder ( Order  order)
pure virtual

Places a new order and assigns a new broker ID to the order

Parameters
orderThe order to be placed
Returns
True if the request for a new order has been placed, false otherwise

Implements QuantConnect.Interfaces.IBrokerage.

Implemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.

◆ UpdateOrder()

abstract bool QuantConnect.Brokerages.Brokerage.UpdateOrder ( Order  order)
pure virtual

Updates the order with the same id

Parameters
orderThe new order information
Returns
True if the request was made for the order to be updated, false otherwise

Implements QuantConnect.Interfaces.IBrokerage.

Implemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.

◆ CancelOrder()

abstract bool QuantConnect.Brokerages.Brokerage.CancelOrder ( Order  order)
pure virtual

Cancels the order with the specified ID

Parameters
orderThe order to cancel
Returns
True if the request was made for the order to be canceled, false otherwise

Implements QuantConnect.Interfaces.IBrokerage.

Implemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.

◆ Connect()

abstract void QuantConnect.Brokerages.Brokerage.Connect ( )
pure virtual

◆ Disconnect()

abstract void QuantConnect.Brokerages.Brokerage.Disconnect ( )
pure virtual

Disconnects the client from the broker's remote servers

Implements QuantConnect.Interfaces.IBrokerage.

Implemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.

◆ Dispose()

virtual void QuantConnect.Brokerages.Brokerage.Dispose ( )
virtual

Dispose of the brokerage instance

Definition at line 142 of file Brokerage.cs.

◆ OnOrderEvents()

virtual void QuantConnect.Brokerages.Brokerage.OnOrderEvents ( List< OrderEvent orderEvents)
protectedvirtual

Event invocator for the OrderFilled event

Parameters
orderEventsThe list of order events

Reimplemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.

Definition at line 151 of file Brokerage.cs.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnOrderEvent()

virtual void QuantConnect.Brokerages.Brokerage.OnOrderEvent ( OrderEvent  e)
protectedvirtual

Event invocator for the OrderFilled event

Parameters
eThe order event

Definition at line 167 of file Brokerage.cs.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnOrderUpdated()

virtual void QuantConnect.Brokerages.Brokerage.OnOrderUpdated ( OrderUpdateEvent  e)
protectedvirtual

Event invocator for the OrderUpdated event

Parameters
eThe update event

Definition at line 176 of file Brokerage.cs.

Here is the call graph for this function:

◆ OnOrderIdChangedEvent()

virtual void QuantConnect.Brokerages.Brokerage.OnOrderIdChangedEvent ( BrokerageOrderIdChangedEvent  e)
protectedvirtual

Event invocator for the OrderIdChanged event

Parameters
eThe BrokerageOrderIdChangedEvent

Definition at line 192 of file Brokerage.cs.

Here is the call graph for this function:

◆ OnOptionPositionAssigned()

virtual void QuantConnect.Brokerages.Brokerage.OnOptionPositionAssigned ( OrderEvent  e)
protectedvirtual

Event invocator for the OptionPositionAssigned event

Parameters
eThe OrderEvent

Definition at line 208 of file Brokerage.cs.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnOptionNotification()

virtual void QuantConnect.Brokerages.Brokerage.OnOptionNotification ( OptionNotificationEventArgs  e)
protectedvirtual

Event invocator for the OptionNotification event

Parameters
eThe OptionNotification event arguments

Definition at line 226 of file Brokerage.cs.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnNewBrokerageOrderNotification()

virtual void QuantConnect.Brokerages.Brokerage.OnNewBrokerageOrderNotification ( NewBrokerageOrderNotificationEventArgs  e)
protectedvirtual

Event invocator for the NewBrokerageOrderNotification event

Parameters
eThe NewBrokerageOrderNotification event arguments

Definition at line 244 of file Brokerage.cs.

Here is the call graph for this function:

◆ OnDelistingNotification()

virtual void QuantConnect.Brokerages.Brokerage.OnDelistingNotification ( DelistingNotificationEventArgs  e)
protectedvirtual

Event invocator for the DelistingNotification event

Parameters
eThe DelistingNotification event arguments

Definition at line 262 of file Brokerage.cs.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnAccountChanged()

virtual void QuantConnect.Brokerages.Brokerage.OnAccountChanged ( AccountEvent  e)
protectedvirtual

Event invocator for the AccountChanged event

Parameters
eThe AccountEvent

Definition at line 280 of file Brokerage.cs.

Here is the call graph for this function:

◆ OnMessage()

virtual void QuantConnect.Brokerages.Brokerage.OnMessage ( BrokerageMessageEvent  e)
protectedvirtual

Event invocator for the Message event

Parameters
eThe error

Definition at line 298 of file Brokerage.cs.

Here is the call graph for this function:

◆ GetAccountHoldings() [1/2]

virtual List<Holding> QuantConnect.Brokerages.Brokerage.GetAccountHoldings ( Dictionary< string, string >  brokerageData,
IEnumerable< Security securities 
)
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 324 of file Brokerage.cs.

Here is the call graph for this function:

◆ GetCashBalance() [1/2]

virtual List<CashAmount> QuantConnect.Brokerages.Brokerage.GetCashBalance ( Dictionary< string, string >  brokerageData,
CashBook  cashBook 
)
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 353 of file Brokerage.cs.

Here is the call graph for this function:

◆ GetOpenOrders()

abstract List<Order> QuantConnect.Brokerages.Brokerage.GetOpenOrders ( )
pure virtual

Gets all open orders on the account. NOTE: The order objects returned do not have QC order IDs.

Returns
The open orders returned from IB

Implements QuantConnect.Interfaces.IBrokerage.

Implemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.

◆ GetAccountHoldings() [2/2]

abstract List<Holding> QuantConnect.Brokerages.Brokerage.GetAccountHoldings ( )
pure virtual

Gets all holdings for the account

Returns
The current holdings from the account

Implements QuantConnect.Interfaces.IBrokerage.

Implemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage, and QuantConnect.Brokerages.Paper.PaperBrokerage.

◆ GetCashBalance() [2/2]

abstract List<CashAmount> QuantConnect.Brokerages.Brokerage.GetCashBalance ( )
pure virtual

Gets the current cash balance for each currency held in the brokerage account

Returns
The current cash balance for each currency available for trading

Implements QuantConnect.Interfaces.IBrokerage.

Implemented in QuantConnect.Brokerages.Backtesting.BacktestingBrokerage, and QuantConnect.Brokerages.Paper.PaperBrokerage.

Here is the caller graph for this function:

◆ GetHistory()

virtual IEnumerable<BaseData> QuantConnect.Brokerages.Brokerage.GetHistory ( HistoryRequest  request)
virtual

Gets the history for the requested security

Parameters
requestThe historical data request
Returns
An enumerable of bars covering the span specified in the request

Implements QuantConnect.Interfaces.IBrokerage.

Definition at line 409 of file Brokerage.cs.

◆ GetOrderPosition()

static OrderPosition QuantConnect.Brokerages.Brokerage.GetOrderPosition ( OrderDirection  orderDirection,
decimal  holdingsQuantity 
)
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)

Parameters
orderDirectionThe order direction
holdingsQuantityThe current holdings quantity
Returns
The order position

Definition at line 422 of file Brokerage.cs.

◆ ShouldPerformCashSync()

virtual bool QuantConnect.Brokerages.Brokerage.ShouldPerformCashSync ( DateTime  currentTimeUtc)
virtual

Returns whether the brokerage should perform the cash synchronization

Parameters
currentTimeUtcThe current time (UTC)
Returns
True if the cash sync should be performed

Implements QuantConnect.Interfaces.IBrokerageCashSynchronizer.

Definition at line 449 of file Brokerage.cs.

◆ PerformCashSync()

virtual bool QuantConnect.Brokerages.Brokerage.PerformCashSync ( IAlgorithm  algorithm,
DateTime  currentTimeUtc,
Func< TimeSpan >  getTimeSinceLastFill 
)
virtual

Synchronizes the cashbook with the brokerage account

Parameters
algorithmThe algorithm instance
currentTimeUtcThe current time (UTC)
getTimeSinceLastFillA function which returns the time elapsed since the last fill
Returns
True if the cash sync was performed successfully

Implements QuantConnect.Interfaces.IBrokerageCashSynchronizer.

Definition at line 468 of file Brokerage.cs.

Here is the call graph for this function:

Member Data Documentation

◆ AccountInstantlyUpdated

virtual bool QuantConnect.Brokerages.Brokerage.AccountInstantlyUpdated => false

Specifies whether the brokerage will instantly update account balances

Definition at line 397 of file Brokerage.cs.

◆ LastSyncDate

DateTime QuantConnect.Brokerages.Brokerage.LastSyncDate => LastSyncDateTimeUtc.ConvertFromUtc(TimeZones.NewYork).Date
protected

Gets the date of the last sync (New York time zone)

Definition at line 437 of file Brokerage.cs.

◆ LastSyncDateTimeUtc

DateTime QuantConnect.Brokerages.Brokerage.LastSyncDateTimeUtc => new DateTime(Interlocked.Read(ref _lastSyncTimeTicks))

Gets the datetime of the last sync (UTC)

Definition at line 442 of file Brokerage.cs.

Property Documentation

◆ Name

string QuantConnect.Brokerages.Brokerage.Name
get

Gets the name of the brokerage

Definition at line 92 of file Brokerage.cs.

◆ IsConnected

abstract bool QuantConnect.Brokerages.Brokerage.IsConnected
get

Returns true if we're currently connected to the broker

Definition at line 97 of file Brokerage.cs.

◆ AccountBaseCurrency

virtual string QuantConnect.Brokerages.Brokerage.AccountBaseCurrency
getprotected set

Returns the brokerage account's base currency

Definition at line 402 of file Brokerage.cs.

Event Documentation

◆ OrderIdChanged

EventHandler<BrokerageOrderIdChangedEvent> QuantConnect.Brokerages.Brokerage.OrderIdChanged

Event that fires each time the brokerage order id changes

Definition at line 45 of file Brokerage.cs.

◆ OrdersStatusChanged

EventHandler<List<OrderEvent> > QuantConnect.Brokerages.Brokerage.OrdersStatusChanged

Event that fires each time the status for a list of orders change

Definition at line 49 of file Brokerage.cs.

◆ OrderUpdated

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 57 of file Brokerage.cs.

◆ OptionPositionAssigned

EventHandler<OrderEvent> QuantConnect.Brokerages.Brokerage.OptionPositionAssigned

Event that fires each time a short option position is assigned

Definition at line 62 of file Brokerage.cs.

◆ OptionNotification

EventHandler<OptionNotificationEventArgs> QuantConnect.Brokerages.Brokerage.OptionNotification

Event that fires each time an option position has changed

Definition at line 67 of file Brokerage.cs.

◆ NewBrokerageOrderNotification

EventHandler<NewBrokerageOrderNotificationEventArgs> QuantConnect.Brokerages.Brokerage.NewBrokerageOrderNotification

Event that fires each time there's a brokerage side generated order

Definition at line 72 of file Brokerage.cs.

◆ DelistingNotification

EventHandler<DelistingNotificationEventArgs> QuantConnect.Brokerages.Brokerage.DelistingNotification

Event that fires each time a delisting occurs

Definition at line 77 of file Brokerage.cs.

◆ AccountChanged

EventHandler<AccountEvent> QuantConnect.Brokerages.Brokerage.AccountChanged

Event that fires each time a user's brokerage account is changed

Definition at line 82 of file Brokerage.cs.

◆ Message

EventHandler<BrokerageMessageEvent> QuantConnect.Brokerages.Brokerage.Message

Event that fires when an error is encountered in the brokerage

Definition at line 87 of file Brokerage.cs.


The documentation for this class was generated from the following file: