1919using QuantConnect . Data . UniverseSelection ;
2020using QuantConnect . Interfaces ;
2121using QuantConnect . Securities ;
22- using QuantConnect . Securities . Equity ;
2322
2423namespace QuantConnect . Algorithm . CSharp
2524{
2625 /// <summary>
26+ /// Regression algorithm testing the behavior of the algorithm when a security is removed and re-added.
27+ /// It asserts that the securities are marked as non-tradable when removed and that they are tradable when re-added.
28+ /// It also asserts that the algorithm receives the correct security changed events for the added and removed securities.
29+ ///
30+ /// This specific algorithm tests this behavior for equities.
2731 /// </summary>
2832 public class SecurityInitializationOnReAdditionForEquityRegressionAlgorithm : QCAlgorithm , IRegressionAlgorithmDefinition
2933 {
30- private Security _equity ;
34+ private Security _security ;
3135 private Queue < DateTime > _tradableDates ;
3236 private bool _securityWasRemoved ;
3337
38+ protected virtual DateTime StartTimeToUse => new DateTime ( 2013 , 10 , 05 ) ;
39+
40+ protected virtual DateTime EndTimeToUse => new DateTime ( 2013 , 10 , 30 ) ;
41+
3442 public override void Initialize ( )
3543 {
36- SetStartDate ( 2013 , 10 , 04 ) ;
37- SetEndDate ( 2013 , 10 , 30 ) ;
44+ SetStartDate ( StartTimeToUse ) ;
45+ SetEndDate ( EndTimeToUse ) ;
3846
39- _equity = AddEquity ( ) ;
47+ _security = AddSecurity ( ) ;
4048
41- _tradableDates = new ( QuantConnect . Time . EachTradeableDay ( _equity . Exchange . Hours , StartDate , EndDate ) ) ;
49+ _tradableDates = new ( QuantConnect . Time . EachTradeableDay ( _security . Exchange . Hours , StartDate , EndDate ) ) ;
4250
43- Schedule . On ( DateRules . EveryDay ( _equity . Symbol ) , TimeRules . Midnight , ( ) =>
51+ Schedule . On ( DateRules . EveryDay ( _security . Symbol ) , TimeRules . Midnight , ( ) =>
4452 {
4553 var currentTradableDate = _tradableDates . Dequeue ( ) ;
4654 if ( currentTradableDate != Time . Date )
@@ -54,21 +62,24 @@ public override void Initialize()
5462 }
5563
5664 // Remove the security every day
57- Debug ( $ "[{ Time } ] Removing the equity ") ;
58- _securityWasRemoved = RemoveSecurity ( _equity . Symbol ) ;
65+ Debug ( $ "[{ Time } ] Removing the security ") ;
66+ _securityWasRemoved = RemoveSecurity ( _security . Symbol ) ;
5967
6068 if ( ! _securityWasRemoved )
6169 {
62- throw new RegressionTestException ( $ "Expected the equity to be removed") ;
70+ throw new RegressionTestException ( $ "Expected the security to be removed") ;
71+ }
72+
73+ if ( _security . IsTradable )
74+ {
75+ throw new RegressionTestException ( $ "Expected the security to be not tradable after removing it") ;
6376 }
6477 } ) ;
6578 }
6679
67- private Equity AddEquity ( )
80+ protected virtual Security AddSecurity ( )
6881 {
69- var equity = AddEquity ( "SPY" ) ;
70-
71- return equity ;
82+ return AddEquity ( "SPY" ) ;
7283 }
7384
7485 public override void OnSecuritiesChanged ( SecurityChanges changes )
@@ -80,30 +91,30 @@ public override void OnSecuritiesChanged(SecurityChanges changes)
8091 throw new RegressionTestException ( $ "Expected no securities to be added. Got { changes . AddedSecurities . Count } ") ;
8192 }
8293
83- if ( ! changes . RemovedSecurities . Contains ( _equity ) )
94+ if ( ! changes . RemovedSecurities . Contains ( _security ) )
8495 {
85- throw new RegressionTestException ( $ "Expected the equity to be removed. Got { changes . RemovedSecurities . Count } ") ;
96+ throw new RegressionTestException ( $ "Expected the security to be removed. Got { changes . RemovedSecurities . Count } ") ;
8697 }
8798
8899 _securityWasRemoved = false ;
89100
90101 // Add the security back
91- Debug ( $ "[{ Time } ] Re-adding the equity ") ;
92- var reAddedEquity = AddEquity ( ) ;
102+ Debug ( $ "[{ Time } ] Re-adding the security ") ;
103+ var reAddedSecurity = AddSecurity ( ) ;
93104
94- if ( ! ReferenceEquals ( reAddedEquity , _equity ) )
105+ if ( ! ReferenceEquals ( reAddedSecurity , _security ) )
95106 {
96- throw new RegressionTestException ( $ "Expected the re-added equity to be the same as the original equity ") ;
107+ throw new RegressionTestException ( $ "Expected the re-added security to be the same as the original security ") ;
97108 }
98109
99- if ( ! reAddedEquity . IsTradable )
110+ if ( ! reAddedSecurity . IsTradable )
100111 {
101- throw new RegressionTestException ( $ "Expected the re-added equity to be tradable") ;
112+ throw new RegressionTestException ( $ "Expected the re-added security to be tradable") ;
102113 }
103114 }
104- else if ( ! changes . AddedSecurities . Contains ( _equity ) )
115+ else if ( ! changes . AddedSecurities . Contains ( _security ) )
105116 {
106- throw new RegressionTestException ( $ "Expected the equity to be added back") ;
117+ throw new RegressionTestException ( $ "Expected the security to be added back") ;
107118 }
108119 }
109120
@@ -128,12 +139,12 @@ public override void OnEndOfAlgorithm()
128139 /// <summary>
129140 /// Data Points count of all timeslices of algorithm
130141 /// </summary>
131- public long DataPoints => 4823 ;
142+ public virtual long DataPoints => 4036 ;
132143
133144 /// <summary>
134145 /// Data Points count of the algorithm history
135146 /// </summary>
136- public int AlgorithmHistoryDataPoints => 0 ;
147+ public virtual int AlgorithmHistoryDataPoints => 0 ;
137148
138149 /// <summary>
139150 /// Final status of the algorithm
@@ -143,7 +154,7 @@ public override void OnEndOfAlgorithm()
143154 /// <summary>
144155 /// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
145156 /// </summary>
146- public Dictionary < string , string > ExpectedStatistics => new Dictionary < string , string >
157+ public virtual Dictionary < string , string > ExpectedStatistics => new Dictionary < string , string >
147158 {
148159 { "Total Orders" , "0" } ,
149160 { "Average Win" , "0%" } ,
@@ -164,8 +175,8 @@ public override void OnEndOfAlgorithm()
164175 { "Beta" , "0" } ,
165176 { "Annual Standard Deviation" , "0" } ,
166177 { "Annual Variance" , "0" } ,
167- { "Information Ratio" , "-4.884 " } ,
168- { "Tracking Error" , "0.108 " } ,
178+ { "Information Ratio" , "-5.028 " } ,
179+ { "Tracking Error" , "0.11 " } ,
169180 { "Treynor Ratio" , "0" } ,
170181 { "Total Fees" , "$0.00" } ,
171182 { "Estimated Strategy Capacity" , "$0" } ,
0 commit comments