Ejemplo n.º 1
0
        private void DoArbitrage(Observation observation, ArbitrageInfo info)
        {
            var volume = Math.Min(observation.PerVolume, observation.AvailabeVolume);

            volume = Math.Min(volume, info.SpreadVolume);

            var orderBuyId  = Guid.NewGuid();
            var orderSellId = Guid.NewGuid();
            var buyRequest  = new OrderRequest()
            {
                ClientOrderId = orderBuyId.ToString(),
                CurrencyPair  = observation.CurrencyPair,
                OrderType     = OrderType.Market,
                Price         = 0m,
                TradeType     = TradeType.Buy,
                Volume        = volume,
            };
            var sellRequest = new OrderRequest()
            {
                ClientOrderId = orderSellId.ToString(),
                CurrencyPair  = observation.CurrencyPair,
                OrderType     = OrderType.Market,
                Price         = 0m,
                TradeType     = TradeType.Sell,
                Volume        = volume,
            };
            var buyResult  = _exchangeTradeService.MakeANewOrder(observation.FromExchangeName, buyRequest);
            var sellResult = _exchangeTradeService.MakeANewOrder(observation.ToExchangeName, sellRequest);

            if (!buyResult.IsSuccessful)
            {
                var message = $"Make a buy failed {buyResult.Message} {observation.GetName()}";
                _messageService.Error(observation.Id, observation.Name, message);
                observation.RunningStatus = RunningStatus.Error;
                _logger.LogError(message);
            }
            if (!sellResult.IsSuccessful)
            {
                var message = $"Make a sell order failed {sellResult.Message} {observation.GetName()}";
                _messageService.Error(observation.Id, observation.Name, message);
                observation.RunningStatus = RunningStatus.Error;
                _logger.LogError(message);
            }
            if (buyResult.IsSuccessful ^ sellResult.IsSuccessful)
            {
                var message = $"only one order is executed!!!! buy {buyResult.IsSuccessful} sell {sellResult.IsSuccessful}  {observation.GetName()}";
                _messageService.Error(observation.Id, observation.Name, message);
                _logger.LogCritical(message);
            }
            if (buyResult.IsSuccessful && sellResult.IsSuccessful)
            {
                _observationService.SubtractAvailabeVolume(observation.Id, volume);

                var arbitrage = new Arbitrage
                {
                    DateCreated     = DateTime.UtcNow,
                    Id              = Guid.NewGuid(),
                    ObservationId   = observation.Id,
                    ObservationName = observation.Name,
                    Volume          = volume,
                    Spread          = info.SpreadValue
                };
                _arbitrageService.Add(arbitrage);

                var buyOrder = new Order
                {
                    ArbitrageId     = arbitrage.Id,
                    ObservationId   = arbitrage.ObservationId,
                    ObservationName = arbitrage.ObservationName,
                    DateCreated     = DateTime.UtcNow,
                    ExchangeName    = observation.FromExchangeName,
                    Id           = Guid.NewGuid(),
                    OrderStatus  = buyResult.Data.Status,
                    Price        = info.FromPrice,
                    RemoteId     = buyResult.Data.Id,
                    CurrencyPair = observation.CurrencyPair,
                    TradeType    = TradeType.Buy,
                    Volume       = volume,
                };
                var sellOrder = new Order
                {
                    ArbitrageId     = arbitrage.Id,
                    ObservationId   = arbitrage.ObservationId,
                    ObservationName = arbitrage.ObservationName,
                    DateCreated     = DateTime.UtcNow,
                    ExchangeName    = observation.ToExchangeName,
                    Id           = Guid.NewGuid(),
                    OrderStatus  = buyResult.Data.Status,
                    Price        = info.ToPrice,
                    RemoteId     = buyResult.Data.Id,
                    CurrencyPair = observation.CurrencyPair,
                    TradeType    = TradeType.Sell,
                    Volume       = volume
                };
                _orderService.Add(buyOrder, sellOrder);
                _messageService.Write(observation.Id, observation.Name, $"{observation.Name} do a arbitrage spreadValue:{info.SpreadValue:f2} volume:{volume:f5} {DateTime.UtcNow}");
            }
        }