gs-quant 日期工具:`is_business_day` 交易日判断函数完整指南

gs-quant 日期工具:`is_business_day` 交易日判断函数完整指南 gs-quant 日期工具is_business_day交易日判断函数完整指南【免费下载链接】gs-quantPython toolkit for quantitative finance项目地址: https://gitcode.com/GitHub_Trending/gs/gs-quant本篇技术指南围绕 gs-quantPython 量化金融工具包gs_quant.datetime.date模块中的is_business_day函数展开讲解其函数签名、参数语义、返回规则、底层实现GsCalendar NumPy 工作日历以及在真实回测与定价流程中的典型用法。读完本文你将掌握如何借助 gs-quant 快速判断任意日期是否为工作日并结合节假日日历、自定义周末掩码与批量日期输入构建符合自身业务规则的交易日历逻辑。1. 函数概览与签名is_business_day是 gs-quant 日期处理模块的核心函数之一定义于 gs_quant/datetime/date.py其文档由 docs/functions/gs_quant.datetime.date.is_business_day.rst 通过 Sphinxautofunction指令自动生成。完整签名如下def is_business_day( dates: DateOrDates, calendars: Union[str, tuple[str, ...]] (), week_mask: Optional[str] None, ) - Union[bool, tuple[bool, ...]]其中DateOrDates Union[dt.date, Iterable[dt.date]]即既可以传入单个日期也可以传入一组可迭代的日期集合。该函数判断每个日期是否为工作日其判断标准由两部分叠加而成默认周末定义周六、周日 可选节假日日历。2. 参数语义详解2.1dates输入日期支持单个datetime.date对象此时返回单个bool支持日期可迭代对象如列表、元组、NumPy 数组此时返回与输入顺序一致的tuple[bool, ...]每个元素对应一个日期的判断结果。import datetime as dt # 单日期 → bool is_business_day(dt.date.today()) # 多日期 → tuple is_business_day([dt.date(2024, 12, 24), dt.date(2024, 12, 25), dt.date(2024, 12, 26)])2.2calendars节假日日历默认值为空元组()即仅按周末规则判断不叠加任何节假日。传入日历名称字符串或字符串元组后会在周末判断的基础上进一步剔除节假日。日历名称的来源包括交易所代码如NYSE纽交所、LSE等对应Dataset.GS.HOLIDAY数据集货币代码ISO 4217如USD、GBP对应Dataset.GS.HOLIDAY_CURRENCY数据集PricingLocation枚举成员NYC/LDN/TKO/HKG其定义位于 gs_quant/target/common.py代表交易所所在地理区域。官方文档示例 import datetime as dt is_business_day(dt.date(2019, 7, 4), calendars(NYSE,)) False # 2019-07-04 是美国独立日NYSE 休市一个日期可以同时传入多个日历例如calendars(NYSE, USD)只要任一日历将其标记为节假日即视为非工作日。2.3week_mask自定义周末掩码week_mask用于自定义哪些天被视为周末。默认值为None此时使用GsCalendar.DEFAULT_WEEK_MASK 1111100即周一至周五为工作日1周六、周日为周末0。掩码为 7 个字符的字符串按周一到周日的顺序排列遵循 NumPybusday系列函数的 weekmask 语法。# 周一~周五工作日1111100 # 周日至周四工作日如部分中东市场0111110 is_business_day(dt.date(2024, 12, 1), week_mask0111110) # 2024-12-01 为周日3. 返回值规则输入单个日期 → 返回bool输入日期集合 → 返回tuple[bool, ...]长度与输入一致工作日定义为非周末满足week_mask且不在所选日历的节假日列表中的日期。从源码看返回逻辑非常简洁res np.is_busday(dates, busdaycalcalendar.business_day_calendar(week_mask))若结果类型为np.ndarray则转换为元组否则直接返回布尔值。这意味着底层真正执行判断的是 NumPy 的numpy.is_busdaygs-quant 在其之上封装了节假日数据获取与工作日历构建的完整能力。4. 底层实现GsCalendar 与 NumPy 工作日历4.1 调用链is_business_day(dates, calendars, week_mask) └─ GsCalendar.get(calendars) # 构建日历对象 └─ GsCalendar.business_day_calendar() # 生成 np.busdaycalendar └─ np.is_busday(dates, busdaycal...) # 逐日期判断核心实现位于 gs_quant/datetime/gscalendar.pycalendar GsCalendar.get(calendars) res np.is_busday(dates, busdaycalcalendar.business_day_calendar(week_mask)) return tuple(res) if isinstance(res, np.ndarray) else res4.2GsCalendar关键属性DATE_LOW_LIMIT dt.date(1952, 1, 1)、DATE_HIGH_LIMIT dt.date(2052, 12, 31)节假日数据查询的时间边界超出此范围的日期不会获得节假日数据数据源本身覆盖此区间。DEFAULT_WEEK_MASK 1111100默认周末掩码。holidays属性将传入的日历拆分为货币与交易所两类分别从Dataset.GS.HOLIDAY按exchange字段查询与Dataset.GS.HOLIDAY_CURRENCY按currency字段查询两个数据集拉取休市日并取并集两个数据集的枚举定义见 gs_quant/data/dataset.py。两级缓存节假日列表使用TTLCache(maxsize128, ttl600)10 分钟缓存数据集覆盖信息使用TTLCache(maxsize128, ttl3600)1 小时缓存重复调用不会反复请求数据服务。skip_valid_check默认True当传入无效日历名称时会输出Ignoring invalid calendar {item}. This will throw in future versions of gs-quant.警告而非报错传入False则直接抛出ValueError。business_day_calendar(week_mask)按week_mask键惰性构建并缓存numpy.busdaycalendar将节假日列表转换为np.datetime64数组作为holidays参数。4.3 货币与交易所的分类识别GsCalendar.is_currency()用于区分传入项属于货币还是交易所Currency枚举成员或可转换为 ISO 4217 货币代码的字符串被归为货币类否则视为交易所。PricingLocation与Currency枚举均定义于 gs_quant/target/common.pyCurrency见 L665PricingLocation见 L4356。5. 实战示例5.1 基本用法import datetime as dt from gs_quant.datetime.date import is_business_day # 今天是否为工作日默认周六、周日休市 is_business_day(dt.date.today()) # 指定日期是否为 NYSE 工作日含节假日判断 is_business_day(dt.date(2019, 7, 4), calendars(NYSE,)) # 独立日 → False # 批量判断 dates [dt.date(2024, 12, 25), dt.date(2024, 12, 26)] is_business_day(dates, calendars(NYSE,)) # (False, True) # 圣诞节休市12 月 26 日开市5.2 结合时区今天使用gs_quant.datetime.date.today(location)可返回指定定价地点PricingLocation.NYC/LDN/TKO/HKG的当前日期gs_quant/datetime/date.py可与is_business_day组合判断当地是否为交易日from gs_quant.common import PricingLocation from gs_quant.datetime.date import today, is_business_day is_business_day(today(PricingLocation.LDN), calendars(LSE,))5.3 自定义周末非标准交易周# 将周日也视为工作日如部分零售结算场景 is_business_day(dt.date(2024, 12, 1), week_mask1111110) # 周日 → True6. 在真实业务流程中的应用6.1 回测引擎中的交易日过滤is_business_day并非孤立工具它被 gs-quant 自身的回测引擎直接使用。在 gs_quant/backtests/predefined_asset_engine.py 与L182中引擎根据策略配置的calendars参数过滤非交易日if self.calendars is None or self.calendars.lower() weekend or is_business_day(date, self.calendars): # 仅在交易日执行策略逻辑这说明calendarsNone或weekend表示仅按周末过滤而传入交易所代码后则叠加节假日。相关测试 gs_quant/test/backtest/test_backtest_predefined.py 也通过 mockis_business_day来控制回测行为验证了其在流程中的关键地位。6.2 测试覆盖仓库中的单元测试验证了底层日历行为gs_quant/test/datetime_/test_gscalendar.py 通过 mock 数据验证GsCalendar支持单一日历如PricingLocation.NYC与多元组日历如(PricingLocation.NYC, PricingLocation.LDN)并确认holidays属性能正确聚合两个市场的休市日由于is_business_day委托给np.is_busday其布尔语义天然与 NumPy 保持一致批量输入返回元组的行为可直接通过上述示例验证。7. 与其他日期函数的配套使用is_business_day属于 gs-quant 日期工具族全部位于 gs_quant/datetime/date.py它们共享calendars与week_mask两个参数语义完全一致可自由组合函数作用is_business_day(dates, calendars, week_mask)判断日期是否为工作日本文主题business_day_offset(dates, offsets, roll, calendars, week_mask)将日期沿工作日方向偏移 N 天roll支持raise/forward/preceding等方向business_day_count(begin_dates, end_dates, calendars, week_mask)统计两个日期之间的工作日天数prev_business_date(dates, calendars, week_mask)返回给定日期前一个工作日默认当天date_range(begin, end, calendars, week_mask)生成一段连续的工作日日期序列例如判断从今天起第 5 个工作日是否为工作日from gs_quant.datetime.date import business_day_offset, is_business_day target business_day_offset(dt.date.today(), 5, rollforward, calendars(NYSE,)) is_business_day(target, calendars(NYSE,)) # 恒为 True偏移结果必然落在工作日business_day_offset与business_day_count同样基于GsCalendar与 NumPybusday系列函数见 gs_quant/datetime/date.py因此三者对节假日与周末的处理规则完全一致适合组合进同一套日历逻辑中。8. 使用建议与限制网络依赖传入calendars后节假日数据来自 gs-quant 数据服务Dataset.GS.HOLIDAY/HOLIDAY_CURRENCY需要有效的 gs-quant 会话配置仅使用默认周末规则不传calendars则完全离线。日期范围节假日数据仅覆盖 1952-01-01 至 2052-12-31超出该区间的日期无法获得节假日信息仍可按周末规则判断。无效日历默认skip_valid_checkTrue时无效日历名只产生警告如需严格校验可显式构造GsCalendar(calendars, skip_valid_checkFalse)。返回类型批量输入始终返回元组而非 NumPy 数组便于直接与 Python 原生逻辑互操作。缓存节假日数据有 10 分钟 TTL 缓存长时间运行的任务中如需强制刷新可调用GsCalendar.reset()清空缓存见 gs_quant/datetime/gscalendar.py。掌握is_business_day及其背后的GsCalendar机制你就能在 gs-quant 中统一处理周末 节假日的复合交易日语义并将其无缝嵌入回测、定价日期推算与交易日历定制等场景。【免费下载链接】gs-quantPython toolkit for quantitative finance项目地址: https://gitcode.com/GitHub_Trending/gs/gs-quant创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考