Hello,

I converted one of your tutorial strategy into Algorithm Framework (AF) but they don’t produce the same result. Divergence starts from 2009-5-4 at ticker BBD.
Source strategy: https://www.quantconnect.com/tutorials/strategy-library/stock-selection-strategy-based-on-fundamental-factors

After debugging, I found that the newly fine selected symbols are not included in the AddedSecurities of OnSecuritiesChanged(), if they have already been added and removed before.
Further investigation shows that both ActiveSecurities and RemovedSecurites keep growing in AF after securities were removed by fine selection universe.
E.g., the BBD was added on 2009-02-03 and was removed on 2009-04-02.  In original strategy, BBD no longer exists in ActiveSecurities after it was removed from universe but it keeps existing in AF strategy’s ActiveSecurities. Therefore, on 2009-5-4, the original strategy can re-add BBD to trade however in AF strategy, BBD is not included in AddedSecurities and cannot be traded again, though both strategies generate the same universe.

The AF ActiveSecurities and RemovedSecurites will keep record of previously removed securities, making them disappear in AddedSecurities and therefore prevent AlphaModel to generate insights from them. As a result, each symbol can only be traded and liquidated once. When the fine universe select them again, trading results diverge between the original and AF strategy.

I tried to manually remove not invested security in OnData() by: result=self.ActiveSecurities.Remove(security.Symbol)
Although result shows false, the security still remains in ActiveSecurities.Keys and cannot be removed.

Therefore I cannot use AF to reproduce the same performance as the original strategy.

Please tell me if this is a bug or if I miss something. Thank you.

Regards
Zhe
 

Author