/* * 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 System.Collections.Generic; using Newtonsoft.Json; using QuantConnect.Logging; using QuantConnect.Orders; using QuantConnect.Statistics; namespace QuantConnect.Packets { /// /// Backtest result packet: send backtest information to GUI for user consumption. /// public class BacktestResultPacket : Packet { /// /// User Id placing this task /// [JsonProperty(PropertyName = "iUserID")] public int UserId = 0; /// /// Project Id of the this task. /// [JsonProperty(PropertyName = "iProjectID")] public int ProjectId = 0; /// /// User Session Id /// [JsonProperty(PropertyName = "sSessionID")] public string SessionId = ""; /// /// BacktestId for this result packet /// [JsonProperty(PropertyName = "sBacktestID")] public string BacktestId = ""; /// /// Compile Id for the algorithm which generated this result packet. /// [JsonProperty(PropertyName = "sCompileID")] public string CompileId = ""; /// /// Start of the backtest period as defined in Initialize() method. /// [JsonProperty(PropertyName = "dtPeriodStart")] public DateTime PeriodStart = DateTime.Now; /// /// End of the backtest period as defined in the Initialize() method. /// [JsonProperty(PropertyName = "dtPeriodFinish")] public DateTime PeriodFinish = DateTime.Now; /// /// DateTime (EST) the user requested this backtest. /// [JsonProperty(PropertyName = "dtDateRequested")] public DateTime DateRequested = DateTime.Now; /// /// DateTime (EST) when the backtest was completed. /// [JsonProperty(PropertyName = "dtDateFinished")] public DateTime DateFinished = DateTime.Now; /// /// Progress of the backtest as a percentage from 0-1 based on the days lapsed from start-finish. /// [JsonProperty(PropertyName = "dProgress")] public decimal Progress = 0; /// /// Runmode for this backtest. /// /// The RunMode property has been made obsolete and all backtests will be run in series mode. [Obsolete("The RunMode property has been made obsolete and all backtests will be run in series mode.")] [JsonProperty(PropertyName = "eRunMode")] public RunMode RunMode = RunMode.Series; /// /// Name of this backtest. /// [JsonProperty(PropertyName = "sName")] public string Name = String.Empty; /// /// Result data object for this backtest /// [JsonProperty(PropertyName = "oResults")] public BacktestResult Results = new BacktestResult(); /// /// Processing time of the algorithm (from moment the algorithm arrived on the algorithm node) /// [JsonProperty(PropertyName = "dProcessingTime")] public double ProcessingTime = 0; /// /// Estimated number of tradeable days in the backtest based on the start and end date or the backtest /// [JsonProperty(PropertyName = "iTradeableDates")] public int TradeableDates = 0; /// /// Default constructor for JSON Serialization /// public BacktestResultPacket() : base(PacketType.BacktestResult) { } /// /// Compose the packet from a JSON string: /// public BacktestResultPacket(string json) : base (PacketType.BacktestResult) { try { var packet = JsonConvert.DeserializeObject(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; } catch (Exception err) { Log.Trace("BacktestResultPacket(): Error converting json: " + err); } } /// /// Compose result data packet - with tradable dates from the backtest job task and the partial result packet. /// /// Job that started this request /// Results class for the Backtest job /// Progress of the packet. For the packet we assume progess of 100%. public BacktestResultPacket(BacktestNodePacket job, BacktestResult results, decimal progress = 1m) : base(PacketType.BacktestResult) { try { Progress = Math.Round(progress, 3); SessionId = job.SessionId; PeriodFinish = job.PeriodFinish; PeriodStart = job.PeriodStart; CompileId = job.CompileId; Channel = job.Channel; BacktestId = job.BacktestId; Results = results; Name = job.Name; UserId = job.UserId; ProjectId = job.ProjectId; SessionId = job.SessionId; TradeableDates = job.TradeableDates; } catch (Exception err) { Log.Error(err); } } } // End Queue Packet: /// /// Backtest results object class - result specific items from the packet. /// public class BacktestResult : Result { /// /// Rolling window detailed statistics. /// public Dictionary RollingWindow = new Dictionary(); /// /// Rolling window detailed statistics. /// public AlgorithmPerformance TotalPerformance = null; /// /// Default Constructor /// public BacktestResult() { } /// /// Constructor for the result class using dictionary objects. /// public BacktestResult(IDictionary charts, IDictionary orders, IDictionary profitLoss, IDictionary statistics, IDictionary runtimeStatistics, Dictionary rollingWindow, AlgorithmPerformance totalPerformance = null) { Charts = charts; Orders = orders; ProfitLoss = profitLoss; Statistics = statistics; RuntimeStatistics = runtimeStatistics; RollingWindow = rollingWindow; TotalPerformance = totalPerformance; } } } // End of Namespace: