-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
BacktestResultPacket.cs
249 lines (223 loc) · 9.31 KB
/
BacktestResultPacket.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using Newtonsoft.Json;
using QuantConnect.Orders;
using QuantConnect.Logging;
using QuantConnect.Statistics;
using System.Collections.Generic;
namespace QuantConnect.Packets
{
/// <summary>
/// Backtest result packet: send backtest information to GUI for user consumption.
/// </summary>
public class BacktestResultPacket : Packet
{
/// <summary>
/// User Id placing this task
/// </summary>
[JsonProperty(PropertyName = "iUserID")]
public int UserId;
/// <summary>
/// Project Id of the this task.
/// </summary>
[JsonProperty(PropertyName = "iProjectID")]
public int ProjectId;
/// <summary>
/// User Session Id
/// </summary>
[JsonProperty(PropertyName = "sSessionID")]
public string SessionId = string.Empty;
/// <summary>
/// BacktestId for this result packet
/// </summary>
[JsonProperty(PropertyName = "sBacktestID")]
public string BacktestId = string.Empty;
/// <summary>
/// OptimizationId for this result packet if any
/// </summary>
[JsonProperty(PropertyName = "sOptimizationID")]
public string OptimizationId;
/// <summary>
/// Compile Id for the algorithm which generated this result packet.
/// </summary>
[JsonProperty(PropertyName = "sCompileID")]
public string CompileId = string.Empty;
/// <summary>
/// Start of the backtest period as defined in Initialize() method.
/// </summary>
[JsonProperty(PropertyName = "dtPeriodStart")]
public DateTime PeriodStart;
/// <summary>
/// End of the backtest period as defined in the Initialize() method.
/// </summary>
[JsonProperty(PropertyName = "dtPeriodFinish")]
public DateTime PeriodFinish;
/// <summary>
/// DateTime (EST) the user requested this backtest.
/// </summary>
[JsonProperty(PropertyName = "dtDateRequested")]
public DateTime DateRequested;
/// <summary>
/// DateTime (EST) when the backtest was completed.
/// </summary>
[JsonProperty(PropertyName = "dtDateFinished")]
public DateTime DateFinished;
/// <summary>
/// Progress of the backtest as a percentage from 0-1 based on the days lapsed from start-finish.
/// </summary>
[JsonProperty(PropertyName = "dProgress")]
public decimal Progress;
/// <summary>
/// Name of this backtest.
/// </summary>
[JsonProperty(PropertyName = "sName")]
public string Name = string.Empty;
/// <summary>
/// Result data object for this backtest
/// </summary>
[JsonProperty(PropertyName = "oResults")]
public BacktestResult Results = new ();
/// <summary>
/// Processing time of the algorithm (from moment the algorithm arrived on the algorithm node)
/// </summary>
[JsonProperty(PropertyName = "dProcessingTime")]
public double ProcessingTime;
/// <summary>
/// Estimated number of tradeable days in the backtest based on the start and end date or the backtest
/// </summary>
[JsonProperty(PropertyName = "iTradeableDates")]
public int TradeableDates;
/// <summary>
/// Default constructor for JSON Serialization
/// </summary>
public BacktestResultPacket()
: base(PacketType.BacktestResult)
{
PeriodStart = PeriodFinish = DateRequested = DateFinished = DateTime.UtcNow;
}
/// <summary>
/// Compose the packet from a JSON string:
/// </summary>
public BacktestResultPacket(string json)
: base (PacketType.BacktestResult)
{
try
{
var packet = JsonConvert.DeserializeObject<BacktestResultPacket>(json, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});
CompileId = packet.CompileId;
Channel = packet.Channel;
PeriodFinish = packet.PeriodFinish;
PeriodStart = packet.PeriodStart;
Progress = packet.Progress;
SessionId = packet.SessionId;
BacktestId = packet.BacktestId;
Type = packet.Type;
UserId = packet.UserId;
DateFinished = packet.DateFinished;
DateRequested = packet.DateRequested;
Name = packet.Name;
ProjectId = packet.ProjectId;
Results = packet.Results;
ProcessingTime = packet.ProcessingTime;
TradeableDates = packet.TradeableDates;
OptimizationId = packet.OptimizationId;
}
catch (Exception err)
{
Log.Trace($"BacktestResultPacket(): Error converting json: {err}");
}
}
/// <summary>
/// Compose result data packet - with tradable dates from the backtest job task and the partial result packet.
/// </summary>
/// <param name="job">Job that started this request</param>
/// <param name="results">Results class for the Backtest job</param>
/// <param name="endDate">The algorithms backtest end date</param>
/// <param name="startDate">The algorithms backtest start date</param>
/// <param name="progress">Progress of the packet. For the packet we assume progess of 100%.</param>
public BacktestResultPacket(BacktestNodePacket job, BacktestResult results, DateTime endDate, DateTime startDate, decimal progress = 1m)
: this()
{
try
{
Progress = Math.Round(progress, 3);
SessionId = job.SessionId;
PeriodFinish = endDate;
PeriodStart = startDate;
CompileId = job.CompileId;
Channel = job.Channel;
BacktestId = job.BacktestId;
OptimizationId = job.OptimizationId;
Results = results;
Name = job.Name;
UserId = job.UserId;
ProjectId = job.ProjectId;
SessionId = job.SessionId;
TradeableDates = job.TradeableDates;
}
catch (Exception err) {
Log.Error(err);
}
}
/// <summary>
/// Creates an empty result packet, useful when the algorithm fails to initialize
/// </summary>
/// <param name="job">The associated job packet</param>
/// <returns>An empty result packet</returns>
public static BacktestResultPacket CreateEmpty(BacktestNodePacket job)
{
return new BacktestResultPacket(job, new BacktestResult(new BacktestResultParameters(
new Dictionary<string, Chart>(), new Dictionary<int, Order>(), new Dictionary<DateTime, decimal>(),
new Dictionary<string, string>(), new SortedDictionary<string, string>(), new Dictionary<string, AlgorithmPerformance>(),
new List<OrderEvent>(), new AlgorithmPerformance(), new AlgorithmConfiguration(), new Dictionary<string, string>()
)), DateTime.UtcNow, DateTime.UtcNow);
}
} // End Queue Packet:
/// <summary>
/// Backtest results object class - result specific items from the packet.
/// </summary>
public class BacktestResult : Result
{
/// <summary>
/// Rolling window detailed statistics.
/// </summary>
[JsonProperty(PropertyName = "rollingWindow")]
public Dictionary<string, AlgorithmPerformance> RollingWindow = new Dictionary<string, AlgorithmPerformance>();
/// <summary>
/// Rolling window detailed statistics.
/// </summary>
[JsonProperty(PropertyName = "totalPerformance")]
public AlgorithmPerformance TotalPerformance = null;
/// <summary>
/// Default Constructor
/// </summary>
public BacktestResult()
{
}
/// <summary>
/// Constructor for the result class using dictionary objects.
/// </summary>
public BacktestResult(BacktestResultParameters parameters) : base(parameters)
{
RollingWindow = parameters.RollingWindow;
TotalPerformance = parameters.TotalPerformance;
}
}
} // End of Namespace: