quantopian / zipline

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

24/7 Trading #614

Open vertangelx opened 9 years ago

vertangelx commented 9 years ago

How can we define our own trading days and hours for special markets? For example, what settings will you change and how will you change it for a market that trades 24/7?

ssanderson commented 9 years ago

ping @jfkirk

ssanderson commented 9 years ago

@vertangelx I have a mostly-finished branch that overhauls the process for creating a custom trading calendar. That code lives in https://github.com/quantopian/zipline/pull/556, though I'm not actively working on it at the moment. I haven't thought deeply about how to accomodate 24-hour markets, though I think @jfkirk has looked at that problem to some extent while working on support for futures.

dannychua commented 9 years ago

@ssanderson @jfkirk +1 interested in 24/7 markets

warren-oneill commented 9 years ago

Hi all, I currently have a prototype implement for the German Epex energy market which is also 24/7. What I did was a bit of a work-around but I basically I made a new tradingcalendar file and set the trading_days to be all days in the range and set the market hours to be between 00:00 and 23:59 although setting close to be at 00:00 on the following day could be better.

I then also had to make sure that the benchmark and treasury curves also had the same days as trading_days. To do this I wrote my own version of the load_market_data which generates a constant returns benchmark with the correct calendar and for the treasury curves I used reindex and forward-fill.

Finally you must intialize the TradingEnvironmnet with the correct calendar and loader before you run your algo:

trading.env = TradingEnvironment(env_trading_calendar=calendar_epex, load=load_epex)

Here are the links to the calendar and loader code.

One thing you have to watch out for is the timezone changes since this can push open or close into another day when converting to UTC but I'm hoping that this issue has been resolved with #579

vertangelx commented 9 years ago

@warren-oneill How do you use loader_power.py to get load_epex that you pass into TradingEnvironment?

import loader_power as load_epex produced a TypeError: 'module' object is not callable.

trading.environment = TradingEnvironment(env_trading_calendar=calendar_epex, load=load_epex)

/Users/vertangelx/anaconda/envs/zipline/lib/python2.7/site-packages/zipline/finance/trading.pyc in __init__(self, load, bm_symbol, exchange_tz, max_date, env_trading_calendar)
     99 
    100         self.benchmark_returns, treasury_curves_map = \
--> 101             load(self.bm_symbol)
    102 
    103         self.treasury_curves = pd.DataFrame(treasury_curves_map).T
warren-oneill commented 9 years ago

Try

from zipline.data.loader_power import load_market_data as load_epex

trading.environment = TradingEnvironment(env_trading_calendar=calendar_epex, load=load_epex)
vertangelx commented 9 years ago

@warren-oneill I'm now getting an error AssertionError: Period end falls before the first known trading day.

data.AAA.price.head()

timestamp
2015-04-30 20:00:00+00:00    237.413
2015-04-30 21:00:00+00:00    237.914
2015-04-30 22:00:00+00:00    239.400
2015-04-30 23:00:00+00:00    238.550
2015-05-01 00:00:00+00:00    235.926
Freq: H, Name: price, dtype: float64

print calendar_epex.trading_days

DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
           '2013-01-05', '2013-01-06', '2013-01-07', '2013-01-08',
           '2013-01-09', '2013-01-10', 
           ...
           '2016-06-13', '2016-06-14', '2016-06-15', '2016-06-16',
           '2016-06-17', '2016-06-18', '2016-06-19', '2016-06-20',
           '2016-06-21', '2016-06-22'],
          dtype='datetime64[ns]', length=1269, freq='D', tz='UTC')
warren-oneill commented 9 years ago

Try defining the start and end of the simulation

from zipline.utils.factory import create_simulation_parameters

start = data.index[0]
end = data.index[-1]
sim_params = create_simulation_parameters(start=start, end=end)
trading.environment = TradingEnvironment(env_trading_calendar=calendar_epex, load=load_epex,
                                                                     sim_params=sim_params)

where start and end are the first and last timestamp of your data.

vertangelx commented 9 years ago

@warren-oneill A different error this time, thank you for your patience! Not sure why it's looking for 2015-04-29. which is 1 day before the first timestamp of data.

KeyError: Timestamp('2015-04-29 00:00:00+0000', tz='UTC')

Did some prints:

print data.AAA.index[0]
print data.AAA.index[-1]

2015-04-30 20:00:00+00:00
2015-06-14 02:00:00+00:00
warren-oneill commented 9 years ago

are you using the newest version of zipline installed from github?

vertangelx commented 9 years ago

@warren-oneill zp.__version__ gave '0.8.0rc1'. I did another update of zipline with pip install git+https://github.com/quantopian/zipline --upgrade but the same error persist

Tried start = data.index[10] but that did not help.

warren-oneill commented 9 years ago

It could be something to do with timezone changes but its hard to say.

ProZachJ commented 7 years ago

Has anyone had any luck with this? I've implemented my own calendar as suggested above, but have run into the benchmark issue (at least I think that is what is causing the error). The example code @warren-oneill put up isn't visible anymore so having some difficulty understanding what other changes I need to make to get my custom calendar running.

Here is what I've done so far.

https://github.com/quantopian/zipline/compare/master...ProZachJ:poloniex_bundle