/* * 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.Linq; using Newtonsoft.Json; using QuantConnect.Orders; using QuantConnect.Logging; using QuantConnect.Securities; using System.Collections.Generic; namespace QuantConnect.Packets { /// /// Live result packet from a lean engine algorithm. /// public class LiveResultPacket : Packet { /// /// User Id sending result packet /// public int UserId { get; set; } /// /// Project Id of the result packet /// public int ProjectId { get; set; } /// /// Live Algorithm Id (DeployId) for this result packet /// public string DeployId { get; set; } = string.Empty; /// /// Result data object for this result packet /// public LiveResult Results { get; set; } = new LiveResult(); /// /// 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); Channel = packet.Channel; DeployId = packet.DeployId; Type = packet.Type; UserId = packet.UserId; ProjectId = packet.ProjectId; Results = packet.Results; } 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 { DeployId = job.DeployId; Results = results; UserId = job.UserId; ProjectId = job.ProjectId; 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 SortedDictionary(), new List(), new Dictionary(), new AlgorithmConfiguration(), new Dictionary()))); } } // End Queue Packet: /// /// Live results object class for packaging live result data. /// public class LiveResult : Result { private CashBook _cashBook; /// /// Holdings dictionary of algorithm holdings information /// [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public IDictionary Holdings { get; set; } /// /// Cashbook for the algorithm's live results. /// [JsonIgnore] public CashBook CashBook { get { return _cashBook; } set { _cashBook = value; Cash = _cashBook?.ToDictionary(pair => pair.Key, pair => pair.Value); AccountCurrency = CashBook?.AccountCurrency; AccountCurrencySymbol = AccountCurrency != null ? Currencies.GetCurrencySymbol(AccountCurrency) : null; } } /// /// Cash for the algorithm's live results. /// [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public Dictionary Cash { get; set; } /// /// The algorithm's account currency /// [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public string AccountCurrency { get; set; } /// /// The algorithm's account currency /// [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public string AccountCurrencySymbol { get; set; } /// /// Default Constructor /// public LiveResult() { } /// /// Constructor for the result class for dictionary objects /// public LiveResult(LiveResultParameters parameters) : base(parameters) { Holdings = parameters.Holdings; CashBook = parameters.CashBook; } } } // End of Namespace: