Skip to content

ITransactionHandler

QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler

Bases: IOrderProcessor, IOrderEventProvider

Transaction handlers define how the transactions are processed and set the order fill information. The pass this information back to the algorithm portfolio and ensure the cash and portfolio are synchronized.

is_active

is_active: bool

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

orders

orders: ConcurrentDictionary[int, Order]

Gets the permanent storage for all orders

order_events

order_events: Iterable[OrderEvent]

Gets all order events

order_tickets

order_tickets: ConcurrentDictionary[int, OrderTicket]

Gets the permanent storage for all order tickets

new_order_event

new_order_event: _EventContainer[
    Callable[[Object, OrderEvent], Any], Any
]

Event fired when there is a new OrderEvent

orders_count

orders_count: int

Gets the current number of orders that have been processed

add_open_order

add_open_order(order: Order, algorithm: IAlgorithm) -> None

Register an already open Order

exit

exit() -> None

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

initialize

initialize(
    algorithm: IAlgorithm,
    brokerage: IBrokerage,
    result_handler: IResultHandler,
) -> None

Initializes the transaction handler for the specified algorithm using the specified brokerage implementation

process_synchronous_events

process_synchronous_events() -> None

Process any synchronous events from the primary algorithm thread.

get_open_orders

get_open_orders(
    filter: Callable[[Order], bool] = None,
) -> List[Order]

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

Parameters:

Name Type Description Default
filter Callable[[Order], bool]

Delegate used to filter the orders

None

Returns:

Type Description
List[Order]

All filtered open orders this order provider currently holds.

get_open_order_tickets

get_open_order_tickets(
    filter: Callable[[OrderTicket], bool] = None,
) -> Iterable[OrderTicket]

Gets and enumerable of opened OrderTicket matching the specified filter

Parameters:

Name Type Description Default
filter Callable[[OrderTicket], bool]

The filter predicate used to find the required order tickets. If null is specified then all tickets are returned

None

Returns:

Type Description
Iterable[OrderTicket]

An enumerable of opened OrderTicket matching the specified filter.

get_order_by_id

get_order_by_id(order_id: int) -> Order

Get the order by its id

Parameters:

Name Type Description Default
order_id int

Order id to fetch

required

Returns:

Type Description
Order

A clone of the order with the specified id, or null if no match is found.

get_orders

get_orders(
    filter: Callable[[Order], bool] = None,
) -> Iterable[Order]

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

Parameters:

Name Type Description Default
filter Callable[[Order], bool]

Delegate used to filter the orders

None

Returns:

Type Description
Iterable[Order]

All orders this order provider currently holds by the specified filter.

get_orders_by_brokerage_id

get_orders_by_brokerage_id(
    brokerage_id: str,
) -> List[Order]

Gets the Lean orders by its brokerage id

Parameters:

Name Type Description Default
brokerage_id str

The brokerage id to fetch

required

Returns:

Type Description
List[Order]

The orders matching the brokerage id, or null if no match is found.

get_order_ticket

get_order_ticket(order_id: int) -> OrderTicket

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

Parameters:

Name Type Description Default
order_id int

The order's id

required

Returns:

Type Description
OrderTicket

The order ticket with the specified id, or null if not found.

get_order_tickets

get_order_tickets(
    filter: Callable[[OrderTicket], bool] = None,
) -> Iterable[OrderTicket]

Gets and enumerable of OrderTicket matching the specified filter

Parameters:

Name Type Description Default
filter Callable[[OrderTicket], bool]

The filter predicate used to find the required order tickets. If null is specified then all tickets are returned

None

Returns:

Type Description
Iterable[OrderTicket]

An enumerable of OrderTicket matching the specified filter.

get_projected_holdings

get_projected_holdings(
    security: Security,
) -> ProjectedHoldings

Calculates the projected holdings for the specified security based on the current open orders.

Parameters:

Name Type Description Default
security Security

The security

required

Returns:

Type Description
ProjectedHoldings

The projected holdings for the specified security, which is the sum of the current holdings plus the sum of the open orders quantity.

process

process(request: OrderRequest) -> OrderTicket

Adds the specified order to be processed

Parameters:

Name Type Description Default
request OrderRequest

The OrderRequest to be processed

required

Returns:

Type Description
OrderTicket

The OrderTicket for the corresponding OrderRequest.order_id.