private void BinanceWebsocketPublic() { var successDepth = m_socketClient.SubscribeToDepthStream("bnbbtc", (data) => { // handle data }); var successTrades = m_socketClient.SubscribeToTradesStream("bnbbtc", (data) => { // handle data }); var successKline = m_socketClient.SubscribeToKlineStream("bnbbtc", KlineInterval.OneMinute, (data) => { // handle data }); var successSymbol = m_socketClient.SubscribeToSymbolTicker("bnbbtc", (data) => { // handle data }); var successSymbols = m_socketClient.SubscribeToAllSymbolTicker((data) => { // handle data }); var successOrderBook = m_socketClient.SubscribeToPartialBookDepthStream("bnbbtc", 10, (data) => { // handle data }); }
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); }
public void TickerThread(ExchangeData Data) { InProgress = true; using (ApiSocketClient = new BinanceSocketClient()) { //ApiSocketClient.SubscribeToAccountUpdateStream("ikKz2GYMzCSNFjPzPeJHGYQI6sXwVNIzX7KNg31FDZkRE9OVrOE3al8PQwS8", (data) => //{ // var d = data; //}); //ETHBTC //BTCUSDT //QTUMBTC var successDepth = ApiSocketClient.SubscribeToDepthStream(Data.ExchangeType, (data) => { if (data.Asks.Any()) { Data.Ask = (decimal)data.Asks.OrderBy(x => x.Price).First().Price; } if (data.Bids.Any()) { Data.Bid = (decimal)data.Bids.OrderByDescending(x => x.Price).First().Price; } Data.UpdateTime = DateTime.UtcNow; }); if (!successDepth.Success) { throw new Exception(successDepth.Error.Message); } while (InProgress) { Thread.Sleep(500); } ApiSocketClient.UnsubscribeFromStream(successDepth.Data); Data.Status = EnumData.ExchangeStatus.停止; } }
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(); }
public void SubscribingToDepthStream_Should_TriggerWhenDepthStreamMessageIsReceived() { // 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); BinanceStreamDepth result = null; var client = new BinanceSocketClient { SocketFactory = factory.Object }; var subscibtion = client.SubscribeToDepthStream("test", (test) => result = test); var data = new BinanceStreamDepth() { Event = "TestDepthStream", EventTime = new DateTime(2017, 1, 1), Symbol = "test", UpdateId = 1, Asks = new List <BinanceOrderBookEntry>() { new BinanceOrderBookEntry() { Price = 1.1, Quantity = 2.2 }, new BinanceOrderBookEntry() { Price = 3.3, Quantity = 4.4 } }, Bids = new List <BinanceOrderBookEntry>() { new BinanceOrderBookEntry() { Price = 5.5, Quantity = 6.6 }, new BinanceOrderBookEntry() { Price = 7.7, Quantity = 8.8 } } }; // act socket.Raise(r => r.OnMessage += null, new MessagedEventArgs(JsonConvert.SerializeObject(data), false, false, true, new byte[2])); // assert Assert.IsTrue(subscibtion.Success); Assert.IsTrue(subscibtion.Data != 0); Assert.IsNotNull(result); Assert.IsTrue(Compare.PublicInstancePropertiesEqual(data, result, "Bids", "Asks")); Assert.IsTrue(Compare.PublicInstancePropertiesEqual(data.Asks[0], result.Asks[0])); Assert.IsTrue(Compare.PublicInstancePropertiesEqual(data.Asks[1], result.Asks[1])); Assert.IsTrue(Compare.PublicInstancePropertiesEqual(data.Bids[0], result.Bids[0])); Assert.IsTrue(Compare.PublicInstancePropertiesEqual(data.Bids[1], result.Bids[1])); }
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(); }