コード例 #1
0
 public void Unsubscribe(string symbol, CandleStickInterval interval)
 {
     this.Unsubscribe(new List <string>()
     {
         symbol
     }, interval);
 }
コード例 #2
0
        public void Subscribe(List <string> symbols, CandleStickInterval interval)
        {
            SubscriptionOptions subOptions = new SubscriptionOptions
            {
                Method  = "subscribe",
                Topic   = "kline_" + interval.GetAttribute <Descriptor>().Identifier,
                Symbols = symbols
            };

            if (!this.symbolsSubscribedTo.ContainsKey(interval))
            {
                this.symbolsSubscribedTo.Add(interval, new List <string>());
            }

            foreach (string symbol in subOptions.Symbols)
            {
                if (!this.symbolsSubscribedTo[interval].Contains(symbol))
                {
                    this.symbolsSubscribedTo[interval].Add(symbol);
                }
                else
                {
                    subOptions.Symbols.Remove(symbol);
                }
            }

            this.WebSocket.Send(JsonConvert.SerializeObject(subOptions));
        }
コード例 #3
0
        public void Unsubscribe(List <string> symbols, CandleStickInterval interval)
        {
            SubscriptionOptions subOptions = new SubscriptionOptions
            {
                Method  = "unsubscribe",
                Topic   = "kline_" + interval.GetAttribute <Descriptor>().Identifier,
                Symbols = symbols
            };

            this.symbolsSubscribedTo[interval].RemoveAll(x => subOptions.Symbols.Contains(x));

            this.WebSocket.Send(JsonConvert.SerializeObject(subOptions));
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: DefiDax/BinanceDex
        public static async Task SingleStream()
        {
            #region IoC, logging, configuration setup

            _height = 0;
            _locker = new object();

            IConfigurationRoot configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                                               .AddJsonFile("appsettings.json", true, false)
                                               .Build();

            ServiceProvider services = new ServiceCollection().AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace)
                                                                          .AddFile(configuration.GetSection("Logging:File")))
                                       .AddDexApi(configuration)
                                       .AddDexWebSockets(configuration)
                                       .AddAutoMapperConfiguration()
                                       .BuildServiceProvider();
            #endregion

            #region Options

            // Choose the symbol you want to track, the limit of the cache and the interval you want to track.
            const string symbol = @"OWT-303_BNB";
            const int    limit  = 30;
            const CandleStickInterval interval = CandleStickInterval.Minutes_1;

            #endregion

            // Get Services
            IBinanceDexApi        api                  = services.GetService <IBinanceDexApi>();
            IMapper               objectMapper         = services.GetService <IMapper>();
            ICandleStickWebSocket candleStickWebSocket = services.GetService <ICandleStickWebSocket>();


            // Get initial kline history from API and print to console
            IEnumerable <CandleStick> candles = await api.GetCandleSticksAsync(new GetCandleStickOptions { Symbol = symbol, Interval = interval, Limit = limit });

            Display(candles);


            // Storing the history in a sorted dictionary which we'll use as a cache when using the websockets
            Dictionary <long, CandleStick>       csTemp           = candles.ToDictionary(candleStick => candleStick.OpenTime, candleStick => candleStick);
            SortedDictionary <long, CandleStick> candleStickCache = new SortedDictionary <long, CandleStick>(csTemp, new ReverseComparer <long>());


            // Configuring the websocket
            candleStickWebSocket.OnDisconnect        += (sender, eventArgs) => Console.WriteLine("Disconnected");
            candleStickWebSocket.OnCandleStickUpdate += (sender, update) =>
            {
                // Using a lock to avoid competing for the console window
                lock (_locker)
                {
                    // In case data arrives out of order, check event height is > previous height
                    if (update.EventHeight < _height)
                    {
                        return;
                    }

                    _height = update.EventHeight;

                    // Update our cache
                    candleStickCache.AddOrReplace(update.CandleStick.OpenTime, objectMapper.Map <CandleStick>(update.CandleStick));

                    // Remove old candles as new ones arrive, maintaining the buffer specified in the `limit` const
                    if (candleStickCache.Count > limit)
                    {
                        candleStickCache.Skip(limit)
                        .ToList()
                        .ForEach(c => candleStickCache.Remove(c.Key));
                    }

                    // Print to console
                    Display(candleStickCache.Select(x => x.Value).Reverse());
                }
            };

            candleStickWebSocket.Connect();
            candleStickWebSocket.Subscribe(symbol, interval);
            await Console.In.ReadLineAsync();

            candleStickWebSocket.UnsubscribeAll();
            candleStickWebSocket.Close();
            candleStickWebSocket.Dispose();
        }