Ejemplo n.º 1
0
        public Session()
        {
            BindHost = string.Empty;
            Host     = "127.0.0.1";
            Port     = 9810;
            UpdateReadonlyAttributes();

            _messageFilters  = new Dictionary <string, bool>();
            Reading          = true;
            AutoWriteFilters = true;
            Reading          = false;

            Messages.MessageAdded += (sender, ev) =>
            {
                OrderBook.Process(ev.Message);
            };

            Messages.Reset += (sender, ev) =>
            {
                OrderBook.Clear(_retain);
            };
        }
Ejemplo n.º 2
0
        private void Run()
        {
            //var subServerTime = Observable.Interval(TimeSpan.FromSeconds(2)).Publish();
            //var subExchangeInfo = Observable.Interval(TimeSpan.FromMinutes(5)).Publish();
            //subServerTime.Subscribe(
            //   (long x) =>
            //   {
            //       var result = client.GetServerTime2();
            //       UpdateStatus();
            //       if (result.Success)
            //       {
            //           ServerTime = result.Data.serverTime.FromUnixTimestamp();
            //       }
            //   }).DisposeWith(Disposables);
            //subExchangeInfo.Subscribe(
            //    (long x) =>
            //    {
            //        var result = client.GetExchangeInfo2();
            //        UpdateStatus();
            //        if (result.Success)
            //        {
            //            ProcessExchangeInfo(result.Data.symbols.Select(CreateSymbolInformation));
            //        }
            //    }).DisposeWith(Disposables);

            var subServerTime   = client.SubscribeServerTime().Publish();
            var subExchangeInfo = client.SubscribeExchangeInfo().Publish();

            var sub24hrPriceTicker   = Observable.Interval(TimeSpan.FromSeconds(10)).Publish();
            var subCurrentSymbol     = this.WhenAnyValue(vm => vm.CurrentSymbol).Where(id => id != null).DistinctUntilChanged().Publish();
            var sub24hrPriceTickerWs = client.SubscribeMarketSummariesAsync(null).Publish();

            subServerTime.Subscribe(
                (Binance.ServerTime x) =>
            {
                ServerTime = x.serverTime.FromUnixTimestamp();
                UpdateStatus();
            }).DisposeWith(Disposables);

            subExchangeInfo.Subscribe(
                (Binance.ExchangeInfo x) =>
            {
                ProcessExchangeInfo(x.symbols.Select(CreateSymbolInformation));
                UpdateStatus();
            }).DisposeWith(Disposables);

            sub24hrPriceTicker.Subscribe(
                (long x) =>
            {
                var result = client.Get24hrPriceTicker2();
                UpdateStatus();
                if (result.Success)
                {
                    Process24hrPriceTicker(result.Data.Select(ToPriceTicker));
                }
            }).DisposeWith(Disposables);

            sub24hrPriceTickerWs.Subscribe(
                (Binance.WsPriceTicker24hr ticker) =>
            {
                OnRefreshMarketSummary2(ToPriceTicker(ticker));
            }).DisposeWith(Disposables);

            subCurrentSymbol.Subscribe(
                (string symbol) =>
            {
                RecentTrades.Clear();
                OrderBook.Clear();
                if (HasTradesPush)
                {
                    var obs = SubscribeRecentTrades(symbol, TradesMaxItemCount);
                    TradesHandle.Disposable = obs.Subscribe(OnTrade);
                }
                if (HasTradesPull)
                {
                    //var obs = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(10));
                    //PublicTradesHandle.Disposable = obs.Subscribe(
                    //    (long x) =>
                    //    {
                    //        var result = client.GetRecentTrades2(symbol, TradesMaxItemCount);
                    //        if (result.Success)
                    //        {
                    //            var trades = result.Data.Select(ToPublicTrade).Reverse().ToList();
                    //            trades.ForEach(t => t.Symbol = symbol);
                    //            ProcessPublicTrades(trades);
                    //        }
                    //    });
                }

                if (HasOrderBookPull)
                {
                    var obs2 = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(2));
                    DepthHandle.Disposable = obs2.Subscribe(
                        (long x) =>
                    {
                        var result = client.GetDepth2(symbol, OrderBookMaxItemCount);
                        if (result.Success)
                        {
                            var depth = result.Data;
                            var asks  = depth.asks.Select(a => new OrderBookEntry()
                            {
                                Price = decimal.Parse(a[0]), Quantity = decimal.Parse(a[1]), Side = TradeSide.Sell
                            });
                            var bids = depth.bids.Select(b => new OrderBookEntry()
                            {
                                Price = decimal.Parse(b[0]), Quantity = decimal.Parse(b[1]), Side = TradeSide.Buy
                            });
                            ProcessOrderBook(asks.Reverse().Concat(bids));
                        }
                    });
                }
            }).DisposeWith(Disposables);

            subServerTime.Connect().DisposeWith(Disposables);
            subExchangeInfo.Connect().DisposeWith(Disposables);
            //sub24hrPriceTicker.Connect().DisposeWith(Disposables);
            subCurrentSymbol.Connect().DisposeWith(Disposables);
            sub24hrPriceTickerWs.Connect().DisposeWith(Disposables);

            TradesHandle.DisposeWith(Disposables);
            DepthHandle.DisposeWith(Disposables);
        }