Lean  $LEAN_TAG$
QuantConnect.Brokerages.Backtesting.BacktestingBrokerage Class Reference

Represents a brokerage to be used during backtesting. This is intended to be only be used with the BacktestingTransactionHandler More...

Inheritance diagram for QuantConnect.Brokerages.Backtesting.BacktestingBrokerage:
[legend]

Public Member Functions

 BacktestingBrokerage (IAlgorithm algorithm)
 Creates a new BacktestingBrokerage for the specified algorithm More...
 
override List< OrderGetOpenOrders ()
 Gets all open orders on the account More...
 
override List< HoldingGetAccountHoldings ()
 Gets all holdings for the account More...
 
override List< CashAmountGetCashBalance ()
 Gets the current cash balance for each currency held in the brokerage account More...
 
override bool PlaceOrder (Order order)
 Places a new order and assigns a new broker ID to the order More...
 
override bool UpdateOrder (Order order)
 Updates the order with the same ID More...
 
override bool CancelOrder (Order order)
 Cancels the order with the specified ID More...
 
virtual void Scan ()
 Scans all the outstanding orders and applies the algorithm model fills to generate the order events More...
 
override void Connect ()
 The BacktestingBrokerage is always connected. This is a no-op. More...
 
override void Disconnect ()
 The BacktestingBrokerage is always connected. This is a no-op. More...
 
void ProcessDelistings (Delistings delistings)
 Process delistings More...
 
- Public Member Functions inherited from QuantConnect.Brokerages.Brokerage
virtual void Dispose ()
 Dispose of the brokerage instance 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

override bool IsConnected => true
 Gets the connection status More...
 
- Public Attributes inherited from QuantConnect.Brokerages.Brokerage
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

 BacktestingBrokerage (IAlgorithm algorithm, string name)
 Creates a new BacktestingBrokerage for the specified algorithm More...
 
override void OnOrderEvents (List< OrderEvent > orderEvents)
 Event invocator for the OrderFilled event More...
 
- Protected Member Functions inherited from QuantConnect.Brokerages.Brokerage
 Brokerage (string name)
 Creates a new Brokerage instance with the specified name 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...
 
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...
 

Properties

IAlgorithm Algorithm [get]
 This is the algorithm under test More...
 
- Properties inherited from QuantConnect.Brokerages.Brokerage
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, OrderLeanOrderByZeroCrossBrokerageOrderId = 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...
 

Additional Inherited Members

- Static Protected Member Functions inherited from QuantConnect.Brokerages.Brokerage
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 inherited from QuantConnect.Brokerages.Brokerage
DateTime LastSyncDate => LastSyncDateTimeUtc.ConvertFromUtc(TimeZones.NewYork).Date
 Gets the date of the last sync (New York time zone) More...
 
- Events inherited from QuantConnect.Brokerages.Brokerage
EventHandler< BrokerageOrderIdChangedEventOrderIdChanged
 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< 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
 Event that fires each time the status for a list of orders change More...
 
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 a brokerage to be used during backtesting. This is intended to be only be used with the BacktestingTransactionHandler

Definition at line 37 of file BacktestingBrokerage.cs.

Constructor & Destructor Documentation

◆ BacktestingBrokerage() [1/2]

QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.BacktestingBrokerage ( IAlgorithm  algorithm)

Creates a new BacktestingBrokerage for the specified algorithm

Parameters
algorithmThe algorithm instance

Definition at line 58 of file BacktestingBrokerage.cs.

◆ BacktestingBrokerage() [2/2]

QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.BacktestingBrokerage ( IAlgorithm  algorithm,
string  name 
)
protected

Creates a new BacktestingBrokerage for the specified algorithm

Parameters
algorithmThe algorithm instance
nameThe name of the brokerage

Definition at line 68 of file BacktestingBrokerage.cs.

Member Function Documentation

◆ GetOpenOrders()

override List<Order> QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.GetOpenOrders ( )
virtual

Gets all open orders on the account

Returns
The open orders returned from IB

Implements QuantConnect.Brokerages.Brokerage.

Definition at line 87 of file BacktestingBrokerage.cs.

Here is the call graph for this function:

◆ GetAccountHoldings()

override List<Holding> QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.GetAccountHoldings ( )
virtual

Gets all holdings for the account

Returns
The current holdings from the account

Implements QuantConnect.Brokerages.Brokerage.

Reimplemented in QuantConnect.Brokerages.Paper.PaperBrokerage.

Definition at line 96 of file BacktestingBrokerage.cs.

◆ GetCashBalance()

override List<CashAmount> QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.GetCashBalance ( )
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.Brokerages.Brokerage.

Reimplemented in QuantConnect.Brokerages.Paper.PaperBrokerage.

Definition at line 108 of file BacktestingBrokerage.cs.

◆ PlaceOrder()

override bool QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.PlaceOrder ( Order  order)
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.Brokerages.Brokerage.

Definition at line 118 of file BacktestingBrokerage.cs.

Here is the call graph for this function:

◆ UpdateOrder()

override bool QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.UpdateOrder ( Order  order)
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.Brokerages.Brokerage.

Definition at line 152 of file BacktestingBrokerage.cs.

Here is the call graph for this function:

◆ CancelOrder()

override bool QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.CancelOrder ( Order  order)
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.Brokerages.Brokerage.

Definition at line 191 of file BacktestingBrokerage.cs.

Here is the call graph for this function:

◆ Scan()

virtual void QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.Scan ( )
virtual

Scans all the outstanding orders and applies the algorithm model fills to generate the order events

Reimplemented in QuantConnect.Brokerages.Paper.PaperBrokerage.

Definition at line 232 of file BacktestingBrokerage.cs.

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

◆ OnOrderEvents()

override void QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.OnOrderEvents ( List< OrderEvent orderEvents)
protectedvirtual

Event invocator for the OrderFilled event

Parameters
orderEventsThe list of order events

Reimplemented from QuantConnect.Brokerages.Brokerage.

Definition at line 484 of file BacktestingBrokerage.cs.

Here is the caller graph for this function:

◆ Connect()

override void QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.Connect ( )
virtual

The BacktestingBrokerage is always connected. This is a no-op.

Implements QuantConnect.Brokerages.Brokerage.

Definition at line 496 of file BacktestingBrokerage.cs.

◆ Disconnect()

override void QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.Disconnect ( )
virtual

The BacktestingBrokerage is always connected. This is a no-op.

Implements QuantConnect.Brokerages.Brokerage.

Definition at line 504 of file BacktestingBrokerage.cs.

◆ ProcessDelistings()

void QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.ProcessDelistings ( Delistings  delistings)

Process delistings

Parameters
delistingsDelistings to process

Definition at line 523 of file BacktestingBrokerage.cs.

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

Member Data Documentation

◆ IsConnected

override bool QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.IsConnected => true

Gets the connection status

The BacktestingBrokerage is always connected

Definition at line 81 of file BacktestingBrokerage.cs.

Property Documentation

◆ Algorithm

IAlgorithm QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.Algorithm
getprotected

This is the algorithm under test

Definition at line 52 of file BacktestingBrokerage.cs.


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