Пример #1
0
        private (ImmutableHashSet <string>, ImmutableDictionary <string, ExternalOrderbook>) FindBroken(
            string assetPairId, ImmutableDictionary <string, ExternalOrderbook> enabledOrderbooks)
        {
            if (!_extPricesSettingsService.IsStepEnabled(OrderbookGeneratorStepDomainEnum.FindBroken, assetPairId))
            {
                return(ImmutableHashSet <string> .Empty, enabledOrderbooks);
            }

            // 1.7*bid > ask > bid > 0 (see LWDEV-4587)
            var brokenExchanges = enabledOrderbooks.Values
                                  .Where(o =>
            {
                var bestPrices = _bestPricesService.Calc(o);
                return(bestPrices.BestBid <= 0 ||
                       bestPrices.BestAsk < bestPrices.BestBid ||
                       5m / 3m * bestPrices.BestBid <= bestPrices.BestAsk);
            })
                                  .Select(o => o.ExchangeName)
                                  .ToImmutableHashSet();
            var nonBrokenExchanges = enabledOrderbooks.RemoveRange(brokenExchanges);

            return(brokenExchanges, nonBrokenExchanges);
        }
Пример #2
0
        public Orderbook Aggregate(Orderbook originalOrderbook)
        {
            if (!_extPricesSettingsService.IsStepEnabled(
                    OrderbookGeneratorStepDomainEnum.AggregateOrderbook,
                    originalOrderbook.AssetPairId))
            {
                return(originalOrderbook);
            }

            var settings = _extPricesSettingsService.GetAggregateOrderbookSettings(originalOrderbook.AssetPairId);

            return(new Orderbook(originalOrderbook.AssetPairId,
                                 Aggregate(settings, originalOrderbook.Bids),
                                 Aggregate(settings, originalOrderbook.Asks)));
        }
        public Orderbook Transform(ExternalOrderbook primaryOrderbook,
                                   IReadOnlyDictionary <string, BestPrices> bestPrices)
        {
            var isArbitrageFreeSpreadEnabled = _extPricesSettingsService.IsStepEnabled(
                OrderbookGeneratorStepDomainEnum.GetArbitrageFreeSpread,
                primaryOrderbook.AssetPairId);

            var arbitrageFreeSpread = isArbitrageFreeSpreadEnabled
                ? GetArbitrageFreeSpread(bestPrices)
                : GetArbitrageFreeSpread(
                ImmutableDictionary.CreateRange(bestPrices.Where(p => p.Key == primaryOrderbook.ExchangeName)));
            var primaryBestPrices = bestPrices[primaryOrderbook.ExchangeName];
            var bidShift          = arbitrageFreeSpread.WorstBid - primaryBestPrices.BestBid; // negative
            var askShift          = arbitrageFreeSpread.WorstAsk - primaryBestPrices.BestAsk; // positive
            var volumeMultiplier  =
                _extPricesSettingsService.GetVolumeMultiplier(primaryOrderbook.AssetPairId,
                                                              primaryOrderbook.ExchangeName);
            var priceMarkups = _extPricesSettingsService.GetPriceMarkups(primaryOrderbook.AssetPairId);

            return(Transform(primaryOrderbook, bidShift + priceMarkups.Bid, askShift + priceMarkups.Ask, volumeMultiplier));
        }
        public ExchangeQuality GetPrimaryExchange(string assetPairId,
                                                  ImmutableDictionary <string, ExchangeErrorStateDomainEnum> errors,
                                                  DateTime now, string currentProcessingExchange)
        {
            if (!_extPricesSettingsService.IsStepEnabled(OrderbookGeneratorStepDomainEnum.ChoosePrimary, assetPairId))
            {
                var presetPrimaryExchange = _extPricesSettingsService.GetPresetPrimaryExchange(assetPairId);
                _primaryExchanges[assetPairId] = presetPrimaryExchange;
                return(new ExchangeQuality(presetPrimaryExchange, 0, ExchangeErrorStateDomainEnum.Valid, false, null));
            }

            var exchangeQualities = CalcExchangeQualities(assetPairId, errors, now, currentProcessingExchange);
            var primaryQuality    = CheckPrimaryStatusAndSwitchIfNeeded(assetPairId, exchangeQualities);

            if (primaryQuality == null)
            {
                return(null);
            }

            _stopTradesService.SetPrimaryOrderbookState(assetPairId, primaryQuality.ExchangeName, now,
                                                        primaryQuality.HedgingPreference, primaryQuality.ErrorState);
            return(primaryQuality);
        }