Пример #1
0
        private void RunObservatoin(Observation observation, CancellationToken token)
        {
            while (!token.IsCancellationRequested)
            {
                if (!_running || observation.RunningStatus != RunningStatus.Running)
                {
                    Thread.Sleep(2 * 1000);
                    continue;
                }
                try
                {
                    var bookBuy  = _exchangeDataService.GetOrderBook(observation.FromExchangeName, observation.CurrencyPair);
                    var bookSell = _exchangeDataService.GetOrderBook(observation.ToExchangeName, observation.CurrencyPair);
                    var timeFrom = _exchangeDataService.GetLastUpdated(observation.FromExchangeName);
                    var timeTo   = _exchangeDataService.GetLastUpdated(observation.ToExchangeName);
                    var ts       = Math.Abs((timeFrom - timeTo).TotalSeconds);
                    var ts2      = Math.Abs((DateTime.UtcNow - timeFrom).TotalSeconds);
                    if (Math.Max(ts, ts2) > 10)
                    {
                        _logger.LogError("Date is not updated");
                        continue;// the data has no be udpated recently;
                    }
                    if (bookBuy.Bids.Count > 0 && bookSell.Asks.Count > 0)
                    {
                        var buy_ask_0    = bookBuy.Asks[0];                    //the sell price of the exchange which we want to buy.
                        var sell_bid_0   = bookSell.Bids[0];                   //the buy price of the exchange which we wat to sell.
                        var spreadValue  = sell_bid_0.Price - buy_ask_0.Price; //some one buy price is greater than the price some one want to sell. then we have a chance to make a arbitrage
                        var spreadVolume = Math.Min(sell_bid_0.Volume, buy_ask_0.Volume);

                        var info = new ArbitrageInfo
                        {
                            SpreadValue  = spreadValue,
                            SpreadVolume = spreadVolume,
                            FromPrice    = buy_ask_0.Price,
                            ToPrice      = sell_bid_0.Price,
                        };

                        var canArbitrage  = _opportunityService.CheckCurrentPrice(observation, info);
                        var lastArbitrage = _opportunityService.CheckLastArbitrage(observation.Id);
                        if (canArbitrage & lastArbitrage)
                        {
                            DoArbitrage(observation, info);
                        }
                    }
                    Thread.Sleep(200);
                }
                catch (Exception ex)
                {
                    _messageService.Error(observation.Id, observation.Name, "Get an unhandled exception" + ex.ToString());
                    observation.RunningStatus = RunningStatus.Error;
                    _observationService.Update(observation);
                    _logger.LogCritical(ex, "RunObservation failed.");
                }
            }
        }
Пример #2
0
        public IActionResult Get()
        {
            var observations = _observationService.GetObservations();
            var list         = new List <WatchModel>();

            foreach (var item in observations)
            {
                var orderBook = new WatchOrderBookModel();

                var sell_book = _exchangeDataService.GetOrderBook(item.ToExchangeName, item.CurrencyPair);
                var buy_book  = _exchangeDataService.GetOrderBook(item.FromExchangeName, item.CurrencyPair);
                var bid1      = new OrderBookItem();
                var ask1      = new OrderBookItem();
                if (sell_book.Bids.Count > 0)
                {
                    bid1           = sell_book.Bids[0];
                    orderBook.Bid1 = bid1.ToString();
                    orderBook.Bid2 = sell_book.Bids[1].ToString();
                    orderBook.Bid3 = sell_book.Bids[2].ToString();
                }
                else
                {
                    orderBook.Bid1 = "";
                    orderBook.Bid2 = "";
                    orderBook.Bid3 = "";
                }
                if (buy_book.Asks.Count > 0)
                {
                    ask1           = buy_book.Asks[0];
                    orderBook.Ask1 = ask1.ToString();
                    orderBook.Ask2 = buy_book.Asks[1].ToString();
                    orderBook.Ask3 = buy_book.Asks[2].ToString();
                }
                else
                {
                    orderBook.Ask1 = "";
                    orderBook.Ask2 = "";
                    orderBook.Ask3 = "";
                }
                orderBook.SpreadValue  = (bid1.Price - ask1.Price).ToString("f2");
                orderBook.SpreadVolume = Math.Min(bid1.Volume, ask1.Volume).ToString("f4");

                var model = new WatchModel()
                {
                    Observation = item,
                    OrderBook   = orderBook
                };
                list.Add(model);
            }
            return(Ok(list));
        }