Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Algorithm.CSharp/StatisticsResultsAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class StatisticsResultsAlgorithm : QCAlgorithm, IRegressionAlgorithmDefin
{
private const string MostTradedSecurityStatistic = "Most Traded Security";
private const string MostTradedSecurityTradeCountStatistic = "Most Traded Security Trade Count";
private const string InitializeStatistic = "Initialize Statistic";

private Symbol _spy;

Expand Down Expand Up @@ -61,6 +62,9 @@ public override void Initialize()

_fastIbmEma = EMA(_spy, 10, Resolution.Minute);
_slowIbmEma = EMA(_spy, 30, Resolution.Minute);

// Custom summary statistics can also be set during initialization
SetSummaryStatistic(InitializeStatistic, 1);
}

public override void OnData(Slice slice)
Expand Down Expand Up @@ -152,6 +156,10 @@ public override void OnEndOfAlgorithm()
{
throw new RegressionTestException($"Statistic {MostTradedSecurityTradeCountStatistic} should be in the summary statistics");
}
if (!statistics.TryGetValue(InitializeStatistic, out var initializeStatistic) || initializeStatistic != "1")
{
throw new RegressionTestException($"Statistic {InitializeStatistic} set during initialization should be in the summary statistics");
}
var mostTradeSecurityKvp = _tradeCounts.MaxBy(kvp => kvp.Value);
CheckMostTradedSecurityStatistic(statistics, mostTradeSecurityKvp.Key, mostTradeSecurityKvp.Value);
}
Expand Down
6 changes: 6 additions & 0 deletions Algorithm.Python/StatisticsResultsAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class StatisticsResultsAlgorithm(QCAlgorithm):

most_traded_security_statistic = "Most Traded Security"
most_traded_security_trade_count_statistic = "Most Traded Security Trade Count"
initialize_statistic = "Initialize Statistic"

def initialize(self):
self.set_start_date(2013, 10, 7)
Expand All @@ -39,6 +40,9 @@ def initialize(self):

self.trade_counts = {self.spy: 0, self.ibm: 0}

# Custom summary statistics can also be set during initialization
self.set_summary_statistic(StatisticsResultsAlgorithm.initialize_statistic, 1)

def on_data(self, data: Slice):
if not self.slow_spy_ema.is_ready: return

Expand Down Expand Up @@ -97,6 +101,8 @@ def on_end_of_algorithm(self):
raise AssertionError(f"Statistic {StatisticsResultsAlgorithm.most_traded_security_statistic} should be in the summary statistics")
if StatisticsResultsAlgorithm.most_traded_security_trade_count_statistic not in statistics:
raise AssertionError(f"Statistic {StatisticsResultsAlgorithm.most_traded_security_trade_count_statistic} should be in the summary statistics")
if StatisticsResultsAlgorithm.initialize_statistic not in statistics or statistics[StatisticsResultsAlgorithm.initialize_statistic] != "1":
raise AssertionError(f"Statistic {StatisticsResultsAlgorithm.initialize_statistic} set during initialization should be in the summary statistics")

most_trade_security, most_trade_security_trade_count = self.get_most_trade_security()
self.check_most_traded_security_statistic(statistics, most_trade_security, most_trade_security_trade_count)
Expand Down
8 changes: 8 additions & 0 deletions Algorithm/QCAlgorithm.Plotting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,14 @@ public void SetSummaryStatistic(string name, string value)
throw new ArgumentException($"'{name}' is a reserved statistic name.");
}

if (_statisticsService == null)
{
// the statistics service is attached after Initialize, keep the statistic until then
_pendingSummaryStatistics ??= new();
_pendingSummaryStatistics[name] = value;
return;
}

_statisticsService.SetSummaryStatistic(name, value);
}

Expand Down
11 changes: 11 additions & 0 deletions Algorithm/QCAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public partial class QCAlgorithm : MarshalByRefObject, IAlgorithm
private ConcurrentQueue<string> _logMessages = new ConcurrentQueue<string>();
private ConcurrentQueue<string> _errorMessages = new ConcurrentQueue<string>();
private IStatisticsService _statisticsService;
// summary statistics set before the statistics service is attached, e.g. during Initialize
private Dictionary<string, string> _pendingSummaryStatistics;
private IBrokerageModel _brokerageModel;

private bool _sentBroadcastCommandsDisabled;
Expand Down Expand Up @@ -3914,6 +3916,15 @@ public void SetStatisticsService(IStatisticsService statisticsService)
if (_statisticsService == null)
{
_statisticsService = statisticsService;

if (_pendingSummaryStatistics != null)
{
foreach (var (name, value) in _pendingSummaryStatistics)
{
_statisticsService.SetSummaryStatistic(name, value);
}
_pendingSummaryStatistics = null;
}
}
}

Expand Down
Loading