Skip to content

BrokerageTransactionHandler

QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler

Bases: Object, ITransactionHandler

Transaction handler for all brokerages

new_order_event

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

Event fired when there is a new OrderEvent

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

orders_count

orders_count: int

Gets the current number of orders that have been processed

is_active

is_active: bool

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

time_since_last_fill

time_since_last_fill: timedelta

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

This codeEntityType is protected.

current_time_utc

current_time_utc: datetime

Gets current time UTC. This is here to facilitate testing

This codeEntityType is protected.

round_order_prices

round_order_prices(
    order: Order, security: Security
) -> None
round_order_prices(
    order: Order,
    security: Security,
    combo_is_ready: bool,
    orders: Dictionary[Order, Security],
) -> None

Rounds the order prices to its security minimum price variation.

add_open_order

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

Register an already open Order

add_order

add_order(request: SubmitOrderRequest) -> OrderTicket

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

Parameters:

Name Type Description Default
request SubmitOrderRequest

A request detailing the order to be submitted

required

Returns:

Type Description
OrderTicket

New unique, increasing orderid.

cancel_order

cancel_order(request: CancelOrderRequest) -> OrderTicket

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

Parameters:

Name Type Description Default
request CancelOrderRequest

Request containing the specific order id to remove

required

exit

exit() -> None

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

get_open_orders

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

Gets open orders matching the specified filter

Parameters:

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

Delegate used to filter the orders

None

Returns:

Type Description
List[Order]

All 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

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 order by its brokerage id

Parameters:

Name Type Description Default
brokerage_id str

The brokerage id to fetch

required

Returns:

Type Description
List[Order]

The first order 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

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.

handle_order_request

handle_order_request(request: OrderRequest) -> None

Handles a generic order request

Parameters:

Name Type Description Default
request OrderRequest

OrderRequest to be handled

required

Returns:

Type Description
None

OrderResponse for request.

initialize

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

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

Parameters:

Name Type Description Default
algorithm IAlgorithm

The algorithm instance

required
brokerage IBrokerage

The brokerage implementation to process orders and fire fill events

required
result_handler IResultHandler
required

initialize_transaction_thread

initialize_transaction_thread() -> None

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

This codeEntityType is protected.

process

process(request: OrderRequest) -> OrderTicket

Adds the specified order to be processed

Parameters:

Name Type Description Default
request OrderRequest

The order to be processed

required

process_asynchronous_events

process_asynchronous_events() -> None

Processes asynchronous events on the transaction handler's thread

process_synchronous_events

process_synchronous_events() -> None

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

round_off_order

round_off_order(order: Order, security: Security) -> float

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

run

run(thread_id: int) -> None

Primary thread entry point to launch the transaction thread.

This codeEntityType is protected.

update_order

update_order(request: UpdateOrderRequest) -> OrderTicket

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

Parameters:

Name Type Description Default
request UpdateOrderRequest

Request detailing how the order should be updated

required

wait_for_order_submission

wait_for_order_submission(ticket: OrderTicket) -> None

Wait for the order to be handled by the _processingThreads

This codeEntityType is protected.

Parameters:

Name Type Description Default
ticket OrderTicket

The OrderTicket expecting to be submitted

required