Lean
$LEAN_TAG$
|
Transaction handler for all brokerages More...
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< OrderTicket > | GetOrderTickets (Func< OrderTicket, bool > filter=null) |
Gets and enumerable of OrderTicket matching the specified filter More... | |
IEnumerable< OrderTicket > | GetOpenOrderTickets (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< Order > | GetOrdersByBrokerageId (string brokerageId) |
Gets the order by its brokerage id More... | |
IEnumerable< Order > | GetOrders (Func< Order, bool > filter=null) |
Gets all orders matching the specified filter. Specifying null will return an enumerable of all orders. More... | |
List< Order > | GetOpenOrders (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< OrderEvent > | OrderEvents => _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... | |
Protected Attributes | |
IBusyCollection< OrderRequest > | _orderRequestQueue |
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... | |
readonly CancelPendingOrders | _cancelPendingOrders = new CancelPendingOrders() |
The _cancelPendingOrders instance will help to keep track of CancelPending orders and their Status More... | |
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 | |
ConcurrentDictionary< int, Order > | Orders [get] |
Gets the permanent storage for all orders More... | |
ConcurrentDictionary< int, OrderTicket > | OrderTickets [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, Order > | Orders [get] |
Gets the permanent storage for all orders More... | |
IEnumerable< OrderEvent > | OrderEvents [get] |
Gets all order events More... | |
ConcurrentDictionary< int, OrderTicket > | OrderTickets [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< OrderEvent > | NewOrderEvent |
Event fired when there is a new OrderEvent More... | |
Events inherited from QuantConnect.Securities.IOrderEventProvider | |
EventHandler< OrderEvent > | NewOrderEvent |
Event fired when there is a new OrderEvent More... | |
Transaction handler for all brokerages
Definition at line 37 of file BrokerageTransactionHandler.cs.
|
virtual |
Creates a new BrokerageTransactionHandler to process orders using the specified brokerage implementation
algorithm | The algorithm instance |
brokerage | The 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 150 of file BrokerageTransactionHandler.cs.
|
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 214 of file BrokerageTransactionHandler.cs.
OrderTicket QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.Process | ( | OrderRequest | request | ) |
Adds the specified order to be processed
request | The order to be processed |
Implements QuantConnect.Securities.IOrderProcessor.
Definition at line 232 of file BrokerageTransactionHandler.cs.
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.
request | A request detailing the order to be submitted |
Definition at line 262 of file BrokerageTransactionHandler.cs.
|
protectedvirtual |
Wait for the order to be handled by the _processingThread
ticket | The OrderTicket expecting to be submitted |
Reimplemented in QuantConnect.Lean.Engine.TransactionHandlers.BacktestingTransactionHandler.
Definition at line 334 of file BrokerageTransactionHandler.cs.
OrderTicket QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.UpdateOrder | ( | UpdateOrderRequest | request | ) |
Update an order yet to be filled such as stop or limit orders.
request | Request detailing how the order should be updated |
Does not apply if the order is already fully filled
Definition at line 349 of file BrokerageTransactionHandler.cs.
OrderTicket QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.CancelOrder | ( | CancelOrderRequest | request | ) |
Remove this order from outstanding queue: user is requesting a cancel.
request | Request containing the specific order id to remove |
Definition at line 431 of file BrokerageTransactionHandler.cs.
IEnumerable<OrderTicket> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.GetOrderTickets | ( | Func< OrderTicket, bool > | filter = null | ) |
Gets and enumerable of OrderTicket matching the specified filter
filter | The filter predicate used to find the required order tickets |
Implements QuantConnect.Securities.IOrderProvider.
Definition at line 506 of file BrokerageTransactionHandler.cs.
IEnumerable<OrderTicket> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.GetOpenOrderTickets | ( | Func< OrderTicket, bool > | filter = null | ) |
Gets and enumerable of opened OrderTicket matching the specified filter
filter | The filter predicate used to find the required order tickets |
Implements QuantConnect.Securities.IOrderProvider.
Definition at line 516 of file BrokerageTransactionHandler.cs.
OrderTicket QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.GetOrderTicket | ( | int | orderId | ) |
Gets the order ticket for the specified order id. Returns null if not found
orderId | The order's id |
Implements QuantConnect.Securities.IOrderProvider.
Definition at line 526 of file BrokerageTransactionHandler.cs.
Order QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.GetOrderById | ( | int | orderId | ) |
Get the order by its id
orderId | Order id to fetch |
Implements QuantConnect.Securities.IOrderProvider.
Definition at line 540 of file BrokerageTransactionHandler.cs.
List<Order> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.GetOrdersByBrokerageId | ( | string | brokerageId | ) |
Gets the order by its brokerage id
brokerageId | The brokerage id to fetch |
Implements QuantConnect.Securities.IOrderProvider.
Definition at line 557 of file BrokerageTransactionHandler.cs.
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.
filter | Delegate used to filter the orders |
Implements QuantConnect.Securities.IOrderProvider.
Definition at line 583 of file BrokerageTransactionHandler.cs.
List<Order> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.GetOpenOrders | ( | Func< Order, bool > | filter = null | ) |
Gets open orders matching the specified filter
filter | Delegate used to filter the orders |
Implements QuantConnect.Securities.IOrderProvider.
Definition at line 598 of file BrokerageTransactionHandler.cs.
|
protected |
Primary thread entry point to launch the transaction thread.
Definition at line 611 of file BrokerageTransactionHandler.cs.
|
virtual |
Processes asynchronous events on the transaction handler's thread
Reimplemented in QuantConnect.Lean.Engine.TransactionHandlers.BacktestingTransactionHandler.
Definition at line 637 of file BrokerageTransactionHandler.cs.
|
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 645 of file BrokerageTransactionHandler.cs.
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 699 of file BrokerageTransactionHandler.cs.
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 725 of file BrokerageTransactionHandler.cs.
void QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.HandleOrderRequest | ( | OrderRequest | request | ) |
Handles a generic order request
request | OrderRequest to be handled |
Definition at line 747 of file BrokerageTransactionHandler.cs.
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 1596 of file BrokerageTransactionHandler.cs.
|
protected |
Rounds the order prices to its security minimum price variation.
This procedure is needed to meet brokerage precision requirements.
Definition at line 1625 of file BrokerageTransactionHandler.cs.
|
protected |
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 60 of file BrokerageTransactionHandler.cs.
|
protected |
The _cancelPendingOrders instance will help to keep track of CancelPending orders and their Status
Definition at line 101 of file BrokerageTransactionHandler.cs.
IEnumerable<OrderEvent> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.OrderEvents => _orderEvents |
Gets all order events
Definition at line 126 of file BrokerageTransactionHandler.cs.
int QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.OrdersCount => _totalOrderCount |
Gets the current number of orders that have been processed
Definition at line 142 of file BrokerageTransactionHandler.cs.
|
protected |
Gets the amount of time since the last call to algorithm.Portfolio.ProcessFill(fill)
Definition at line 1585 of file BrokerageTransactionHandler.cs.
|
protected |
Gets current time UTC. This is here to facilitate testing
Definition at line 1591 of file BrokerageTransactionHandler.cs.
|
get |
Gets the permanent storage for all orders
Definition at line 116 of file BrokerageTransactionHandler.cs.
|
get |
Gets the permanent storage for all order tickets
Definition at line 132 of file BrokerageTransactionHandler.cs.
|
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 224 of file BrokerageTransactionHandler.cs.
EventHandler<OrderEvent> QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.NewOrderEvent |
Event fired when there is a new OrderEvent
Definition at line 110 of file BrokerageTransactionHandler.cs.