Esempio n. 1
0
        public void UnsubscribingAccountUpdate_Should_CloseSocket()
        {
            // 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("test", "test")
            {
                SocketFactory = factory.Object
            };

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

            // act
            client.UnsubscribeFromAccountUpdateStream();

            // assert
            Assert.IsTrue(closed == 1);
        }
Esempio n. 2
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();
        }