Skip to content

Commit 0fb02a7

Browse files
committed
Some cleanup
1 parent fe8dd81 commit 0fb02a7

File tree

1 file changed

+40
-41
lines changed

1 file changed

+40
-41
lines changed

Common/Api/OptimizationBacktestJsonConverter.cs

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,35 @@ namespace QuantConnect.Api
3030
/// </summary>
3131
public class OptimizationBacktestJsonConverter : JsonConverter
3232
{
33-
private static Dictionary<string, int> StatisticsIndices = new()
34-
{
35-
{ PerformanceMetrics.Alpha, 0 },
36-
{ PerformanceMetrics.AnnualStandardDeviation, 1 },
37-
{ PerformanceMetrics.AnnualVariance, 2 },
38-
{ PerformanceMetrics.AverageLoss, 3 },
39-
{ PerformanceMetrics.AverageWin, 4 },
40-
{ PerformanceMetrics.Beta, 5 },
41-
{ PerformanceMetrics.CompoundingAnnualReturn, 6 },
42-
{ PerformanceMetrics.Drawdown, 7 },
43-
{ PerformanceMetrics.EstimatedStrategyCapacity, 8 },
44-
{ PerformanceMetrics.Expectancy, 9 },
45-
{ PerformanceMetrics.InformationRatio, 10 },
46-
{ PerformanceMetrics.LossRate, 11 },
47-
{ PerformanceMetrics.NetProfit, 12 },
48-
{ PerformanceMetrics.ProbabilisticSharpeRatio, 13 },
49-
{ PerformanceMetrics.ProfitLossRatio, 14 },
50-
{ PerformanceMetrics.SharpeRatio, 15 },
51-
{ PerformanceMetrics.TotalFees, 16 },
52-
{ PerformanceMetrics.TotalOrders, 17 },
53-
{ PerformanceMetrics.TrackingError, 18 },
54-
{ PerformanceMetrics.TreynorRatio, 19 },
55-
{ PerformanceMetrics.WinRate, 20 },
56-
{ PerformanceMetrics.SortinoRatio, 21 },
57-
{ PerformanceMetrics.StartEquity, 22 },
58-
{ PerformanceMetrics.EndEquity, 23 },
59-
{ PerformanceMetrics.PortfolioTurnover, 24 },
60-
{ PerformanceMetrics.DrawdownRecovery, 25 },
61-
};
62-
63-
private static string[] StatisticNames { get; } = StatisticsIndices
64-
.OrderBy(kvp => kvp.Value)
65-
.Select(kvp => kvp.Key)
66-
.ToArray();
33+
private static string[] StatisticNames =
34+
[
35+
PerformanceMetrics.Alpha,
36+
PerformanceMetrics.AnnualStandardDeviation,
37+
PerformanceMetrics.AnnualVariance,
38+
PerformanceMetrics.AverageLoss,
39+
PerformanceMetrics.AverageWin,
40+
PerformanceMetrics.Beta,
41+
PerformanceMetrics.CompoundingAnnualReturn,
42+
PerformanceMetrics.Drawdown,
43+
PerformanceMetrics.EstimatedStrategyCapacity,
44+
PerformanceMetrics.Expectancy,
45+
PerformanceMetrics.InformationRatio,
46+
PerformanceMetrics.LossRate,
47+
PerformanceMetrics.NetProfit,
48+
PerformanceMetrics.ProbabilisticSharpeRatio,
49+
PerformanceMetrics.ProfitLossRatio,
50+
PerformanceMetrics.SharpeRatio,
51+
PerformanceMetrics.TotalFees,
52+
PerformanceMetrics.TotalOrders,
53+
PerformanceMetrics.TrackingError,
54+
PerformanceMetrics.TreynorRatio,
55+
PerformanceMetrics.WinRate,
56+
PerformanceMetrics.SortinoRatio,
57+
PerformanceMetrics.StartEquity,
58+
PerformanceMetrics.EndEquity,
59+
PerformanceMetrics.PortfolioTurnover,
60+
PerformanceMetrics.DrawdownRecovery,
61+
];
6762

6863
// Only 21 Lean statistics where supported when the serialized statistics where a json array
6964
private static int ArrayStatisticsCount = 21;
@@ -138,16 +133,20 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
138133
writer.WriteStartObject();
139134

140135
var customStatisticsNames = new HashSet<string>();
141-
136+
int idx;
142137
foreach (var (name, statisticValue, index) in optimizationBacktest.Statistics
143-
.Select(kvp => (Name: kvp.Key, kvp.Value, Index: StatisticsIndices.TryGetValue(kvp.Key, out var index) ? index : int.MaxValue))
138+
.Select(kvp => (
139+
Name: kvp.Key,
140+
kvp.Value,
141+
Index: (idx = Array.IndexOf(StatisticNames, kvp.Key)) != -1 ? idx : int.MaxValue
142+
))
144143
.OrderBy(t => t.Index)
145144
.ThenByDescending(t => t.Name))
146145
{
147146
var statistic = statisticValue.Replace("%", string.Empty, StringComparison.InvariantCulture);
148147
if (Currencies.TryParse(statistic, out var result))
149148
{
150-
writer.WritePropertyName(index < StatisticsIndices.Count ? index.ToStringInvariant() : name);
149+
writer.WritePropertyName(index < StatisticNames.Length? index.ToStringInvariant() : name);
151150
writer.WriteValue(result);
152151
}
153152
}
@@ -203,9 +202,9 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
203202
if (jStatistics.Type == JTokenType.Array)
204203
{
205204
var statsCount = Math.Min(ArrayStatisticsCount, (jStatistics as JArray).Count);
206-
statistics = new Dictionary<string, string>(StatisticsIndices
207-
.Where(kvp => kvp.Value < statsCount)
208-
.Select(kvp => KeyValuePair.Create(kvp.Key, jStatistics[kvp.Value].Value<string>()))
205+
statistics = new Dictionary<string, string>(StatisticNames
206+
.Take(statsCount)
207+
.Select((x, i) => KeyValuePair.Create(x, jStatistics[i].Value<string>()))
209208
.Where(kvp => kvp.Value != null));
210209
}
211210
else
@@ -251,7 +250,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
251250
[MethodImpl(MethodImplOptions.AggressiveInlining)]
252251
private static bool TryConvertToLeanStatisticIndex(string statistic, out int index)
253252
{
254-
return int.TryParse(statistic, out index) && index >= 0 && index < StatisticsIndices.Count;
253+
return int.TryParse(statistic, out index) && index >= 0 && index < StatisticNames.Length;
255254
}
256255
}
257256
}

0 commit comments

Comments
 (0)