Пример #1
0
 /// <summary>
 /// Create a new instance
 /// </summary>
 /// <param name="symbol">The symbol of the order book</param>
 /// <param name="options">The options for the order book</param>
 public BinanceSymbolOrderBook(string symbol, BinanceOrderBookOptions options = null) : base(symbol, options ?? new BinanceOrderBookOptions())
 {
     limit        = options?.Limit;
     restClient   = new BinanceClient();
     socketClient = new BinanceSocketClient();
 }
Пример #2
0
        public void InitializeWebsocket()
        {
            _binanceSocketClient = new binance.BinanceSocketClient();
            _binanceSocketClient.SubscribeToAllSymbolTickerUpdates((data) =>
            {
                //TODO new tick event
                //TODO other items to register to this event - Trade Execution
                List<string> tickers = ActiveInstruments.Where(a => a.Exchange == ExchangeName.Binance).Select(x => x.Symbol).ToList();
                var BinanceTicks = data.Where(s => tickers.Contains(s.Symbol)).ToList();
                for (byte num = 0; num < BinanceTicks.Count(); num++)
                {
                    if (ins != null)
                    {
                        Prices[ins] = BinanceTicks[num].CurrentDayClosePrice;
                        askQuotes[ins] = BinanceTicks[num].BestAskPrice;
                        bidQuotes[ins] = BinanceTicks[num].BestBidPrice;
                    }
                }
            });
            if (_listenKey == null)
            {
                var bin = _binanceClient.StartUserStream();
                if (bin.Success)
                {
                    _listenKey = bin.Data;
                }
                else
                {
                    HandleErrors(bin.Error);
                }
            }
            _binanceSocketClient.SubscribeToUserDataUpdates(_listenKey,
            (accountInfoUpdate) =>
            {
                //TODO new position event
                var BinanceStreamAccount = accountInfoUpdate;
                var BinanceStreamPositions = accountInfoUpdate.Balances.Where(a => a.Total > 0).ToList();
                List<Position> ballist = new List<Position>();
                for (byte n = 0; n < BinanceStreamPositions.Count(); n++)
                {
                    Position bal = new Position(
                        BinanceStreamPositions[n].Asset,
                        ExchangeName.Binance,
                        BinanceStreamPositions[n].Asset == "BNB" ? Math.Min(0, BinanceStreamPositions[n].Total - 1) : BinanceStreamPositions[n].Total
                        );
                    ballist.Add(bal);
                }
                BinanceLivePositions = ballist.Where(a => a.Total > 0 || a.Asset == "BNB").ToList();
            },
            (orderInfoUpdate) =>
            {
                //TODO new update order event

                var BinanceStreamOrders = orderInfoUpdate;
                Order binanceOrder = ConvertOrder(BinanceStreamOrders);
                if (BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).Any())
                {
                    //UpdateOrderEvent
                    if (BinanceStreamOrders.Status == Binance.Net.Objects.OrderStatus.Canceled || BinanceStreamOrders.Status == Binance.Net.Objects.OrderStatus.Filled)
                    {
                        for (byte n = 0; n < BinanceLiveOrders.Count(); n++)
                        {
                            if (BinanceLiveOrders[n].ClientOrderId == BinanceStreamOrders.OriginalClientOrderId.ToString())
                            {
                                BinanceLiveOrders.RemoveAt(n);
                                if (BinanceStreamOrders.Status == OrderStatus.Filled && ActiveInstruments.Where(a => a.Symbol.Contains(BinanceStreamOrders.Symbol)).Any())
                                {
                                    if (BinanceStreamOrders.Side == OrderSide.Buy)
                                    {
                                        SymbolData[ActiveInstruments.Single(a => a.Symbol.Contains(BinanceStreamOrders.Symbol))].EntryPrice = BinanceStreamOrders.Price;
                                    }
                                    else
                                    {
                                        SymbolData[ActiveInstruments.Single(a => a.Symbol.Contains(BinanceStreamOrders.Symbol))].EntryPrice = 0;
                                    }
                                }
                                break;
                            }
                        }
                    }
                    else
                    {
                        //NewOrderEvent
                        BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).First().ClientOrderId = BinanceStreamOrders.ClientOrderId;
                        BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).First().Time = BinanceStreamOrders.Time;
                        BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).First().IcebergQuantity = BinanceStreamOrders.IcebergQuantity;
                        BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).First().StopPrice = BinanceStreamOrders.StopPrice;
                        BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).First().Side = BinanceStreamOrders.Side;
                        BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).First().Type = BinanceStreamOrders.Type;
                        BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).First().TimeInForce = BinanceStreamOrders.TimeInForce;
                        BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).First().UpdateTime = BinanceStreamOrders.EventTime;
                        BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).First().Status = BinanceStreamOrders.Status;
                        BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).First().ExecutedQuantity = BinanceStreamOrders.AccumulatedQuantityOfFilledTrades;
                        BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).First().OriginalQuantity = BinanceStreamOrders.CummulativeQuoteQuantity;
                        BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).First().Price = BinanceStreamOrders.Price;
                        BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).First().Symbol = BinanceStreamOrders.Symbol;
                        BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).First().CummulativeQuoteQuantity = BinanceStreamOrders.Quantity;
                        BinanceLiveOrders.Where(a => a.OrderId == BinanceStreamOrders.OrderId).First().IsWorking = BinanceStreamOrders.IsWorking;
                    }
                }
            },
            (ocoOrderUpdate) =>
            {
            },
            (accountPositionUpdate) =>
            {

            },
            (accountBalanceUpdate) =>
            {

            });
        }
 public BinanceSymbolOrderBook(string symbol, int?limit = null, LogVerbosity logVerbosity = LogVerbosity.Info, IEnumerable <TextWriter> logWriters = null) : base("Binance", symbol, limit == null, logVerbosity, logWriters)
 {
     this.limit   = limit;
     restClient   = new BinanceClient();
     socketClient = new BinanceSocketClient();
 }