Hi
I'm coding up a classic equities Momentum portfolio strategy which uses an sma on the index as a filter to allow buying or commence scale out of portfolio positions . I'm using Coarse and Fine filtering on the Universe, then ranking those stocks by a custom momentum indicator and using risk parity position sizing based on recent volatility to establish and rebalance the portfolio.
While the strategy doesn't trade the SPY, I've added it to the data feeds (and hence Universe I believe) to be able to calculate a moving average and use close > ma or below as filter as noted above. I've used the SymbolData pattern (holding a dictionary of symbols and instantiating a SymbolData object to handle indicators, warm them up, get values etc).
However the the numerical precision / factor file stuff is causing a problem that I don't understand - it's messing up the data being sent to OnData. Basically it seems that something entirely unrelated to my strategy (factor data) is causing issues with the backtest.
Would appreciate some help from someone more experienced here. Is there a better approach that avoids this issue, or is it a bug in LEAN?
1998-02-02 00:00:00 :Due to numerical precision issues in the factor file, data for the following symbols was adjust to a later starting date: [SPY, 1/1/1998]1998-02-02 00:00:00 :The starting dates for the following symbols have been adjusted to match their map files first date: [SPY, 1998-01-02]1998-02-02 00:00:00 :Runtime Error: 'SPY' wasn't found in the Slice object, likely because there was no-data at this moment in time and it wasn't possible to fillforward historical data. Please check the data exists before accessing it with data.ContainsKey("SPY") in Slice.cs:line 329
Simon David
This issue is also occurring for Algorithm Framework style strategies.
As I'm fairly new to LEAN, it's certainly possible that there is a canonical pattern that avoids this issue, but I've done a fair bit of research across example strategies, and some extensive debugging on the strategy execution for this specific implementation, and this problem only occurs after a symbol in the datafeed is adjusted for the factor file numerical precision issues, so it really looks like that adjustment process is stuffing up the integrity of the backtesting datafeed. Given my strategy doesn't use factor data, that's definitely unexpected and kinda weird.
Simon David
This might be because of the order in which the Algorithm Engine runs
surprisingly the Slice object is updated to current before Universe Selection for classic style strategies, so gets passed to OnData before OnSecurityChanges has a chance to run for this slice. However for Algorithm Framework Style strategies, universe selection occurs before Update is called in the AlphaModel so in theory it should work (and we wouldn't see the issue listed above).
Simon David
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!