The attached backtest runs successfully.
But if I change the start and end dates from:
self.SetStartDate(2010, 1, 1)
self.SetEndDate(2010, 3, 31)
to:
self.SetStartDate(2010, 1, 1)
self.SetEndDate(2010, 12, 31)
Then the backtest will throw the following runtime error:
Runtime Error: IndexError : list index out of range IndexError : list index out of range
This is the stack trace:
System.Exception: IndexError : list index out of range ---> Python.Runtime.PythonException: IndexError : list index out of range
at Python.Runtime.PyObject.Invoke (Python.Runtime.PyTuple args, Python.Runtime.PyDict kw) [0x00033] in :0
at Python.Runtime.PyObject.InvokeMethod (System.String name, Python.Runtime.PyTuple args, Python.Runtime.PyDict kw) [0x00007] in :0
at Python.Runtime.PyObject.TryInvokeMember (System.Dynamic.InvokeMemberBinder binder, System.Object[] args, System.Object& result) [0x0003e] in :0
at (wrapper dynamic-method) System.Object.CallSite.Target(System.Runtime.CompilerServices.Closure, System.Runtime.CompilerServices.CallSite, object, QuantConnect.Data.UniverseSelection.SecurityChanges)
at QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper.OnFrameworkSecuritiesChanged (QuantConnect.Data.UniverseSelection.SecurityChanges changes) [0x0004f] in <60f7e514d9f742459223002854eb0074>:0
at QuantConnect.Lean.Engine.AlgorithmManager.Run (QuantConnect.Packets.AlgorithmNodePacket job, QuantConnect.Interfaces.IAlgorithm algorithm, QuantConnect.Lean.Engine.DataFeeds.ISynchronizer synchronizer, QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler transactions, QuantConnect.Lean.Engine.Results.IResultHandler results, QuantConnect.Lean.Engine.RealTime.IRealTimeHandler realtime, QuantConnect.Lean.Engine.Server.ILeanManager leanManager, QuantConnect.Lean.Engine.Alpha.IAlphaHandler alphas, System.Threading.CancellationToken token) [0x008a6] in Lean.Engine.Alpha.IAlphaHandler alphas, System.Threading.CancellationToken token) [0x008a6] in <07c77997a4904cb4925edfc7c062ccfe>:0
--- End of inner exception stack trace ---
I couldn't figure out what went wrong because I don't know which file and line in my code that is throwing this exception.
Please help.
Shile Wen
Hi Moeh,
The error is caused by the warmup function in alphamodel line 75, because sometimes the history DataFrame is empty. As a fix, I suggest adding the line
if history.empty: return
at the beginning of the warmup function to make sure indexing errors don’t occur. I've also attached a backtest with the fix.For the future, a great way to solve these problems could be through our debugger, and a guide on how to use it can be found here.
Best,
Shile Wen
Moeh12
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!