-
-
Couldn't load subscription status.
- Fork 3.8k
Description
Expected Behavior
The algorithm receives a "removed" security notification in SecurityChanges.RemovedSecurities (in OnSecuritiesChanged) only once when the security is deselected from a universe, even if the algorithm has an open position for that security.
Actual Behavior
When a universe selects a security and the algorithm opens a position for it, the security is kept by the universe because of the open position, but the algorithm still gets the "removed" notification in OnSecuritiesChanged.
But since the universe keeps the security (as "pending for removal" when the position is liquidated), on subsequent selections that security is added to the SecurityChanges.RemovedSecurities every time.
Potential Solution
Don't add securities "pending for removal" to the removed securities collection in UniverseSelection on subsequent selections.
Reproducing the Problem
https://www.quantconnect.cloud/backtest/d1c89dc0872425cbcabe1db3b799b001/?theme=chrome
See the logs in OnSecuritiesChanged:
2024-04-23 00:00:00 : [4/23/2024 12:00:00 AM] :: Removed securities: SPY R735QTJ8XC9X
2024-04-24 00:00:00 : [4/24/2024 12:00:00 AM] :: Removed securities: AAPL R735QTJ8XC9X, SPY R735QTJ8XC9X
2024-04-25 00:00:00 : [4/25/2024 12:00:00 AM] :: Removed securities: GOOCV VP83T1ZUHROL, SPY R735QTJ8XC9X
2024-04-26 00:00:00 : [4/26/2024 12:00:00 AM] :: Removed securities: AAPL R735QTJ8XC9X, SPY R735QTJ8XC9X
2024-04-27 00:00:00 : [4/27/2024 12:00:00 AM] :: Removed securities: GOOCV VP83T1ZUHROL, SPY R735QTJ8XC9X
2024-04-30 00:00:00 : [4/30/2024 12:00:00 AM] :: Removed securities: AAPL R735QTJ8XC9X, SPY R735QTJ8XC9X
2024-05-01 00:00:00 : [5/1/2024 12:00:00 AM] :: Removed securities: GOOCV VP83T1ZUHROL, SPY R735QTJ8XC9X
2024-05-02 00:00:00 : [5/2/2024 12:00:00 AM] :: Removed securities: AAPL R735QTJ8XC9X, SPY R735QTJ8XC9X
2024-05-03 00:00:00 : [5/3/2024 12:00:00 AM] :: Removed securities: GOOCV VP83T1ZUHROL, SPY R735QTJ8XC9X
2024-05-04 00:00:00 : [5/4/2024 12:00:00 AM] :: Removed securities: AAPL R735QTJ8XC9X, SPY R735QTJ8XC9X
2024-05-07 00:00:00 : [5/7/2024 12:00:00 AM] :: Removed securities: GOOCV VP83T1ZUHROL, SPY R735QTJ8XC9X
2024-05-08 00:00:00 : [5/8/2024 12:00:00 AM] :: Removed securities: AAPL R735QTJ8XC9X, SPY R735QTJ8XC9X
2024-05-09 00:00:00 : [5/9/2024 12:00:00 AM] :: Removed securities: GOOCV VP83T1ZUHROL, SPY R735QTJ8XC9X
2024-05-10 00:00:00 : [5/10/2024 12:00:00 AM] :: Removed securities: AAPL R735QTJ8XC9X, SPY R735QTJ8XC9X
2024-05-11 00:00:00 : [5/11/2024 12:00:00 AM] :: Removed securities: GOOCV VP83T1ZUHROL, SPY R735QTJ8XC9X
2024-05-14 00:00:00 : [5/14/2024 12:00:00 AM] :: Removed securities: AAPL R735QTJ8XC9X, SPY R735QTJ8XC9X
2024-05-15 00:00:00 : [5/15/2024 12:00:00 AM] :: Removed securities: GOOCV VP83T1ZUHROL, SPY R735QTJ8XC9X
2024-05-16 00:00:00 : [5/16/2024 12:00:00 AM] :: Removed securities: AAPL R735QTJ8XC9X, SPY R735QTJ8XC9X
2024-05-17 00:00:00 : [5/17/2024 12:00:00 AM] :: Removed securities: GOOCV VP83T1ZUHROL, SPY R735QTJ8XC9X
2024-05-18 00:00:00 : [5/18/2024 12:00:00 AM] :: Removed securities: AAPL R735QTJ8XC9X, SPY R735QTJ8XC9X
System Information
'QC Cloud
Windows 11
Checklist
- I have completely filled out this template
- I have confirmed that this issue exists on the current
masterbranch - I have confirmed that this is not a duplicate issue by searching issues
- I have provided detailed steps to reproduce the issue