private void Unsubscribe() { if (!symbolCancellationTokenSource.IsCancellationRequested) { symbolCancellationTokenSource.Cancel(); symbolCancellationTokenSource.Dispose(); } orderBookSemaphoreSlim.Dispose(); tradesSemaphoreSlim.Dispose(); if (OrderBook != null) { OrderBook.Clear(); } OrderBook = null; if (TradesChart != null) { TradesChart.Clear(); } TradesChart = null; Trades = null; IsActive = false; }
public override async Task CandlestickNotificationsAsync(List <StrategyNotification> candlestickNotifications) { await candlestickSemaphoreSlim.WaitAsync(cancellationTokenSource.Token); try { if (cancellationTokenSource.IsCancellationRequested) { return; } var candlestickNotification = candlestickNotifications.Last(); var cs = JsonConvert.DeserializeObject <List <TradeView.Interface.Model.Candlestick> >(candlestickNotification.Message); Candlestick last = null; if (CandlesticksChart != null) { last = CandlesticksChart.LastOrDefault(); } if (last == null) { var firstTrade = TradesChart.First(); var candlesticks = cs.OrderBy(c => c.OpenTime).Select(c => c.ToViewCandlestick()).ToList(); CandlesticksChart = new ChartValues <Candlestick>(candlesticks); var labels = candlesticks.Select(c => c.CloseTime.ToString("H:mm:ss")); CandlestickLabels = new ObservableCollection <string>(labels); } else { var candlesticks = cs.Where(c => c.OpenTime.ToLocalTime() >= last.OpenTime) .OrderBy(c => c.OpenTime) .ToList(); var first = candlesticks.FirstOrDefault(); if (first != null && first.OpenTime.ToLocalTime().Equals(last.OpenTime)) { last.High = first.High; last.Low = first.Low; last.Close = first.Close; last.CloseTime = first.CloseTime.ToLocalTime(); last.Volume = first.Volume; last.QuoteAssetVolume = first.QuoteAssetVolume; last.TakerBuyBaseAssetVolume = first.TakerBuyBaseAssetVolume; last.TakerBuyQuoteAssetVolume = first.TakerBuyQuoteAssetVolume; last.NumberOfTrades = first.NumberOfTrades; } if (candlesticks.Count > 1) { var newCandlesticks = candlesticks.Skip(1).Select(c => c.ToViewCandlestick()).ToList(); CandlesticksChart.AddRange(newCandlesticks); var labels = newCandlesticks.Select(c => c.CloseTime.ToString("H:mm:ss")); CandlestickLabels.AddRange(labels); } } } catch (Exception ex) { OnException($"{Strategy.Name} : CandlestickNotificationsAsync - {ex.Message}", ex); } finally { candlestickSemaphoreSlim.Release(); } await Task.FromResult <object>(null); }