Esempio n. 1
0
        public void Calculate_Limit_Orders()
        {
            // arrange

            var quote = new Quote("BTCUSD", DateTime.UtcNow, 6000, 5000, "lykke");

            var instrument = new Instrument
            {
                AssetPairId = "BTCUSD",
                QuoteSource = "lykke",
                Mode        = InstrumentMode.Active,
                Levels      = 3,
                Markup      = .1m,
                MinSpread   = .01m,
                MaxSpread   = .06m
            };

            decimal volume = 10;

            var expectedLimitOrders = new[]
            {
                new LimitOrder(6630.250m, 3.33333333m, LimitOrderType.Sell),
                new LimitOrder(6705.875m, 3.33333333m, LimitOrderType.Sell),
                new LimitOrder(6781.500m, 3.33333333m, LimitOrderType.Sell)
            };

            // act

            IReadOnlyCollection <LimitOrder> actualLimitOrders =
                EvenlyDistributedVolumeTrader.Calculate(quote, instrument, volume, 3, 8);

            // assert

            Assert.IsTrue(AreEqual(expectedLimitOrders, actualLimitOrders));
        }
Esempio n. 2
0
        private async Task ProcessInstrumentAsync(Instrument instrument)
        {
            Quote quote = _quoteService.Get(instrument.QuoteSource, instrument.AssetPairId);

            if (quote == null)
            {
                _log.WarningWithDetails("No quote for instrument", new { instrument.AssetPairId });
                return;
            }

            AssetPair assetPair = _assetPairsReadModelRepository.TryGetIfEnabled(instrument.AssetPairId);

            if (assetPair == null)
            {
                _log.WarningWithDetails("Asset pair not found", new { instrument.AssetPairId });
                return;
            }

            Balance balance = await _balanceService.GetByAssetIdAsync(assetPair.BaseAssetId);

            IReadOnlyCollection <LimitOrder> limitOrders = EvenlyDistributedVolumeTrader.Calculate(quote, instrument,
                                                                                                   balance.Amount, assetPair.Accuracy, assetPair.InvertedAccuracy);

            await ValidateQuoteAsync(limitOrders, quote);

            ValidateMinVolume(limitOrders, assetPair.MinVolume);

            await _orderBookService.UpdateAsync(new OrderBook
            {
                AssetPairId = instrument.AssetPairId,
                Time        = DateTime.UtcNow,
                LimitOrders = limitOrders
            });

            if (instrument.Mode != InstrumentMode.Active)
            {
                SetError(limitOrders, LimitOrderError.Idle);
            }

            limitOrders = limitOrders
                          .Where(o => o.Error == LimitOrderError.None)
                          .ToArray();

            try
            {
                await _lykkeExchangeService.ApplyAsync(instrument.AssetPairId, limitOrders);
            }
            catch (Exception exception)
            {
                _log.WarningWithDetails("An error occurred while processing limit orders", exception, limitOrders);

                SetError(limitOrders, LimitOrderError.Unknown, "ME error");
            }
        }