quantopian / zipline

Zipline, a Pythonic Algorithmic Trading Library
https://www.zipline.io
Apache License 2.0
17.47k stars 4.71k forks source link

Calendar hardcoded to US stock market #2434

Open willianpaixao opened 5 years ago

willianpaixao commented 5 years ago

Hi fellow maintainers,

I've been trying to ingest non-USA CSV bundles with Zipline, but it proved much harder than I calculated. Turns out that there are some hardcoded variables that bind the process to only NYSE and UTC data. Follow some lines I had to change in order to have a successful (without crashes) bundle ingestion:

https://github.com/quantopian/zipline/blob/05a6080eed951f80da3b6f7ee4962101884f328e/zipline/data/bundles/csvdir.py#L161 https://github.com/quantopian/zipline/blob/05a6080eed951f80da3b6f7ee4962101884f328e/zipline/data/bundles/csvdir.py#L227

When I tried to fix the values, I've got an error here:

File "zipline/data/bundles/csvdir.py", line 161, in csvdir_bundle      
    metadata['exchange'] = calendar_name                                                                                 

NameError: name 'calendar_name' is not defined

It looked weird but I didn't have time to follow up and debug it.

My current state is simply change the hardcoded values from 'NYSE' to 'BVMF' and it works. Any suggestions on how to fix it for the long term?

willianpaixao commented 4 years ago

@ssanderson @freddiev4 any feedback?

billmoling commented 4 years ago

what you have made to get it work? @willianpaixao

llllllllll commented 4 years ago

Instead of setting an exchange of CSVDIR, we should have an environment variable that can be set at ingest time where users can set the calendar. Maybe something like CSVDIR_EXCHANGE=XNYS or something. The string would need to be a name that trading_calendars can parse. We can preserve the default calendar of XNYS (which NYSE is an alias of) if a user doesn't provide a value.

I think the code changes needed to make this work are:

  1. Change line 161 to: metadata['exchange'] = os.environ.get('CSVDIR_EXCHANGE', 'XNYS')
  2. Remove line 227
billmoling commented 4 years ago

@llllllllll Thanks very much. It works.

willianpaixao commented 3 years ago

@llllllllll I've followed your suggestion then tested #2674 locally and it works.