Esempio n. 1
0
        public async Task <string> RePlaceOrderAsync(string orderId, string symbol, decimal price, decimal size, MarketSide side)
        {
            try
            {
                var request = new BulkLimitOrderRequest();
                request.AssetPairId          = symbol;
                request.CancelPreviousOrders = false;
                request.Orders.Add(new BulkOrder()
                {
                    OldId  = orderId,
                    Side   = side == MarketSide.Long ? Side.Buy : Side.Sell,
                    Price  = price.ToString(CultureInfo.InvariantCulture),
                    Volume = size.ToString(CultureInfo.InvariantCulture)
                });

                var result = await Client.PrivateApi.PlaceBulkLimitOrderAsync(request);

                if (result.Error != null)
                {
                    Console.WriteLine($"ERROR: Cannot PlaceBulkLimitOrderAsync ({orderId}): {result.Error.Message}");
                    return(null);
                }

                var status = result.Payload.Statuses.FirstOrDefault();

                if (status == null || status.Error != ErrorCode.Success)
                {
                    Console.WriteLine($"ERROR: Cannot PlaceBulkLimitOrderAsync ({orderId}): {status.Error}");
                    return(null);
                }

                return(status.Id);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"ERROR: Cannot RePlaceOrder ({orderId}):");
                Console.WriteLine(ex);
                return(null);
            }
        }
        public override async Task <BulkLimitOrderResponse> PlaceBulkLimitOrder(BulkLimitOrderRequest request, ServerCallContext context)
        {
            var walletId = context.GetHttpContext().User.GetWalletId();

            var items = request.Orders?.ToArray() ?? Array.Empty <BulkOrder>();

            var orders = new List <MultiOrderItemModel>();

            foreach (var item in items)
            {
                var order = new MultiOrderItemModel
                {
                    Id          = Guid.NewGuid().ToString(),
                    Price       = Convert.ToDouble(item.Price),
                    Volume      = Convert.ToDouble(item.Volume),
                    OrderAction = _mapper.Map <OrderAction>(item.Side),
                    OldId       = string.IsNullOrEmpty(item.OldId) ? null : item.OldId
                };

                orders.Add(order);
            }

            var multiOrder = new MultiLimitOrderModel
            {
                Id                   = Guid.NewGuid().ToString(),
                ClientId             = walletId,
                AssetPairId          = request.AssetPairId,
                CancelPreviousOrders = request.CancelPreviousOrders,
                Orders               = orders.ToArray()
            };

            if (request.OptionalCancelModeCase != BulkLimitOrderRequest.OptionalCancelModeOneofCase.None)
            {
                multiOrder.CancelMode = _mapper.Map <Lykke.MatchingEngine.Connector.Models.Api.CancelMode>(request.CancelMode);
            }

            MultiLimitOrderResponse response = await _matchingEngineClient.PlaceMultiLimitOrderAsync(multiOrder);

            if (response == null)
            {
                return(new BulkLimitOrderResponse
                {
                    Error = new Error
                    {
                        Code = _mapper.Map <ErrorCode>(HftApiErrorCode.MeRuntime),
                        Message = "ME not available"
                    }
                });
            }

            var bulkResponse = new BulkLimitOrderResponse
            {
                Payload = new BulkLimitOrderResponse.Types.BulkLimitOrderPayload
                {
                    AssetPairId = request.AssetPairId
                }
            };

            bulkResponse.Payload.Statuses.AddRange(response.Statuses?.Select(x => new BulkOrderItemStatus
            {
                Id     = x.Id,
                Price  = x.Price.ToString(CultureInfo.InvariantCulture),
                Volume = x.Volume.ToString(CultureInfo.InvariantCulture),
                Error  = _mapper.Map <ErrorCode>(x.Status.ToHftApiError().code)
            }));

            return(bulkResponse);
        }