18 using Newtonsoft.Json;
19 using Newtonsoft.Json.Linq;
50 return typeof(
Order).IsAssignableFrom(objectType);
57 public override void WriteJson(JsonWriter writer,
object value, JsonSerializer serializer)
59 throw new NotImplementedException(
"The OrderJsonConverter does not implement a WriteJson method;.");
69 public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
71 var jObject = JObject.Load(reader);
86 var orderType = (
OrderType)(jObject[
"Type"]?.Value<int>() ?? jObject[
"type"].Value<
int>());
87 var order = CreateOrder(orderType, jObject);
90 order.Id = jObject[
"Id"]?.Value<
int>() ?? jObject[
"id"].Value<int>();
92 var jsonStatus = jObject[
"Status"] ?? jObject[
"status"];
93 var jsonTime = jObject[
"Time"] ?? jObject[
"time"];
94 if (jsonStatus.Type == JTokenType.Integer)
96 order.Status = (
OrderStatus)jsonStatus.Value<
int>();
98 else if (jsonStatus.Type == JTokenType.Null)
107 if (jsonTime !=
null && jsonTime.Type != JTokenType.Null)
109 order.Time = jsonTime.Value<DateTime>();
115 order.Time = (jObject[
"CreatedTime"]?.Value<DateTime>() ?? jObject[
"createdTime"].Value<DateTime>());
118 var orderSubmissionData = jObject[
"OrderSubmissionData"] ?? jObject[
"orderSubmissionData"];
119 if (orderSubmissionData !=
null && orderSubmissionData.Type != JTokenType.Null)
121 var bidPrice = orderSubmissionData[
"BidPrice"]?.Value<decimal>() ?? orderSubmissionData[
"bidPrice"].Value<decimal>();
122 var askPrice = orderSubmissionData[
"AskPrice"]?.Value<decimal>() ?? orderSubmissionData[
"askPrice"].Value<decimal>();
123 var lastPrice = orderSubmissionData[
"LastPrice"]?.Value<decimal>() ?? orderSubmissionData[
"lastPrice"].Value<decimal>();
127 var priceAdjustmentMode = jObject[
"PriceAdjustmentMode"] ?? jObject[
"priceAdjustmentMode"];
128 if (priceAdjustmentMode !=
null && priceAdjustmentMode.Type != JTokenType.Null)
130 var value = priceAdjustmentMode.Value<
int>();
134 var lastFillTime = jObject[
"LastFillTime"] ?? jObject[
"lastFillTime"];
135 var lastUpdateTime = jObject[
"LastUpdateTime"] ?? jObject[
"lastUpdateTime"];
136 var canceledTime = jObject[
"CanceledTime"] ?? jObject[
"canceledTime"];
138 if (canceledTime !=
null && canceledTime.Type != JTokenType.Null)
140 order.CanceledTime = canceledTime.Value<DateTime>();
142 if (lastFillTime !=
null && lastFillTime.Type != JTokenType.Null)
144 order.LastFillTime = lastFillTime.Value<DateTime>();
146 if (lastUpdateTime !=
null && lastUpdateTime.Type != JTokenType.Null)
148 order.LastUpdateTime = lastUpdateTime.Value<DateTime>();
150 var tag = jObject[
"Tag"] ?? jObject[
"tag"];
151 if (tag !=
null && tag.Type != JTokenType.Null)
153 order.Tag = tag.Value<
string>();
157 order.Tag =
string.Empty;
160 order.Quantity = jObject[
"Quantity"]?.Value<decimal>() ?? jObject[
"quantity"].Value<decimal>();
161 var orderPrice = jObject[
"Price"] ?? jObject[
"price"];
162 if (orderPrice !=
null && orderPrice.Type != JTokenType.Null)
164 order.Price = orderPrice.Value<decimal>();
168 order.Price =
default(decimal);
171 var priceCurrency = jObject[
"PriceCurrency"] ?? jObject[
"priceCurrency"];
172 if (priceCurrency !=
null && priceCurrency.Type != JTokenType.Null)
174 order.PriceCurrency = priceCurrency.Value<
string>();
176 order.BrokerId = jObject[
"BrokerId"]?.Select(x => x.Value<
string>()).ToList() ?? jObject[
"brokerId"].Select(x => x.Value<
string>()).ToList();
177 var jsonContingentId = jObject[
"ContingentId"] ?? jObject[
"contingentId"];
178 if (jsonContingentId !=
null && jsonContingentId.Type != JTokenType.Null)
180 order.ContingentId = jsonContingentId.Value<
int>();
183 var timeInForce = jObject[
"Properties"]?[
"TimeInForce"] ?? jObject[
"TimeInForce"] ?? jObject[
"Duration"];
184 if (timeInForce ==
null)
186 timeInForce = jObject[
"properties"]?[
"timeInForce"] ?? jObject[
"timeInForce"] ?? jObject[
"duration"];
188 order.Properties.TimeInForce = (timeInForce !=
null)
189 ? CreateTimeInForce(timeInForce, jObject)
192 if (jObject.SelectTokens(
"Symbol.ID").Any())
195 var ticker = jObject.SelectTokens(
"Symbol.Value").Single().Value<
string>();
198 else if (jObject.SelectTokens(
"symbol.id").Any())
201 var ticker = jObject.SelectTokens(
"symbol.value").Single().Value<
string>();
206 string market =
null;
209 var suppliedMarket = jObject.SelectTokens(
"Symbol.ID.Market") ?? jObject.SelectTokens(
"symbol.ID.Market");
210 if (suppliedMarket.Any())
212 market = suppliedMarket.Single().Value<
string>();
216 var securityType = (
SecurityType)(jObject[
"SecurityType"]?.Value<int>() ?? jObject[
"securityType"].Value<
int>());
218 var symbolValueUpperCase = jObject.SelectTokens(
"Symbol.Value");
219 var symbolValueCamelCase = jObject.SelectTokens(
"symbol.value");
220 string ticker =
default;
221 if (symbolValueUpperCase.Any())
224 ticker = symbolValueUpperCase.Single().Value<
string>();
226 else if (symbolValueCamelCase.Any())
229 ticker = symbolValueCamelCase.Single().Value<
string>();
233 ticker = jObject[
"Symbol"]?.Value<
string>() ?? jObject[
"symbol"]?.Value<string>();
240 order.Symbol =
Symbol.
Create(ticker, securityType, market);
249 private static Order CreateOrder(
OrderType orderType, JObject jObject)
259 order =
new LimitOrder {
LimitPrice = jObject[
"LimitPrice"]?.Value<decimal>() ?? jObject[
"limitPrice"]?.Value<decimal>() ??
default(decimal) };
263 order =
new StopMarketOrder
265 StopPrice = jObject[
"stopPrice"]?.Value<decimal>() ?? jObject[
"StopPrice"]?.Value<decimal>() ??
default(decimal)
270 order =
new StopLimitOrder
272 LimitPrice = jObject[
"LimitPrice"]?.Value<decimal>() ?? jObject[
"limitPrice"]?.Value<decimal>() ??
default(decimal),
273 StopPrice = jObject[
"stopPrice"]?.Value<decimal>() ?? jObject[
"StopPrice"]?.Value<decimal>() ??
default(decimal)
278 order =
new TrailingStopOrder
280 StopPrice = jObject[
"StopPrice"]?.Value<decimal>() ?? jObject[
"stopPrice"]?.Value<decimal>() ??
default(decimal),
281 TrailingAmount = jObject[
"TrailingAmount"]?.Value<decimal>() ?? jObject[
"trailingAmount"]?.Value<decimal>() ??
default(decimal),
282 TrailingAsPercentage = jObject[
"TrailingAsPercentage"]?.Value<
bool>() ?? jObject[
"trailingAsPercentage"]?.Value<bool>() ??
default(
bool)
287 order =
new LimitIfTouchedOrder
289 LimitPrice = jObject[
"LimitPrice"]?.Value<decimal>() ?? jObject[
"limitPrice"]?.Value<decimal>() ??
default(decimal),
290 TriggerPrice = jObject[
"TriggerPrice"]?.Value<decimal>() ?? jObject[
"triggerPrice"]?.Value<decimal>() ??
default(decimal)
295 order =
new MarketOnOpenOrder();
299 order =
new MarketOnCloseOrder();
303 order =
new OptionExerciseOrder();
307 order =
new ComboMarketOrder() { GroupOrderManager = DeserializeGroupOrderManager(jObject) };
311 order =
new ComboLimitOrder() { GroupOrderManager = DeserializeGroupOrderManager(jObject) };
315 order =
new ComboLegLimitOrder
317 GroupOrderManager = DeserializeGroupOrderManager(jObject),
318 LimitPrice = jObject[
"LimitPrice"]?.Value<decimal>() ?? jObject[
"limitPrice"]?.Value<decimal>() ??
default(decimal)
323 throw new ArgumentOutOfRangeException();
331 private static TimeInForce CreateTimeInForce(JToken timeInForce, JObject jObject)
334 if (timeInForce is JValue)
336 var value = timeInForce.
Value<
int>();
341 return TimeInForce.GoodTilCanceled;
344 return TimeInForce.Day;
347 var expiry = jObject[
"DurationValue"].Value<DateTime>();
348 return TimeInForce.GoodTilDate(expiry);
351 throw new Exception($
"Unknown time in force value: {value}");
356 return timeInForce.ToObject<TimeInForce>();
362 private static GroupOrderManager DeserializeGroupOrderManager(JObject jObject)
364 var groupOrderManagerJObject = jObject[
"GroupOrderManager"] ?? jObject[
"groupOrderManager"];
367 if (groupOrderManagerJObject ==
null)
369 throw new ArgumentException(
"OrderJsonConverter.DeserializeGroupOrderManager(): JObject does not have a GroupOrderManager");
372 var result =
new GroupOrderManager(
373 groupOrderManagerJObject[
"Id"]?.Value<int>() ?? groupOrderManagerJObject[
"id"].Value<int>(),
374 groupOrderManagerJObject[
"Count"]?.Value<int>() ?? groupOrderManagerJObject[
"count"].Value<int>(),
375 groupOrderManagerJObject[
"Quantity"]?.Value<decimal>() ?? groupOrderManagerJObject[
"quantity"].Value<decimal>(),
376 groupOrderManagerJObject[
"LimitPrice"]?.Value<decimal>() ?? groupOrderManagerJObject[
"limitPrice"].Value<decimal>()
379 foreach (var orderId
in (groupOrderManagerJObject[
"OrderIds"]?.Values<int>() ?? groupOrderManagerJObject[
"orderIds"].Values<int>()))
381 result.OrderIds.Add(orderId);