private async Task <bool> ProcessSocketCandleMessage(string table, string json) { const ExchangeChannel channel = ExchangeChannel.Candle; var candleChannel = ExchangeConfig.SupportedWebsocketChannels[channel]; if (table.Contains(candleChannel)) { var timePeriod = ExchangeConfig.SupportedTimePeriods .Select(x => new { x.Key, x.Value }) .FirstOrDefault(x => x.Value == table.Replace(candleChannel, ""))?.Key; if (!timePeriod.HasValue) { return(false); } var candleModels = DeserialiseJsonToModel <CandleResponse, CandleModel>(json); foreach (var model in candleModels) { model.Period = timePeriod.Value; } await UpdateSubscriptions(channel, candleModels); return(true); } return(false); }
protected async Task UpdateSubscriptions(ExchangeChannel channel, IEnumerable models) { if (_subscriptions.ContainsKey(channel)) { var subs = _subscriptions[channel]; foreach (var subscription in subs) { await subscription.TriggerUpdate(models); } } }
private async Task <bool> ProcessSocketTradeMessage(string table, string json) { const ExchangeChannel channel = ExchangeChannel.Trade; var tradeChannel = ExchangeConfig.SupportedWebsocketChannels[channel]; if (!table.Contains(tradeChannel)) { return(false); } await UpdateSubscriptions(channel, DeserialiseJsonToModel <TradeResponse, TradeModel>(json)); return(true); }
public async Task SubscribeToSocketAsync <T>(ExchangeChannel channel, IStreamSubscription subscription, Instrument?instrument = null) where T : ExchangeDto { if (!ExchangeConfig.SupportedWebsocketChannels.ContainsKey(channel)) { return; } if (!_subscriptions.ContainsKey(channel)) { _subscriptions.Add(channel, new List <IStreamSubscription>()); } _subscriptions[channel].Add(subscription); await _socketClient.SubscribeAsync(await GetSocketRequest(channel, instrument)); }
private string GetSocketRequest(ExchangeChannel channel) { switch (channel) { case ExchangeChannel.Trade: return(BitmexUtils.GetSocketRequest(ExchangeConfig.SupportedWebsocketChannels[ExchangeChannel.Trade], ToSymbol(Instrument.BTCUSD))); case ExchangeChannel.Book: return(BitmexUtils.GetSocketRequest(ExchangeConfig.SupportedWebsocketChannels[ExchangeChannel.Book], ToSymbol(Instrument.BTCUSD))); case ExchangeChannel.Candle: var paths = ExchangeConfig.SupportedTimePeriods.Values.Select(x => $"{ExchangeConfig.SupportedWebsocketChannels[ExchangeChannel.Candle]}{x}").ToList(); var args = paths.Select(x => new[] { ToSymbol(Instrument.BTCUSD) }).ToList(); return(BitmexUtils.GetSocketRequest(paths, args)); default: throw new ArgumentOutOfRangeException(nameof(channel), channel, null); } }
protected override Task <string> GetSocketRequest(ExchangeChannel channel, Instrument?instrument = null) { var result = ""; switch (channel) { case ExchangeChannel.Trade: result = GetSocketRequest(ExchangeConfig.SupportedWebsocketChannels[ExchangeChannel.Trade], instrument.HasValue ? ToSymbol(instrument.Value) : null); break; case ExchangeChannel.Book: result = GetSocketRequest(ExchangeConfig.SupportedWebsocketChannels[ExchangeChannel.Book], instrument.HasValue ? ToSymbol(instrument.Value) : null); break; case ExchangeChannel.Candle: var paths = ExchangeConfig.SupportedTimePeriods.Values .Select(x => $"{ExchangeConfig.SupportedWebsocketChannels[ExchangeChannel.Candle]}{x}") .ToList(); var args = instrument.HasValue ? paths.Select(x => new[] { ToSymbol(instrument.Value) }).ToList() : null; result = GetSocketRequest(paths, args); break; case ExchangeChannel.Position: result = GetSocketRequest(ExchangeConfig.SupportedWebsocketChannels[ExchangeChannel.Position], instrument.HasValue ? $"filter={ToSymbol(instrument.Value)}" : null); break; default: throw new ArgumentOutOfRangeException(nameof(channel), channel, null); } return(Task.FromResult(result)); }
public Task SubscribeToStreamAsync <T>(ExchangeChannel channel, IStreamSubscription subscription, Instrument?instrument = null) where T : ExchangeDto { return(_exchangeApi.SubscribeToSocketAsync <T>(channel, subscription, instrument)); }
protected abstract Task <string> GetSocketRequest(ExchangeChannel channel, Instrument?instrument = null);