private void StartWebsockets()
        {
            var successSymbols = m_socketClient.SubscribeToAllSymbolTicker((data) =>
            {
                //Console.WriteLine(">>>>> {0} BINANCE tickers", data.Length);
                for (int i = 0; i < data.Length; ++i)
                {
                    m_tick[data[i].Symbol] = data[i];
                }
            });

            var lk = m_client.StartUserStream();

            if (lk.Success)
            {
                m_listenKey = lk.Data.ListenKey;
                var successsUser = m_socketClient.SubscribeToUserStream(m_listenKey,
                                                                        UpdateAccount,
                                                                        UpdateOrder
                                                                        );
            }
            else
            {
                Console.WriteLine("Error {0} getting BINANCE ListenKey from StartUserStream(): {1}", lk.Error.Code, lk.Error.Message);
            }
        }
Esempio n. 2
0
        private async Task GetAllSymbols()
        {
            using (var client = new BinanceClient())
            {
                var result = await client.GetAllPricesAsync();

                if (result.Success)
                {
                    AllPrices = new ObservableCollection <BinanceSymbolViewModel>(result.Data.Select(r => new BinanceSymbolViewModel(r.Symbol, r.Price)).ToList());
                }
                else
                {
                    messageBoxService.ShowMessage($"Error requesting data: {result.Error.Message}", "error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

            socketClient = new BinanceSocketClient();
            socketClient.SubscribeToAllSymbolTicker(data => {
                foreach (var ud in data)
                {
                    var symbol = AllPrices.SingleOrDefault(p => p.Symbol == ud.Symbol);
                    if (symbol != null)
                    {
                        symbol.Price = ud.CurrentDayClosePrice;
                    }
                }
            });
        }
Esempio n. 3
0
        public void SubscribingToAllSymbolTicker_Should_TriggerWhenAllSymbolTickerStreamMessageIsReceived()
        {
            // arrange
            var socket = new Mock <IWebsocket>();

            socket.Setup(s => s.Close());
            socket.Setup(s => s.Connect()).Returns(Task.FromResult(true));
            socket.Setup(s => s.SetEnabledSslProtocols(It.IsAny <System.Security.Authentication.SslProtocols>()));

            var factory = new Mock <IWebsocketFactory>();

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

            BinanceStreamTick[] result = null;
            var client = new BinanceSocketClient {
                SocketFactory = factory.Object
            };

            client.SubscribeToAllSymbolTicker((test) => result = test);

            var data = new[]
            {
                new BinanceStreamTick()
                {
                    BestAskPrice          = 0.1m,
                    BestAskQuantity       = 0.2m,
                    BestBidPrice          = 0.3m,
                    BestBidQuantity       = 0.4m,
                    CloseTradesQuantity   = 0.5m,
                    CurrentDayClosePrice  = 0.6m,
                    FirstTradeId          = 1,
                    HighPrice             = 0.7m,
                    LastTradeId           = 2,
                    LowPrice              = 0.8m,
                    OpenPrice             = 0.9m,
                    PrevDayClosePrice     = 1.0m,
                    PriceChange           = 1.1m,
                    PriceChangePercentage = 1.2m,
                    StatisticsCloseTime   = new DateTime(2017, 1, 2),
                    StatisticsOpenTime    = new DateTime(2017, 1, 1),
                    Symbol = "test",
                    TotalTradedBaseAssetVolume  = 1.3m,
                    TotalTradedQuoteAssetVolume = 1.4m,
                    TotalTrades     = 3,
                    WeightedAverage = 1.5m
                }
            };

            // act
            socket.Raise(r => r.OnMessage += null, JsonConvert.SerializeObject(data));

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(TestHelpers.PublicInstancePropertiesEqual(data[0], result[0]));
        }
Esempio n. 4
0
    private void loadSocket()
    {
        var _client = new BinanceSocketClient();

        var successOrderBook = _client.SubscribeToAllSymbolTicker((data) =>
        {
            try
            {
                if (data != null)
                {
                    foreach (var item in data)
                    {
                        bool found = false;
                        for (int i = 0; i < lstTicker.Count; i++)
                        {
                            if (lstTicker[i].symbol.Trim().ToUpper() == item.Symbol.Trim().ToUpper())
                            {
                                found             = true;
                                Ticker ticker     = new Ticker();
                                lstTicker[i].tick = item;
                            }
                        }
                        if (!found)
                        {
                            Ticker ticker = new Ticker();
                            ticker.tick   = item;
                            ticker.symbol = item.Symbol.Trim().ToUpper();
                            lstTicker.Add(ticker);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);
            }
        });

        Console.WriteLine(successOrderBook.Success);
        Console.WriteLine(successOrderBook.Error);
        Console.WriteLine(successOrderBook.Data);
    }
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();
        }