量化交易学习(四十三)backtrader文档——交易日历

今天这篇是backtrader文档的学习笔记。主要介绍了交易日历。

官方文档链接:https://www.backtrader.com/docu/tradingcalendar/tradingcalendar/

交易日历

1.9.42.116版本增加了对交易日历的支持。例如,在以下场景重新采样时,这非常有用:

  • 每日到每周重新采样现在可以提供每周条形图以及本周的最后一个条形图。

    之所以如此,是因为交易日历可以提前确定下一个交易日和一周的最后一个交易日

  • 当会话结束不是常规结束时(可以在数据源中指定),不到一天的数据重新采样为日频数据

交易日历接口

有一个TradingCalendarBase基类可用作任何交易日历的基础。它定义了两 个必须重写的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class TradingCalendarBase(with_metaclass(MetaParams, object)):
def _nextday(self, day):
'''
Returns the next trading day (datetime/date instance) after ``day``
(datetime/date instance) and the isocalendar components

The return value is a tuple with 2 components: (nextday, (y, w, d))
where (y, w, d)
'''
raise NotImplementedError

def schedule(self, day):
'''
Returns a tuple with the opening and closing times (``datetime.time``)
for the given ``date`` (``datetime/date`` instance)
'''
raise NotImplementedError

实现

PandasMarketCalendar

此实现基于一个简洁的包,该包是 Quantopian 的初始功能的衍生产品。这个软件包位于:pandas_market_calendars,可以轻松安装:

1
pip install pandas_market_calendars

该实现具有以下接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class PandasMarketCalendar(TradingCalendarBase):
'''
Wrapper of ``pandas_market_calendars`` for a trading calendar. The package
``pandas_market_calendar`` must be installed

Params:

- ``calendar`` (default ``None``)

The param ``calendar`` accepts the following:

- string: the name of one of the calendars supported, for example
`NYSE`. The wrapper will attempt to get a calendar instance

- calendar instance: as returned by ``get_calendar('NYSE')``

- ``cachesize`` (default ``365``)

Number of days to cache in advance for lookup

See also:

- https://github.com/rsheftel/pandas_market_calendars

- http://pandas-market-calendars.readthedocs.io/

'''
params = (
('calendar', None), # A pandas_market_calendars instance or exch name
('cachesize', 365), # Number of days to cache in advance
)

TradingCalendar

此实现允许使用自行收集的信息构建日历,指定假期、早起时间、非交易工作日以及开盘和收盘时间:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class TradingCalendar(TradingCalendarBase):
'''
Wrapper of ``pandas_market_calendars`` for a trading calendar. The package
``pandas_market_calendar`` must be installed

Params:

- ``open`` (default ``time.min``)

Regular start of the session

- ``close`` (default ``time.max``)

Regular end of the session

- ``holidays`` (default ``[]``)

List of non-trading days (``datetime.datetime`` instances)

- ``earlydays`` (default ``[]``)

List of tuples determining the date and opening/closing times of days
which do not conform to the regular trading hours where each tuple has
(``datetime.datetime``, ``datetime.time``, ``datetime.time`` )

- ``offdays`` (default ``ISOWEEKEND``)

A list of weekdays in ISO format (Monday: 1 -> Sunday: 7) in which the
market doesn't trade. This is usually Saturday and Sunday and hence the
default

'''
params = (
('open', time.min),
('close', _time_max),
('holidays', []), # list of non trading days (date)
('earlydays', []), # list of tuples (date, opentime, closetime)
('offdays', ISOWEEKEND), # list of non trading (isoweekdays)
)

使用方式

全局交易日历

通过Cerebro可以添加全局日历,该日历是所有数据源的默认日历,除非为数据源指定了日历:

1
2
3
4
5
6
7
8
9
10
11
12
def addcalendar(self, cal):
'''Adds a global trading calendar to the system. Individual data feeds
may have separate calendars which override the global one

``cal`` can be an instance of ``TradingCalendar`` a string or an
instance of ``pandas_market_calendars``. A string will be will be
instantiated as a ``PandasMarketCalendar`` (which needs the module
``pandas_market_calendar`` installed in the system.

If a subclass of `TradingCalendarBase` is passed (not an instance) it
will be instantiated
'''

为数据源指定交易日历

通过calendar参数遵循与addcalendar方法相同的约定来指定。

例如:

1
2
3
4
...
data = bt.feeds.YahooFinanceData(dataname='YHOO', calendar='NYSE', ...)
cerebro.adddata(data)
...

这一篇就到这里啦。欢迎大家点赞、转发、私信。还没有关注我的朋友可以关注 江达小记

江达小记