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
@@ -108,7 +108,7 @@ namespace QuantConnect.Algorithm.CSharp
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 5568;
public long DataPoints => 5500;
/// <summary>
/// Data Points count of the algorithm history
@@ -121,29 +121,29 @@ namespace QuantConnect.Algorithm.CSharp
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Trades", "74"},
{"Average Win", "2.44%"},
{"Average Loss", "-2.28%"},
{"Compounding Annual Return", "-4.581%"},
{"Drawdown", "30.500%"},
{"Expectancy", "-0.081"},
{"Net Profit", "-8.951%"},
{"Sharpe Ratio", "-0.137"},
{"Probabilistic Sharpe Ratio", "2.627%"},
{"Average Win", "2.37%"},
{"Average Loss", "-2.26%"},
{"Compounding Annual Return", "-4.832%"},
{"Drawdown", "29.700%"},
{"Expectancy", "-0.089"},
{"Net Profit", "-9.431%"},
{"Sharpe Ratio", "-0.176"},
{"Probabilistic Sharpe Ratio", "2.229%"},
{"Loss Rate", "56%"},
{"Win Rate", "44%"},
{"Profit-Loss Ratio", "1.07"},
{"Alpha", "-0.071"},
{"Beta", "0.978"},
{"Annual Standard Deviation", "0.152"},
{"Annual Variance", "0.023"},
{"Information Ratio", "-0.748"},
{"Tracking Error", "0.097"},
{"Treynor Ratio", "-0.021"},
{"Total Fees", "$264.99"},
{"Estimated Strategy Capacity", "$55000000.00"},
{"Profit-Loss Ratio", "1.05"},
{"Alpha", "-0.064"},
{"Beta", "0.768"},
{"Annual Standard Deviation", "0.139"},
{"Annual Variance", "0.019"},
{"Information Ratio", "-0.709"},
{"Tracking Error", "0.108"},
{"Treynor Ratio", "-0.032"},
{"Total Fees", "$262.70"},
{"Estimated Strategy Capacity", "$54000000.00"},
{"Lowest Capacity Asset", "IBM R735QTJ8XC9X"},
{"Portfolio Turnover", "5.06%"},
{"OrderListHash", "bdb23325dc6c1fa04f63a329346be794"}
{"Portfolio Turnover", "5.05%"},
{"OrderListHash", "f8384f12fbcd59d3ad6e70bf86c97cbf"}
};
}
}
@@ -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>