Exemplo n.º 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.");
                }
            }
        }
Exemplo n.º 2
0
 public IActionResult Put([FromBody] Observation model)
 {
     _observationService.Update(model);
     return(NoContent());
 }