Пример #1
0
        private async Task <double> GetCurrencyRateAsync(AssetPair assetPair, string baseAssetId)
        {
            if (assetPair.QuotingAssetId == baseAssetId)
            {
                return(1);
            }

            if ((assetPair.QuotingAssetId != baseAssetId) && (assetPair.BaseAssetId == baseAssetId))
            {
                var asset = await _assetPairQuoteRepository.GetByIdAsync(assetPair.Id);

                return(asset.Ask);
            }

            var requiredAssetPair = await _dictionaryProxy.GetAssetPairAsync(baseAssetId, assetPair.QuotingAssetId);

            if (requiredAssetPair != null)
            {
                var assetPairQuote = await _assetPairQuoteRepository.GetByIdAsync(requiredAssetPair.Id);

                return(assetPairQuote.Ask);
            }
            else
            {
                requiredAssetPair = await _dictionaryProxy.GetAssetPairAsync(assetPair.QuotingAssetId, baseAssetId);

                var assetPairQuote = await _assetPairQuoteRepository.GetByIdAsync(requiredAssetPair.Id);

                return(1 / assetPairQuote.Ask);
            }
        }
        public async Task <OrderInfo> OpenOrderAsync(string accountId, string assetPairId, double volume, double definedPrice)
        {
            if (!double.IsNaN(definedPrice))
            {
                var orderInfo = new PendingOrder
                {
                    ClientId     = accountId,
                    AssetPairId  = assetPairId,
                    Volume       = volume,
                    Id           = Guid.NewGuid().ToString(),
                    CreatedAt    = DateTime.UtcNow,
                    DefinedPrice = definedPrice
                };

                await _pendingOrderRepository.AddAsync(orderInfo);

                return(Mapper.Map <OrderInfo>(orderInfo));
            }
            else
            {
                var currentQuote = await _assetPairQuoteRepository.GetByIdAsync(assetPairId);

                if (currentQuote == null)
                {
                    throw new InvalidOperationException();
                }

                var orderInfo = new MarketOrder
                {
                    ClientId    = accountId,
                    AssetPairId = assetPairId,
                    Volume      = volume,
                    Id          = Guid.NewGuid().ToString(),
                    CreatedAt   = currentQuote.DateTime
                };

                orderInfo.Price = orderInfo.OrderAction == OrderAction.Buy ? currentQuote.Ask : currentQuote.Bid;

                await _marketOrderRepository.AddAsync(orderInfo);

                return(Mapper.Map <OrderInfo>(orderInfo));
            }
        }