Skip to content

BaseResultsHandler

QuantConnect.Lean.Engine.Results.BaseResultsHandler

BaseResultsHandler()

Bases: Object

Provides base functionality to the implementations of IResultHandler

Creates a new instance

This codeEntityType is protected.

STRATEGY_EQUITY_KEY

STRATEGY_EQUITY_KEY: str = 'Strategy Equity'

String message saying: Strategy Equity

EQUITY_KEY

EQUITY_KEY: str = 'Equity'

String message saying: Equity

RETURN_KEY

RETURN_KEY: str = 'Return'

String message saying: Return

BENCHMARK_KEY

BENCHMARK_KEY: str = 'Benchmark'

String message saying: Benchmark

DRAWDOWN_KEY

DRAWDOWN_KEY: str = 'Drawdown'

String message saying: Drawdown

PORTFOLIO_TURNOVER_KEY

PORTFOLIO_TURNOVER_KEY: str = 'Portfolio Turnover'

String message saying: PortfolioTurnover

PORTFOLIO_MARGIN_KEY

PORTFOLIO_MARGIN_KEY: str = 'Portfolio Margin'

String message saying: Portfolio Margin

ASSETS_SALES_VOLUME_KEY

ASSETS_SALES_VOLUME_KEY: str = 'Assets Sales Volume'

String message saying: Portfolio Margin

main_update_interval

main_update_interval: timedelta

The main loop update interval

This codeEntityType is protected.

chart_update_interval

chart_update_interval: timedelta

The chart update interval

This codeEntityType is protected.

last_delta_order_position

last_delta_order_position: int

The last position consumed from the ITransactionHandler.order_events by get_delta_orders

This codeEntityType is protected.

last_delta_order_events_position

last_delta_order_events_position: int

The last position consumed from the ITransactionHandler.order_events while determining delta order events

This codeEntityType is protected.

serializer_settings

serializer_settings: Any

Serializer settings to use

This codeEntityType is protected.

current_algorithm_equity

current_algorithm_equity: Bar

The current aggregated equity bar for sampling. It will be aggregated with values from the get_portfolio_value

This codeEntityType is protected.

is_active

is_active: bool

Boolean flag indicating the thread is still active.

messages

messages: ConcurrentQueue[Packet]

Live packet messaging queue. Queue the messages here and send when the result queue is ready.

charts

charts: ConcurrentDictionary[str, Chart]

Storage for the price and equity charts of the live results.

exit_triggered

exit_triggered: bool

True if the exit has been triggered

This codeEntityType is protected.

exit_event

exit_event: ManualResetEvent

Event set when exit is triggered

This codeEntityType is protected.

log_store

log_store: List[LogEntry]

The log store instance

This codeEntityType is protected.

algorithm_performance_charts

algorithm_performance_charts: List[str]

Algorithms performance related chart names

This codeEntityType is protected.

chart_lock

chart_lock: Object

Lock to be used when accessing the chart collection

This codeEntityType is protected.

project_id

project_id: int

The algorithm project id

This codeEntityType is protected.

ram_allocation

ram_allocation: str

The maximum amount of RAM (in MB) this algorithm is allowed to utilize

This codeEntityType is protected.

compile_id

compile_id: str

The algorithm unique compilation id

This codeEntityType is protected.

algorithm_id

algorithm_id: str

The algorithm job id. This is the deploy id for live, backtesting id for backtesting

This codeEntityType is protected.

start_time

start_time: datetime

The result handler start time

This codeEntityType is protected.

runtime_statistics

runtime_statistics: Dictionary[str, str]

Customizable dynamic statistics IAlgorithm.runtime_statistics

This codeEntityType is protected.

state

state: Dictionary[str, str]

State of the algorithm

This codeEntityType is protected.

messaging_handler

messaging_handler: IMessagingHandler

The handler responsible for communicating messages to listeners

This codeEntityType is protected.

transaction_handler

transaction_handler: ITransactionHandler

The transaction handler used to get the algorithms Orders information

This codeEntityType is protected.

starting_portfolio_value

starting_portfolio_value: float

The algorithms starting portfolio value. Used to calculate the portfolio return

This codeEntityType is protected.

algorithm

algorithm: IAlgorithm

The algorithm instance

This codeEntityType is protected.

algorithm_currency_symbol

algorithm_currency_symbol: str

Algorithm currency symbol, used in charting

This codeEntityType is protected.

daily_portfolio_value

daily_portfolio_value: float

Closing portfolio value. Used to calculate daily performance.

This codeEntityType is protected.

cumulative_max_portfolio_value

cumulative_max_portfolio_value: float

Cumulative max portfolio value. Used to calculate drawdown underwater.

This codeEntityType is protected.

resample_period

resample_period: timedelta

Sampling period for timespans between resamples of the charting equity.

This codeEntityType is protected.

notification_period

notification_period: timedelta

How frequently the backtests push messages to the browser.

This codeEntityType is protected.

results_destination_folder

results_destination_folder: str

Directory location to store results

This codeEntityType is protected.

map_file_provider

map_file_provider: IMapFileProvider

The map file provider instance to use

This codeEntityType is protected.

generate_statistics_results

generate_statistics_results(
    charts: Dictionary[str, Chart],
    profit_loss: SortedDictionary[datetime, float] = None,
    estimated_strategy_capacity: CapacityEstimate = None,
) -> StatisticsResults
generate_statistics_results(
    estimated_strategy_capacity: CapacityEstimate = None,
) -> StatisticsResults

Signature descriptions:

  • Will generate the statistics results and update the provided runtime statistics

  • Calculates and gets the current statistics for the algorithm. It will use the current charts and profit loss information calculated from the current transaction record to generate the results.

Returns:

Type Description
StatisticsResults

The current statistics.

sample

sample(time: Union[datetime, date]) -> None
sample(
    chart_name: str,
    series_name: str,
    series_index: int,
    series_type: SeriesType,
    value: ISeriesPoint,
    unit: str = "$",
) -> None

Signature descriptions:

  • Samples portfolio equity, benchmark, and daily performance Called by scheduled event every night at midnight algorithm time

  • Add a sample to the chart specified by the chart_name, and series_name.

Parameters:

Name Type Description Default
time Optional[Union[datetime, date]]

Current UTC time in the AlgorithmManager loop

None
chart_name Optional[str]

String chart name to place the sample.

None
series_name Optional[str]

Series name for the chart.

None
series_index Optional[int]

Series chart index - which chart should this series belong

None
series_type Optional[SeriesType]

Series type for the chart.

None
value Optional[ISeriesPoint]

Value for the chart sample.

None
unit Optional[str]

Unit for the chart axis

'$'

add_to_log_store

add_to_log_store(message: str) -> None

Save an algorithm message to the log store. Uses a different timestamped method of adding messaging to interweve debug and logging messages.

This codeEntityType is protected.

Parameters:

Name Type Description Default
message str

String message to store

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

exit

exit() -> None

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

get_algorithm_runtime_statistics

get_algorithm_runtime_statistics(
    summary: Dictionary[str, str],
    capacity_estimate: CapacityEstimate = None,
) -> SortedDictionary[str, str]

Gets the algorithm runtime statistics

This codeEntityType is protected.

get_algorithm_state

get_algorithm_state(
    end_time: Optional[datetime] = None,
) -> Dictionary[str, str]

Gets the algorithm state data

This codeEntityType is protected.

get_benchmark_value

get_benchmark_value() -> float

Gets the current benchmark value

This codeEntityType is protected.

get_delta_orders

get_delta_orders(
    order_events_start_position: int,
    should_stop: Callable[[int], bool],
) -> Dictionary[int, Order]

Gets the orders generated starting from the provided ITransactionHandler.order_events position

This codeEntityType is protected.

Returns:

Type Description
Dictionary[int, Order]

The delta orders.

get_net_return

get_net_return() -> float

Gets the algorithm net return

This codeEntityType is protected.

get_portfolio_value

get_portfolio_value() -> float

Gets the current portfolio value

This codeEntityType is protected.

get_results_path

get_results_path(filename: str) -> str

Gets the full path for a results file

This codeEntityType is protected.

Parameters:

Name Type Description Default
filename str

The filename to add to the path

required

Returns:

Type Description
str

The full path, including the filename.

get_server_statistics

get_server_statistics(
    utc_now: Union[datetime, date],
) -> Dictionary[str, str]

Gets the current Server statistics

This codeEntityType is protected.

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

on_securities_changed

on_securities_changed(changes: SecurityChanges) -> None

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

order_event

order_event(new_event: OrderEvent) -> None

New order event for the algorithm

Parameters:

Name Type Description Default
new_event OrderEvent

New event details

required

process_algorithm_logs

process_algorithm_logs(
    message_queue_limit: Optional[int] = None,
) -> None

Processes algorithm logs. Logs of the same type are batched together one per line and are sent out

This codeEntityType is protected.

purge_queue

purge_queue() -> None

Purge/clear any outstanding messages in message queue.

This codeEntityType is protected.

run

run() -> None

Result handler update method

This codeEntityType is protected.

sample_benchmark

sample_benchmark(
    time: Union[datetime, date], value: float
) -> None

Sample the current benchmark performance directly with a time-value pair.

This codeEntityType is protected.

Parameters:

Name Type Description Default
time Union[datetime, date]

Time of the sample.

required
value float

Current benchmark value.

required

sample_capacity

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

Sample estimated strategy capacity

This codeEntityType is protected.

Parameters:

Name Type Description Default
time Union[datetime, date]

Time of the sample

required

sample_drawdown

sample_drawdown(
    time: Union[datetime, date],
    current_portfolio_value: float,
) -> None

Sample drawdown of equity of the strategy

This codeEntityType is protected.

Parameters:

Name Type Description Default
time Union[datetime, date]

Time of the sample

required
current_portfolio_value float

Current equity value

required

sample_equity

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

Sample the current equity of the strategy directly with time and using the current algorithm equity value in current_algorithm_equity

This codeEntityType is protected.

Parameters:

Name Type Description Default
time Union[datetime, date]

Equity candlestick end time

required

sample_exposure

sample_exposure(
    time: Union[datetime, date],
    current_portfolio_value: float,
) -> None

Sample portfolio exposure long/short ratios by security type

This codeEntityType is protected.

Parameters:

Name Type Description Default
time Union[datetime, date]

Time of the sample

required
current_portfolio_value float

Current value of the portfolio

required

sample_performance

sample_performance(
    time: Union[datetime, date], value: float
) -> None

Sample the current daily performance directly with a time-value pair.

This codeEntityType is protected.

Parameters:

Name Type Description Default
time Union[datetime, date]

Time of the sample.

required
value float

Current daily performance value.

required

sample_portfolio_turnover

sample_portfolio_turnover(
    time: Union[datetime, date],
    current_portfolio_value: float,
) -> None

Sample portfolio turn over of the strategy

This codeEntityType is protected.

Parameters:

Name Type Description Default
time Union[datetime, date]

Time of the sample

required
current_portfolio_value float

Current equity value

required

sample_sales_volume

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

Sample assets sales volume

This codeEntityType is protected.

Parameters:

Name Type Description Default
time Union[datetime, date]

Time of the sample

required

save_logs

save_logs(id: str, logs: List[LogEntry]) -> str

Returns the location of the logs

Parameters:

Name Type Description Default
id str

Id that will be incorporated into the algorithm log name

required
logs List[LogEntry]

The logs to save

required

Returns:

Type Description
str

The path to the logs.

save_results

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

Save the results to disk

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 list of security asset types the algorithm uses to browser.

set_algorithm

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

Set the Algorithm instance for ths result.

Parameters:

Name Type Description Default
algorithm IAlgorithm

Algorithm we're working on.

required
starting_portfolio_value float

Algorithm starting capital for statistics calculations

required

set_algorithm_state

set_algorithm_state(error: str, stack: str) -> None

Sets the algorithm state data

This codeEntityType is protected.

stop_update_runner

stop_update_runner() -> None

Stops the update runner task

This codeEntityType is protected.

store_insights

store_insights() -> None

Save insight results to persistent storage

This codeEntityType is protected.

store_order_events

store_order_events(
    utc_time: Union[datetime, date],
    order_events: List[OrderEvent],
) -> None

Stores the order events

This codeEntityType is protected.

Parameters:

Name Type Description Default
utc_time Union[datetime, date]

The utc date associated with these order events

required
order_events List[OrderEvent]

The order events to store

required

store_result

store_result(packet: Packet) -> None

Save the snapshot of the total results to storage.

This codeEntityType is protected.

Parameters:

Name Type Description Default
packet Packet

Packet to store.

required

summary_statistic

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

Sets or updates a custom summary statistic

This codeEntityType is protected.

Parameters:

Name Type Description Default
name str

The statistic name

required
value str

The statistic value

required

total_trades_count

total_trades_count() -> int

Helper method to get the total trade count statistic

This codeEntityType is protected.

update_algorithm_equity

update_algorithm_equity() -> None

Updates the current equity bar with the current equity value from get_portfolio_value

This codeEntityType is protected.

update_benchmark_value

update_benchmark_value(
    time: Union[datetime, date], force: bool = False
) -> None

This codeEntityType is protected.

update_portfolio_values

update_portfolio_values(
    time: Union[datetime, date], force: bool = False
) -> None

This codeEntityType is protected.