Lean
$LEAN_TAG$
|
Provides base functionality to the implementations of IResultHandler More...
Public Member Functions | |
virtual void | OrderEvent (OrderEvent newEvent) |
New order event for the algorithm More... | |
virtual void | Exit () |
Terminate the result thread and apply any required exit procedures like sending final results More... | |
virtual void | Initialize (ResultHandlerInitializeParameters parameters) |
Initialize the result handler with this result packet. More... | |
virtual void | OnSecuritiesChanged (SecurityChanges changes) |
Event fired each time that we add/remove securities from the data feed More... | |
virtual string | SaveLogs (string id, List< LogEntry > logs) |
Returns the location of the logs More... | |
virtual void | SaveResults (string name, Result result) |
Save the results to disk More... | |
virtual void | Sample (DateTime time) |
Samples portfolio equity, benchmark, and daily performance Called by scheduled event every night at midnight algorithm time More... | |
Public Attributes | |
bool | IsActive => _updateRunner != null && _updateRunner.IsAlive |
Boolean flag indicating the thread is still active. More... | |
Protected Member Functions | |
BaseResultsHandler () | |
Creates a new instance More... | |
virtual Dictionary< string, string > | GetServerStatistics (DateTime utcNow) |
Gets the current Server statistics More... | |
virtual void | StoreOrderEvents (DateTime utcTime, List< OrderEvent > orderEvents) |
Stores the order events More... | |
virtual void | StoreInsights () |
Save insight results to persistent storage More... | |
virtual Dictionary< int, Order > | GetDeltaOrders (int orderEventsStartPosition, Func< int, bool > shouldStop) |
Gets the orders generated starting from the provided ITransactionHandler.OrderEvents position More... | |
abstract void | Run () |
Result handler update method More... | |
string | GetResultsPath (string filename) |
Gets the full path for a results file More... | |
void | PurgeQueue () |
Purge/clear any outstanding messages in message queue. More... | |
void | StopUpdateRunner () |
Stops the update runner task More... | |
decimal | GetNetReturn () |
Gets the algorithm net return More... | |
abstract void | StoreResult (Packet packet) |
Save the snapshot of the total results to storage. More... | |
virtual decimal | GetPortfolioValue () |
Gets the current portfolio value More... | |
virtual decimal | GetBenchmarkValue (DateTime time) |
Gets the current benchmark value More... | |
virtual void | SampleEquity (DateTime time) |
Sample the current equity of the strategy directly with time and using the current algorithm equity value in CurrentAlgorithmEquity More... | |
virtual void | SamplePerformance (DateTime time, decimal value) |
Sample the current daily performance directly with a time-value pair. More... | |
virtual void | SampleBenchmark (DateTime time, decimal value) |
Sample the current benchmark performance directly with a time-value pair. More... | |
virtual void | SampleDrawdown (DateTime time, decimal currentPortfolioValue) |
Sample drawdown of equity of the strategy More... | |
virtual void | SamplePortfolioTurnover (DateTime time, decimal currentPortfolioValue) |
Sample portfolio turn over of the strategy More... | |
virtual void | SampleSalesVolume (DateTime time) |
Sample assets sales volume More... | |
virtual void | SampleExposure (DateTime time, decimal currentPortfolioValue) |
Sample portfolio exposure long/short ratios by security type More... | |
virtual void | SampleCapacity (DateTime time) |
Sample estimated strategy capacity More... | |
abstract void | Sample (string chartName, string seriesName, int seriesIndex, SeriesType seriesType, ISeriesPoint value, string unit="$") |
Add a sample to the chart specified by the chartName, and seriesName. More... | |
SortedDictionary< string, string > | GetAlgorithmRuntimeStatistics (Dictionary< string, string > summary, CapacityEstimate capacityEstimate=null) |
Gets the algorithm runtime statistics More... | |
void | SetAlgorithmState (string error, string stack) |
Sets the algorithm state data More... | |
Dictionary< string, string > | GetAlgorithmState (DateTime? endTime=null) |
Gets the algorithm state data More... | |
StatisticsResults | GenerateStatisticsResults (Dictionary< string, Chart > charts, SortedDictionary< DateTime, decimal > profitLoss=null, CapacityEstimate estimatedStrategyCapacity=null) |
Will generate the statistics results and update the provided runtime statistics More... | |
int | TotalTradesCount () |
Helper method to get the total trade count statistic More... | |
StatisticsResults | GenerateStatisticsResults (CapacityEstimate estimatedStrategyCapacity=null) |
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. More... | |
virtual void | AddToLogStore (string message) |
Save an algorithm message to the log store. Uses a different timestamped method of adding messaging to interweve debug and logging messages. More... | |
void | ProcessAlgorithmLogs (int? messageQueueLimit=null) |
Processes algorithm logs. Logs of the same type are batched together one per line and are sent out More... | |
void | SummaryStatistic (string name, string value) |
Sets or updates a custom summary statistic More... | |
void | UpdateAlgorithmEquity () |
Updates the current equity bar with the current equity value from GetPortfolioValue More... | |
Protected Attributes | |
TimeSpan | ChartUpdateInterval = TimeSpan.FromMinutes(1) |
The chart update interval More... | |
int | LastDeltaOrderPosition |
The last position consumed from the ITransactionHandler.OrderEvents by GetDeltaOrders More... | |
int | LastDeltaOrderEventsPosition |
The last position consumed from the ITransactionHandler.OrderEvents while determining delta order events More... | |
volatile bool | ExitTriggered |
True if the exit has been triggered More... | |
IMessagingHandler | MessagingHandler |
The handler responsible for communicating messages to listeners More... | |
ITransactionHandler | TransactionHandler |
The transaction handler used to get the algorithms Orders information More... | |
decimal | DailyPortfolioValue |
Closing portfolio value. Used to calculate daily performance. More... | |
decimal | CumulativeMaxPortfolioValue |
Cumulative max portfolio value. Used to calculate drawdown underwater. More... | |
string | ResultsDestinationFolder |
Directory location to store results More... | |
Properties | |
virtual TimeSpan | MainUpdateInterval = TimeSpan.FromSeconds(3) [get] |
The main loop update interval More... | |
JsonSerializerSettings | SerializerSettings [get, set] |
Serializer settings to use More... | |
Bar | CurrentAlgorithmEquity [get, set] |
The current aggregated equity bar for sampling. It will be aggregated with values from the GetPortfolioValue More... | |
ConcurrentQueue< Packet > | Messages [get, set] |
Live packet messaging queue. Queue the messages here and send when the result queue is ready. More... | |
ConcurrentDictionary< string, Chart > | Charts [get, set] |
Storage for the price and equity charts of the live results. More... | |
ManualResetEvent | ExitEvent [get] |
Event set when exit is triggered More... | |
List< LogEntry > | LogStore [get] |
The log store instance More... | |
List< string > | AlgorithmPerformanceCharts = new List<string> { StrategyEquityKey, BenchmarkKey } [get] |
Algorithms performance related chart names More... | |
object | ChartLock [get] |
Lock to be used when accessing the chart collection More... | |
int | ProjectId [get, set] |
The algorithm project id More... | |
string | RamAllocation [get, set] |
The maximum amount of RAM (in MB) this algorithm is allowed to utilize More... | |
string | CompileId [get, set] |
The algorithm unique compilation id More... | |
string | AlgorithmId [get, set] |
The algorithm job id. This is the deploy id for live, backtesting id for backtesting More... | |
DateTime | StartTime [get] |
The result handler start time More... | |
Dictionary< string, string > | RuntimeStatistics [get] |
Customizable dynamic statistics IAlgorithm.RuntimeStatistics More... | |
Dictionary< string, string > | State [get, set] |
State of the algorithm More... | |
decimal | StartingPortfolioValue [get, set] |
The algorithms starting portfolio value. Used to calculate the portfolio return More... | |
virtual IAlgorithm | Algorithm [get, set] |
The algorithm instance More... | |
string | AlgorithmCurrencySymbol [get, set] |
Algorithm currency symbol, used in charting More... | |
TimeSpan | ResamplePeriod [get, set] |
Sampling period for timespans between resamples of the charting equity. More... | |
TimeSpan | NotificationPeriod [get, set] |
How frequently the backtests push messages to the browser. More... | |
IMapFileProvider | MapFileProvider [get, set] |
The map file provider instance to use More... | |
Provides base functionality to the implementations of IResultHandler
Definition at line 43 of file BaseResultsHandler.cs.
|
protected |
Creates a new instance
Definition at line 265 of file BaseResultsHandler.cs.
|
virtual |
New order event for the algorithm
newEvent | New event details |
Reimplemented in QuantConnect.Lean.Engine.Results.LiveTradingResultHandler, QuantConnect.Lean.Engine.Results.BacktestingResultHandler, and QuantConnect.Lean.Engine.Results.RegressionResultHandler.
Definition at line 301 of file BaseResultsHandler.cs.
|
virtual |
Terminate the result thread and apply any required exit procedures like sending final results
Reimplemented in QuantConnect.Lean.Engine.Results.LiveTradingResultHandler, QuantConnect.Lean.Engine.Results.BacktestingResultHandler, and QuantConnect.Lean.Engine.Results.RegressionResultHandler.
Definition at line 308 of file BaseResultsHandler.cs.
|
protectedvirtual |
Gets the current Server statistics
Definition at line 318 of file BaseResultsHandler.cs.
|
protectedvirtual |
Stores the order events
utcTime | The utc date associated with these order events |
orderEvents | The order events to store |
Reimplemented in QuantConnect.Lean.Engine.Results.LiveTradingResultHandler.
Definition at line 333 of file BaseResultsHandler.cs.
|
protectedvirtual |
Save insight results to persistent storage
Method called by the storing timer and on exit
Definition at line 352 of file BaseResultsHandler.cs.
|
protectedvirtual |
Gets the orders generated starting from the provided ITransactionHandler.OrderEvents position
Definition at line 379 of file BaseResultsHandler.cs.
|
virtual |
Initialize the result handler with this result packet.
parameters | DTO parameters class to initialize a result handler |
Reimplemented in QuantConnect.Lean.Engine.Results.LiveTradingResultHandler, and QuantConnect.Lean.Engine.Results.BacktestingResultHandler.
Definition at line 417 of file BaseResultsHandler.cs.
|
protectedpure virtual |
Result handler update method
Implemented in QuantConnect.Lean.Engine.Results.LiveTradingResultHandler, and QuantConnect.Lean.Engine.Results.BacktestingResultHandler.
|
protected |
Gets the full path for a results file
filename | The filename to add to the path |
Definition at line 455 of file BaseResultsHandler.cs.
|
virtual |
Event fired each time that we add/remove securities from the data feed
Reimplemented in QuantConnect.Lean.Engine.Results.LiveTradingResultHandler, and QuantConnect.Lean.Engine.Results.RegressionResultHandler.
Definition at line 463 of file BaseResultsHandler.cs.
|
virtual |
Returns the location of the logs
id | Id that will be incorporated into the algorithm log name |
logs | The logs to save |
Reimplemented in QuantConnect.Lean.Engine.Results.LiveTradingResultHandler.
Definition at line 473 of file BaseResultsHandler.cs.
|
virtual |
Save the results to disk
name | The name of the results |
result | The results to save |
Reimplemented in QuantConnect.Lean.Engine.Results.RegressionResultHandler.
Definition at line 487 of file BaseResultsHandler.cs.
|
protected |
Purge/clear any outstanding messages in message queue.
Definition at line 495 of file BaseResultsHandler.cs.
|
protected |
Stops the update runner task
Definition at line 503 of file BaseResultsHandler.cs.
|
protected |
Gets the algorithm net return
Definition at line 512 of file BaseResultsHandler.cs.
|
protectedpure virtual |
Save the snapshot of the total results to storage.
packet | Packet to store. |
Implemented in QuantConnect.Lean.Engine.Results.LiveTradingResultHandler, and QuantConnect.Lean.Engine.Results.BacktestingResultHandler.
|
protectedvirtual |
Gets the current portfolio value
Useful so that live trading implementation can freeze the returned value if there is no user exchange open so we ignore extended market hours updates
Reimplemented in QuantConnect.Lean.Engine.Results.LiveTradingResultHandler.
Definition at line 531 of file BaseResultsHandler.cs.
|
protectedvirtual |
Gets the current benchmark value
Useful so that live trading implementation can freeze the returned value if there is no user exchange open so we ignore extended market hours updates
time | Time to resolve benchmark value at |
Reimplemented in QuantConnect.Lean.Engine.Results.LiveTradingResultHandler.
Definition at line 542 of file BaseResultsHandler.cs.
|
virtual |
Samples portfolio equity, benchmark, and daily performance Called by scheduled event every night at midnight algorithm time
time | Current UTC time in the AlgorithmManager loop |
Reimplemented in QuantConnect.Lean.Engine.Results.LiveTradingResultHandler.
Definition at line 557 of file BaseResultsHandler.cs.
|
protectedvirtual |
Sample the current equity of the strategy directly with time and using the current algorithm equity value in CurrentAlgorithmEquity
time | Equity candlestick end time |
Definition at line 601 of file BaseResultsHandler.cs.
|
protectedvirtual |
Sample the current daily performance directly with a time-value pair.
time | Time of the sample. |
value | Current daily performance value. |
Reimplemented in QuantConnect.Lean.Engine.Results.RegressionResultHandler.
Definition at line 614 of file BaseResultsHandler.cs.
|
protectedvirtual |
Sample the current benchmark performance directly with a time-value pair.
time | Time of the sample. |
value | Current benchmark value. |
Definition at line 629 of file BaseResultsHandler.cs.
|
protectedvirtual |
Sample drawdown of equity of the strategy
time | Time of the sample |
currentPortfolioValue | Current equity value |
Definition at line 639 of file BaseResultsHandler.cs.
|
protectedvirtual |
Sample portfolio turn over of the strategy
time | Time of the sample |
currentPortfolioValue | Current equity value |
Definition at line 655 of file BaseResultsHandler.cs.
|
protectedvirtual |
Sample assets sales volume
time | Time of the sample |
Definition at line 690 of file BaseResultsHandler.cs.
|
protectedvirtual |
Sample portfolio exposure long/short ratios by security type
time | Time of the sample |
currentPortfolioValue | Current value of the portfolio |
Definition at line 706 of file BaseResultsHandler.cs.
|
protectedvirtual |
Sample estimated strategy capacity
time | Time of the sample |
Reimplemented in QuantConnect.Lean.Engine.Results.BacktestingResultHandler.
Definition at line 772 of file BaseResultsHandler.cs.
|
protectedpure virtual |
Add a sample to the chart specified by the chartName, and seriesName.
chartName | String chart name to place the sample. |
seriesName | Series name for the chart. |
seriesIndex | Series chart index - which chart should this series belong |
seriesType | Series type for the chart. |
value | Value for the chart sample. |
unit | Unit for the chart axis |
Sample can be used to create new charts or sample equity - daily performance.
Implemented in QuantConnect.Lean.Engine.Results.LiveTradingResultHandler, and QuantConnect.Lean.Engine.Results.BacktestingResultHandler.
|
protected |
Gets the algorithm runtime statistics
Definition at line 797 of file BaseResultsHandler.cs.
|
protected |
Sets the algorithm state data
Definition at line 831 of file BaseResultsHandler.cs.
|
protected |
Gets the algorithm state data
Definition at line 840 of file BaseResultsHandler.cs.
|
protected |
Will generate the statistics results and update the provided runtime statistics
Definition at line 865 of file BaseResultsHandler.cs.
|
protected |
Helper method to get the total trade count statistic
Definition at line 917 of file BaseResultsHandler.cs.
|
protected |
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.
Definition at line 928 of file BaseResultsHandler.cs.
|
protectedvirtual |
Save an algorithm message to the log store. Uses a different timestamped method of adding messaging to interweve debug and logging messages.
message | String message to store |
Reimplemented in QuantConnect.Lean.Engine.Results.LiveTradingResultHandler, QuantConnect.Lean.Engine.Results.BacktestingResultHandler, and QuantConnect.Lean.Engine.Results.RegressionResultHandler.
Definition at line 950 of file BaseResultsHandler.cs.
|
protected |
Processes algorithm logs. Logs of the same type are batched together one per line and are sent out
Definition at line 963 of file BaseResultsHandler.cs.
|
protected |
Sets or updates a custom summary statistic
name | The statistic name |
value | The statistic value |
Definition at line 1025 of file BaseResultsHandler.cs.
|
protected |
Updates the current equity bar with the current equity value from GetPortfolioValue
Definition at line 1045 of file BaseResultsHandler.cs.
|
protected |
The chart update interval
Definition at line 74 of file BaseResultsHandler.cs.
|
protected |
The last position consumed from the ITransactionHandler.OrderEvents by GetDeltaOrders
Definition at line 79 of file BaseResultsHandler.cs.
|
protected |
The last position consumed from the ITransactionHandler.OrderEvents while determining delta order events
Definition at line 84 of file BaseResultsHandler.cs.
bool QuantConnect.Lean.Engine.Results.BaseResultsHandler.IsActive => _updateRunner != null && _updateRunner.IsAlive |
Boolean flag indicating the thread is still active.
Definition at line 130 of file BaseResultsHandler.cs.
|
protected |
True if the exit has been triggered
Definition at line 145 of file BaseResultsHandler.cs.
|
protected |
The handler responsible for communicating messages to listeners
Definition at line 207 of file BaseResultsHandler.cs.
|
protected |
The transaction handler used to get the algorithms Orders information
Definition at line 212 of file BaseResultsHandler.cs.
|
protected |
Closing portfolio value. Used to calculate daily performance.
Definition at line 233 of file BaseResultsHandler.cs.
|
protected |
Cumulative max portfolio value. Used to calculate drawdown underwater.
Definition at line 238 of file BaseResultsHandler.cs.
|
protected |
Directory location to store results
Definition at line 255 of file BaseResultsHandler.cs.
|
getprotected |
The main loop update interval
Definition at line 69 of file BaseResultsHandler.cs.
|
getsetprotected |
Serializer settings to use
Definition at line 89 of file BaseResultsHandler.cs.
|
getsetprotected |
The current aggregated equity bar for sampling. It will be aggregated with values from the GetPortfolioValue
Definition at line 106 of file BaseResultsHandler.cs.
|
getset |
Live packet messaging queue. Queue the messages here and send when the result queue is ready.
Definition at line 135 of file BaseResultsHandler.cs.
|
getset |
Storage for the price and equity charts of the live results.
Definition at line 140 of file BaseResultsHandler.cs.
|
getprotected |
Event set when exit is triggered
Definition at line 150 of file BaseResultsHandler.cs.
|
getprotected |
The log store instance
Definition at line 155 of file BaseResultsHandler.cs.
|
getprotected |
Algorithms performance related chart names
Used to calculate the probabilistic sharpe ratio
Definition at line 161 of file BaseResultsHandler.cs.
|
getprotected |
Lock to be used when accessing the chart collection
Definition at line 166 of file BaseResultsHandler.cs.
|
getsetprotected |
The algorithm project id
Definition at line 171 of file BaseResultsHandler.cs.
|
getsetprotected |
The maximum amount of RAM (in MB) this algorithm is allowed to utilize
Definition at line 176 of file BaseResultsHandler.cs.
|
getsetprotected |
The algorithm unique compilation id
Definition at line 181 of file BaseResultsHandler.cs.
|
getsetprotected |
The algorithm job id. This is the deploy id for live, backtesting id for backtesting
Definition at line 187 of file BaseResultsHandler.cs.
|
getprotected |
The result handler start time
Definition at line 192 of file BaseResultsHandler.cs.
|
getprotected |
Customizable dynamic statistics IAlgorithm.RuntimeStatistics
Definition at line 197 of file BaseResultsHandler.cs.
|
getsetprotected |
State of the algorithm
Definition at line 202 of file BaseResultsHandler.cs.
|
getsetprotected |
The algorithms starting portfolio value. Used to calculate the portfolio return
Definition at line 218 of file BaseResultsHandler.cs.
|
getsetprotected |
The algorithm instance
Definition at line 223 of file BaseResultsHandler.cs.
|
getsetprotected |
Algorithm currency symbol, used in charting
Definition at line 228 of file BaseResultsHandler.cs.
|
getsetprotected |
Sampling period for timespans between resamples of the charting equity.
Specifically critical for backtesting since with such long timeframes the sampled data can get extreme.
Definition at line 244 of file BaseResultsHandler.cs.
|
getsetprotected |
How frequently the backtests push messages to the browser.
Update frequency of notification packets
Definition at line 250 of file BaseResultsHandler.cs.
|
getsetprotected |
The map file provider instance to use
Definition at line 260 of file BaseResultsHandler.cs.