Example #1
0
        private void StartLoad()
        {
            try
            {
                IsConnected = _bitmexApiSocketService.Connect();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            if (IsConnected)
            {
                _bitmexApiSocketService.Subscribe(BitmexApiSubscriptionInfo <IEnumerable <InstrumentDto> > .Create(SubscriptionType.instrument,
                                                                                                                   dtos =>
                {
                    foreach (var instrumentDto in dtos)
                    {
                        lock (_syncObj)
                        {
                            var existing = Instruments.FirstOrDefault(a => a.Symbol == instrumentDto.Symbol);
                            if (existing != null)
                            {
                                Mapper.Map <InstrumentDto, InstrumentModel>(instrumentDto, existing);
                            }
                            else
                            {
                                Instruments.Add(Mapper.Map <InstrumentDto, InstrumentModel>(instrumentDto));
                            }
                        }
                    }
                }));

                _bitmexApiSocketService.Subscribe(BitmexApiSubscriptionInfo <IEnumerable <OrderDto> > .Create(SubscriptionType.order,
                                                                                                              dtos =>
                {
                    foreach (var order in dtos)
                    {
                        lock (_syncObjOrders)
                        {
                            var existing = OrderUpdates.FirstOrDefault(a => a.OrderId == order.OrderId);
                            if (existing != null)
                            {
                                Mapper.Map <OrderDto, OrderUpdateModel>(order, existing);
                            }
                            else
                            {
                                OrderUpdates.Add(Mapper.Map <OrderDto, OrderUpdateModel>(order));
                            }
                        }

                        OnPropertyChanged(nameof(OrderUpdates));
                    }
                }));
            }
        }
        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.");
            });
        }
Example #3
0
        public BitmexDOMListener(IBitmexApiSocketService svc, StrategySettings settings)
        {
            _svc = svc;
            _svc.Connect();
            _svc.Subscribe(BitmetSocketSubscriptions.CreateOrderBookL2Subsription(
                               msg =>
            {
                foreach (var dto in msg.Data)
                {
                    if (!string.Equals(dto.Symbol, settings.Instrument.Symbol, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    var tmp = OnDOMChanged;
                    if (tmp != null)
                    {
                        tmp();
                    }
                }
            }
                               ));
        }
        private void StartLoad()
        {
            try
            {
                IsConnected = _bitmexApiSocketService.Connect();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            if (IsConnected)
            {/*
              * _bitmexApiSocketService.Subscribe(BitmetSocketSubscriptions.CreateInstrumentSubsription(
              *     message =>
              *     {
              *         foreach (var instrumentDto in message.Data)
              *         {
              *             lock (_syncObj)
              *             {
              *                 var existing = Instruments.FirstOrDefault(a => a.Symbol == instrumentDto.Symbol);
              *                 if (existing != null && message.Action == BitmexActions.Update)
              *                 {
              *                     Mapper.Map<InstrumentDto, InstrumentModel>(instrumentDto, existing);
              *                 }
              *                 else if (message.Action != BitmexActions.Partial && message.Action != BitmexActions.Delete)
              *                 {
              *                     Instruments.Add(Mapper.Map<InstrumentDto, InstrumentModel>(instrumentDto));
              *                 }
              *             }
              *         }
              *     }));
              */
                _bitmexApiSocketService.Subscribe(BitmetSocketSubscriptions.CreateOrderSubsription(
                                                      message =>
                {
                    lock (_syncObjOrders)
                    {
                        if (message.Action == BitmexActions.Partial)
                        {
                            OrderUpdates.Clear();
                        }

                        foreach (var order in message.Data)
                        {
                            var existing = OrderUpdates.FirstOrDefault(a => a.OrderId == order.OrderId);

                            if (existing != null && message.Action == BitmexActions.Update)
                            {
                                if (order.LeavesQty != null && order.OrderQty != null)
                                {
                                    Mapper.Map <OrderDto, OrderUpdateModel>(order, existing);
                                }

                                if (order.OrdStatus == "Canceled")
                                {
                                    OrderUpdates.Remove(existing);
                                }
                            }
                            else if (message.Action == BitmexActions.Partial || message.Action == BitmexActions.Insert)
                            {
                                OrderUpdates.Add(Mapper.Map <OrderDto, OrderUpdateModel>(order));
                            }
                        }
                        OnPropertyChanged(nameof(OrderUpdates));
                        _listOrderID = "";
                        foreach (var order in OrderUpdates)
                        {
                            _listOrderID += order.OrderId.ToString() + "\n";
                        }
                        OnPropertyChanged(nameof(ListOrderID));
                    }
                }, null));

                /*
                 * _bitmexApiSocketService.Subscribe(BitmetSocketSubscriptions.CreateOrderBook10Subsription(
                 *  message =>
                 *  {
                 *      foreach (var dto in message.Data)
                 *      {
                 *          if (dto.Symbol != "XBTUSD")
                 *          {
                 *              continue;
                 *          }
                 *
                 *          lock (_syncObjOrderBook10)
                 *          {
                 *              OrderBook10 = dto.Asks.Select(a =>
                 *                  new OrderBookModel { Direction = "Sell", Price = a[0], Size = a[1] })
                 *                  .Union(dto.Bids.Select(a =>
                 *                      new OrderBookModel { Direction = "Buy", Price = a[0], Size = a[1] })).ToList();
                 *          }
                 *
                 *          OnPropertyChanged(nameof(OrderBook10));
                 *      }
                 *  }));*/

                _bitmexApiSocketService.Subscribe(BitmetSocketSubscriptions.CreateOrderBookL2_25Subsription(
                                                      message =>
                {
                    List <OrderBookModel> tempBidWall = new List <OrderBookModel>();
                    List <OrderBookModel> tempAskWall = new List <OrderBookModel>();
                    lock (_syncObjOrderBookL2_25)
                    {
                        if (message.Action == BitmexActions.Partial)
                        {
                            OrderBookL2_25.Clear();
                        }
                        foreach (var dto in message.Data)
                        {
                            if (dto.Symbol != _pair)
                            {
                                continue;
                            }

                            if (message.Action == BitmexActions.Insert || message.Action == BitmexActions.Partial)
                            {
                                OrderBookL2_25.Add(Mapper.Map <OrderBookDto, OrderBookModel>(dto));
                            }
                            if (message.Action == BitmexActions.Delete)
                            {
                                var existing = OrderBookL2_25.FirstOrDefault(a => a.Id == dto.Id);
                                if (existing != null)
                                {
                                    OrderBookL2_25.Remove(existing);
                                }
                            }

                            if (message.Action == BitmexActions.Update)
                            {
                                var existing = OrderBookL2_25.FirstOrDefault(a => a.Id == dto.Id);
                                if (existing == null)
                                {
                                    //OrderBookL2.Add(Mapper.Map<OrderBookDto, OrderBookModel>(dto));
                                }
                                else
                                {
                                    existing.Size = dto.Size;
                                }
                            }


                            OnPropertyChanged(nameof(OrderBookL2_25));
                        }

                        MakeBuyBookAndSellBook(tempBidWall, tempAskWall);
                        UpdateBidDepthChart(tempBidWall);
                        UpdateAskDepthChart(tempAskWall);
                    }
                }, _pair));
            }
        }
        private void StartLoad()
        {
            try
            {
                IsConnected = _bitmexApiSocketService.Connect();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            if (IsConnected)
            {
                _bitmexApiSocketService.Subscribe(BitmetSocketSubscriptions.CreateInstrumentSubsription(
                                                      message =>
                {
                    foreach (var instrumentDto in message.Data)
                    {
                        lock (_syncObj)
                        {
                            var existing = Instruments.FirstOrDefault(a => a.Symbol == instrumentDto.Symbol);
                            if (existing != null && message.Action == BitmexActions.Update)
                            {
                                Mapper.Map <InstrumentDto, InstrumentModel>(instrumentDto, existing);
                            }
                            else if (message.Action != BitmexActions.Partial && message.Action != BitmexActions.Delete)
                            {
                                Instruments.Add(Mapper.Map <InstrumentDto, InstrumentModel>(instrumentDto));
                            }
                        }
                    }
                }));

                _bitmexApiSocketService.Subscribe(BitmetSocketSubscriptions.CreateOrderSubsription(
                                                      message =>
                {
                    foreach (var order in message.Data)
                    {
                        lock (_syncObjOrders)
                        {
                            var existing = OrderUpdates.FirstOrDefault(a => a.OrderId == order.OrderId);
                            if (existing != null && message.Action == BitmexActions.Update)
                            {
                                Mapper.Map <OrderDto, OrderUpdateModel>(order, existing);
                            }
                            else if (message.Action != BitmexActions.Partial && message.Action != BitmexActions.Delete)
                            {
                                OrderUpdates.Add(Mapper.Map <OrderDto, OrderUpdateModel>(order));
                            }
                        }

                        OnPropertyChanged(nameof(OrderUpdates));
                    }
                }));

                _bitmexApiSocketService.Subscribe(BitmetSocketSubscriptions.CreateOrderBook10Subsription(
                                                      message =>
                {
                    foreach (var dto in message.Data)
                    {
                        if (dto.Symbol != "XBTUSD")
                        {
                            continue;
                        }

                        lock (_syncObjOrderBook10)
                        {
                            OrderBook10 = dto.Asks.Select(a =>
                                                          new OrderBookModel {
                                Direction = "Sell", Price = a[0], Size = a[1]
                            })
                                          .Union(dto.Asks.Select(a =>
                                                                 new OrderBookModel {
                                Direction = "Buy", Price = a[0], Size = a[1]
                            })).ToList();
                        }

                        OnPropertyChanged(nameof(OrderBook10));
                    }
                }));

                _bitmexApiSocketService.Subscribe(BitmetSocketSubscriptions.CreateOrderBookL2Subsription(
                                                      message =>
                {
                    foreach (var dto in message.Data)
                    {
                        if (dto.Symbol != "XBTUSD")
                        {
                            continue;
                        }

                        lock (_syncObjOrderBookL2)
                        {
                            if (message.Action == BitmexActions.Insert || message.Action == BitmexActions.Partial)
                            {
                                OrderBookL2.Add(Mapper.Map <OrderBookDto, OrderBookModel>(dto));
                            }
                            if (message.Action == BitmexActions.Delete)
                            {
                                var existing = OrderBookL2.FirstOrDefault(a => a.Id == dto.Id);
                                if (existing != null)
                                {
                                    OrderBookL2.Remove(existing);
                                }
                            }

                            if (message.Action == BitmexActions.Update)
                            {
                                var existing = OrderBookL2.FirstOrDefault(a => a.Id == dto.Id);
                                if (existing == null)
                                {
                                    OrderBookL2.Add(Mapper.Map <OrderBookDto, OrderBookModel>(dto));
                                }
                                else
                                {
                                    Mapper.Map <OrderBookDto, OrderBookModel>(dto, existing);
                                }
                            }
                        }

                        OnPropertyChanged(nameof(OrderBook10));
                    }
                }));
            }
        }