BacktestingResultHandler
QuantConnect.Lean.Engine.Results.BacktestingResultHandler
BacktestingResultHandler()
Bases: BaseResultsHandler, IResultHandler
Backtesting result handler passes messages back from the Lean to the User.
Creates a new instance
final_statistics
final_statistics: Dictionary[str, str]
A dictionary containing summary statistics
messages
messages: ConcurrentQueue[Packet]
Live packet messaging queue. Queue the messages here and send when the result queue is ready.
is_active
is_active: bool
Boolean flag indicating the thread is still active.
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.
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.
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_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.
add_to_log_store
add_to_log_store(message: str) -> None
Add message to LogStore
This codeEntityType is protected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Message to add |
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
Sends a packet communicating an update 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 |
configure_console_text_writer
configure_console_text_writer(
algorithm: IAlgorithm,
) -> None
Configures the Console.Out and Console.ErrorTextWriter instances. By default, we forward Console.WriteLine(string) to IAlgorithm.debug. This is perfect for running in the cloud, but since they're processed asynchronously, the ordering of these messages with respect to Log messages is broken. This can lead to differences in regression test logs based solely on the ordering of messages. To disable this forwarding, set "forward-console-messages" to false in the configuration.
This codeEntityType is protected.
debug_message
debug_message(message: str) -> None
Send a debug message back to the browser console.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Message we'd like shown in console. |
required |
error_message
error_message(message: str, stacktrace: str = ...) -> None
Send an error message back to the browser highlighted in red with a stacktrace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
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.
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 |
order_event
order_event(new_event: OrderEvent) -> None
Handle order event
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_event
|
OrderEvent
|
Event to process |
required |
process_synchronous_events
process_synchronous_events(
force_process: bool = False,
) -> None
Process the synchronous result events, sampling and message reading. This method is triggered from the algorithm manager thread.
run
run() -> None
The main processing method steps through the messaging queue and processes the messages one by one.
This codeEntityType is protected.
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 the current runtime statistics of the algorithm. These are banner/title statistics which show at the top of the live trading results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Runtime headline statistic name |
required |
value
|
str
|
Runtime headline statistic value |
required |
sample
sample(
chart_name: str,
series_name: str,
series_index: int,
series_type: SeriesType,
value: ISeriesPoint,
unit: str = "$",
) -> None
Add a sample to the chart specified by the chart_name, and series_name.
This codeEntityType is protected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chart_name
|
str
|
String chart name to place the sample. |
required |
series_index
|
int
|
Type of chart we should create if it doesn't already exist. |
required |
series_name
|
str
|
Series name for the chart. |
required |
series_type
|
SeriesType
|
Series type for the chart. |
required |
value
|
ISeriesPoint
|
Value for the chart sample. |
required |
unit
|
str
|
Unit of the sample |
'$'
|
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_range
sample_range(updates: List[Chart]) -> None
Add a range of samples from the users algorithms to the end of our current list.
This codeEntityType is protected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
updates
|
List[Chart]
|
Chart updates since the last request. |
required |
send_final_result
send_final_result() -> None
Send a final analysis result back to the IDE.
This codeEntityType is protected.
send_status_update
send_status_update(
status: AlgorithmStatus, message: str = ...
) -> None
Send an algorithm status update to the browser.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
AlgorithmStatus
|
Status enum value. |
required |
message
|
str
|
Additional optional status message. |
...
|
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_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 |
split_packets
split_packets(
delta_charts: Dictionary[str, Chart],
delta_orders: Dictionary[int, Order],
runtime_statistics: SortedDictionary[str, str],
progress: float,
server_statistics: Dictionary[str, str],
) -> Iterable[BacktestResultPacket]
Run over all the data and break it into smaller packets to ensure they all arrive at the terminal
statistics_results
statistics_results() -> StatisticsResults
Calculates and gets the current statistics for the algorithm
Returns:
| Type | Description |
|---|---|
StatisticsResults
|
The current statistics. |
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 |
system_debug_message
system_debug_message(message: str) -> None
Send a system debug message back to the browser console.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Message we'd like shown in console. |
required |
on_securities_changed
on_securities_changed(changes: SecurityChanges) -> None
Event fired each time that we add/remove securities from the data feed
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.
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. |
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.
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.
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_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. |
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 |
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.