Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calendar Offsets Issue #194

Open
sorensenj50 opened this issue May 30, 2023 · 1 comment
Open

Calendar Offsets Issue #194

sorensenj50 opened this issue May 30, 2023 · 1 comment

Comments

@sorensenj50
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants