Zipline KeyError accessing self._calendar_offsets with sid

22 Views Asked by At

I am trying to backtest a futures trading strategy with daily frequency. I have a database of several thousand futures contracts, which need to be stitched toghether via Zipline's calculation of the roll. I was able to create a bundle via ingest, but I am running into an error as soon as I execute zipline.run_algorithm, more specifically, this code:

hist = data.history(
        context.universe, 
        fields=['close','volume'], 
        frequency='1d', 
        bar_count=250,
    )

In calculating the history, it appears zipline is already calculating the roll (a point I am a bit confused about since I have also seen the roll calculated elsewhere in several tutorials), and in this calculation it is trying to access self._calendar_offsets with each sid. The error is happening when it tries to access self._calendar_offsets with sid = 2.

except Exception as exc:
    629     raise NoDataOnDate(
    630         "day={0} is outside of calendar={1}".format(day, self.sessions)
    631     ) from exc
--> 632 offset = day_loc - self._calendar_offsets[sid]
    633 if offset < 0:
    634     raise NoDataBeforeDate(
    635         "No data on or before day={0} for sid={1}".format(day, sid)
    636     )

KeyError: 2

I suspect this has to do with how I am assigning sids. Should I be assigning unique sids to every contract or every chain of contracts (with the same underlying asset, i.e. Corn)? The tutorial code I am working with does the former. Thanks for your help!

0

There are 0 best solutions below