Expire Instead of Clear Insights In Base PCM In OnSecuritiesChanged (#7251)
Regression Tests / build (push) Has been cancelled
Benchmarks / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled
Python Virtual Environments / build (push) Has been cancelled

* Expire Instead of Clear Insights In Base PCM In OnSecuritiesChanged

* Fixes Regression Test

The regression was incorrect because `_removedSymbols` didn't retain the removed symbols from previous `OnSecuritiesChanged` calls, and the algorithm relances once per month. E.g. On day 1, SPY and FB were removed, on day 3 AAPL and IBM were removed and `removedSymbols` would only include AAPL and IBM.

We should hve fixed this problem before with:

```csharp
if (_removedSymbols == null) _removedSymbols = new List<Symbol>();
_removedSymbols.AddRange(changes.RemovedSecurities.Select(x => x.Symbol));
```

However the change to use Expire fixes the issue.

* Removes Unused List of Removed Symbols
This commit is contained in:
Alexandre Catarino
2023-05-18 15:20:17 +01:00
committed by GitHub
parent a1d5b1bd31
commit fd76171604
2 changed files with 23 additions and 32 deletions
@@ -30,7 +30,6 @@ namespace QuantConnect.Algorithm.Framework.Portfolio
public class PortfolioConstructionModel : IPortfolioConstructionModel
{
private Func<DateTime, DateTime?> _rebalancingFunc;
private List<Symbol> _removedSymbols;
private DateTime? _rebalancingTime;
private bool _securityChanges;
@@ -103,14 +102,6 @@ namespace QuantConnect.Algorithm.Framework.Portfolio
var targets = new List<IPortfolioTarget>();
// Create flatten target for each security that was removed from the universe
if (_removedSymbols != null)
{
var universeDeselectionTargets = _removedSymbols.Select(symbol => new PortfolioTarget(symbol, 0));
targets.AddRange(universeDeselectionTargets);
_removedSymbols = null;
}
var lastActiveInsights = PythonWrapper?.GetTargetInsights()
?? GetTargetInsights();
@@ -162,8 +153,8 @@ namespace QuantConnect.Algorithm.Framework.Portfolio
_securityChanges = changes != SecurityChanges.None;
// Get removed symbol and invalidate them in the insight collection
_removedSymbols = changes.RemovedSecurities.Select(x => x.Symbol).ToList();
algorithm?.Insights.Clear(_removedSymbols.ToArray());
var removedSymbols = changes.RemovedSecurities.Select(x => x.Symbol);
algorithm?.Insights.Expire(removedSymbols);
}
/// <summary>