Lean  $LEAN_TAG$
SerializedOrderEvent.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 using System.ComponentModel;
17 using Newtonsoft.Json;
18 using Newtonsoft.Json.Converters;
19 
21 {
22  /// <summary>
23  /// Data transfer object used for serializing an <see cref="OrderEvent"/> that was just generated by an algorithm
24  /// </summary>
25  public class SerializedOrderEvent
26  {
27  /// <summary>
28  /// The unique order event id
29  /// </summary>
30  [JsonProperty("id")]
31  public virtual string Id => $"{AlgorithmId}-{OrderId}-{OrderEventId}";
32 
33  /// <summary>
34  /// Algorithm Id, BacktestId or DeployId
35  /// </summary>
36  [JsonProperty("algorithmId")]
37  public string AlgorithmId { get; set; }
38 
39  /// <summary>
40  /// Id of the order this event comes from.
41  /// </summary>
42  [JsonProperty("orderId")]
43  public int OrderId { get; set; }
44 
45  /// <summary>
46  /// The unique order event id for each order
47  /// </summary>
48  [JsonProperty("orderEventId")]
49  public int OrderEventId { get; set; }
50 
51  /// <summary>
52  /// Easy access to the order symbol associated with this event.
53  /// </summary>
54  [JsonProperty("symbol")]
55  public string Symbol { get; set; }
56 
57  /// <summary>
58  /// The mapped symbol value
59  /// </summary>
60  [JsonProperty(PropertyName = "symbolValue")]
61  public string SymbolValue { get; set; }
62 
63  /// <summary>
64  /// The symbols permanent ticker. For equities, by convention this is the first ticker symbol for which the security traded
65  /// </summary>
66  [JsonProperty(PropertyName = "symbolPermtick")]
67  public string SymbolPermtick { get; set; }
68 
69  /// <summary>
70  /// The time of this event in unix timestamp
71  /// </summary>
72  [JsonProperty("time")]
73  public double Time { get; set; }
74 
75  /// <summary>
76  /// Status message of the order.
77  /// </summary>
78  [JsonProperty("status"), JsonConverter(typeof(StringEnumConverter), true)]
79  public OrderStatus Status { get; set; }
80 
81  /// <summary>
82  /// The fee amount associated with the order
83  /// </summary>
84  [JsonProperty("orderFeeAmount", DefaultValueHandling = DefaultValueHandling.Ignore)]
85  public decimal? OrderFeeAmount { get; set; }
86 
87  /// <summary>
88  /// The fee currency associated with the order
89  /// </summary>
90  [JsonProperty("orderFeeCurrency", DefaultValueHandling = DefaultValueHandling.Ignore)]
91  public string OrderFeeCurrency { get; set; }
92 
93  /// <summary>
94  /// Fill price information about the order
95  /// </summary>
96  [JsonProperty("fillPrice")]
97  public decimal FillPrice { get; set; }
98 
99  /// <summary>
100  /// Currency for the fill price
101  /// </summary>
102  [JsonProperty("fillPriceCurrency")]
103  public string FillPriceCurrency { get; set; }
104 
105  /// <summary>
106  /// Number of shares of the order that was filled in this event.
107  /// </summary>
108  [JsonProperty("fillQuantity")]
109  public decimal FillQuantity { get; set; }
110 
111  /// <summary>
112  /// Order direction.
113  /// </summary>
114  [JsonProperty("direction"), JsonConverter(typeof(StringEnumConverter), true)]
115  public OrderDirection Direction { get; set; }
116 
117  /// <summary>
118  /// Any message from the exchange.
119  /// </summary>
120  [DefaultValue(""), JsonProperty("message", DefaultValueHandling = DefaultValueHandling.Ignore)]
121  public string Message { get; set; }
122 
123  /// <summary>
124  /// True if the order event is an assignment
125  /// </summary>
126  [JsonProperty("isAssignment")]
127  public bool IsAssignment { get; set; }
128 
129  /// <summary>
130  /// The current order quantity
131  /// </summary>
132  [JsonProperty("quantity")]
133  public decimal Quantity { get; set; }
134 
135  /// <summary>
136  /// The current stop price
137  /// </summary>
138  [JsonProperty("stopPrice", DefaultValueHandling = DefaultValueHandling.Ignore)]
139  public decimal? StopPrice { get; set; }
140 
141  /// <summary>
142  /// The current limit price
143  /// </summary>
144  [JsonProperty("limitPrice", DefaultValueHandling = DefaultValueHandling.Ignore)]
145  public decimal? LimitPrice { get; set; }
146 
147  /// <summary>
148  /// True if the order event's option is In-The-Money (ITM)
149  /// </summary>
150  [JsonProperty("isInTheMoney", DefaultValueHandling = DefaultValueHandling.Ignore)]
151  public bool IsInTheMoney { get; set; }
152 
153  /// <summary>
154  /// Empty constructor required for JSON converter.
155  /// </summary>
157  {
158  }
159 
160  /// <summary>
161  /// Creates a new instances based on the provided order event and algorithm Id
162  /// </summary>
163  public SerializedOrderEvent(OrderEvent orderEvent, string algorithmId)
164  {
165  AlgorithmId = algorithmId;
166  OrderId = orderEvent.OrderId;
167  OrderEventId = orderEvent.Id;
168  Symbol = orderEvent.Symbol.ID.ToString();
169  SymbolValue = orderEvent.Symbol.Value;
170  SymbolPermtick = orderEvent.Symbol.ID.Symbol;
172  Status = orderEvent.Status;
173  if (orderEvent.OrderFee.Value.Currency != Currencies.NullCurrency)
174  {
175  OrderFeeAmount = orderEvent.OrderFee.Value.Amount;
176  OrderFeeCurrency = orderEvent.OrderFee.Value.Currency;
177  }
178  FillPrice = orderEvent.FillPrice;
180  FillQuantity = orderEvent.FillQuantity;
181  Direction = orderEvent.Direction;
182  Message = orderEvent.Message;
183  IsAssignment = orderEvent.IsAssignment;
184  IsInTheMoney = orderEvent.IsInTheMoney;
185  Quantity = orderEvent.Quantity;
186  StopPrice = orderEvent.StopPrice;
187  LimitPrice = orderEvent.LimitPrice;
188  }
189 
190  #region BackwardsCompatibility
191 
192  [JsonProperty("algorithm-id")]
193  string OldAlgorithmId
194  {
195  set
196  {
197  AlgorithmId = value;
198  }
199  }
200  [JsonProperty("order-id")]
201  int OldOrderId
202  {
203  set
204  {
205  OrderId = value;
206  }
207  }
208  [JsonProperty("order-event-id")]
209  int OldOrderEventId
210  {
211  set
212  {
213  OrderEventId = value;
214  }
215  }
216  [JsonProperty(PropertyName = "symbol-value")]
217  string OldSymbolValue
218  {
219  set
220  {
221  SymbolValue = value;
222  }
223  }
224  [JsonProperty(PropertyName = "symbol-permtick")]
225  string OldSymbolPermtick
226  {
227  set
228  {
229  SymbolPermtick = value;
230  }
231  }
232  [JsonProperty("order-fee-amount", DefaultValueHandling = DefaultValueHandling.Ignore)]
233  decimal? OldOrderFeeAmount
234  {
235  set
236  {
237  OrderFeeAmount = value;
238  }
239  }
240  [JsonProperty("order-fee-currency", DefaultValueHandling = DefaultValueHandling.Ignore)]
241  string OldOrderFeeCurrency
242  {
243  set
244  {
245  OrderFeeCurrency = value;
246  }
247  }
248  [JsonProperty("fill-price")]
249  decimal OldFillPrice
250  {
251  set
252  {
253  FillPrice = value;
254  }
255  }
256  [JsonProperty("fill-price-currency")]
257  string OldFillPriceCurrency
258  {
259  set
260  {
261  FillPriceCurrency = value;
262  }
263  }
264  [JsonProperty("fill-quantity")]
265  decimal OldFillQuantity
266  {
267  set
268  {
269  FillQuantity = value;
270  }
271  }
272  [JsonProperty("is-assignment")]
273  bool OldIsAssignment
274  {
275  set
276  {
277  IsAssignment = value;
278  }
279  }
280  [JsonProperty("stop-price", DefaultValueHandling = DefaultValueHandling.Ignore)]
281  decimal? OldStopPrice
282  {
283  set
284  {
285  StopPrice = value;
286  }
287  }
288  [JsonProperty("limit-price", DefaultValueHandling = DefaultValueHandling.Ignore)]
289  decimal? OldLimitPrice
290  {
291  set
292  {
293  LimitPrice = value;
294  }
295  }
296  [JsonProperty("is-in-the-money", DefaultValueHandling = DefaultValueHandling.Ignore)]
297  bool OldIsInTheMoney
298  {
299  set
300  {
301  IsInTheMoney = value;
302  }
303  }
304  #endregion
305  }
306 }