// ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local private void MergeCandle(string assetPair, CandleTimeInterval interval, ICandle candle) { if (candle.AssetPairId != assetPair) { throw new InvalidOperationException($"Candle {candle.ToJson()} has invalid AssetPriceId"); } if (candle.TimeInterval != interval) { throw new InvalidOperationException($"Candle {candle.ToJson()} has invalid TimeInterval"); } if (candle.PriceType != PriceType) { throw new InvalidOperationException($"Candle {candle.ToJson()} has invalid PriceType"); } // 1. Check if candle with specified time already exist // 2. If found - merge, else - add to list var tick = GetIntervalTick(candle.Timestamp, interval); // Considering that Candles is ordered by Tick for (var i = 0; i < Candles.Count; ++i) { var currentCandle = Candles[i]; // While currentCandle.Tick < tick - just skipping // That's it, merge to existing candle if (currentCandle.Tick == tick) { currentCandle.InplaceMergeWith(candle); return; } // No candle is found but there are some candles after, so we should insert candle right before them if (currentCandle.Tick > tick) { Candles.Insert(i, candle.ToItem(tick)); return; } } // No candle is found, and no candles after, so just add to the end Candles.Add(candle.ToItem(tick)); }