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... | |
Static Public Attributes | |
const string | StrategyEquityKey = "Strategy Equity" |
String message saying: Strategy Equity More... | |
const string | EquityKey = "Equity" |
String message saying: Equity More... | |
const string | ReturnKey = "Return" |
String message saying: Return More... | |
const string | BenchmarkKey = "Benchmark" |
String message saying: Benchmark More... | |
const string | DrawdownKey = "Drawdown" |
String message saying: Drawdown More... | |
const string | PortfolioTurnoverKey = "Portfolio Turnover" |
String message saying: PortfolioTurnover More... | |
const string | PortfolioMarginKey = "Portfolio Margin" |
String message saying: Portfolio Margin More... | |
const string | AssetsSalesVolumeKey = "Assets Sales Volume" |
String message saying: Portfolio Margin 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 | |
volatile bool | ExitTriggered |
True if the exit has been triggered More... | |
Properties | |
virtual TimeSpan | MainUpdateInterval = TimeSpan.FromSeconds(3) [get] |
The main loop update interval More... | |
TimeSpan | ChartUpdateInterval = TimeSpan.FromMinutes(1) [get, set] |
The chart update interval More... | |
int | LastDeltaOrderPosition [get, set] |
The last position consumed from the ITransactionHandler.OrderEvents by GetDeltaOrders More... | |
int | LastDeltaOrderEventsPosition [get, set] |
The last position consumed from the ITransactionHandler.OrderEvents while determining delta order events 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... | |
IMessagingHandler | MessagingHandler [get, set] |
The handler responsible for communicating messages to listeners More... | |
ITransactionHandler | TransactionHandler [get, set] |
The transaction handler used to get the algorithms Orders information 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... | |
decimal | DailyPortfolioValue [get, set] |
Closing portfolio value. Used to calculate daily performance. More... | |
decimal | CumulativeMaxPortfolioValue [get, set] |
Cumulative max portfolio value. Used to calculate drawdown underwater. 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... | |
string | ResultsDestinationFolder [get, set] |
Directory location to store results 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 297 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 333 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 340 of file BaseResultsHandler.cs.
|
protectedvirtual |
Gets the current Server statistics
Definition at line 350 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 365 of file BaseResultsHandler.cs.
|
protectedvirtual |
Save insight results to persistent storage
Method called by the storing timer and on exit
Definition at line 384 of file BaseResultsHandler.cs.
|
protectedvirtual |
Gets the orders generated starting from the provided ITransactionHandler.OrderEvents position
Definition at line 411 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 449 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 487 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 495 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 505 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 519 of file BaseResultsHandler.cs.
|
protected |
Purge/clear any outstanding messages in message queue.
Definition at line 527 of file BaseResultsHandler.cs.
|
protected |
Stops the update runner task
Definition at line 535 of file BaseResultsHandler.cs.
|
protected |
Gets the algorithm net return
Definition at line 544 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 563 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 574 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 589 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 633 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 646 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 661 of file BaseResultsHandler.cs.
|
protectedvirtual |
Sample drawdown of equity of the strategy
time | Time of the sample |
currentPortfolioValue | Current equity value |
Definition at line 671 of file BaseResultsHandler.cs.
|
protectedvirtual |
Sample portfolio turn over of the strategy
time | Time of the sample |
currentPortfolioValue | Current equity value |
Definition at line 687 of file BaseResultsHandler.cs.
|
protectedvirtual |
Sample assets sales volume
time | Time of the sample |
Definition at line 722 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 738 of file BaseResultsHandler.cs.
|
protectedvirtual |
Sample estimated strategy capacity
time | Time of the sample |
Reimplemented in QuantConnect.Lean.Engine.Results.BacktestingResultHandler.
Definition at line 804 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 829 of file BaseResultsHandler.cs.
|
protected |
Sets the algorithm state data
Definition at line 863 of file BaseResultsHandler.cs.
|
protected |
Gets the algorithm state data
Definition at line 872 of file BaseResultsHandler.cs.
|
protected |
Will generate the statistics results and update the provided runtime statistics
Definition at line 897 of file BaseResultsHandler.cs.
|
protected |
Helper method to get the total trade count statistic
Definition at line 949 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 960 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 982 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 995 of file BaseResultsHandler.cs.
|
protected |
Sets or updates a custom summary statistic
name | The statistic name |
value | The statistic value |
Definition at line 1057 of file BaseResultsHandler.cs.
|
protected |
Updates the current equity bar with the current equity value from GetPortfolioValue
Definition at line 1077 of file BaseResultsHandler.cs.
|
static |
String message saying: Strategy Equity
Definition at line 61 of file BaseResultsHandler.cs.
|
static |
String message saying: Equity
Definition at line 66 of file BaseResultsHandler.cs.
|
static |
String message saying: Return
Definition at line 71 of file BaseResultsHandler.cs.
|
static |
String message saying: Benchmark
Definition at line 76 of file BaseResultsHandler.cs.
|
static |
String message saying: Drawdown
Definition at line 81 of file BaseResultsHandler.cs.
|
static |
String message saying: PortfolioTurnover
Definition at line 86 of file BaseResultsHandler.cs.
|
static |
String message saying: Portfolio Margin
Definition at line 91 of file BaseResultsHandler.cs.
|
static |
String message saying: Portfolio Margin
Definition at line 96 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 162 of file BaseResultsHandler.cs.
|
protected |
True if the exit has been triggered
Definition at line 177 of file BaseResultsHandler.cs.
|
getprotected |
The main loop update interval
Definition at line 101 of file BaseResultsHandler.cs.
|
getsetprotected |
The chart update interval
Definition at line 106 of file BaseResultsHandler.cs.
|
getsetprotected |
The last position consumed from the ITransactionHandler.OrderEvents by GetDeltaOrders
Definition at line 111 of file BaseResultsHandler.cs.
|
getsetprotected |
The last position consumed from the ITransactionHandler.OrderEvents while determining delta order events
Definition at line 116 of file BaseResultsHandler.cs.
|
getsetprotected |
Serializer settings to use
Definition at line 121 of file BaseResultsHandler.cs.
|
getsetprotected |
The current aggregated equity bar for sampling. It will be aggregated with values from the GetPortfolioValue
Definition at line 138 of file BaseResultsHandler.cs.
|
getset |
Live packet messaging queue. Queue the messages here and send when the result queue is ready.
Definition at line 167 of file BaseResultsHandler.cs.
|
getset |
Storage for the price and equity charts of the live results.
Definition at line 172 of file BaseResultsHandler.cs.
|
getprotected |
Event set when exit is triggered
Definition at line 182 of file BaseResultsHandler.cs.
|
getprotected |
The log store instance
Definition at line 187 of file BaseResultsHandler.cs.
|
getprotected |
Algorithms performance related chart names
Used to calculate the probabilistic sharpe ratio
Definition at line 193 of file BaseResultsHandler.cs.
|
getprotected |
Lock to be used when accessing the chart collection
Definition at line 198 of file BaseResultsHandler.cs.
|
getsetprotected |
The algorithm project id
Definition at line 203 of file BaseResultsHandler.cs.
|
getsetprotected |
The maximum amount of RAM (in MB) this algorithm is allowed to utilize
Definition at line 208 of file BaseResultsHandler.cs.
|
getsetprotected |
The algorithm unique compilation id
Definition at line 213 of file BaseResultsHandler.cs.
|
getsetprotected |
The algorithm job id. This is the deploy id for live, backtesting id for backtesting
Definition at line 219 of file BaseResultsHandler.cs.
|
getprotected |
The result handler start time
Definition at line 224 of file BaseResultsHandler.cs.
|
getprotected |
Customizable dynamic statistics IAlgorithm.RuntimeStatistics
Definition at line 229 of file BaseResultsHandler.cs.
|
getsetprotected |
State of the algorithm
Definition at line 234 of file BaseResultsHandler.cs.
|
getsetprotected |
The handler responsible for communicating messages to listeners
Definition at line 239 of file BaseResultsHandler.cs.
|
getsetprotected |
The transaction handler used to get the algorithms Orders information
Definition at line 244 of file BaseResultsHandler.cs.
|
getsetprotected |
The algorithms starting portfolio value. Used to calculate the portfolio return
Definition at line 250 of file BaseResultsHandler.cs.
|
getsetprotected |
The algorithm instance
Definition at line 255 of file BaseResultsHandler.cs.
|
getsetprotected |
Algorithm currency symbol, used in charting
Definition at line 260 of file BaseResultsHandler.cs.
|
getsetprotected |
Closing portfolio value. Used to calculate daily performance.
Definition at line 265 of file BaseResultsHandler.cs.
|
getsetprotected |
Cumulative max portfolio value. Used to calculate drawdown underwater.
Definition at line 270 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 276 of file BaseResultsHandler.cs.
|
getsetprotected |
How frequently the backtests push messages to the browser.
Update frequency of notification packets
Definition at line 282 of file BaseResultsHandler.cs.
|
getsetprotected |
Directory location to store results
Definition at line 287 of file BaseResultsHandler.cs.
|
getsetprotected |
The map file provider instance to use
Definition at line 292 of file BaseResultsHandler.cs.