尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

gs_quant InflationSwap 通胀互换工具:字段体系、定价流程与实战回测指南

gs_quant InflationSwap 通胀互换工具:字段体系、定价流程与实战回测指南 gs_quant InflationSwap 通胀互换工具字段体系、定价流程与实战回测指南【免费下载链接】gs-quantPython toolkit for quantitative finance项目地址: https://gitcode.com/GitHub_Trending/gs/gs-quant导读本文基于 gs-quant 开源仓库中的 InflationSwap 类文档 展开深入讲解通胀互换Inflation Swap这一利率类工具在 gs_quant 中的完整建模方式。文章覆盖该类全部属性字段、与PayReceive/BusinessDayConvention/SwapClearingHouse等枚举的取值关系、scale_in_place缩放逻辑、基于PricingContext的定价与风险度量流程并给出两个可直接复用的实战场景通胀保值组合构建与基于GenericEngine的通胀对冲回测策略。读完本文你将掌握如何在 gs_quant 中构造、命名、定价并对冲通胀互换。InflationSwap 在 gs_quant 中的定位在 gs_quant 中InflationSwap属于gs_quant.target.instrument模块中众多Instrument子类之一源码定义位于 gs_quant/target/instrument.py。它是一个零息票zero coupon香草型通胀互换将固定现金流与挂钩通胀指数如 CPI、RPI 等的浮动现金流互换。从类定义可以确认几个固定属性initFalse构造时不可修改由框架自动填充asset_class固定为AssetClass.Rates利率资产类别type_固定为AssetType.InflationSwap序列化为 JSON 时使用字段名typeconfig(field_nametype, excludeexclude_none)。类上叠加的装饰器dataclass_json(letter_caseLetterCase.CAMEL)与handle_camel_case_args表明该模型既是 dataclass又支持 CamelCase JSON 序列化/反序列化并允许以驼峰命名的关键字参数构造实例。注意文档页 docs/classes/gs_quant.instrument.InflationSwap.rst 中列出的方法由基类gs_quant.base.Priceable提供见文档内:doc:gs_quant.base.Priceable引用InflationSwap自身额外实现了scale_in_place缩放方法。字段体系从 Pay/Receive 到 Clearing House 的完整参数解析InflationSwap的全部属性字段及其类型、默认值如下以源码 instrument.py 为准字段类型默认值说明pay_or_receiveOptional[PayReceive]None支付/收取固定端Pay 或 Receivetermination_dateOptional[Union[datetime.date, str]]None终止日期支持日期或期限字符串如30ynotional_currencyOptional[Currency]None名义本金币种effective_dateOptional[Union[datetime.date, str]]None生效日期支持日期或远期起始字符串notional_amountOptional[Union[float, str]]None名义本金金额indexOptional[str]None挂钩通胀指数标识floating_rate_business_day_conventionOptional[BusinessDayConvention]None浮动端工作日惯例fixed_rateOptional[Union[float, str]]None固定端利率fixed_rate_business_day_conventionOptional[BusinessDayConvention]None固定端工作日惯例feeOptional[float]0.0费用upfront feebase_cpiOptional[float]None基准 CPI 值用于计算通胀浮动端clearing_houseOptional[SwapClearingHouse]None清算所asset_classOptional[AssetClass]initFalseAssetClass.Rates资产类别框架填充type_Optional[AssetType]initFalseAssetType.InflationSwap工具类型序列化为typenameOptional[str]None工具名称其中部分字段继承了基类Instrument的公共属性如name、metadata、provider、resolution_key、unresolved、instrument_quantity、quantity_、dataclass_json_config等这些在文档页中也以autoattribute形式列出。关键枚举取值PayReceive指定固定端方向定义于 gs_quant/target/common.py支持Pay、Payer、Receive、Receiver、Straddle、Rec等取值。对通胀互换而言Pay表示支付固定利率并收取通胀浮动端Receive表示收取固定利率并支付通胀浮动端。使用时应优先使用Pay/Receive其他为等价别名。BusinessDayConvention工作日调整惯例定义于 gs_quant/target/common.py取值为Following、Modified_Following、Previous、Unadjusted。它同时作用于固定端fixed_rate_business_day_convention和浮动端floating_rate_business_day_convention决定现金流支付日落在节假日时如何顺延或前移。SwapClearingHouse清算所定义于 gs_quant/target/common.py取值为LCH、EUREX、JSCC、CME、NONE。该字段控制互换的清算安排NONE表示非清算双边交易。Currency名义币种notional_currency使用gs_quant.common.Currency枚举如EUR、USD、GBP。实战中常配合期限字符串如30y、50y一起构造工具。构造通胀互换最小示例与参数解释与仓库中其他Instrument子类一致InflationSwap支持位置参数与关键字参数两种构造方式。参考仓库自带的 040309_inflation_hedging_strategy.ipynb 中的实际用法from gs_quant.instrument import InflationSwap # 支付固定端、30y 到期、EUR、名义 1 亿 infl_hedge InflationSwap( pay_or_receivePay, termination_date30y, notional_currencyEUR, notional_amount100e6, name30yhedge ) # 收取固定端、50y 到期、EUR、名义 1 亿 infl InflationSwap( pay_or_receiveReceive, termination_date50y, notional_currencyEUR, notional_amount100e6, name50y )从上述真实示例可以看到pay_or_receive直接传入字符串Pay/Receive经handle_camel_case_args与 dataclass 类型转换后自动归一化为PayReceive枚举termination_date使用期限字符串30y/50ygs_quant 会自动解析为对应终止日期name用于标识工具便于在组合与回测中区分如30yhedge与50y。如需显式指定固定利率、基准 CPI 或清算所可以追加参数swap InflationSwap( pay_or_receiveReceive, termination_date10y, notional_currencyUSD, notional_amount50e6, effective_date2y, # 远期起始2 年后生效 indexUSCPI, # 挂钩美国 CPI 指数 fixed_rate2.35, # 固定端利率% base_cpi258.0, # 基准 CPI fixed_rate_business_day_conventionModified Following, floating_rate_business_day_conventionModified Following, fee0.0, # 默认即 0.0 clearing_houseNONE, # 双边非清算 namemy inflation swap )各参数含义effective_date决定起息日可写日期或远期字符串index决定浮动端挂钩的通胀指数base_cpi作为浮动端计算的基准通胀水平fixed_rate为固定端利率两个 business day convention 参数控制两端的现金流支付日调整规则。工具缩放scale_in_place 的源码级行为InflationSwap是少数自带缩放方法实现的Instrument其scale_in_place定义于 instrument.py。行为要点如下缩放因子为None或1时直接返回不做任何修改对已解析resolved工具执行缩放前若check_resolvedTrue默认则会先校验未解析时抛出RuntimeError(Can only scale resolved instruments)对未解析工具缩放要求notional_amount与pay_or_receive均已设置且规模类字段notional_amount、fee必须是数值类型否则抛错缩放逻辑notional_amount * abs(scaling)即名义金额始终按缩放因子的绝对值放大当scaling 0时方向反转Pay与Receive互换见源码中的flip_dict且fee符号取反在check_resolvedTrue或fee非空时执行。该方法的实际价值在于回测或策略引擎中可基于同一工具模板快速生成不同规模、不同方向的版本无需重新解析re-resolve工具。定价与风险度量与 PricingContext 的协作InflationSwap继承自gs_quant.base.Priceable其定价、风险度量通过PricingContext异步执行。基类中的关键入口见 gs_quant/priceable.pyprice(currencyNone)计算本地货币下的现值Present Value支持传入目标币种其余度量如dollar_price、calc各类 risk measure均在PricingContext上下文内以 future 形式返回调用result()获取实际数值。典型用法from gs_quant.instrument import InflationSwap from gs_quant.markets import PricingContext swap InflationSwap(Receive, 10y, EUR) with PricingContext(): price_f swap.price() # 返回 future price price_f.result() # 实际现值EUR仓库中的通胀研究 notebook inflation_covid19_recovery_trade.ipynb 展示了完整的组合化用法使用HistoricalPricingContext进行历史回看定价将多只InflationSwap装入Portfolio聚合风险再配合gs_quant.risk.IRFwdRate等风险度量与gs_quant.timeseries中的correlation、std、diff做宏观驱动因子分析。其中工具构造被封装的函数即为最小模型from gs_quant.instrument import InflationSwap def create_inflation_swap(trade): trade_name, ccy, payrec, fwd, tenor trade return InflationSwap( pay_or_receivepayrec, termination_datetenor, notional_currencyccy, effective_datefwd, nametrade_name )这印证了InflationSwap既可以独立定价也可以作为Portfolio成员参与组合层面的历史风险分析。通胀相关的时间序列度量与InflationSwap配套gs_quant 在 gs_quant/timeseries/measures_inflation.py 中提供了inflation_swap_rate通胀互换即期率与inflation_swap_term等时间序列度量其内部通过QueryType.SWAP_RATE查询 TDAPI 通胀互换率资产见同文件 L167-L170 的_currency_to_tdapi_inflation_swap_rate_asset。相关实现由 test_measures_inflation.py 中的test_inflation_swap_rate等用例验证可用于构建通胀曲线、验证工具的浮动端定价输入。实战一构建通胀保值组合参考仓库事件研究 notebook inflation_covid19_recovery_trade.ipynb 的思路可以按“方向 币种 期限 远期起始”构造一组通胀互换来描绘通胀曲线from gs_quant.instrument import InflationSwap from gs_quant.markets.portfolio import Portfolio trades [ # (名称, 币种, 方向, 远期起始, 期限) (US 1y, USD, Receive, None, 1y), (US 5y, USD, Receive, None, 5y), (US 10y, USD, Receive, None, 10y), (US 30y, USD, Receive, None, 30y), ] swaps [create_inflation_swap(t) for t in trades] portfolio Portfolio(swaps) with PricingContext(): prices [s.price() for s in swaps] pv portfolio.price() print({s.name: p.result() for s, p in zip(swaps, prices)}) print(组合现值:, pv.result())注意事项若需要历史时点的定价请将PricingContext换成HistoricalPricingContext详见 gs_quant/markets/historical.py未指定index与fixed_rate时框架会基于币种与市场数据自动解析默认通胀指数与均衡固定利率name建议唯一便于在组合聚合、回测结果与报告中定位单只工具。实战二基于 GenericEngine 的通胀对冲回测仓库自带的可复现示例 040309_inflation_hedging_strategy.ipynb 展示了一条完整链路构造两只InflationSwap50y 多头 30y 对冲以PeriodicTrigger周期性触发AddTradeAction与HedgeAction最后交给GenericEngine每日运行回测from gs_quant.backtests import (AddTradeAction, HedgeAction, PeriodicTrigger, PeriodicTriggerRequirements, Strategy, GenericEngine) from gs_quant.instrument import InflationSwap # 50y 通胀互换 30y 对冲 infl_hedge InflationSwap( pay_or_receivePay, termination_date30y, notional_currencyEUR, notional_amount100e6, name30yhedge ) infl InflationSwap( pay_or_receiveReceive, termination_date50y, notional_currencyEUR, notional_amount100e6, name50y ) freq 1b # 每日触发 trig_req PeriodicTriggerRequirements(start_datestart_date, end_dateend_date, frequencyfreq) actions [ AddTradeAction(infl, 1m), HedgeAction(InflationDelta(aggregation_levelType), infl_hedge, freq), ] triggers PeriodicTrigger(trig_req, actions) strategy Strategy(None, triggers) GE GenericEngine() backtest GE.run_backtest(strategy, startstart_date, endend_date, frequency1b, show_progressTrue)这段示例的价值在于InflationSwap不仅支持静态定价还能无缝嵌入 gs_quant 的事件驱动回测引擎其InflationDelta风险被用于驱动对冲逻辑aggregation_levelType表示按工具类型聚合风险从而实现“组合构建 → 风险度量 → 动态对冲”的完整量化工作流。参考与延伸阅读类文档docs/classes/gs_quant.instrument.InflationSwap.rst属性列表另见基类 gs_quant.base.Priceable源码实现gs_quant/target/instrument.py相关枚举gs_quant/target/common.pyPayReceive、L307-L314BusinessDayConvention、L4784-L4792SwapClearingHouse定价基类gs_quant/priceable.py组合分析示例gs_quant/content/events/00_gsquant_meets_markets/01_ideas_for_risk_re_rating/inflation_covid19_recovery_trade.ipynb回测对冲示例gs_quant/documentation/04_backtesting/examples/03_GenericEngine/040309_inflation_hedging_strategy.ipynb通胀度量与测试gs_quant/timeseries/measures_inflation.pyinflation_swap_rate/inflation_swap_term与 gs_quant/test/timeseries/test_measures_inflation.py【免费下载链接】gs-quantPython toolkit for quantitative finance项目地址: https://gitcode.com/GitHub_Trending/gs/gs-quant创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表