I am having an issue accessing the 'close' field in the History dataframe for the S&P Futures Contract with the highest open interest. See code below:
class MultidimensionalNadionCircuit(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 5, 18) # Set Start Date
self.SetEndDate(2020, 5, 18) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.SP = self.AddFuture(Futures.Indices.SP500EMini,Resolution.Minute)
self.SP.SetFilter(0, 90)
def OnData(self, slice):
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]
hist = self.History(self.liquidContract.Symbol, 60, Resolution.Minute)
hist =hist["close"]
def OnEndOfDay(self):
pass
In the hist=hist["close"] statement, I get an error: Runtime Error: Trying to retrieve an element from a collection using a key that does not exist in that collection throws a KeyError exception KeyError : 'close' . Shouldn't I be able top access the 'close' column of the hist dataframe? Is this a bug?
John Lewis
Does anyone have any updates about this?
Alexandre Catarino
Hi John,
Sorry about the wait.
We have answered this issue at:
Getting KeyError Exception when Accessing History Object #8224
John Lewis
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!