stefan-jansen / zipline-reloaded

Zipline, a Pythonic Algorithmic Trading Library
https://zipline.ml4trading.io
Apache License 2.0
1.02k stars 198 forks source link

Calendar Offsets Issue #194

Open sorensenj50 opened 1 year ago

sorensenj50 commented 1 year ago

Dear Zipline Maintainers,

I apologize if this is the wrong place to post this issue. I have already posted in on Stack Overflow but haven't gotten a response yet.

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.

If my problem requires further details about my environment or my code, I am happy to respond. My only hunch is the SID issue--beyond that I don't know what could be driving this error. I am sorry for not providing a MVP--I would appreciate even general advice about what could be causing this error (i.e. data issue, something when i'm ingesting it, how i'm handling SIDs, etc). If it is necessary, I can try to make an MVP that shows this problem but I suspect that it would be highly non-trivial work.

MBounouar commented 1 year ago

Yes, every contract should have a unique sid. But what is important for the futures to work, is to pay attention to the root_symbol, and that all the the futures contracts important dates are correctly defined and aligned with the calendar your are using, otherwise you will run into all kind of issues during the contract roll. Check how A. Clenow and https://github.com/hsm207/trading_evolved ingest the data.