Пример #1
0
        /// <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);
        }
Пример #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="OrderProperties"/> class
 /// </summary>
 public OrderProperties()
 {
     TimeInForce = TimeInForce.GoodTilCanceled;
 }
Пример #3
0
        /// <summary>
        /// Creates a new Order instance from a SerializedOrder instance
        /// </summary>
        /// <remarks>Used by the <see cref="SerializedOrderJsonConverter"/></remarks>
        public static Order FromSerialized(SerializedOrder serializedOrder)
        {
            var sid    = SecurityIdentifier.Parse(serializedOrder.Symbol);
            var symbol = new Symbol(sid, sid.Symbol);

            TimeInForce timeInForce = null;
            var         type        = System.Type.GetType($"QuantConnect.Orders.TimeInForces.{serializedOrder.TimeInForceType}", throwOnError: false, ignoreCase: true);

            if (type != null)
            {
                timeInForce = (TimeInForce)Activator.CreateInstance(type, true);
                if (timeInForce is GoodTilDateTimeInForce)
                {
                    var expiry = QuantConnect.Time.UnixTimeStampToDateTime(serializedOrder.TimeInForceExpiry.Value);
                    timeInForce = new GoodTilDateTimeInForce(expiry);
                }
            }

            var      createdTime = QuantConnect.Time.UnixTimeStampToDateTime(serializedOrder.CreatedTime);
            DateTime?expiryDate  = null;

            if (serializedOrder.ExpiryDate.HasValue)
            {
                expiryDate = QuantConnect.Time.UnixTimeStampToDateTime(serializedOrder.ExpiryDate.Value);
            }
            OrderReason?orderReason = null;

            if (serializedOrder.Reason != "")
            {
                orderReason = serializedOrder.Reason == "Open" ? OrderReason.Open : OrderReason.Close;
            }

            var order = CreateOrder(serializedOrder.OrderId, serializedOrder.Type, symbol, serializedOrder.Quantity,
                                    DateTime.SpecifyKind(createdTime, DateTimeKind.Utc),
                                    serializedOrder.Tag,
                                    new OrderProperties {
                TimeInForce = timeInForce
            },
                                    serializedOrder.LimitPrice ?? 0,
                                    serializedOrder.StopPrice ?? 0,
                                    expiryDate,
                                    orderReason);

            order.OrderSubmissionData = new OrderSubmissionData(serializedOrder.SubmissionBidPrice,
                                                                serializedOrder.SubmissionAskPrice,
                                                                serializedOrder.SubmissionLastPrice);

            order.BrokerId      = serializedOrder.BrokerId;
            order.ContingentId  = serializedOrder.ContingentId;
            order.Price         = serializedOrder.Price;
            order.PriceCurrency = serializedOrder.PriceCurrency;
            order.Status        = serializedOrder.Status;

            if (serializedOrder.LastFillTime.HasValue)
            {
                var time = QuantConnect.Time.UnixTimeStampToDateTime(serializedOrder.LastFillTime.Value);
                order.LastFillTime = DateTime.SpecifyKind(time, DateTimeKind.Utc);
            }
            if (serializedOrder.LastUpdateTime.HasValue)
            {
                var time = QuantConnect.Time.UnixTimeStampToDateTime(serializedOrder.LastUpdateTime.Value);
                order.LastUpdateTime = DateTime.SpecifyKind(time, DateTimeKind.Utc);
            }
            if (serializedOrder.CanceledTime.HasValue)
            {
                var time = QuantConnect.Time.UnixTimeStampToDateTime(serializedOrder.CanceledTime.Value);
                order.CanceledTime = DateTime.SpecifyKind(time, DateTimeKind.Utc);
            }

            return(order);
        }