/* * 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.Securities; namespace QuantConnect.Packets { /// /// Live result packet from a lean engine algorithm. /// public class LiveResultPacket : Packet { /// /// User Id sending result packet /// [JsonProperty(PropertyName = "iUserID")] public int UserId = 0; /// /// Project Id of the result packet /// [JsonProperty(PropertyName = "iProjectID")] public int ProjectId = 0; /// /// User session Id who issued the result packet /// [JsonProperty(PropertyName = "sSessionID")] public string SessionId = ""; /// /// Live Algorithm Id (DeployId) for this result packet /// [JsonProperty(PropertyName = "sDeployID")] public string DeployId = ""; /// /// Compile Id algorithm which generated this result packet /// [JsonProperty(PropertyName = "sCompileID")] public string CompileId = ""; /// /// Result data object for this result packet /// [JsonProperty(PropertyName = "oResults")] public LiveResult Results = new LiveResult(); /// /// Processing time / running time for the live algorithm. /// [JsonProperty(PropertyName = "dProcessingTime")] public double ProcessingTime = 0; /// /// Default constructor for JSON Serialization /// public LiveResultPacket() : base(PacketType.LiveResult) { } /// /// Compose the packet from a JSON string: /// public LiveResultPacket(string json) : base(PacketType.LiveResult) { try { var packet = JsonConvert.DeserializeObject(json); CompileId = packet.CompileId; Channel = packet.Channel; SessionId = packet.SessionId; DeployId = packet.DeployId; Type = packet.Type; UserId = packet.UserId; ProjectId = packet.ProjectId; Results = packet.Results; ProcessingTime = packet.ProcessingTime; } catch (Exception err) { Log.Trace($"LiveResultPacket(): Error converting json: {err}"); } } /// /// Compose Live Result Data Packet - With tradable dates /// /// Job that started this request /// Results class for the Backtest job public LiveResultPacket(LiveNodePacket job, LiveResult results) :base (PacketType.LiveResult) { try { SessionId = job.SessionId; CompileId = job.CompileId; DeployId = job.DeployId; Results = results; UserId = job.UserId; ProjectId = job.ProjectId; SessionId = job.SessionId; Channel = job.Channel; } catch (Exception err) { Log.Error(err); } } /// /// Creates an empty result packet, useful when the algorithm fails to initialize /// /// The associated job packet /// An empty result packet public static LiveResultPacket CreateEmpty(LiveNodePacket job) { return new LiveResultPacket(job, new LiveResult(new LiveResultParameters( new Dictionary(), new Dictionary(), new Dictionary(), new Dictionary(), new CashBook(), new Dictionary(), new Dictionary(), new List(), new Dictionary(), new AlphaRuntimeStatistics()))); } } // End Queue Packet: /// /// Live results object class for packaging live result data. /// public class LiveResult : Result { /// /// Holdings dictionary of algorithm holdings information /// [JsonProperty(PropertyName = "Holdings", NullValueHandling = NullValueHandling.Ignore)] public IDictionary Holdings; /// /// Cashbook for the algorithm's live results. /// [JsonProperty(PropertyName = "Cash", NullValueHandling = NullValueHandling.Ignore)] public CashBook Cash; /// /// Default Constructor /// public LiveResult() { } /// /// Constructor for the result class for dictionary objects /// public LiveResult(LiveResultParameters parameters) { Charts = parameters.Charts; Orders = parameters.Orders; ProfitLoss = parameters.ProfitLoss; Statistics = parameters.Statistics; Holdings = parameters.Holdings; Cash = parameters.CashBook; RuntimeStatistics = parameters.RuntimeStatistics; OrderEvents = parameters.OrderEvents; ServerStatistics = parameters.ServerStatistics; AlphaRuntimeStatistics = parameters.AlphaRuntimeStatistics; } } } // End of Namespace: