private void InitializeSubs() { _subLiquidation = BitmetSocketSubscriptions.CreateLiquidationSubsription(message => { foreach (var dto in message.Data) { dataQueue.Add(dto); } }); _bitmexApiSocketService.Subscribe(_subLiquidation); _subInstrument = BitmetSocketSubscriptions.CreateInstrumentSubsription(message => { foreach (var dto in message.Data) { _lastupdate = DateTime.Now; } }); _bitmexApiSocketService.Subscribe(_subInstrument); dataQueue.CompleteAdding(); dataQueue = new BlockingCollection <LiquidationDto>(new ConcurrentQueue <LiquidationDto>()); Task.Run(async() => { while (!dataQueue.IsCompleted) { LiquidationDto liquidationDto = null; try { liquidationDto = dataQueue.Take(); } catch (InvalidOperationException) { } if (liquidationDto != null) { await HandleLiquidation(liquidationDto); } } Console.WriteLine("\r\nNo more items to take."); }); }
private async Task HandleLiquidation(LiquidationDto dto) { try { using (var context = new CollectorContext()) { _lastupdate = DateTime.Now; if (dto.Price != null) { if (dto.LeavesQty != null) { if (await _collectorContext.Liquidations.FirstOrDefaultAsync(a => a.LiquidationId == dto.OrderId) == null) { Console.WriteLine(DateTime.Now + ": " + dto.Symbol + " " + (dto.Side == "Sell" ? "long" : "short") + " liquidation at " + dto.Price.Value + " with quantity " + dto.LeavesQty.Value); Debug.WriteLine(DateTime.Now + ": " + dto.Symbol + " " + (dto.Side == "Sell" ? "long" : "short") + " liquidation at " + dto.Price.Value + " with quantity " + dto.LeavesQty.Value); await context.Liquidations.AddAsync(new Liquidation() { LiquidationId = dto.OrderId, Symbol = dto.Symbol, Timestamp = DateTime.Now.ToUniversalTime(), Direction = dto.Side == "Sell" ? "Long" : "Short", Price = dto.Price.Value, Quantity = dto.LeavesQty.Value }); await context.SaveChangesAsync(); } } } } } catch (Exception e) { Console.WriteLine(e); } }