Security initialization on re-addition (#8681)
API Tests / build (push) Has been cancelled
Benchmarks / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
Regression Tests / build (push) Has been cancelled
Report Generator Tests / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled
Syntax Tests / build (push) Has been cancelled
Python Virtual Environments / build (push) Has been cancelled

* Initialize security after addition and re-addition

* Improve regression algorithms

* Add universe selection regression algorithm

* Add minor comment
This commit is contained in:
Jhonathan Abreu
2025-04-09 16:26:05 -04:00
committed by GitHub
parent ea41fedb30
commit e90309678e
9 changed files with 470 additions and 35 deletions
@@ -24,6 +24,8 @@ namespace QuantConnect.Algorithm.CSharp
/// It asserts that the securities are marked as non-tradable when removed and that they are tradable when re-added.
/// It also asserts that the algorithm receives the correct security changed events for the added and removed securities.
///
/// Additionally, it tests that the security is initialized after every addition, and no more.
///
/// This specific algorithm tests this behavior for manually added option contracts.
/// </summary>
public class SecurityInitializationOnReAdditionForManuallyAddedOptionRegressionAlgorithm : SecurityInitializationOnReAdditionForEquityRegressionAlgorithm
@@ -36,15 +38,48 @@ namespace QuantConnect.Algorithm.CSharp
342.9m,
new DateTime(2014, 07, 19));
private int _securityAdditionsCount;
protected override DateTime StartTimeToUse => new DateTime(2014, 06, 04);
protected override DateTime EndTimeToUse => new DateTime(2014, 06, 20);
protected override Security AddSecurity()
{
_securityAdditionsCount++;
return AddOptionContract(_optionContractSymbol, Resolution.Daily);
}
protected override void AssertSecurityInitializationCount(Dictionary<Security, int> securityInializationCounts, Security security)
{
// The first time the contract is added, the underlying equity will be added and initialized as well.
// The following times the contract is added, the underlying equity will not be added again.
var expectedSecuritiesInitialized = 1;
if (_securityAdditionsCount == 1)
{
expectedSecuritiesInitialized = 2;
}
if (securityInializationCounts.Count != expectedSecuritiesInitialized)
{
throw new RegressionTestException($"Expected {expectedSecuritiesInitialized} security to be initialized. " +
$"Got {securityInializationCounts.Count}");
}
if (!securityInializationCounts.TryGetValue(security, out var count) || count != 1)
{
throw new RegressionTestException($"Expected the option contract to be initialized once and once only, " +
$"but was initialized {count} times");
}
if (expectedSecuritiesInitialized == 2 &&
!securityInializationCounts.TryGetValue(Securities[security.Symbol.Underlying], out count) || count != 1)
{
throw new RegressionTestException($"Expected the underlying security to be initialized once and once only, " +
$"but was initialized {count} times");
}
}
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
@@ -53,7 +88,7 @@ namespace QuantConnect.Algorithm.CSharp
/// <summary>
/// Data Points count of the algorithm history
/// </summary>
public override int AlgorithmHistoryDataPoints => 0;
public override int AlgorithmHistoryDataPoints => 5;
/// <summary>
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm