Hey Quants,I'm relatively new to Quantconnect and come as you might have guessed from the Quantopian community :). When trying out a backtest, every now and then a RuntimeError occurs at special times.
The attached backtest from 2010-today works without problems. If you start the same algorithm for the period 2005-today it aborts July 2006 with this error message:
Runtime Error: In Scheduled Event 'SPY: MonthStart: SPY: 15 min before MarketClose', AttributeError : 'Index' object has no attribute 'levels' AttributeError : 'Index' object has no attribute 'levels'
Stacktrace:
QuantConnect.Scheduling.ScheduledEventException: In Scheduled Event 'SPY: MonthStart: SPY: 15 min before MarketClose', ---> System.Exception: AttributeError : 'Index' object has no attribute 'levels' ---> Python.Runtime.PythonException: AttributeError : 'Index' object has no attribute 'levels'
at Python.Runtime.PyObject.Invoke (Python.Runtime.PyObject[] args) [0x00035] in <c56ab175820d412caf052e079c2ab9ef>:0
at QuantConnect.Scheduling.ScheduleManager+<>c__DisplayClass15_0.<On>b__0 (System.String name, System.DateTime time) [0x00011] in <76bf9fe7f75d4895a52327a40a07b107>:0
at QuantConnect.Scheduling.ScheduledEvent.OnEventFired (System.DateTime triggerTime) [0x00027] in <76bf9fe7f75d4895a52327a40a07b107>:0
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
Do you know any fix to this issue?Or maybe a workaround, to except that particular RuntimeError?
Thank you for your help.
Derek Melchin
Hi Rainer,
This error is because we are calling the `.index.levels` property on an empty DataFrame. To resolve this, we can define `calc_return` to use `CurrentSlice` instead of making a History call for the last minute of data.
Additionally, since we are trading at the close instead of midnight, we should use
self.UniverseSettings.Resolution = Resolution.Minute
to avoid stale fills.
Lastly, the algorithm above was calling `AddEquity` in `rebalance`. This is redundant as the universe selection methods manage the data subscriptions.
See the attached backtest for reference, which has the 3 changes above implemented. Going forward, consider replacing the remaining History calls with a SimpleMovingAverage indicator or a RollingWindow.
Best,
Derek Melchin
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.
Rainer Gabel
Hey Derek,
thanks for your fast support. Your code upgrade fixed this issue. However running in Jul 2008 there is another error generating endless canceled orders leading to a Backtest Handled Error: You have exceeded maximum number of orders (10000), for unlimited orders upgrade your account.Â
Would you mind checking this issue too?
Best,
Rainer
Rainer Gabel
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!