Esempio n. 1
0
        public void UnsubscribingAll_Should_CloseAllSockets()
        {
            // arrange
            int closed = 0;
            var socket = new Mock <IWebsocket>();

            socket.Setup(s => s.Close()).Raises(s => s.OnClose += null, new ClosedEventArgs(0, "", true));
            socket.Setup(s => s.Connect());
            socket.Setup(s => s.Url).Returns("test");
            socket.Setup(s => s.SetEnabledSslProtocols(It.IsAny <System.Security.Authentication.SslProtocols>()));
            socket.Object.OnClose += (sender, args) =>
            {
                closed++;
            };

            var factory = new Mock <IWebsocketFactory>();

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

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

            client.SubscribeToTradesStream("test", (data) => { });
            client.SubscribeToDepthStream("test", (data) => { });

            // act
            client.UnsubscribeAllStreams();

            // assert
            Assert.IsTrue(closed == 2);
        }
        private void BinanceWebsocketHandleEvents()
        {
            // SUBSCRIBE
            var sub = m_socketClient.SubscribeToAllSymbolTicker(data =>
            {
                Console.WriteLine("Reveived list update");
            });

            // HANDLE EVENTS
            sub.Data.Closed += () =>
            {
                Console.WriteLine("Socket closed");
            };

            sub.Data.Error += (e) =>
            {
                Console.WriteLine("Socket error " + e.Message);
            };

            Thread.Sleep(15000);

            // UNSUBSCRIBE
            m_socketClient.UnsubscribeFromStream(sub.Data);

            // Additionaly, all sockets can be closed with the UnsubscribeAllStreams method.
            m_socketClient.UnsubscribeAllStreams();
        }
Esempio n. 3
0
 internal void UnsubscribeAllStreams()
 {
     socketClient.UnsubscribeAllStreams();
 }
Esempio n. 4
0
        static void Main(string[] args)
        {
            BinanceDefaults.SetDefaultApiCredentials("APIKEY", "APISECRET");
            BinanceDefaults.SetDefaultLogVerbosity(LogVerbosity.Debug);
            BinanceDefaults.SetDefaultLogOutput(Console.Out);

            using (var client = new BinanceClient())
                using (var socketClient = new BinanceSocketClient())
                {
                    // Public
                    var ping          = client.Ping();
                    var serverTime    = client.GetServerTime();
                    var orderBook     = client.GetOrderBook("BNBBTC", 10);
                    var aggTrades     = client.GetAggregatedTrades("BNBBTC", startTime: DateTime.UtcNow.AddMinutes(-2), endTime: DateTime.UtcNow, limit: 10);
                    var klines        = client.GetKlines("BNBBTC", KlineInterval.OneHour, startTime: DateTime.UtcNow.AddHours(-10), endTime: DateTime.UtcNow, limit: 10);
                    var prices24h     = client.Get24HPrices("BNBBTC");
                    var allPrices     = client.GetAllPrices();
                    var allBookPrices = client.GetAllBookPrices();

                    // Private
                    var openOrders      = client.GetOpenOrders("BNBBTC");
                    var allOrders       = client.GetAllOrders("BNBBTC");
                    var testOrderResult = client.PlaceTestOrder("BNBBTC", OrderSide.Buy, OrderType.Limit, TimeInForce.GoodTillCancel, 1, 1);
                    var queryOrder      = client.QueryOrder("BNBBTC", allOrders.Data[0].OrderId);
                    var orderResult     = client.PlaceOrder("BNBBTC", OrderSide.Sell, OrderType.Limit, TimeInForce.GoodTillCancel, 10, 0.0002);
                    var cancelResult    = client.CancelOrder("BNBBTC", orderResult.Data.OrderId);
                    var accountInfo     = client.GetAccountInfo();
                    var myTrades        = client.GetMyTrades("BNBBTC");


                    // Withdrawal/deposit
                    var withdrawalHistory = client.GetWithdrawHistory();
                    var depositHistory    = client.GetDepositHistory();
                    var withdraw          = client.Withdraw("ASSET", "ADDRESS", 0);


                    // Streams
                    var successDepth = socketClient.SubscribeToDepthStream("bnbbtc", (data) =>
                    {
                        // handle data
                    });
                    var successTrades = socketClient.SubscribeToTradesStream("bnbbtc", (data) =>
                    {
                        // handle data
                    });
                    var successKline = socketClient.SubscribeToKlineStream("bnbbtc", KlineInterval.OneMinute, (data) =>
                    {
                        // handle data
                    });

                    var successStart   = client.StartUserStream();
                    var successAccount = socketClient.SubscribeToAccountUpdateStream(successStart.Data.ListenKey, (data) =>
                    {
                        // handle data
                    });
                    var successOrder = socketClient.SubscribeToOrderUpdateStream(successStart.Data.ListenKey, (data) =>
                    {
                        // handle data
                    });

                    socketClient.UnsubscribeFromStream(successDepth.Data);
                    socketClient.UnsubscribeFromAccountUpdateStream();
                    socketClient.UnsubscribeAllStreams();
                }

            Console.ReadLine();
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            BinanceClient.SetDefaultOptions(new BinanceClientOptions()
            {
                ApiCredentials = new ApiCredentials("APIKEY", "APISECRET"),
                LogVerbosity   = LogVerbosity.Debug,
                LogWriters     = new List <TextWriter> {
                    Console.Out
                }
            });
            BinanceSocketClient.SetDefaultOptions(new BinanceSocketClientOptions()
            {
                ApiCredentials = new ApiCredentials("APIKEY", "APISECRET"),
                LogVerbosity   = LogVerbosity.Debug,
                LogWriters     = new List <TextWriter> {
                    Console.Out
                }
            });

            using (var client = new BinanceClient())
            {
                // Public
                var ping             = client.Ping();
                var exchangeInfo     = client.GetExchangeInfo();
                var serverTime       = client.GetServerTime();
                var orderBook        = client.GetOrderBook("BNBBTC", 10);
                var aggTrades        = client.GetAggregatedTrades("BNBBTC", startTime: DateTime.UtcNow.AddMinutes(-2), endTime: DateTime.UtcNow, limit: 10);
                var klines           = client.GetKlines("BNBBTC", KlineInterval.OneHour, startTime: DateTime.UtcNow.AddHours(-10), endTime: DateTime.UtcNow, limit: 10);
                var price            = client.GetPrice("BNBBTC");
                var prices24h        = client.Get24HPrice("BNBBTC");
                var allPrices        = client.GetAllPrices();
                var allBookPrices    = client.GetAllBookPrices();
                var historicalTrades = client.GetHistoricalTrades("BNBBTC");

                // Private
                var openOrders      = client.GetOpenOrders("BNBBTC");
                var allOrders       = client.GetAllOrders("BNBBTC");
                var testOrderResult = client.PlaceTestOrder("BNBBTC", OrderSide.Buy, OrderType.Limit, 1, price: 1, timeInForce: TimeInForce.GoodTillCancel);
                var queryOrder      = client.QueryOrder("BNBBTC", allOrders.Data[0].OrderId);
                var orderResult     = client.PlaceOrder("BNBBTC", OrderSide.Sell, OrderType.Limit, 10, price: 0.0002m, timeInForce: TimeInForce.GoodTillCancel);
                var cancelResult    = client.CancelOrder("BNBBTC", orderResult.Data.OrderId);
                var accountInfo     = client.GetAccountInfo();
                var myTrades        = client.GetMyTrades("BNBBTC");

                // Withdrawal/deposit
                var withdrawalHistory = client.GetWithdrawHistory();
                var depositHistory    = client.GetDepositHistory();
                var withdraw          = client.Withdraw("ASSET", "ADDRESS", 0);
            }

            var socketClient = new BinanceSocketClient();
            // Streams
            var successDepth = socketClient.SubscribeToDepthStream("bnbbtc", (data) =>
            {
                // handle data
            });
            var successTrades = socketClient.SubscribeToTradesStream("bnbbtc", (data) =>
            {
                // handle data
            });
            var successKline = socketClient.SubscribeToKlineStream("bnbbtc", KlineInterval.OneMinute, (data) =>
            {
                // handle data
            });
            var successTicker = socketClient.SubscribeToAllSymbolTicker((data) =>
            {
                // handle data
            });
            var successSingleTicker = socketClient.SubscribeToSymbolTicker("bnbbtc", (data) =>
            {
                // handle data
            });

            string listenKey;

            using (var client = new BinanceClient())
                listenKey = client.StartUserStream().Data.ListenKey;

            var successAccount = socketClient.SubscribeToUserStream(listenKey, data =>
            {
                // Hanlde account info data
            },
                                                                    data =>
            {
                // Hanlde order update info data
            });

            socketClient.UnsubscribeAllStreams();

            Console.ReadLine();
        }