Lean  $LEAN_TAG$
LiveResultPacket.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
17 using System;
18 using System.Linq;
19 using Newtonsoft.Json;
20 using QuantConnect.Orders;
21 using QuantConnect.Logging;
23 using System.Collections.Generic;
24 
25 namespace QuantConnect.Packets
26 {
27  /// <summary>
28  /// Live result packet from a lean engine algorithm.
29  /// </summary>
30  public class LiveResultPacket : Packet
31  {
32  /// <summary>
33  /// User Id sending result packet
34  /// </summary>
35  public int UserId { get; set; }
36 
37  /// <summary>
38  /// Project Id of the result packet
39  /// </summary>
40  public int ProjectId { get; set; }
41 
42  /// <summary>
43  /// Live Algorithm Id (DeployId) for this result packet
44  /// </summary>
45  public string DeployId { get; set; } = string.Empty;
46 
47  /// <summary>
48  /// Result data object for this result packet
49  /// </summary>
50  public LiveResult Results { get; set; } = new LiveResult();
51 
52  /// <summary>
53  /// Default constructor for JSON Serialization
54  /// </summary>
56  : base(PacketType.LiveResult)
57  { }
58 
59  /// <summary>
60  /// Compose the packet from a JSON string:
61  /// </summary>
62  public LiveResultPacket(string json)
63  : base(PacketType.LiveResult)
64  {
65  try
66  {
67  var packet = JsonConvert.DeserializeObject<LiveResultPacket>(json);
68  Channel = packet.Channel;
69  DeployId = packet.DeployId;
70  Type = packet.Type;
71  UserId = packet.UserId;
72  ProjectId = packet.ProjectId;
73  Results = packet.Results;
74  }
75  catch (Exception err)
76  {
77  Log.Trace($"LiveResultPacket(): Error converting json: {err}");
78  }
79  }
80 
81  /// <summary>
82  /// Compose Live Result Data Packet - With tradable dates
83  /// </summary>
84  /// <param name="job">Job that started this request</param>
85  /// <param name="results">Results class for the Backtest job</param>
87  :base (PacketType.LiveResult)
88  {
89  try
90  {
91  DeployId = job.DeployId;
92  Results = results;
93  UserId = job.UserId;
94  ProjectId = job.ProjectId;
95  Channel = job.Channel;
96  }
97  catch (Exception err) {
98  Log.Error(err);
99  }
100  }
101 
102  /// <summary>
103  /// Creates an empty result packet, useful when the algorithm fails to initialize
104  /// </summary>
105  /// <param name="job">The associated job packet</param>
106  /// <returns>An empty result packet</returns>
108  {
109  return new LiveResultPacket(job, new LiveResult(new LiveResultParameters(
110  new Dictionary<string, Chart>(), new Dictionary<int, Order>(), new Dictionary<DateTime, decimal>(),
111  new Dictionary<string, Holding>(), new CashBook(), new Dictionary<string, string>(),
112  new SortedDictionary<string, string>(), new List<OrderEvent>(), new Dictionary<string, string>(),
113  new AlgorithmConfiguration(), new Dictionary<string, string>())));
114  }
115  } // End Queue Packet:
116 
117 
118  /// <summary>
119  /// Live results object class for packaging live result data.
120  /// </summary>
121  public class LiveResult : Result
122  {
123  private CashBook _cashBook;
124 
125  /// <summary>
126  /// Holdings dictionary of algorithm holdings information
127  /// </summary>
128  [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
129  public IDictionary<string, Holding> Holdings { get; set; }
130 
131  /// <summary>
132  /// Cashbook for the algorithm's live results.
133  /// </summary>
134  [JsonIgnore]
135  public CashBook CashBook
136  {
137  get
138  {
139  return _cashBook;
140  }
141  set
142  {
143  _cashBook = value;
144 
145  Cash = _cashBook?.ToDictionary(pair => pair.Key, pair => pair.Value);
148  }
149  }
150 
151  /// <summary>
152  /// Cash for the algorithm's live results.
153  /// </summary>
154  [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
155  public Dictionary<string, Cash> Cash { get; set; }
156 
157  /// <summary>
158  /// The algorithm's account currency
159  /// </summary>
160  [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
161  public string AccountCurrency { get; set; }
162 
163  /// <summary>
164  /// The algorithm's account currency
165  /// </summary>
166  [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
167  public string AccountCurrencySymbol { get; set; }
168 
169  /// <summary>
170  /// Default Constructor
171  /// </summary>
172  public LiveResult()
173  { }
174 
175  /// <summary>
176  /// Constructor for the result class for dictionary objects
177  /// </summary>
178  public LiveResult(LiveResultParameters parameters) : base(parameters)
179  {
180  Holdings = parameters.Holdings;
181  CashBook = parameters.CashBook;
182  }
183  }
184 } // End of Namespace: