From c5644201ade208f666b9926848c01c5e0008a0c3 Mon Sep 17 00:00:00 2001 From: Jhonathan Abreu Date: Wed, 15 Apr 2026 10:35:28 -0400 Subject: [PATCH] Fix live trading charts sampling logic on extended market hours --- Engine/Results/LiveTradingResultHandler.cs | 23 +++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Engine/Results/LiveTradingResultHandler.cs b/Engine/Results/LiveTradingResultHandler.cs index 954c95cd3411..2a33aefd59cf 100644 --- a/Engine/Results/LiveTradingResultHandler.cs +++ b/Engine/Results/LiveTradingResultHandler.cs @@ -1119,7 +1119,12 @@ public virtual void ProcessSynchronousEvents(bool forceProcess = false) /// public override void OnSecuritiesChanged(SecurityChanges changes) { - if (_sampleChartAlways) + // the security that made us sample always might have been removed, if that's the case we need to check again if we should sample always or not + if (_sampleChartAlways && changes.RemovedSecurities.Count > 0 && changes.RemovedSecurities.Any(x => ShouldSampleChartsAlways(x.Symbol))) + { + _sampleChartAlways = false; + } + if (_sampleChartAlways) { return; } @@ -1133,10 +1138,7 @@ public override void OnSecuritiesChanged(SecurityChanges changes) } // if the user added Crypto, Forex, Daily or an extended market hours subscription just sample always, one way trip. - _sampleChartAlways = symbol.SecurityType == QuantConnect.SecurityType.Crypto - || symbol.SecurityType == QuantConnect.SecurityType.Forex - || Algorithm.SubscriptionManager.SubscriptionDataConfigService.GetSubscriptionDataConfigs(symbol) - .Any(config => config.ExtendedMarketHours || config.Resolution == Resolution.Daily); + _sampleChartAlways = ShouldSampleChartsAlways(symbol); if (_sampleChartAlways) { // we set it once to true @@ -1151,6 +1153,17 @@ public override void OnSecuritiesChanged(SecurityChanges changes) } } + /// + /// Checks whether the symbol subscription would make the result handler sample charts always + /// + private bool ShouldSampleChartsAlways(Symbol symbol) + { + return symbol.SecurityType == QuantConnect.SecurityType.Crypto + || symbol.SecurityType == QuantConnect.SecurityType.Forex + || Algorithm.SubscriptionManager.SubscriptionDataConfigService.GetSubscriptionDataConfigs(symbol) + .Any(config => config.ExtendedMarketHours); + } + /// /// True if user exchange are open and we should update portfolio and benchmark value ///