Beispiel #1
0
        public void SubscribingToKlineStream_Should_TriggerWhenKlineStreamMessageIsReceived()
        {
            // arrange
            var socket = new Mock <IWebsocket>();

            socket.Setup(s => s.Close());
            socket.Setup(s => s.Connect());
            socket.Setup(s => s.Url).Returns("test");
            socket.Setup(s => s.SetEnabledSslProtocols(It.IsAny <System.Security.Authentication.SslProtocols>()));

            var factory = new Mock <IWebsocketFactory>();

            factory.Setup(s => s.CreateWebsocket(It.IsAny <string>())).Returns(socket.Object);

            BinanceStreamKline result = null;
            var client = new BinanceSocketClient {
                SocketFactory = factory.Object
            };

            client.SubscribeToKlineStream("test", KlineInterval.OneMinute, (test) => result = test);

            var data = new BinanceStreamKline()
            {
                Event     = "TestKlineStream",
                EventTime = new DateTime(2017, 1, 1),
                Symbol    = "test",
                Data      = new BinanceStreamKlineInner()
                {
                    ActiveBuyVolume      = 0.1,
                    Close                = 0.2,
                    EndTime              = new DateTime(2017, 1, 2),
                    Final                = true,
                    FirstTrade           = 10000000000,
                    High                 = 0.3,
                    Interval             = KlineInterval.OneMinute,
                    LastTrade            = 2000000000000,
                    Low                  = 0.4,
                    Open                 = 0.5,
                    QuoteActiveBuyVolume = 0.6,
                    QuoteVolume          = 0.7,
                    StartTime            = new DateTime(2017, 1, 1),
                    Symbol               = "test",
                    TradeCount           = 10,
                    Volume               = 0.8
                }
            };

            // act
            socket.Raise(r => r.OnMessage += null, new MessagedEventArgs(JsonConvert.SerializeObject(data), false, false, true, new byte[2]));

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(Compare.PublicInstancePropertiesEqual(data, result, "Data"));
            Assert.IsTrue(Compare.PublicInstancePropertiesEqual(data.Data, result.Data));
        }
Beispiel #2
0
        private async Task SaveCandle(BinanceStreamKline kline, NatsClient natsClient)
        {
            if (kline.Final)
            {
                Candle candle = kline.ToCandle();
                long?  id     = await _candlesProcessor.Create(candle);

                _logger.LogTrace($"Saved candle from {Exchange.Description} - {candle.Symbol}");

                await natsClient.PubAsJsonAsync(_settings.Value.CandlesQueueName, new Notification <Candle>() { Code = ActionCode.CREATED.Code, Payload = candle });
            }
        }
        public static Candle ToCandle(this BinanceStreamKline kline)
        {
            Candle candle = new Candle();

            candle.ExchangeCode   = ExchangeCode.BINANCE.Code;
            candle.Symbol         = kline.Symbol.ToUpper();
            candle.OpenTime       = kline.OpenTime;
            candle.Open           = kline.Open;
            candle.CloseTime      = kline.CloseTime;
            candle.Close          = kline.Close;
            candle.High           = kline.High;
            candle.Low            = kline.Low;
            candle.Volume         = kline.Volume;
            candle.NumberOfTrades = kline.TradeCount;
            candle.PeriodCode     = kline.Interval.ToPeriodCode().Code;
            candle.Time           = new DateTime(kline.OpenTime.Year, kline.OpenTime.Month, kline.OpenTime.Day, kline.OpenTime.Hour, kline.OpenTime.Minute, 0);

            return(candle);
        }