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  /// User session Id who issued the result packet
44  /// </summary>
45  public string SessionId { get; set; } = string.Empty;
46 
47  /// <summary>
48  /// Live Algorithm Id (DeployId) for this result packet
49  /// </summary>
50  public string DeployId { get; set; } = string.Empty;
51 
52  /// <summary>
53  /// Compile Id algorithm which generated this result packet
54  /// </summary>
55  public string CompileId { get; set; } = string.Empty;
56 
57  /// <summary>
58  /// Result data object for this result packet
59  /// </summary>
60  public LiveResult Results { get; set; } = new LiveResult();
61 
62  /// <summary>
63  /// Processing time / running time for the live algorithm.
64  /// </summary>
65  public double ProcessingTime { get; set; }
66 
67  /// <summary>
68  /// Default constructor for JSON Serialization
69  /// </summary>
71  : base(PacketType.LiveResult)
72  { }
73 
74  /// <summary>
75  /// Compose the packet from a JSON string:
76  /// </summary>
77  public LiveResultPacket(string json)
78  : base(PacketType.LiveResult)
79  {
80  try
81  {
82  var packet = JsonConvert.DeserializeObject<LiveResultPacket>(json);
83  CompileId = packet.CompileId;
84  Channel = packet.Channel;
85  SessionId = packet.SessionId;
86  DeployId = packet.DeployId;
87  Type = packet.Type;
88  UserId = packet.UserId;
89  ProjectId = packet.ProjectId;
90  Results = packet.Results;
91  ProcessingTime = packet.ProcessingTime;
92  }
93  catch (Exception err)
94  {
95  Log.Trace($"LiveResultPacket(): Error converting json: {err}");
96  }
97  }
98 
99  /// <summary>
100  /// Compose Live Result Data Packet - With tradable dates
101  /// </summary>
102  /// <param name="job">Job that started this request</param>
103  /// <param name="results">Results class for the Backtest job</param>
105  :base (PacketType.LiveResult)
106  {
107  try
108  {
109  SessionId = job.SessionId;
110  CompileId = job.CompileId;
111  DeployId = job.DeployId;
112  Results = results;
113  UserId = job.UserId;
114  ProjectId = job.ProjectId;
115  SessionId = job.SessionId;
116  Channel = job.Channel;
117  }
118  catch (Exception err) {
119  Log.Error(err);
120  }
121  }
122 
123  /// <summary>
124  /// Creates an empty result packet, useful when the algorithm fails to initialize
125  /// </summary>
126  /// <param name="job">The associated job packet</param>
127  /// <returns>An empty result packet</returns>
129  {
130  return new LiveResultPacket(job, new LiveResult(new LiveResultParameters(
131  new Dictionary<string, Chart>(), new Dictionary<int, Order>(), new Dictionary<DateTime, decimal>(),
132  new Dictionary<string, Holding>(), new CashBook(), new Dictionary<string, string>(),
133  new SortedDictionary<string, string>(), new List<OrderEvent>(), new Dictionary<string, string>(),
134  new AlgorithmConfiguration(), new Dictionary<string, string>())));
135  }
136  } // End Queue Packet:
137 
138 
139  /// <summary>
140  /// Live results object class for packaging live result data.
141  /// </summary>
142  public class LiveResult : Result
143  {
144  private CashBook _cashBook;
145 
146  /// <summary>
147  /// Holdings dictionary of algorithm holdings information
148  /// </summary>
149  [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
150  public IDictionary<string, Holding> Holdings { get; set; }
151 
152  /// <summary>
153  /// Cashbook for the algorithm's live results.
154  /// </summary>
155  [JsonIgnore]
156  public CashBook CashBook
157  {
158  get
159  {
160  return _cashBook;
161  }
162  set
163  {
164  _cashBook = value;
165 
166  Cash = _cashBook?.ToDictionary(pair => pair.Key, pair => pair.Value);
169  }
170  }
171 
172  /// <summary>
173  /// Cash for the algorithm's live results.
174  /// </summary>
175  [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
176  public Dictionary<string, Cash> Cash { get; set; }
177 
178  /// <summary>
179  /// The algorithm's account currency
180  /// </summary>
181  [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
182  public string AccountCurrency { get; set; }
183 
184  /// <summary>
185  /// The algorithm's account currency
186  /// </summary>
187  [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
188  public string AccountCurrencySymbol { get; set; }
189 
190  /// <summary>
191  /// Default Constructor
192  /// </summary>
193  public LiveResult()
194  { }
195 
196  /// <summary>
197  /// Constructor for the result class for dictionary objects
198  /// </summary>
199  public LiveResult(LiveResultParameters parameters) : base(parameters)
200  {
201  Holdings = parameters.Holdings;
202  CashBook = parameters.CashBook;
203  }
204  }
205 } // End of Namespace: