Example #1
0
 /// <summary>
 /// Find order by provided unique id
 /// </summary>
 public CryptoOrder FindOrder(string id)
 {
     if (_idToOrder.ContainsKey(id))
     {
         return(_idToOrder[id]);
     }
     return(null);
 }
Example #2
0
        private CryptoOrder ConvertOrder(Order order)
        {
            var id = order.Id.ToString();
            var existingCurrent = ExistingOrders.ContainsKey(id) ? ExistingOrders[id] : null;
            var existingPartial = _partiallyFilledOrders.ContainsKey(id) ? _partiallyFilledOrders[id] : null;
            var existing        = existingPartial ?? existingCurrent;

            var price = Math.Abs(FirstNonZero(order.Price, existing?.Price) ?? 0);

            var amount = Math.Abs(FirstNonZero(order.Amount, existing?.AmountOrig) ?? 0);

            var amountOrig = Math.Abs(order.AmountOrig ?? 0);

            var currentStatus = existing != null &&
                                existing.OrderStatus != CryptoOrderStatus.Undefined &&
                                existing.OrderStatus != CryptoOrderStatus.New &&
                                order.OrderStatus == OrderStatus.Undefined
                ? existing.OrderStatus
                : ConvertOrderStatus(order);

            var newOrder = new CryptoOrder
            {
                //ExchangeName = ExchangeName,

                Pair  = order.Symbol ?? existing?.Pair,
                Price = price,
                //Amount = amount,
                AmountOrig  = amountOrig,
                Side        = ConvertSide(Convert.ToDouble(order.Amount)),
                Id          = id,
                ClientId    = order.Cid.ToString(),
                OrderStatus = ConvertOrderStatus(order.OrderStatus),
                Type        = ConvertOrderType(order.Type.ToString()),
                Created     = order.MtsCreate,
                Updated     = order.MtsUpdate
            };

            if (currentStatus == CryptoOrderStatus.PartiallyFilled)
            {
                // save partially filled orders
                _partiallyFilledOrders[newOrder.Id] = newOrder;
            }

            return(newOrder);
        }
        private CryptoOrder ConvertOrder(OrderResponse order)
        {
            var id              = order?.Id.ToString() ?? "00000";
            var clientId        = order?.ProfileId;
            var existingCurrent = ExistingOrders.ContainsKey(id) ? ExistingOrders[id] : null;
            var existingPartial = _partiallyFilledOrders.ContainsKey(id) ? _partiallyFilledOrders[id] : null;
            var existing        = existingPartial ?? existingCurrent;

            var price = Math.Abs(FirstNonZero(order?.Price, existing?.Price) ?? 0);

            var amount = Math.Abs(FirstNonZero(order?.Amount, existing?.AmountOrig) ?? 0);

            var amountOrig = Math.Abs(order?.Size ?? 0);

            var currentStatus = existing != null &&
                                existing.OrderStatus != CryptoOrderStatus.Undefined &&
                                existing.OrderStatus != CryptoOrderStatus.New &&
                                order?.OrderStatus == OrderStatus.Undefined
                ? existing.OrderStatus
                : ConvertOrderStatus(order);

            var newOrder = new CryptoOrder

            {
                Id    = id,
                Pair  = CryptoPairsHelper.Clean(order?.Pair),
                Price = price,
                //Amount = amount,
                AmountOrig  = amountOrig,
                Side        = ConvertSide(order.Side),
                OrderStatus = ConvertOrderStatus(order),
                Type        = (CryptoOrderType)order.OrderType,
                Created     = ConvertToDatetime(order.MtsCreate),
                ClientId    = clientId
                              //Created = Convert.ToDateTime(order.MtsCreate)
            };

            if (currentStatus == CryptoOrderStatus.PartiallyFilled)
            {
                // save partially filled orders
                _partiallyFilledOrders[newOrder.Id] = newOrder;
            }

            return(newOrder);
        }
        private CryptoOrder ConvertOrder(OrderResponse order)
        {
            var id              = order.Data.Id.ToString();
            var clientId        = order.Data.Id;
            var existingCurrent = ExistingOrders.ContainsKey(id) ? ExistingOrders[id] : null;
            var existingPartial = _partiallyFilledOrders.ContainsKey(id) ? _partiallyFilledOrders[id] : null;
            var existing        = existingPartial ?? existingCurrent;

            var price = Math.Abs(FirstNonZero(order.Data.Price, existing?.Price) ?? 0);

            var amount = Math.Abs(FirstNonZero(order.Data.Amount, existing?.AmountOrig) ?? 0);

            var amountOrig = Math.Abs(order.Data.Amount);

            var currentStatus = existing != null &&
                                existing.OrderStatus != CryptoOrderStatus.Undefined &&
                                existing.OrderStatus != CryptoOrderStatus.New
                                //&&
                                //order.OrderStatus == OrderStatus.Undefined
                ? existing.OrderStatus
                : ConvertOrderStatus(order);

            var newOrder = new CryptoOrder

            {
                Id    = id,
                Pair  = CryptoPairsHelper.Clean(order.Symbol),
                Price = price,
                //Amount = amount,
                AmountOrig  = amountOrig,
                Side        = ConvertSide(order.Data.Amount),
                OrderStatus = ConvertOrderStatus(order),
                Type        = (CryptoOrderType)order.Data.OrderType,
                //TODO
                //Created = ConvertToDatetime(DateTimeOffset.Parse(order.Data.MicroTimeStamp, out dt))
            };

            if (currentStatus == CryptoOrderStatus.PartiallyFilled)
            {
                // save partially filled orders
                _partiallyFilledOrders[newOrder.Id] = newOrder;
            }

            return(newOrder);
        }
        /// <summary>
        /// Convert Bitmex order to crypto order
        /// </summary>
        public CryptoOrder ConvertOrder(Order order)
        {
            var id = order.OrderId;
            var existingCurrent = ExistingOrders.ContainsKey(id) ? ExistingOrders[id] : null;
            var existingPartial = _partiallyFilledOrders.ContainsKey(id) ? _partiallyFilledOrders[id] : null;
            var existing        = existingPartial ?? existingCurrent;

            var price    = Math.Abs(FirstNonZero(order.Price, order.AvgPx, existing?.Price) ?? 0);
            var priceAvg = Math.Abs(FirstNonZero(order.AvgPx, existing?.PriceAverage) ?? 0);

            var isPartial = order.OrdStatus == OrderStatus.Undefined ?
                            existing?.OrderStatus == CryptoOrderStatus.PartiallyFilled :
                            order.OrdStatus == OrderStatus.PartiallyFilled;

            var beforePartialFilledAmount =
                existing?.OrderStatus == CryptoOrderStatus.PartiallyFilled ? Abs(existing.AmountFilledCumulative) : 0;

            var beforePartialFilledAmountQuote =
                existing?.OrderStatus == CryptoOrderStatus.PartiallyFilled ? Abs(existing.AmountFilledCumulativeQuote) : 0;

            var orderQtyQuote  = Abs(order.OrderQty ?? existing?.AmountOrigQuote);
            var cumQtyQuote    = Abs(order.CumQty ?? existing?.AmountFilledCumulativeQuote);
            var leavesQtyQuote = Abs(order.LeavesQty);

            var orderQtyBase  = Abs(ConvertFromContracts(orderQtyQuote, price));
            var cumQtyBase    = Abs(ConvertFromContracts(cumQtyQuote, price));
            var leavesQtyBase = Abs(ConvertFromContracts(order.LeavesQty, price));

            var amountFilledCumulative = FirstNonZero(
                cumQtyBase,
                existing?.AmountFilledCumulative);

            var amountFilledCumulativeQuote = FirstNonZero(
                cumQtyQuote,
                existing?.AmountFilledCumulativeQuote);

            var amountFilled = FirstNonZero(
                cumQtyBase - beforePartialFilledAmount,     // Bitmex doesn't send partial difference when filled
                existing?.AmountFilled);

            var amountFilledQuote = FirstNonZero(
                cumQtyQuote - beforePartialFilledAmountQuote, // Bitmex doesn't send partial difference when filled
                existing?.AmountFilledQuote);

            var amountOrig = isPartial
                ? (cumQtyBase ?? 0) + (leavesQtyBase ?? 0)
                : FirstNonZero(
                orderQtyBase,
                existing?.AmountOrig);

            var amountOrigQuote = isPartial
                ? (cumQtyQuote ?? 0) + (leavesQtyQuote ?? 0)
                : FirstNonZero(
                orderQtyQuote,
                existing?.AmountOrigQuote);

            if (order.Side == BitmexSide.Undefined && existing != null)
            {
                order.Side = existing.AmountGrouped < 0 ? BitmexSide.Sell : BitmexSide.Buy;
            }

            var currentStatus = existing != null &&
                                existing.OrderStatus != CryptoOrderStatus.Undefined &&
                                existing.OrderStatus != CryptoOrderStatus.New &&
                                order.OrdStatus == OrderStatus.Undefined ?
                                existing.OrderStatus :
                                ConvertOrderStatus(order);

            var newOrder = new CryptoOrder
            {
                Id       = id,
                GroupId  = existing?.GroupId ?? null,
                ClientId = !string.IsNullOrWhiteSpace(order.ClOrdId) ?
                           order.ClOrdId :
                           existing?.ClientId,
                Pair                        = order.Symbol ?? existing?.Pair,
                Side                        = order.Side == BitmexSide.Sell ? CryptoOrderSide.Ask : CryptoOrderSide.Bid,
                AmountFilled                = amountFilled,
                AmountFilledCumulative      = amountFilledCumulative,
                AmountOrig                  = amountOrig,
                AmountFilledQuote           = amountFilledQuote,
                AmountFilledCumulativeQuote = amountFilledCumulativeQuote,
                AmountOrigQuote             = amountOrigQuote,
                Created                     = order.TransactTime ?? existing?.Created,
                Updated                     = order.Timestamp ?? existing?.Updated,
                Price                       = price,
                PriceAverage                = priceAvg,
                OrderStatus                 = currentStatus,
                Type                        = existing?.Type ?? ConvertOrderType(order.OrdType),
                TypePrev                    = existing?.TypePrev ?? ConvertOrderType(order.OrdType),
                OnMargin                    = existing?.OnMargin ?? false
            };


            if (currentStatus == CryptoOrderStatus.PartiallyFilled)
            {
                // save partially filled orders
                _partiallyFilledOrders[newOrder.Id] = newOrder;
            }

            return(newOrder);
        }