Hi all, I've been using the techniques from boot camp to filter for the highest OI. This was working fine until I created a new project this morning and tried backtesting with the same code. It is now giving a run time error saying that .liquidContract does not exist.
Error Message:
[ERROR] FATAL UNHANDLED EXCEPTION:Extensions.SetRuntimeError(): Extensions.SetRuntimeError(): RuntimeError at 11/11/2022 2:30:00 PM UTC. Context: OnData Python.Runtime.PythonException: 'HyperActiveOrangeCat' object has no attribute 'liquidContract', File "/QuantConnect/backtesting/./cache/algorithm/project/main.py", line 96, in OnData, price = self.Securities[self.liquidContract.Symbol].Price, at Python.Runtime.PythonException.ThrowLastAsClrException(), at Python.Runtime.PyObject.Invoke(PyTuple args, PyDict kw), at Python.Runtime.PyObject.TryInvoke(InvokeBinder binder, Object[] args, Object& result), at CallSite.Target(Closure , CallSite , Object , PythonSlice ), at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite site, T0 arg0, T1 arg1), at QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper.OnData(Slice slice) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs:line 632, at QuantConnect.Lean.Engine.AlgorithmManager.Run(AlgorithmNodePacket job, IAlgorithm algorithm, ISynchronizer synchronizer, ITransactionHandler transactions, IResultHandler results, IRealTimeHandler realtime, ILeanManager leanManager, IAlphaHandler alphas, CancellationToken token) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Engine/AlgorithmManager.cs:line 523,ApiConnection.TryRequest(backtest/status/update): Error: The operation has timed out.
Stack Trace:
'HyperActiveOrangeCat' object has no attribute 'liquidContract'
at OnData
price = self.Securities[self.liquidContract.Symbol].Price
at Python.Runtime.PythonException.ThrowLastAsClrException()
at Python.Runtime.PyObject.Invoke(PyTuple args in main.py: line 95
Relevant Code
Initialize:
self.es = self.AddFuture(Futures.Indices.SP500EMini, Resolution.Tick)
self.es.SetFilter(5, 100)
OnData(self, slice):
# Sort contracts for highest Open Interest and create a variable for the highest OI
for chain in slice.FutureChains:
self.popularContracts = [contract for contract in chain.Value if contract.OpenInterest > 1000]
if len(self.popularContracts) == 0:
continue
sortedByOIContracts = sorted(self.popularContracts, key=lambda k : k.OpenInterest, reverse=True)
self.liquidContract = sortedByOIContracts[0]
# Variable for liquid contract price
price = self.Securities[self.liquidContract.Symbol].Price
LoganW
Please see this back test of a simpler algorithm with the exact same futures contracts data lines that is working
Louis Szeto
Hi LoganW
There could be slices that contains no FutureChains data due to numerous reasons like non-regular market event or no data at that time. If that is the case on the first OnData call, the line “self.liquidContract = sortedByOIContracts[0]” will not be run, causing that error. We recommend adding a check after the for-loop:
Best
Louis
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.
LoganW
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!