/// <summary> /// Subscribes to order book updates for a symbol /// </summary> /// <param name="symbol">The symbol to subscribe to</param> /// <param name="precision">The precision of the updates</param> /// <param name="frequency">The frequency of updates</param> /// <param name="length">The range for the order book updates, either 25 or 100</param> /// <param name="handler">The handler for the data</param> /// <returns></returns> public async Task <CallResult <UpdateSubscription> > SubscribeToBookUpdatesAsync(string symbol, Precision precision, Frequency frequency, int length, Action <BitfinexOrderBookEntry[]> handler) { if (length != 25 && length != 100) { return(new CallResult <UpdateSubscription>(null, new ArgumentError("Limit should be either 25 or 100"))); } var internalHandler = new Action <JToken>(data => { var dataArray = (JArray)data[1]; if (dataArray[0].Type == JTokenType.Array) { HandleData("Book snapshot", dataArray, handler); } else { HandleSingleToArrayData("Book update", dataArray, handler); } }); var sub = new BitfinexBookSubscriptionRequest( symbol, JsonConvert.SerializeObject(precision, new PrecisionConverter(false)), JsonConvert.SerializeObject(frequency, new FrequencyConverter(false)), length); return(await Subscribe(sub, null, false, internalHandler).ConfigureAwait(false)); }
/// <summary> /// Subscribes to order book updates for a symbol /// </summary> /// <param name="symbol">The symbol to subscribe to</param> /// <param name="precision">The precision of the updates</param> /// <param name="frequency">The frequency of updates</param> /// <param name="length">The range for the order book updates, either 25 or 100</param> /// <param name="handler">The handler for the data</param> /// <returns></returns> public async Task <CallResult <UpdateSubscription> > SubscribeToBookUpdatesAsync(string symbol, Precision precision, Frequency frequency, int length, Action <IEnumerable <BitfinexOrderBookEntry> > handler) { symbol.ValidateBitfinexSymbol(); length.ValidateIntValues(nameof(length), 25, 100); var internalHandler = new Action <JToken>(data => { var dataArray = (JArray)data[1] !; if (dataArray[0].Type == JTokenType.Array) { HandleData("Book snapshot", dataArray, handler); } else { HandleSingleToArrayData("Book update", dataArray, handler); } }); var sub = new BitfinexBookSubscriptionRequest( symbol, JsonConvert.SerializeObject(precision, new PrecisionConverter(false)), JsonConvert.SerializeObject(frequency, new FrequencyConverter(false)), length); return(await Subscribe(sub, null, false, internalHandler).ConfigureAwait(false)); }
/// <inheritdoc /> public async Task <CallResult <UpdateSubscription> > SubscribeToOrderBookUpdatesAsync(string symbol, Precision precision, Frequency frequency, int length, Action <DataEvent <IEnumerable <BitfinexOrderBookEntry> > > handler, Action <DataEvent <int> >?checksumHandler = null, CancellationToken ct = default) { symbol.ValidateBitfinexSymbol(); length.ValidateIntValues(nameof(length), 25, 100); if (precision == Precision.R0) { throw new ArgumentException("Invalid precision R0, use SubscribeToRawBookUpdatesAsync instead"); } var internalHandler = new Action <DataEvent <JToken> >(data => { if (data.Data[1]?.ToString() == "cs") { // Process checksumHandler?.Invoke(data.As(data.Data[2] !.Value <int>(), symbol)); } else { var dataArray = (JArray)data.Data[1] !; if (dataArray.Count == 0) { // Empty array return; } if (dataArray[0].Type == JTokenType.Array) { HandleData("Book snapshot", dataArray, symbol, data, handler, _bookSerializer); } else { HandleSingleToArrayData("Book update", dataArray, symbol, data, handler, _bookSerializer); } } }); var sub = new BitfinexBookSubscriptionRequest( symbol, JsonConvert.SerializeObject(precision, new PrecisionConverter(false)), JsonConvert.SerializeObject(frequency, new FrequencyConverter(false)), length); return(await _baseClient.SubscribeInternalAsync(this, sub, null, false, internalHandler, ct).ConfigureAwait(false)); }