Example #1
0
 private static OrderCommand CreateCommand(AssetPairRate feedData, decimal volume)
 {
     return(new OrderCommand
     {
         Price = feedData.BestPrice,
         Volume = volume,
         CommandType = OrderCommandTypeEnum.SetOrder,
         Direction = feedData.IsBuy ? OrderDirectionEnum.Buy : OrderDirectionEnum.Sell,
     });
 }
Example #2
0
        public async Task <IActionResult> GetAssetPairRates(string assetPairId)
        {
            if (string.IsNullOrWhiteSpace(assetPairId))
            {
                return(BadRequest(ErrorResponse.Create($"{nameof(assetPairId)} has invalid value")));
            }

            try
            {
                AssetPairRate rate = await _ratesService.Get(assetPairId);

                return(Ok(Mapper.Map <AssetPairResponseModel>(rate)));
            }
            catch (Exception ex)
            {
                _log.Error(ex, null, $"request: {new {assetPairId}.ToJson()}");

                if (ex is ApiRequestException apiException)
                {
                    return(apiException.GenerateErrorResponse());
                }
                throw;
            }
        }
Example #3
0
        private IReadOnlyList <OrderCommand> FixNegativeSpreadAndCreateOrderCommands(decimal ordersVolume, string assetPairId, bool isBuy, decimal newBestPrice)
        {
            AssetPairRate feedData = new AssetPairRate
            {
                BestPrice   = newBestPrice,
                AssetPairId = assetPairId,
                IsBuy       = isBuy,
            };

            AssetPairRate pendingFeedData;

            decimal bid;
            decimal ask;

            _quotes.TryGetValue(assetPairId, out var bestBidAsk);

            if (isBuy)
            {
                bid = newBestPrice;
                ask = bestBidAsk?.Ask ?? decimal.MaxValue;
                _pendingSellRates.TryGetValue(assetPairId, out pendingFeedData);

                if (bid >= ask)
                {
                    if (pendingFeedData != null)
                    {
                        if (bid >= pendingFeedData.BestPrice)
                        {
                            _pendingBuyRates[assetPairId] = feedData;
                            return(null);
                        }
                    }
                    else
                    {
                        _pendingBuyRates[assetPairId] = feedData;
                        return(null);
                    }
                }

                if (pendingFeedData != null)
                {
                    _pendingSellRates.Remove(assetPairId);
                }
            }
            else
            {
                bid = bestBidAsk?.Bid ?? decimal.MinValue;
                ask = newBestPrice;
                _pendingBuyRates.TryGetValue(assetPairId, out pendingFeedData);

                if (bid >= ask)
                {
                    if (pendingFeedData != null)
                    {
                        if (bid >= pendingFeedData.BestPrice)
                        {
                            _pendingSellRates[assetPairId] = feedData;
                            return(null);
                        }
                    }
                    else
                    {
                        _pendingSellRates[assetPairId] = feedData;
                        return(null);
                    }
                }

                if (pendingFeedData != null)
                {
                    _pendingBuyRates.Remove(assetPairId);
                }
            }

            _quotes[assetPairId] = new AssetPairBidAsk
            {
                Ask = ask,
                Bid = bid,
            };

            var orders = new[] { feedData, pendingFeedData }.Where(d => d != null)
            .Select(rate => CreateCommand(rate, ordersVolume)).ToList();


            AppendDeleteCommands(orders);

            return(orders);
        }