diff --git a/lib/src/deriv_chart/chart/data_visualization/chart_series/data_series.dart b/lib/src/deriv_chart/chart/data_visualization/chart_series/data_series.dart index 9d09c45dc..d6aad0a63 100644 --- a/lib/src/deriv_chart/chart/data_visualization/chart_series/data_series.dart +++ b/lib/src/deriv_chart/chart/data_visualization/chart_series/data_series.dart @@ -253,12 +253,14 @@ abstract class DataSeries extends Series { oldSeries.entries != null && entries!.last == oldSeries.entries!.last) { prevLastEntry = oldSeries.prevLastEntry as IndexedEntry?; - } else if (oldSeries.entries != null) { + } else if (oldSeries.entries != null && _isSameSeries(oldSeries)) { prevLastEntry = IndexedEntry( oldSeries.entries!.last as T, oldSeries.entries!.length - 1, ); updated = true; + } else { + updated = true; } } else { initialize(); @@ -287,6 +289,24 @@ abstract class DataSeries extends Series { bool isOldDataAvailable(covariant DataSeries? oldSeries) => oldSeries?.entries?.isNotEmpty ?? false; + /// Returns true when [oldSeries] and this series share the same data source, + /// i.e. this series is an incremental update of [oldSeries] rather than an + /// entirely new dataset (e.g. a market/symbol switch). + /// + /// Compares the first entries by epoch and quote: for an incremental update + /// (new tick, new candle) the beginning of the data is unchanged, so the + /// first entries are identical. For a market switch both datasets cover the + /// same time window, but the prices differ, so the first entries will not + /// match and [prevLastEntry] must not carry over. + bool _isSameSeries(DataSeries oldSeries) { + if (input.isEmpty || (oldSeries.entries?.isEmpty ?? true)) { + return false; + } + final T newFirst = input.first; + final Tick oldFirst = oldSeries.entries!.first; + return newFirst.epoch == oldFirst.epoch && newFirst.quote == oldFirst.quote; + } + @override // ignore: avoid_renaming_method_parameters bool shouldRepaint(ChartData? oldDelegate) {