예제 #1
0
        public WebsocketClient(Network network)
        {
            var networkEnv = BinanceEnvironment.GetEnvironment(network);

            _ws = new WebSocket(networkEnv.WssApiAddress);
            _ws.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;
            _ws.EmitOnPing = true;

            _ws.OnMessage += _ws_OnMessage;
            _ws.OnError   += _ws_OnError;

            BlockHeight          = new BlockHeight(this);
            AllMiniTicker        = new AllMiniTicker(this);
            IndividualMiniTicker = new IndividualMiniTicker(this);
            AllTicker            = new AllTicker(this);
            IndividualTicker     = new IndividualTicker(this);
            Klines    = new Klines(this);
            BookDepth = new BookDepth(this);
            DiffDepth = new DiffDepth(this);
            Trades    = new Trades(this);
            Transfer  = new Transfer(this);
            Account   = new Account(this);
            Orders    = new Orders(this);
        }
예제 #2
0
        public void GetHistory(Storage storageInstance)
        {
            if (gettingHistory)
            {
                updateHistoryWhenFinished = true;
                return;
            }

            gettingHistory = true;
            DateTime now = DateTime.UtcNow;


            if (string.IsNullOrWhiteSpace(MACDStatus))
            {
                MACDStatus = "Aquiring";
            }
            if (string.IsNullOrWhiteSpace(RSIStatus))
            {
                RSIStatus = "Aquiring";
            }



            int haveAtleastThisManyKlines = 72;

            //Only get as much history as we need
            //default total klines
            int desiredLookBack;

            if (Klines == null || Klines.Count == 0 || Klines.Count < haveAtleastThisManyKlines)
            {
                // We have no klines, go get some
                desiredLookBack = haveAtleastThisManyKlines;
            }
            else
            {
                //We have klines, but we are out of date, so we'll get our current one, plus any that happenend
                //Get the latest kline
                var latestKline = Klines.OrderByDescending(k => k.CloseTime).FirstOrDefault();
                var hours       = (int)(now - latestKline.CloseTime).TotalHours;

                desiredLookBack = hours + 1;
            }

            var start = now - new TimeSpan(desiredLookBack, 0, 0);


            using (var client = new BinanceClient())
            {
                this.KlineInterval = KlineInterval.OneHour;

                var usedInterval = KlineInterval;
                var usedSymbol   = Symbol;

                var result = client.GetKlines(usedSymbol, usedInterval, start);
                if (result.Success)
                {
                    //TODO: Use Parallel
                    //result.Data.AsParallel().ForAll(kline => new CandleDBRow(kline, SymbolAsset, SymbolCurrency, usedInterval) { Connection = storageInstance.DBConnection }.Save());

                    foreach (var kline in result.Data)
                    {
                        new CandleDBRow(kline, SymbolAsset, SymbolCurrency, usedInterval)
                        {
                            Connection = storageInstance.DBConnection
                        }.Save();
                    }

                    AddKlines(KlineInterval.OneHour, result.Data.ToList());
                }
                else
                {
                    new MessageBoxService().ShowMessage($"Getting Candles for symbol {Symbol} failed: {result.Error.Message}", $"Code: {result.Error.Code}", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                }
            }


            gettingHistory = false;
            if (updateHistoryWhenFinished)
            {
                updateHistoryWhenFinished = false;
                GetHistory(storageInstance);
                // Note: This will use the previous storageInstance, if the instance needs to ever be different, then we can save it later above
            }
        }
예제 #3
0
 private void _ws_OnMessage(object sender, MessageEventArgs e)
 {
     //Pre-parsing message. Doesn't fully deserialize now to dynamic to improve performance
     if (e.Data.StartsWith("{\"stream\":\"orders\""))
     {
         Orders.ProcessRecievedMessage(e.Data);
     }
     else if (e.Data.StartsWith("{\"stream\":\"trades\""))
     {
         Trades.ProcessRecievedMessage(e.Data);
     }
     else if (e.Data.StartsWith("{\"stream\":\"accounts\""))
     {
         Account.ProcessRecievedMessage(e.Data);
     }
     else if (e.Data.StartsWith("{\"stream\":\"marketDiff\""))
     {
         DiffDepth.ProcessRecievedMessage(e.Data);
     }
     else if (e.Data.StartsWith("{\"stream\":\"marketDepth\""))
     {
         BookDepth.ProcessRecievedMessage(e.Data);
     }
     else if (e.Data.StartsWith("{\"stream\":\"blockheight\""))
     {
         BlockHeight.ProcessRecievedMessage(e.Data);
     }
     else if (e.Data.StartsWith("{\"stream\":\"allMiniTickers\""))
     {
         AllMiniTicker.ProcessRecievedMessage(e.Data);
     }
     else if (e.Data.StartsWith("{\"stream\":\"miniTicker\""))
     {
         IndividualMiniTicker.ProcessRecievedMessage(e.Data);
     }
     else if (e.Data.StartsWith("{\"stream\":\"allTickers\""))
     {
         AllTicker.ProcessRecievedMessage(e.Data);
     }
     else if (e.Data.StartsWith("{\"stream\":\"ticker\""))
     {
         IndividualTicker.ProcessRecievedMessage(e.Data);
     }
     else if (e.Data.StartsWith("{\"stream\":\"kline_"))
     {
         Klines.ProcessRecievedMessage(e.Data);
     }
     else if (e.Data.StartsWith("{\"stream\":\"transfers\""))
     {
         Transfer.ProcessRecievedMessage(e.Data);
     }
     else if (e.Data.StartsWith("{\"stream\":\"transfers\""))
     {
         Transfer.ProcessRecievedMessage(e.Data);
     }
     else if (!string.IsNullOrWhiteSpace(e.Data))
     {
         //We might received an error text from backend, have to raise attention if so.
         if (e.Data.Contains("error"))
         {
             throw new WebSocketConnectionException(string.Format("Websocket DEX backend sent error message: {0}", e.Data));
         }
     }
 }