Lean
$LEAN_TAG$
|
Represents a brokerage to be used during backtesting. This is intended to be only be used with the BacktestingTransactionHandler More...
Public Member Functions | |
BacktestingBrokerage (IAlgorithm algorithm) | |
Creates a new BacktestingBrokerage for the specified algorithm More... | |
override List< Order > | GetOpenOrders () |
Gets all open orders on the account More... | |
override List< Holding > | GetAccountHoldings () |
Gets all holdings for the account More... | |
override List< CashAmount > | GetCashBalance () |
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< 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 | |
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< 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... | |
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, 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... | |
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< 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 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.
QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.BacktestingBrokerage | ( | IAlgorithm | algorithm | ) |
Creates a new BacktestingBrokerage for the specified algorithm
algorithm | The algorithm instance |
Definition at line 58 of file BacktestingBrokerage.cs.
|
protected |
Creates a new BacktestingBrokerage for the specified algorithm
algorithm | The algorithm instance |
name | The name of the brokerage |
Definition at line 68 of file BacktestingBrokerage.cs.
|
virtual |
Gets all open orders on the account
Implements QuantConnect.Brokerages.Brokerage.
Definition at line 87 of file BacktestingBrokerage.cs.
|
virtual |
Gets all holdings for the account
Implements QuantConnect.Brokerages.Brokerage.
Reimplemented in QuantConnect.Brokerages.Paper.PaperBrokerage.
Definition at line 96 of file BacktestingBrokerage.cs.
|
virtual |
Gets the current cash balance for each currency held in the brokerage account
Implements QuantConnect.Brokerages.Brokerage.
Reimplemented in QuantConnect.Brokerages.Paper.PaperBrokerage.
Definition at line 108 of file BacktestingBrokerage.cs.
|
virtual |
Places a new order and assigns a new broker ID to the order
order | The order to be placed |
Implements QuantConnect.Brokerages.Brokerage.
Definition at line 118 of file BacktestingBrokerage.cs.
|
virtual |
Updates the order with the same ID
order | The new order information |
Implements QuantConnect.Brokerages.Brokerage.
Definition at line 152 of file BacktestingBrokerage.cs.
|
virtual |
Cancels the order with the specified ID
order | The order to cancel |
Implements QuantConnect.Brokerages.Brokerage.
Definition at line 191 of file BacktestingBrokerage.cs.
|
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.
|
protectedvirtual |
Event invocator for the OrderFilled event
orderEvents | The list of order events |
Reimplemented from QuantConnect.Brokerages.Brokerage.
Definition at line 484 of file BacktestingBrokerage.cs.
|
virtual |
The BacktestingBrokerage is always connected. This is a no-op.
Implements QuantConnect.Brokerages.Brokerage.
Definition at line 496 of file BacktestingBrokerage.cs.
|
virtual |
The BacktestingBrokerage is always connected. This is a no-op.
Implements QuantConnect.Brokerages.Brokerage.
Definition at line 504 of file BacktestingBrokerage.cs.
void QuantConnect.Brokerages.Backtesting.BacktestingBrokerage.ProcessDelistings | ( | Delistings | delistings | ) |
Process delistings
delistings | Delistings to process |
Definition at line 523 of file BacktestingBrokerage.cs.
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.
|
getprotected |
This is the algorithm under test
Definition at line 52 of file BacktestingBrokerage.cs.