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 }); }
private object SubscribeOrderBook(string instrument, string field, int depth) { var key = instrument;// + "|" + field; if (SubscribedDepth.ContainsKey(key)) { BinanceStreamOrderBook stream; if (DepthCache.TryGetValue(key, out stream)) { return(DecodeOrderBook(stream, field, depth)); } else { return(SubscriptionManager.UninitializedValue); } } else { SubscribedDepth.Add(key, true); Task.Run(() => socketClient.SubscribeToPartialBookDepthStream(instrument, 10, (BinanceStreamOrderBook stream) => { DepthCache[key] = stream; CacheOrderBook(stream); }) ); return(SubscriptionManager.UninitializedValue); } }
private void ProcessSell(BinanceClient client, BinanceSocketClient socketClient, CancellationToken token) { token.ThrowIfCancellationRequested(); List <Trades> trades = new List <Trades>(); Application.Current.Dispatcher.Invoke(() => { trades = dbHelper.GetOpenTrades(); }); foreach (var pair in trades) { if (binanceHelper.GetSubscriptionByKey(pair.Symbol) == null) { SafelyWriteToTradeLog("processing pair: " + pair.Symbol + " for sell.."); var subscribePair = socketClient.SubscribeToPartialBookDepthStream(pair.Symbol, 10, data => { OnSymbolSellOrderBookReceived(data, pair); }); if (subscribePair.Error != null) { _logger.LogError(subscribePair.Error.Message); } else { binanceHelper.AddToSubscriptionList(pair.Symbol, subscribePair.Data); } } else { SafelyWriteToTradeLog("processing pair: " + pair.Symbol + " already subscribed for sell.."); } } }
static void loadDataDetailSocket(object obj) { try { ClassDetailOrder classDetailOrder = new ClassDetailOrder(); classDetailOrder.symbol = obj.ToString().ToLower(); array.Add(classDetailOrder); Console.WriteLine("loadDataDetail " + classDetailOrder.symbol); var successOrderBook = binanceSocketClient.SubscribeToPartialBookDepthStream(obj.ToString().ToLower(), 20, (data) => { try { for (int i = 0; i < array.Count; i++) { if ((array[i] as ClassDetailOrder).symbol.ToLower().Trim() == data.Symbol.ToLower().Trim()) { classDetailOrder = new ClassDetailOrder(); classDetailOrder.symbol = obj.ToString().ToLower(); classDetailOrder.book = data; array[i] = classDetailOrder; } } } catch (Exception ex) { Logger.log(ex.Message + ex.StackTrace); } }); while (!successOrderBook.Success) { System.Threading.Thread.Sleep(100); } Logger.log("Succes | " + obj.ToString().ToLower() + " | " + successOrderBook.Success.ToString()); if (successOrderBook.Error != null) { Logger.log("Error " + successOrderBook.Error.ToString()); } //if (successOrderBook.Data != null) // Logger.log("Data " + successOrderBook.Data.ToString()); } catch (Exception ex) { Logger.log(ex.Message + ex.StackTrace); } }
public void SubscribeToBuyPairsOrderBook(Trades pair) { if (binanceHelper.GetSubscriptionByKey(pair.Symbol) == null) { var subscribePair = socketClient.SubscribeToPartialBookDepthStream(pair.Symbol, 10, data => { OnSymbolBuyOrderBookReceived(data, pair); }); if (subscribePair.Error != null) { _logger.LogError(subscribePair.Error.Message); } else { binanceHelper.AddToSubscriptionList(pair.Symbol, subscribePair.Data); } } }
public void SubscribingToPartialBookDepthStream_Should_TriggerWhenPartialBookStreamMessageIsReceived() { // 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 <string>())).Returns(socket.Object); BinanceOrderBook result = null; var client = new BinanceSocketClient { SocketFactory = factory.Object }; client.SubscribeToPartialBookDepthStream("test", 10, (test) => result = test); var data = new BinanceOrderBook() { Asks = new List <BinanceOrderBookEntry>() { new BinanceOrderBookEntry() { Price = 0.1m, Quantity = 0.2m }, new BinanceOrderBookEntry() { Price = 0.3m, Quantity = 0.4m } }, LastUpdateId = 1, Bids = new List <BinanceOrderBookEntry>() }; // act socket.Raise(r => r.OnMessage += null, JsonConvert.SerializeObject(data)); // assert Assert.IsNotNull(result); Assert.IsTrue(Compare.PublicInstancePropertiesEqual(data, result, "Asks", "Bids")); Assert.IsTrue(Compare.PublicInstancePropertiesEqual(data.Asks[0], result.Asks[0])); Assert.IsTrue(Compare.PublicInstancePropertiesEqual(data.Asks[1], result.Asks[1])); }
private void BinanceFun(ViewData item) { using (var BinanceSocketClient = new BinanceSocketClient()) { //var BTCUSDTDepth = BinanceSocketClient.SubscribeToDepthStream(item.Currency, (data) => //{ // var AsksMin = data.Asks.OrderBy(x => x.Price)?.FirstOrDefault()?.Price; // var AsksMax = data.Asks.OrderByDescending(x => x.Price)?.FirstOrDefault()?.Price; // var BidsMin = data.Bids.OrderBy(x => x.Price)?.FirstOrDefault()?.Price; // var BidsMax = data.Bids.OrderByDescending(x => x.Price)?.FirstOrDefault()?.Price; // if (TempOder == 0 && AsksMin.HasValue) // { // TempOder = AsksMin.Value; // ListMsg.Insert(0, new Memo { Msg = "買入:" + TempOder }); // var sourceMemo = new BindingSource(); // sourceMemo.DataSource = ListMsg; // SysHelper.Print(dataGridViewMemo, sourceMemo); // } // else if (BidsMin > (TempOder * (1 + (0.5M / 100)))) // { // ListMsg.Insert(0, new Memo { Msg = "已賣出:" + BidsMin }); // var sourceMemo = new BindingSource(); // sourceMemo.DataSource = ListMsg; // SysHelper.Print(dataGridViewMemo, sourceMemo); // } //}); var successSymbol = BinanceSocketClient.SubscribeToPartialBookDepthStream(item.Currency, 5, (data) => { //dataGridViewMoney.DataSource = null; try { var Ask = data.Asks.Min(x => x.Price) * Ratio; //賣出價 var Bid = data.Bids.Max(x => x.Price) * Ratio; //買入價 if (Ask > item.Ask) { item.StatusAsk = "上漲"; } else { item.StatusAsk = "下跌"; } if (Bid > item.Bid) { item.StatusBid = "上漲"; } else { item.StatusBid = "下跌"; } item.Ask = Ask; //賣出價 item.Bid = Bid; //買入價 var sourceMoney = new BindingSource(); sourceMoney.DataSource = ListVal; SysHelper.Print(dataGridViewMoney, sourceMoney); } catch (Exception) { } }); } }