17 using System.Collections.Concurrent;
18 using System.Collections.Generic;
19 using System.Globalization;
26 public partial class QCAlgorithm
28 private bool _isEmitWarmupPlotWarningSet;
29 private readonly ConcurrentDictionary<string, Chart> _charts =
new ConcurrentDictionary<string, Chart>();
31 private static readonly Dictionary<string, List<string>> ReservedChartSeriesNames =
new Dictionary<string, List<string>>
33 {
"Strategy Equity",
new List<string> {
"Equity",
"Return" } },
34 {
"Capacity",
new List<string> {
"Strategy Capacity" } },
35 {
"Drawdown",
new List<string> {
"Equity Drawdown" } },
36 {
"Benchmark",
new List<string>() {
"Benchmark" } },
37 {
"Assets Sales Volume",
new List<string>() },
38 {
"Exposure",
new List<string>() },
39 {
"Portfolio Margin",
new List<string>() },
40 {
"Portfolio Turnover",
new List<string> {
"Portfolio Turnover" } }
47 [DocumentationAttribute(Charting)]
48 public ConcurrentDictionary<string, string>
RuntimeStatistics {
get; } =
new ConcurrentDictionary<string, string>();
58 _charts.TryAdd(chart.
Name, chart);
68 public void Plot(
string series, decimal value)
71 Plot(
"Strategy Equity", series, value);
80 public void Record(
string series,
int value)
90 public void Record(
string series,
double value)
102 public void Record(
string series, decimal value)
113 public void Plot(
string series,
double value) {
114 Plot(series, value.SafeDecimalCast());
122 public void Plot(
string series,
int value)
124 Plot(series, (decimal)value);
132 public void Plot(
string series,
float value)
134 Plot(series, (
double)value);
142 public void Plot(
string chart,
string series,
double value)
144 Plot(chart, series, value.SafeDecimalCast());
152 public void Plot(
string chart,
string series,
int value)
154 Plot(chart, series, (decimal)value);
162 public void Plot(
string chart,
string series,
float value)
164 Plot(chart, series, (
double)value);
174 public void Plot(
string chart,
string series, decimal value)
176 if (TryGetChartSeries(chart, series, out
Series chartSeries))
178 chartSeries.AddPoint(
UtcTime, value);
192 public void Plot(
string series,
double open,
double high,
double low,
double close)
194 Plot(series, open.SafeDecimalCast(), high.SafeDecimalCast(), low.SafeDecimalCast(), close.SafeDecimalCast());
207 public void Plot(
string series,
float open,
float high,
float low,
float close)
209 Plot(series, (
double)open, (
double)high, (
double)low, (
double)close);
222 public void Plot(
string series,
int open,
int high,
int low,
int close)
224 Plot(series, (decimal)open, (decimal)high, (decimal)low, (decimal)close);
237 public void Plot(
string series, decimal open, decimal high, decimal low, decimal close)
240 Plot(
"Strategy Equity", series, open, high, low, close);
254 public void Plot(
string chart,
string series,
double open,
double high,
double low,
double close)
256 Plot(chart, series, open.SafeDecimalCast(), high.SafeDecimalCast(), low.SafeDecimalCast(), close.SafeDecimalCast());
270 public void Plot(
string chart,
string series,
float open,
float high,
float low,
float close)
272 Plot(chart, series, (
double)open, (
double)high, (
double)low, (
double)close);
286 public void Plot(
string chart,
string series,
int open,
int high,
int low,
int close)
288 Plot(chart, series, (decimal)open, (decimal)high, (decimal)low, (decimal)close);
301 public void Plot(
string chart,
string series, decimal open, decimal high, decimal low, decimal close)
305 candlestickSeries.AddPoint(
UtcTime, open, high, low, close);
334 private bool TryGetChartSeries<T>(
string chartName,
string seriesName, out
T series)
340 if (ReservedChartSeriesNames.TryGetValue(chartName, out var reservedSeriesNames))
342 if (reservedSeriesNames.Count == 0)
344 throw new ArgumentException($
"'{chartName}' is a reserved chart name.");
346 if (reservedSeriesNames.Contains(seriesName))
348 throw new ArgumentException($
"'{seriesName}' is a reserved series name for chart '{chartName}'.");
352 if(!_charts.TryGetValue(chartName, out var chart))
355 _charts[chartName] = chart =
new Chart(chartName);
358 if (!chart.Series.TryGetValue(seriesName, out var chartSeries))
360 chartSeries =
new T() {
Name = seriesName };
361 chart.AddSeries(chartSeries);
366 if (!_isEmitWarmupPlotWarningSet)
368 _isEmitWarmupPlotWarningSet =
true;
369 Debug(
"Plotting is disabled during algorithm warmup in live trading.");
374 series = (
T)chartSeries;
387 [DocumentationAttribute(Charting)]
391 if (!_charts.TryGetValue(chart, out c))
393 _charts[chart] = c =
new Chart(chart);
408 foreach (var indicator
in indicators)
410 Plot(chart, indicator.Name, indicator.Current.Value);
431 foreach (var i
in indicators)
433 if (i ==
null)
continue;
437 i.Updated += (sender, args) =>
439 if (!waitForReady || ilocal.IsReady)
500 _statisticsService.SetSummaryStatistic(name, value);
511 _statisticsService.SetSummaryStatistic(name, value.ToStringInvariant());
522 _statisticsService.SetSummaryStatistic(name, value.ToStringInvariant());
533 _statisticsService.SetSummaryStatistic(name, value.ToStringInvariant());
545 foreach (var chart
in _charts.Values)
547 yield
return chart.GetUpdates();
551 foreach (var series
in chart.Series)
553 series.Value.Purge();