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.

 为了让您使用此日历运行算法,您需要有一个数据包,其中您的资产的日期贯穿了一周中的所有日子。 您可以阅读如何在编写新包的文档中创建自己的数据包,或使用csvdir包从CSV文件创建包。
 

Zipline Trading Calendars的更多相关文章

  1. Zipline Data Bundles

    Data Bundles A data bundle is a collection of pricing data, adjustment data, and an asset database. ...

  2. zipline框架--简介

    Zipline is a Pythonic algorithmic trading library. It is an event-driven system for backtesting. Zip ...

  3. Zipline Risk and Performance Metrics

    Risk and Performance Metrics 风险和性能指标 The risk and performance metrics are summarizing values calcula ...

  4. Zipline Beginner Tutorial

    Zipline Beginner Tutorial Basics Zipline is an open-source algorithmic trading simulator written in ...

  5. zipline目录结构

    下面列出了zipline主要的目录和文件结构和它的说明 ├── ci - 持续集成相关 ├── conda - 生成conda 包相关 ├── docs - 文档 │ ├── notebooks - ...

  6. Zipline入门教程

    Zipline Beginner Tutorial Basics 基础 Zipline is an open-source algorithmic trading simulator written ...

  7. High Frequency Trading (整理中...)

    什么是高频交易系统 1 交易指令完全是由电脑发送,对市场数据的响应延时在微秒级2 系统有专用的软硬件组成,研发时需要大量的计算机专家级的工作3 系统的硬件需要放在离交易所主机很近的位置,所谓co-lo ...

  8. uva 11054 wine trading in gergovia (归纳【好吧这是我自己起的名字】)——yhx

    As you may know from the comic \Asterix and the Chieftain's Shield", Gergovia consists of one s ...

  9. UVa11054 Gergovia的酒交易 Wine trading in Gergovia-递推

    https://vjudge.net/problem/UVA-11054 As you may know from the comic “Asterix and the Chieftain’s Shi ...

随机推荐

  1. Mac OS下配置PHP Nginx PHP-FPM

    首先需要安装homebrew, 不赘述了 php-fpm php-fpm是mac下自带的软件, 而且兼容不同的PHP版本, 不用额外安装, 但是fpm是需要配置的, 在/private/etc下有个模 ...

  2. 0065 MyBatis一级缓存与二级缓存

    数据库中数据虽多,但访问频率却不同,有的数据1s内就会有多次访问,而有些数据几天都没人查询,这时候就可以将访问频率高的数据放到缓存中,就不用去数据库里取了,提高了效率还节约了数据库资源 MyBatis ...

  3. jquery插件-table转Json数据插件

    使 用开源插件Table-to-json: 官方地址:http://lightswitch05.github.io/table-to-json/ 功能说明:将js对象table转换成javascrip ...

  4. imooc 生鲜超市笔记

    1.启动前端项目(Vue.js) cnpm run dev

  5. noj1475(递推题)统计多少个1

    http://acm.nbut.cn/Problem/view.xhtml?id=1475 题意:给出一个数,需要你统计在这个数范围内有多少个1........ 思路:从高位到低位计算,例如1312 ...

  6. DelphiXE8FMX工程实现无边框托动(发送消息)

    1.引用单元 uses Winapi.Windows, FMX.Platform.Win, Winapi.Messages; 2.发送消息 //发送系统消息SendMessage(FmxHandleT ...

  7. CSS学习笔记(9)--详解CSS中:nth-child的用法

    详解CSS中:nth-child的用法 前端的哥们想必都接触过css中一个神奇的玩意,可以轻松选取你想要的标签并给与修改添加样式,是不是很给力,它就是“:nth-child”. 下面我将用几个典型的实 ...

  8. Android——加载模式

    <activity android:name=".MainActivity" android:launchMode="standard"><! ...

  9. JSONObject与JSONArray

    最近在学习过程中用到了稍微复杂点的json数据需要将json数据解析出来,这里就截取一部分作为例子 1.JSONObject介绍 JSONObject-lib包是一个beans,collections ...

  10. JQuery EasyUI DataGrid动态合并(标题)单元) 一

    JS: /** * EasyUI DataGrid根据字段动态合并单元格 * @param fldList 要合并table的id * @param fldList 要合并的列,用逗号分隔(例如:&q ...