Trading Calendars
What is a Trading Calendar? 什么是交易日歷?
A trading calendar represents the timing information of a single market exchange. The timing information is made up of two parts: sessions, and opens/closes. This is represented by the Zipline TradingCalendar
class, and is used as the parent class for all new TradingCalendar
s.
交易日歷表示單個市場交易所的時序信息。 時序信息由兩部分組成:會話和打開/關閉。這由Zipline TradingCalendar類表示,並用作所有新的TradingCalendar的父類。
A session represents a contiguous set of minutes, and has a label that is midnight UTC. It is important to note that a session label should not be considered a specific point in time, and that midnight UTC is just being used for convenience.
會話表示一組連續的分鍾集合,並且具有UTC午夜標簽。需要注意的是,會話標簽不應被視為特定的時間點,並且UTC的午夜時間僅用於方便。
For an average day of the New York Stock Exchange, the market opens at 9:30AM and closes at 4PM. Trading sessions can change depending on the exchange, day of the year, etc.
在紐約證券交易所平均一天,市場在上午9:30開市,並在下午4點關閉。 交易時段可以根據交易所,一年中的某一天等而變化。
Why Should You Care About Trading Calendars? 為什么你應該關心交易日歷?
Let’s say you want to buy a share of some equity on Tuesday, and then sell it on Saturday. If the exchange in which you’re trading that equity is not open on Saturday, then in reality it would not be possible to trade that equity at that time, and you would have to wait until some other number of days past Saturday. Since you wouldn’t be able to place the trade in reality, it would also be unreasonable for your backtest to place a trade on Saturday.
假設您想在周二買入一些股票,然后在周六賣出。 如果您交易該股票的交易所周六未開放,那么實際上當時不可能交易該股票,您必須等到周六之后的其他天數。 由於您無法將交易置於現實之中,因此您的回測在周六進行交易也是不合理的。
In order for you to backtest your strategy, the dates in that are accounted for in your data bundle and the dates in your TradingCalendar
should match up; if the dates don’t match up, then you you’re going to see some errors along the way. This holds for both minutely and daily data.
為了讓您對策略進行回溯測試,您的數據包中會記錄日期,並且您的TradingCalendar中的日期應該匹配; 如果日期不匹配,那么你會看到一些錯誤。 這適用於分鍾級數據和每日數據。
The TradingCalendar Class
The TradingCalendar
class has many properties we should be thinking about if we were to build our own TradingCalendar
for an exchange. These include properties such as:
如果我們要為交易所構建自己的TradingCalendar,TradingCalendar類具有許多我們應該考慮的屬性。 這些包括如下屬性:
- Name of the Exchange
- Timezone
- Open Time
- Close Time
- Regular & Ad hoc Holidays
- Special Opens & Closes
And several others. If you’d like to see all of the properties and methods available to you through the TradingCalendar
API, please take a look at the API Reference
還有其他幾個。 如果您想通過TradingCalendar API查看所有可用的屬性和方法,請查看API參考
Now we’ll take a look at the London Stock Exchange Calendar LSEExchangeCalendar
as an example below:
現在我們來看看倫敦證券交易所日歷 LSEExchangeCalendar,下面是一個例子:
class LSEExchangeCalendar(TradingCalendar): """ Exchange calendar for the London Stock Exchange Open Time: 8:00 AM, GMT Close Time: 4:30 PM, GMT Regularly-Observed Holidays: - New Years Day (observed on first business day on/after) - Good Friday - Easter Monday - Early May Bank Holiday (first Monday in May) - Spring Bank Holiday (last Monday in May) - Summer Bank Holiday (last Monday in May) - Christmas Day - Dec. 27th (if Christmas is on a weekend) - Boxing Day - Dec. 28th (if Boxing Day is on a weekend) """ @property def name(self): return "LSE" @property def tz(self): return timezone('Europe/London') @property def open_time(self): return time(8, 1) @property def close_time(self): return time(16, 30) @property def regular_holidays(self): return HolidayCalendar([ LSENewYearsDay, GoodFriday, EasterMonday, MayBank, SpringBank, SummerBank, Christmas, WeekendChristmas, BoxingDay, WeekendBoxingDay ])
You can create the Holiday
objects mentioned in def regular_holidays(self)` through the `pandas <http://pandas.pydata.org/pandas-docs/stable/>`__ module, ``pandas.tseries.holiday.Holiday
, and also take a look at the LSEExchangeCalendar code as an example, or take a look at the code snippet below.
from pandas.tseries.holiday import ( Holiday, DateOffset, MO ) SomeSpecialDay = Holiday( "Some Special Day", month=1, day=9, offset=DateOffSet(weekday=MO(-1)) )
Building a Custom Trading Calendar 建立自定義交易日歷
Now we’ll build our own custom trading calendar. This calendar will be used for trading assets that can be traded on a 24/7 exchange calendar. This means that it will be open on Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday, and the exchange will open at 12AM and close at 11:59PM. The timezone which we’ll use is UTC.
現在我們將建立自己的自定義交易日歷。 此日歷將用於交易可在24/7交易日歷上交易的資產。 這意味着它將在星期一,星期二,星期三,星期四,星期五,星期六和星期日開放,交易所將於上午12點開盤並於晚上11:59結束。 我們將使用的時區是UTC。
First we’ll start off by importing some modules that will be useful to us. 首先,我們將通過導入一些對我們有用的模塊開始。
# for setting our open and close times from datetime import time # for setting our start and end sessions import pandas as pd # for setting which days of the week we trade on from pandas.tseries.offsets import CustomBusinessDay # for setting our timezone from pytz import timezone # for creating and registering our calendar from zipline.utils.calendars import ( register_calendar, TradingCalendar ) from zipline.utils.memoize import lazyval
And now we’ll actually build this calendar, which we’ll call TFSExchangeCalendar
: 現在我們將構建這個日歷,我們將其稱為TFSExchangeCalendar:
class TFSExchangeCalendar(TradingCalendar): """ An exchange calendar for trading assets 24/7. Open Time: 12AM, UTC Close Time: 11:59PM, UTC """ @property def name(self): """ The name of the exchange, which Zipline will look for when we run our algorithm and pass TFS to the --trading-calendar CLI flag. """ return "TFS" @property def tz(self): """ The timezone in which we'll be running our algorithm. """ return timezone("UTC") @property def open_time(self): """ The time in which our exchange will open each day. """ return time(0, 0) @property def close_time(self): """ The time in which our exchange will close each day. """ return time(23, 59) @lazyval def day(self): """ The days on which our exchange will be open. """ weekmask = "Mon Tue Wed Thu Fri Sat Sun" return CustomBusinessDay( weekmask=weekmask )
Conclusions
In order for you to run your algorithm with this calendar, you’ll need have a data bundle in which your assets have dates that run through all days of the week. You can read about how to make your own data bundle in the Writing a New Bundle documentation, or use the csvdir bundle for creating a bundle from CSV files.