Lean  $LEAN_TAG$
QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler Class Reference

Transaction handler for all brokerages More...

Inheritance diagram for QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler:
[legend]

Public Member Functions

virtual void Initialize (IAlgorithm algorithm, IBrokerage brokerage, IResultHandler resultHandler)
 Creates a new BrokerageTransactionHandler to process orders using the specified brokerage implementation More...
 
OrderTicket Process (OrderRequest request)
 Adds the specified order to be processed More...
 
OrderTicket AddOrder (SubmitOrderRequest request)
 Add an order to collection and return the unique order id or negative if an error. More...
 
OrderTicket UpdateOrder (UpdateOrderRequest request)
 Update an order yet to be filled such as stop or limit orders. More...
 
OrderTicket CancelOrder (CancelOrderRequest request)
 Remove this order from outstanding queue: user is requesting a cancel. More...
 
IEnumerable< OrderTicketGetOrderTickets (Func< OrderTicket, bool > filter=null)
 Gets and enumerable of OrderTicket matching the specified filter More...
 
IEnumerable< OrderTicketGetOpenOrderTickets (Func< OrderTicket, bool > filter=null)
 Gets and enumerable of opened OrderTicket matching the specified filter More...
 
OrderTicket GetOrderTicket (int orderId)
 Gets the order ticket for the specified order id. Returns null if not found More...
 
Order GetOrderById (int orderId)
 Get the order by its id More...
 
List< OrderGetOrdersByBrokerageId (string brokerageId)
 Gets the order by its brokerage id More...
 
IEnumerable< OrderGetOrders (Func< Order, bool > filter=null)
 Gets all orders matching the specified filter. Specifying null will return an enumerable of all orders. More...
 
List< OrderGetOpenOrders (Func< Order, bool > filter=null)
 Gets open orders matching the specified filter More...
 
virtual void ProcessAsynchronousEvents ()
 Processes asynchronous events on the transaction handler's thread More...
 
virtual void ProcessSynchronousEvents ()
 Processes all synchronous events that must take place before the next time loop for the algorithm More...
 
void AddOpenOrder (Order order, IAlgorithm algorithm)
 Register an already open Order More...
 
void Exit ()
 Signal a end of thread request to stop monitoring the transactions. More...
 
void HandleOrderRequest (OrderRequest request)
 Handles a generic order request More...
 
decimal RoundOffOrder (Order order, Security security)
 Rounds off the order towards 0 to the nearest multiple of Lot Size More...
 

Public Attributes

IEnumerable< OrderEventOrderEvents => _orderEvents
 Gets all order events More...
 
int OrdersCount => _totalOrderCount
 Gets the current number of orders that have been processed More...
 

Protected Member Functions

virtual void InitializeTransactionThread ()
 Create and start the transaction thread, who will be in charge of processing the order requests More...
 
virtual void WaitForOrderSubmission (OrderTicket ticket)
 Wait for the order to be handled by the _processingThread More...
 
void Run ()
 Primary thread entry point to launch the transaction thread. More...
 
void RoundOrderPrices (Order order, Security security)
 Rounds the order prices to its security minimum price variation. More...
 
void RoundOrderPrices (Order order, Security security, bool comboIsReady, Dictionary< Order, Security > orders)
 Rounds the order prices to its security minimum price variation. More...
 

Protected Attributes

virtual TimeSpan TimeSinceLastFill
 Gets the amount of time since the last call to algorithm.Portfolio.ProcessFill(fill) More...
 
virtual DateTime CurrentTimeUtc => DateTime.UtcNow
 Gets current time UTC. This is here to facilitate testing More...
 

Properties

IBusyCollection< OrderRequest_orderRequestQueue [get, set]
 OrderQueue holds the newly updated orders from the user algorithm waiting to be processed. Once orders are processed they are moved into the Orders queue awaiting the brokerage response. More...
 
CancelPendingOrders _cancelPendingOrders = new CancelPendingOrders() [get]
 The _cancelPendingOrders instance will help to keep track of CancelPending orders and their Status More...
 
ConcurrentDictionary< int, OrderOrders [get]
 Gets the permanent storage for all orders More...
 
ConcurrentDictionary< int, OrderTicketOrderTickets [get]
 Gets the permanent storage for all order tickets More...
 
bool IsActive [get]
 Boolean flag indicating the Run thread method is busy. False indicates it is completely finished processing and ready to be terminated. More...
 
- Properties inherited from QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler
bool IsActive [get]
 Boolean flag indicating the thread is busy. False indicates it is completely finished processing and ready to be terminated. More...
 
ConcurrentDictionary< int, OrderOrders [get]
 Gets the permanent storage for all orders More...
 
IEnumerable< OrderEventOrderEvents [get]
 Gets all order events More...
 
ConcurrentDictionary< int, OrderTicketOrderTickets [get]
 Gets the permanent storage for all order tickets More...
 
- Properties inherited from QuantConnect.Securities.IOrderProvider
int OrdersCount [get]
 Gets the current number of orders that have been processed More...
 

Events

EventHandler< OrderEventNewOrderEvent
 Event fired when there is a new OrderEvent More...
 
- Events inherited from QuantConnect.Securities.IOrderEventProvider
EventHandler< OrderEventNewOrderEvent
 Event fired when there is a new OrderEvent More...
 

Detailed Description

Transaction handler for all brokerages

Definition at line 38 of file BrokerageTransactionHandler.cs.

Member Function Documentation

◆ Initialize()

virtual void QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.Initialize ( IAlgorithm  algorithm,
IBrokerage  brokerage,
IResultHandler  resultHandler 
)
virtual

Creates a new BrokerageTransactionHandler to process orders using the specified brokerage implementation

Parameters
algorithmThe algorithm instance
brokerageThe brokerage implementation to process orders and fire fill events
resultHandler

Implements QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler.

Reimplemented in QuantConnect.Lean.Engine.TransactionHandlers.BacktestingTransactionHandler.

Definition at line 151 of file BrokerageTransactionHandler.cs.

Here is the call graph for this function:

◆ InitializeTransactionThread()

virtual void QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.InitializeTransactionThread ( )
protectedvirtual

Create and start the transaction thread, who will be in charge of processing the order requests

Reimplemented in QuantConnect.Lean.Engine.TransactionHandlers.BacktestingTransactionHandler.

Definition at line 215 of file BrokerageTransactionHandler.cs.

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

◆ Process()

OrderTicket QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.Process ( OrderRequest  request)

Adds the specified order to be processed

Parameters
requestThe order to be processed

Implements QuantConnect.Securities.IOrderProcessor.

Definition at line 233 of file BrokerageTransactionHandler.cs.

Here is the call graph for this function:

◆ AddOrder()

OrderTicket QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.AddOrder ( SubmitOrderRequest  request)

Add an order to collection and return the unique order id or negative if an error.

Parameters
requestA request detailing the order to be submitted
Returns
New unique, increasing orderid

Definition at line 263 of file BrokerageTransactionHandler.cs.

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

◆ WaitForOrderSubmission()

virtual void QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.WaitForOrderSubmission ( OrderTicket  ticket)
protectedvirtual

Wait for the order to be handled by the _processingThread

Parameters
ticketThe OrderTicket expecting to be submitted

Reimplemented in QuantConnect.Lean.Engine.TransactionHandlers.BacktestingTransactionHandler.

Definition at line 335 of file BrokerageTransactionHandler.cs.

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

◆ UpdateOrder()

OrderTicket QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.UpdateOrder ( UpdateOrderRequest  request)

Update an order yet to be filled such as stop or limit orders.

Parameters
requestRequest detailing how the order should be updated

Does not apply if the order is already fully filled

Definition at line 350 of file BrokerageTransactionHandler.cs.

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

◆ CancelOrder()

OrderTicket QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.CancelOrder ( CancelOrderRequest  request)

Remove this order from outstanding queue: user is requesting a cancel.

Parameters
requestRequest containing the specific order id to remove

Definition at line 432 of file BrokerageTransactionHandler.cs.

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

◆ GetOrderTickets()

IEnumerable<OrderTicket> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.GetOrderTickets ( Func< OrderTicket, bool >  filter = null)

Gets and enumerable of OrderTicket matching the specified filter

Parameters
filterThe filter predicate used to find the required order tickets
Returns
An enumerable of OrderTicket matching the specified filter

Implements QuantConnect.Securities.IOrderProvider.

Definition at line 507 of file BrokerageTransactionHandler.cs.

◆ GetOpenOrderTickets()

IEnumerable<OrderTicket> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.GetOpenOrderTickets ( Func< OrderTicket, bool >  filter = null)

Gets and enumerable of opened OrderTicket matching the specified filter

Parameters
filterThe filter predicate used to find the required order tickets
Returns
An enumerable of opened OrderTicket matching the specified filter

Implements QuantConnect.Securities.IOrderProvider.

Definition at line 517 of file BrokerageTransactionHandler.cs.

◆ GetOrderTicket()

OrderTicket QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.GetOrderTicket ( int  orderId)

Gets the order ticket for the specified order id. Returns null if not found

Parameters
orderIdThe order's id
Returns
The order ticket with the specified id, or null if not found

Implements QuantConnect.Securities.IOrderProvider.

Definition at line 527 of file BrokerageTransactionHandler.cs.

◆ GetOrderById()

Order QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.GetOrderById ( int  orderId)

Get the order by its id

Parameters
orderIdOrder id to fetch
Returns
A clone of the order with the specified id, or null if no match is found

Implements QuantConnect.Securities.IOrderProvider.

Definition at line 541 of file BrokerageTransactionHandler.cs.

Here is the call graph for this function:

◆ GetOrdersByBrokerageId()

List<Order> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.GetOrdersByBrokerageId ( string  brokerageId)

Gets the order by its brokerage id

Parameters
brokerageIdThe brokerage id to fetch
Returns
The first order matching the brokerage id, or null if no match is found

Implements QuantConnect.Securities.IOrderProvider.

Definition at line 558 of file BrokerageTransactionHandler.cs.

◆ GetOrders()

IEnumerable<Order> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.GetOrders ( Func< Order, bool >  filter = null)

Gets all orders matching the specified filter. Specifying null will return an enumerable of all orders.

Parameters
filterDelegate used to filter the orders
Returns
All orders this order provider currently holds by the specified filter

Implements QuantConnect.Securities.IOrderProvider.

Definition at line 586 of file BrokerageTransactionHandler.cs.

◆ GetOpenOrders()

List<Order> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.GetOpenOrders ( Func< Order, bool >  filter = null)

Gets open orders matching the specified filter

Parameters
filterDelegate used to filter the orders
Returns
All open orders this order provider currently holds

Implements QuantConnect.Securities.IOrderProvider.

Definition at line 601 of file BrokerageTransactionHandler.cs.

◆ Run()

void QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.Run ( )
protected

Primary thread entry point to launch the transaction thread.

Definition at line 614 of file BrokerageTransactionHandler.cs.

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

◆ ProcessAsynchronousEvents()

virtual void QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.ProcessAsynchronousEvents ( )
virtual

Processes asynchronous events on the transaction handler's thread

Reimplemented in QuantConnect.Lean.Engine.TransactionHandlers.BacktestingTransactionHandler.

Definition at line 640 of file BrokerageTransactionHandler.cs.

Here is the caller graph for this function:

◆ ProcessSynchronousEvents()

virtual void QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.ProcessSynchronousEvents ( )
virtual

Processes all synchronous events that must take place before the next time loop for the algorithm

Implements QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler.

Reimplemented in QuantConnect.Lean.Engine.TransactionHandlers.BacktestingTransactionHandler.

Definition at line 648 of file BrokerageTransactionHandler.cs.

Here is the call graph for this function:

◆ AddOpenOrder()

void QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.AddOpenOrder ( Order  order,
IAlgorithm  algorithm 
)

Register an already open Order

Implements QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler.

Definition at line 702 of file BrokerageTransactionHandler.cs.

Here is the call graph for this function:

◆ Exit()

void QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.Exit ( )

Signal a end of thread request to stop monitoring the transactions.

Implements QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler.

Definition at line 733 of file BrokerageTransactionHandler.cs.

Here is the call graph for this function:

◆ HandleOrderRequest()

void QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.HandleOrderRequest ( OrderRequest  request)

Handles a generic order request

Parameters
requestOrderRequest to be handled
Returns
OrderResponse for request

Definition at line 755 of file BrokerageTransactionHandler.cs.

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

◆ RoundOffOrder()

decimal QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.RoundOffOrder ( Order  order,
Security  security 
)

Rounds off the order towards 0 to the nearest multiple of Lot Size

Definition at line 1607 of file BrokerageTransactionHandler.cs.

Here is the call graph for this function:

◆ RoundOrderPrices() [1/2]

void QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.RoundOrderPrices ( Order  order,
Security  security 
)
protected

Rounds the order prices to its security minimum price variation.

This procedure is needed to meet brokerage precision requirements.

Definition at line 1636 of file BrokerageTransactionHandler.cs.

◆ RoundOrderPrices() [2/2]

void QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.RoundOrderPrices ( Order  order,
Security  security,
bool  comboIsReady,
Dictionary< Order, Security orders 
)
protected

Rounds the order prices to its security minimum price variation.

This procedure is needed to meet brokerage precision requirements.

Definition at line 1650 of file BrokerageTransactionHandler.cs.

Member Data Documentation

◆ OrderEvents

IEnumerable<OrderEvent> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.OrderEvents => _orderEvents

Gets all order events

Definition at line 127 of file BrokerageTransactionHandler.cs.

◆ OrdersCount

int QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.OrdersCount => _totalOrderCount

Gets the current number of orders that have been processed

Definition at line 143 of file BrokerageTransactionHandler.cs.

◆ TimeSinceLastFill

virtual TimeSpan QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.TimeSinceLastFill
protected
Initial value:
=>
CurrentTimeUtc - new DateTime(Interlocked.Read(ref _lastFillTimeTicks))

Gets the amount of time since the last call to algorithm.Portfolio.ProcessFill(fill)

Definition at line 1596 of file BrokerageTransactionHandler.cs.

◆ CurrentTimeUtc

virtual DateTime QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.CurrentTimeUtc => DateTime.UtcNow
protected

Gets current time UTC. This is here to facilitate testing

Definition at line 1602 of file BrokerageTransactionHandler.cs.

Property Documentation

◆ _orderRequestQueue

IBusyCollection<OrderRequest> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler._orderRequestQueue
getsetprotected

OrderQueue holds the newly updated orders from the user algorithm waiting to be processed. Once orders are processed they are moved into the Orders queue awaiting the brokerage response.

Definition at line 61 of file BrokerageTransactionHandler.cs.

◆ _cancelPendingOrders

CancelPendingOrders QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler._cancelPendingOrders = new CancelPendingOrders()
getprotected

The _cancelPendingOrders instance will help to keep track of CancelPending orders and their Status

Definition at line 102 of file BrokerageTransactionHandler.cs.

◆ Orders

ConcurrentDictionary<int, Order> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.Orders
get

Gets the permanent storage for all orders

Definition at line 117 of file BrokerageTransactionHandler.cs.

◆ OrderTickets

ConcurrentDictionary<int, OrderTicket> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.OrderTickets
get

Gets the permanent storage for all order tickets

Definition at line 133 of file BrokerageTransactionHandler.cs.

◆ IsActive

bool QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.IsActive
get

Boolean flag indicating the Run thread method is busy. False indicates it is completely finished processing and ready to be terminated.

Definition at line 225 of file BrokerageTransactionHandler.cs.

Event Documentation

◆ NewOrderEvent

EventHandler<OrderEvent> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.NewOrderEvent

Event fired when there is a new OrderEvent

Definition at line 111 of file BrokerageTransactionHandler.cs.


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