/// <summary> /// Creates a Time In Force of the correct type /// </summary> private static TimeInForce CreateTimeInForce(JToken timeInForce, JObject jObject) { // for backward-compatibility support deserialization of old JSON format if (timeInForce is JValue) { var value = timeInForce.Value <int>(); switch (value) { case 0: return(TimeInForce.GoodTilCanceled); case 1: return(TimeInForce.Day); case 2: var expiry = jObject["DurationValue"].Value <DateTime>(); return(TimeInForce.GoodTilDate(expiry)); default: throw new Exception($"Unknown time in force value: {value}"); } } // convert with TimeInForceJsonConverter return(timeInForce.ToObject <TimeInForce>()); }
/// <summary> /// Converts an Oanda order into a LEAN order. /// </summary> private Order ConvertOrder(JToken order) { var type = order["type"].ToString(); Order qcOrder; var instrument = order["instrument"].ToString(); var id = order["id"].ToString(); var units = order["units"].ConvertInvariant <decimal>(); var createTime = order["createTime"].ToString(); var securityType = SymbolMapper.GetBrokerageSecurityType(instrument); var symbol = SymbolMapper.GetLeanSymbol(instrument, securityType, Market.Oanda); var time = GetTickDateTimeFromString(createTime); var quantity = units; switch (type) { case "MARKET_IF_TOUCHED": var stopOrder = order.ToObject <MarketIfTouchedOrder>(); qcOrder = new StopMarketOrder { StopPrice = stopOrder.Price.ToDecimal() }; break; case "LIMIT": var limitOrder = order.ToObject <OandaLimitOrder>(); qcOrder = new LimitOrder(symbol, quantity, limitOrder.Price.ToDecimal(), time); break; case "STOP": var stopLimitOrder = order.ToObject <StopOrder>(); var price = stopLimitOrder.Price.ConvertInvariant <decimal>(); var limitPrice = stopLimitOrder.PriceBound.ConvertInvariant <decimal>(); qcOrder = new StopLimitOrder(symbol, quantity, price, limitPrice, time); break; case "MARKET": qcOrder = new MarketOrder(); break; default: throw new NotSupportedException( "An existing " + type + " working order was found and is currently unsupported. Please manually cancel the order before restarting the algorithm."); } qcOrder.Status = OrderStatus.None; qcOrder.BrokerId.Add(id); var gtdTime = order["gtdTime"]; if (gtdTime != null) { var expiry = GetTickDateTimeFromString(gtdTime.ToString()); qcOrder.Properties.TimeInForce = TimeInForce.GoodTilDate(expiry); } return(qcOrder); }