Skip to content

IResultHandler

QuantConnect.Lean.Engine.Results.IResultHandler

Bases: IStatisticsService

Handle the results of the backtest: where should we send the profit, portfolio updates: Backtester or the Live trading platform:

messages

messages: ConcurrentQueue[Packet]

Put messages to process into the queue so they are processed by this thread.

is_active

is_active: bool

Boolean flag indicating the result hander thread is busy. False means it has completely finished and ready to dispose.

deployment_details

deployment_details: ReadOnlyExtendedDictionary[str, str]

Read only view of the deployment details, see add_deployment_detail. Shared with the algorithm

add_deployment_detail

add_deployment_detail(key: str, value: str) -> None

Adds or updates a deployment detail entry. Key value pairs the brokerage, data queue handler or any other component wants to share with the user, through the results, and the algorithm, for example account information. Sensitive data, like credentials, should never be added

Parameters:

Name Type Description Default
key str

The deployment detail key

required
value str

The deployment detail value

required

algorithm_name_updated

algorithm_name_updated(name: str) -> None

Handles updates to the algorithm's name

Parameters:

Name Type Description Default
name str

The new name

required

algorithm_tags_updated

algorithm_tags_updated(tags: HashSet[str]) -> None

Handles updates to the algorithm's tags

Parameters:

Name Type Description Default
tags HashSet[str]

The new tags

required

brokerage_message

brokerage_message(
    brokerage_message_event: BrokerageMessageEvent,
) -> None

Process brokerage message events

Parameters:

Name Type Description Default
brokerage_message_event BrokerageMessageEvent

The brokerage message event

required

debug_message

debug_message(message: str) -> None

Process debug messages with the preconfigured settings.

Parameters:

Name Type Description Default
message str

String debug message

required

error_message

error_message(error: str, stacktrace: str = ...) -> None

Send an error message back to the browser highlighted in red with a stacktrace.

Parameters:

Name Type Description Default
error str

Error message we'd like shown in console.

required
stacktrace str

Stacktrace information string

...

exit

exit() -> None

Terminate the result thread and apply any required exit procedures like sending final results.

initialize

initialize(
    parameters: ResultHandlerInitializeParameters,
) -> None

Initialize the result handler with this result packet.

Parameters:

Name Type Description Default
parameters ResultHandlerInitializeParameters

DTO parameters class to initialize a result handler

required

log_message

log_message(message: str) -> None

Send a logging message to the log list for storage.

Parameters:

Name Type Description Default
message str

Message we'd in the log.

required

on_securities_changed

on_securities_changed(changes: SecurityChanges) -> None

Event fired each time that we add/remove securities from the data feed

on_warmup_finished

on_warmup_finished() -> None

Event fired when the algorithm's warm-up period finishes, right before the algorithm's IAlgorithm.on_warmup_finished callback is triggered

order_event

order_event(new_event: OrderEvent) -> None

Send a new order event.

Parameters:

Name Type Description Default
new_event OrderEvent

Update, processing or cancellation of an order, update the IDE in live mode or ignore in backtesting.

required

process_synchronous_events

process_synchronous_events(
    force_process: bool = False,
) -> None

Process any synchronous events in here that are primarily triggered from the algorithm loop

runtime_error

runtime_error(message: str, stacktrace: str = ...) -> None

Send a runtime error message back to the browser highlighted with in red

Parameters:

Name Type Description Default
message str

Error message.

required
stacktrace str

Stacktrace information string

...

runtime_statistic

runtime_statistic(key: str, value: str) -> None

Set a dynamic runtime statistic to show in the (live) algorithm header

Parameters:

Name Type Description Default
key str

Runtime headline statistic name

required
value str

Runtime headline statistic value

required

sample

sample(time: Union[datetime, date]) -> None

Method to update the IResultHandler with various performance metrics. Called once a day by scheduled event in AlgorithmManager

Parameters:

Name Type Description Default
time Union[datetime, date]

Current time

required

save_results

save_results(name: str, result: Result) -> None

Save the results

Parameters:

Name Type Description Default
name str

The name of the results

required
result Result

The results to save

required

security_type

security_type(types: List[SecurityType]) -> None

Send a list of security types to the browser

Parameters:

Name Type Description Default
types List[SecurityType]

Security types list inside algorithm

required

send_status_update

send_status_update(
    status: AlgorithmStatus, message: str = ...
) -> None

Send a algorithm status update to the user of the algorithms running state.

Parameters:

Name Type Description Default
status AlgorithmStatus

Status enum of the algorithm.

required
message str

Optional string message describing reason for status change.

...

set_algorithm

set_algorithm(
    algorithm: IAlgorithm, starting_portfolio_value: float
) -> None

Set the algorithm of the result handler after its been initialized.

Parameters:

Name Type Description Default
algorithm IAlgorithm

Algorithm object matching IAlgorithm interface

required
starting_portfolio_value float

Algorithm starting capital for statistics calculations

required

system_debug_message

system_debug_message(message: str) -> None

Process system debug messages with the preconfigured settings.

Parameters:

Name Type Description Default
message str

String debug message

required

set_summary_statistic

set_summary_statistic(name: str, value: str) -> None

Sets or updates a custom summary statistic

Parameters:

Name Type Description Default
name str

The statistic name

required
value str

The statistic value

required

statistics_results

statistics_results() -> StatisticsResults

Calculates and gets the current statistics for the algorithm

Returns:

Type Description
StatisticsResults

The current statistics.