protected override void HandleMessage(IEnumerable <Action <CandlestickEventArgs> > callbacks, string stream, string json) { try { var jObject = JObject.Parse(json); var eventType = jObject["e"].Value <string>(); if (eventType == "kline") { //var symbol = jObject["s"].Value<string>(); var eventTime = jObject["E"].Value <long>().ToDateTime(); var kLine = jObject["k"]; var firstTradeId = kLine["f"].Value <long>(); var lastTradeId = kLine["L"].Value <long>(); var isFinal = kLine["x"].Value <bool>(); var candlestick = new Candlestick( kLine["s"].Value <string>(), // symbol kLine["i"].Value <string>() // interval .ToCandlestickInterval(), kLine["t"].Value <long>() // open time .ToDateTime(), kLine["o"].Value <decimal>(), // open kLine["h"].Value <decimal>(), // high kLine["l"].Value <decimal>(), // low kLine["c"].Value <decimal>(), // close kLine["v"].Value <decimal>(), // volume kLine["T"].Value <long>() // close time .ToDateTime(), kLine["q"].Value <decimal>(), // quote asset volume kLine["n"].Value <long>(), // number of trades kLine["V"].Value <decimal>(), // taker buy base asset volume (volume of active buy) kLine["Q"].Value <decimal>() // taker buy quote asset volume (quote volume of active buy) ); var eventArgs = new CandlestickEventArgs(eventTime, candlestick, firstTradeId, lastTradeId, isFinal); try { if (callbacks != null) { foreach (var callback in callbacks) { callback(eventArgs); } } Candlestick?.Invoke(this, eventArgs); } catch (OperationCanceledException) { /* ignore */ } catch (Exception e) { Logger?.LogWarning(e, $"{nameof(CandlestickClient)}.{nameof(HandleMessage)}: Unhandled candlestick event handler exception."); } } else { Logger?.LogWarning($"{nameof(CandlestickClient)}.{nameof(HandleMessage)}: Unexpected event type ({eventType})."); } } catch (OperationCanceledException) { /* ignore */ } catch (Exception e) { Logger?.LogError(e, $"{nameof(CandlestickClient)}.{nameof(HandleMessage)}"); } }
protected override void OnWebSocketEvent(WebSocketStreamEventArgs args, IEnumerable <Action <CandlestickEventArgs> > callbacks) { Logger?.LogDebug($"{nameof(CandlestickWebSocketClient)}: \"{args.Json}\""); try { var jObject = JObject.Parse(args.Json); var eventType = jObject["e"].Value <string>(); if (eventType == "kline") { //var symbol = jObject["s"].Value<string>(); var eventTime = jObject["E"].Value <long>(); var firstTradeId = jObject["k"]["f"].Value <long>(); var lastTradeId = jObject["k"]["L"].Value <long>(); var isFinal = jObject["k"]["x"].Value <bool>(); var candlestick = new Candlestick( jObject["k"]["s"].Value <string>(), // symbol jObject["k"]["i"].Value <string>() .ToCandlestickInterval(), // interval jObject["k"]["t"].Value <long>(), // open time jObject["k"]["o"].Value <decimal>(), // open jObject["k"]["h"].Value <decimal>(), // high jObject["k"]["l"].Value <decimal>(), // low jObject["k"]["c"].Value <decimal>(), // close jObject["k"]["v"].Value <decimal>(), // volume jObject["k"]["T"].Value <long>(), // close time jObject["k"]["q"].Value <decimal>(), // quote asset volume jObject["k"]["n"].Value <long>(), // number of trades jObject["k"]["V"].Value <decimal>(), // taker buy base asset volume (volume of active buy) jObject["k"]["Q"].Value <decimal>() // taker buy quote asset volume (quote volume of active buy) ); var eventArgs = new CandlestickEventArgs(eventTime, args.Token, candlestick, firstTradeId, lastTradeId, isFinal); try { if (callbacks != null) { foreach (var callback in callbacks) { callback(eventArgs); } } Candlestick?.Invoke(this, eventArgs); } catch (OperationCanceledException) { } catch (Exception e) { if (!args.Token.IsCancellationRequested) { Logger?.LogError(e, $"{nameof(CandlestickWebSocketClient)}: Unhandled candlestick event handler exception."); } } } else { Logger?.LogWarning($"{nameof(CandlestickWebSocketClient)}.{nameof(OnWebSocketEvent)}: Unexpected event type ({eventType})."); } } catch (OperationCanceledException) { } catch (Exception e) { if (!args.Token.IsCancellationRequested) { Logger?.LogError(e, $"{nameof(CandlestickWebSocketClient)}.{nameof(OnWebSocketEvent)}"); } } }
/// <summary> /// Deserialize JSON and raise <see cref="CandlestickEventArgs"/> event. /// </summary> /// <param name="json"></param> /// <param name="symbol"></param> /// <param name="token"></param> /// <param name="callback"></param> protected override void DeserializeJsonAndRaiseEvent(string json, Symbol symbol, CancellationToken token, Action <CandlestickEventArgs> callback = null) { Throw.IfNullOrWhiteSpace(json, nameof(json)); Logger?.LogDebug($"{nameof(CandlestickWebSocketClient)}: \"{json}\""); try { var jObject = JObject.Parse(json); var eventType = jObject["e"].Value <string>(); if (eventType == "kline") { //var symbol = jObject["s"].Value<string>(); var eventTime = jObject["E"].Value <long>(); var firstTradeId = jObject["k"]["f"].Value <long>(); var lastTradeId = jObject["k"]["L"].Value <long>(); var isFinal = jObject["k"]["x"].Value <bool>(); var candlestick = new Candlestick( jObject["k"]["s"].Value <string>(), // symbol jObject["k"]["i"].Value <string>() .ToCandlestickInterval(), // interval jObject["k"]["t"].Value <long>(), // open time jObject["k"]["o"].Value <decimal>(), // open jObject["k"]["h"].Value <decimal>(), // high jObject["k"]["l"].Value <decimal>(), // low jObject["k"]["c"].Value <decimal>(), // close jObject["k"]["v"].Value <decimal>(), // volume jObject["k"]["T"].Value <long>(), // close time jObject["k"]["q"].Value <decimal>(), // quote asset volume jObject["k"]["n"].Value <long>(), // number of trades jObject["k"]["V"].Value <decimal>(), // taker buy base asset volume (volume of active buy) jObject["k"]["Q"].Value <decimal>() // taker buy quote asset volume (quote volume of active buy) ); var eventArgs = new CandlestickEventArgs(eventTime, token, candlestick, firstTradeId, lastTradeId, isFinal); try { callback?.Invoke(eventArgs); Candlestick?.Invoke(this, eventArgs); } catch (OperationCanceledException) { } catch (Exception e) { if (!token.IsCancellationRequested) { Logger?.LogError(e, $"{nameof(CandlestickWebSocketClient)}: Unhandled candlestick event handler exception."); } } } else { Logger?.LogWarning($"{nameof(CandlestickWebSocketClient)}.{nameof(DeserializeJsonAndRaiseEvent)}: Unexpected event type ({eventType})."); } } catch (OperationCanceledException) { } catch (Exception e) { if (!token.IsCancellationRequested) { Logger?.LogError(e, $"{nameof(CandlestickWebSocketClient)}.{nameof(DeserializeJsonAndRaiseEvent)}"); } } }