ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

gs-quant FXOption 外汇期权工具:字段详解、交易构建与定价实践

gs-quant FXOption 外汇期权工具:字段详解、交易构建与定价实践 gs-quant FXOption 外汇期权工具字段详解、交易构建与定价实践【免费下载链接】gs-quantPython toolkit for quantitative finance项目地址: https://gitcode.com/GitHub_Trending/gs/gs-quant导读FXOption 是 gs-quant 中用于构建外汇期权vanilla FX option的核心工具类它把一笔期权的成交方向、货币对、名义金额、行权价、到期日、结算方式与期权费等全部经济参数封装为可解析、可定价、可入组合的 dataclass 对象。本文以 docs/classes/gs_quant.instrument.FXOption.rst 为骨架结合 gs_quant/target/instrument.py 的类定义、gs_quant/target/common.py 的枚举定义、官方 skills 文档与测试用例完整讲解 FXOption 的每个字段、枚举取值、期权费陷阱、定价/风险计量与回测用法。读完本文你将能够用 gs-quant 从零构建一笔可交易的外汇期权并正确计算其价格、希腊字母与组合风险。一、FXOption 类概览一个字段即一笔交易的 dataclass在 gs-quant 中FXOption继承自Instrument而Instrument的公共方法继承自Priceable详见 docs/classes/gs_quant.base.Priceable.rst。其核心类定义位于 gs_quant/target/instrument.py#L1170-L1209handle_camel_case_args dataclass_json(letter_caseLetterCase.CAMEL) dataclass(unsafe_hashTrue, reprFalse) class FXOption(Instrument): pair: Optional[str] # 货币对如 EURUSD buy_sell: Optional[BuySell] # Buy / Sell option_type: Optional[OptionType] # Call / Put ... notional_amount: Optional[Union[float, str]] # 名义金额 notional_currency: Optional[Currency] # 名义货币 notional_amount_in_other_currency: Optional[Union[float, str]] # 对手货币名义 strike_price: Optional[Union[float, str]] # 行权价数值或 ATMF 等 settlement_date: Optional[Union[datetime.date, str]] # 结算日 settlement_currency: Optional[Currency] # 结算货币 settlement_rate_option: Optional[str] # 结算汇率参考 method_of_settlement: Optional[OptionSettlementMethod] # Cash / Physical expiration_date: Optional[Union[datetime.date, str]] # 到期日 expiration_time: Optional[str] # 到期时刻 premium: Optional[Union[float, str]] # 期权费 premium_currency: Optional[Currency] # 期权费货币 premium_payment_date: Optional[str] # 期权费支付日 exercise_style: Optional[OptionExerciseStyle] # Auto / Manual asset_class: Optional[AssetClass] AssetClass.FX # 固定为 FX type_: Optional[AssetType] AssetType.Option # 固定为 Option name: Optional[str] # 自定义名称从源码结构看FXOption有如下关键特征资产类别固定asset_class固定为AssetClass.FXtype_固定为AssetType.Option由构造器直接注入initFalse用户无需也不能指定自动 camelCase 序列化通过dataclass_json(letter_caseLetterCase.CAMEL)与handle_camel_case_args装饰器允许以 Python 风格的 snake_case 参数构造同时与服务端 JSON 的 camelCase 字段自动互转如notional_amount↔notionalAmount字段皆可延迟解析所有字段默认值为None未提供的参数会在resolve()或定价时由服务端按市场数据补齐如ATMF行权价、远期汇率等unsafe_hashTrue实例可哈希便于放入集合或在回测框架中作为可交易资产引用。二、快速上手构建一笔 EURUSD 外汇期权官方 skills 文档 gs_quant/skills/gs-quant-overview/instruments.md#L188-L200 给出了最小可运行示例from gs_quant.instrument import FXOption option FXOption( pairEURUSD, # 货币对必填 expiration_date3m, # 到期日支持相对期限 3m 或显式日期 option_typeCall, # 看涨期权 strike_priceATMF, # 平价行权价At-The-Money Forward notional_amount10e6, # 名义金额 1000 万 )仅 5 个参数即可定义一笔完整交易货币对、到期日、方向、行权价与规模。其余经济参数结算日、结算货币、期权费等留空由后续解析/定价流程补全。在此基础上可以更完整地显式指定全部核心参数from gs_quant.instrument import FXOption from gs_quant.common import BuySell, OptionType, Currency, OptionSettlementMethod, OptionExerciseStyle option FXOption( pairEURUSD, buy_sellBuySell.Buy, # 买入期权 option_typeOptionType.Call, # Call / Put notional_amount10e6, # 名义金额 notional_currencyCurrency.EUR, # 名义货币可留空由服务端解析 strike_price1.10, # 显式行权价或 ATMF / 25D 等相对行权 expiration_date3m, # 到期日 expiration_time10am NY, # 到期时刻纽约时区惯例 settlement_date3m2d, # 结算日通常 T2 settlement_currencyCurrency.USD, # 结算货币 method_of_settlementOptionSettlementMethod.Cash, # 现金结算 exercise_styleOptionExerciseStyle.European, # 欧式行权 premium0, # 显式期权费见第五节 premium_currencyCurrency.USD, nameEURUSD 3m Call, # 交易名称便于组合内识别 ) option.resolve() # 解析未指定参数注意expiration_date、settlement_date等日期字段既支持3m、0b这类相对期限字符串也支持datetime.date对象源码字段类型为Optional[Union[datetime.date, str]]。三、核心字段全解3.1 成交方向与期权类型字段类型取值见 gs_quant/target/common.py说明buy_sellBuySellBuy/Sellcommon.py#L317-L322买卖方向option_typeOptionTypeCall/Put/Forward/Binary_Call/Binary_Put/Digital_Call/Digital_Putcommon.py#L4267-L4277普通期权用Call/Put二进制/数字类期权另有专用枚举例如FXMultiCrossBinaryLeg必须使用Binary_Call/Binary_Put3.2 货币对与名义金额pair货币对字符串如EURUSD、USDJPY是构建 FX 期权的最基本参数notional_amount名义金额主货币可为数值或字符串notional_currency名义货币Currency枚举notional_amount_in_other_currency对手货币名义金额。该字段在FXOption、FXOptionLeg中均存在用于指定交叉货币名义若未提供解析时可由汇率折算。3.3 行权价strike_price可接受数值如1.10或相对行权价字符串。仓库内大量示例与测试使用ATMFAt-The-Money Forward平价远期见 gs_quant/test/api/test_risk.py#L46、gs_quant/test/backtest/test_generic_engine.py#L97strike_price也可接受ATMS、25D25-delta等市场惯例表达。3.4 到期、结算与行权字段类型/枚举说明expiration_datedate / str到期日支持3m、1y相对期限expiration_timestr到期时刻如10am NYsettlement_datedate / str结算日通常为到期后两个营业日3m2dsettlement_currencyCurrency结算货币settlement_rate_optionstr结算汇率参考选项method_of_settlementOptionSettlementMethodCash/Physical/ElectDfltCash/ElectDfltPhys/NetSharescommon.py#L4240-L4248exercise_styleOptionExerciseStyleAuto/Manualcommon.py#L4222-L4227指示到期自动行权还是手动行权3.5 期权费字段说明premium期权费金额可为数值或字符串premium_currency期权费货币premium_payment_date期权费支付日3.6 元数据与框架字段dataclass_json_config、metadata、resolution_key、instrument_quantity、quantity_、provider、unresolved、type_等字段属于 dataclass/解析框架的内部属性resolution_key记录解析键unresolved标记未解析字段集合provider指定数据提供方name用于交易标识instrument_quantity/quantity_记录数量信息。这些字段与Priceable基类的resolve()、to_frame()、calc()等方法协同工作参见 docs/classes/gs_quant.base.Priceable.rst。四、scale_in_place原地缩放规模FXOption覆写了scale_in_place(scaling, check_resolvedTrue)方法gs_quant/target/instrument.py#L1192-L1209用于原地调整交易规模。其实现逻辑是def scale_in_place(self, scalingNone, check_resolvedTrue): if scaling is None or scaling 1: return if self.unresolved is None: if check_resolved: raise RuntimeError(Can only scale resolved instruments) if self.notional_amount is None or self.buy_sell is None: raise RuntimeError(Can only scale unresolved instruments with the buysell and primary size fields set) if any(a is not None and not isinstance(a, (int, float)) for a in [self.notional_amount, self.notional_amount_in_other_currency]): raise RuntimeError(All specified size fields must be numeric) self.notional_amount * abs(scaling) if check_resolved or self.notional_amount_in_other_currency is not None: self.notional_amount_in_other_currency * abs(scaling) if scaling 0: flip_dict {BuySell.Buy: BuySell.Sell, BuySell.Sell: BuySell.Buy} self.buy_sell flip_dict[self.buy_sell]要点两个名义字段同时缩放notional_amount与notional_amount_in_other_currency都会被乘以abs(scaling)前提是对手货币名义非空或已解析负缩放反向scaling 0时不仅按绝对值放大规模还会翻转buy_sell方向实现平仓/反向语义已解析检查默认check_resolvedTrue时只允许对已解析resolved的合约缩放对未解析合约需显式关闭检查且buy_sell、notional_amount必须已设置。该能力在回测中被大量使用例如EarlyExitPositionLimitScaledAction通过它实现到期前按比例缩放头寸见 gs_quant/test/backtest/test_generic_engine.py#L1100-L1132。五、期权费陷阱premium0 与 FairPremium这是 FX 期权使用中最重要的实战细节官方文档在 gs_quant/skills/gs-quant-overview/instruments.md#L214-L253 中专门强调构造 FX 期权FXOption、FXBinary、FXMultiCrossBinary 等时若不指定premium解析时服务端会自动设置期权费使DollarPrice归零——这代表一笔公允价值交易期权费恰好抵消期权价值。后果是如果你想知道期权的成本/价值DollarPrice恒为 0。解决办法有二from gs_quant import risk from gs_quant.instrument import FXOption # 错误写法解析后 DollarPrice ≈ 0 option FXOption(pairEURUSD, expiration_date3m, option_typeCall, strike_priceATMF, notional_amount10e6) option.calc(risk.DollarPrice) # 正确写法 1显式 premium0DollarPrice 返回期权真实现值 option FXOption(pairEURUSD, expiration_date3m, option_typeCall, strike_priceATMF, notional_amount10e6, premium0) # -- 关键 option.calc(risk.DollarPrice) # 正确写法 2使用 FairPremium / FairPremiumInPercent # 该计量会忽略工具上已设定的期权费同时尊重期权费结算日 option.calc(risk.FairPremium)经验法则要得到期权真实价格就用premium0DollarPrice要评估公允期权费就用FairPremium/FairPremiumInPercent。六、定价与风险计量6.1 PricingContext 与 LiveMarket 实时定价默认的PricingContext使用收盘市场数据定价而 FX 是 gs-quant 中唯一全面支持实时行情定价的资产类别官方文档明确指出 LiveMarket 定价目前仅对 FX 工具可用FXOption、FXForward、FXBinary、FXMultiCrossBinary 等见 gs_quant/skills/gs-quant-overview/pricing.md#L130-L184from gs_quant.instrument import FXOption from gs_quant.markets import PricingContext, LiveMarket from gs_quant.risk import DollarPrice option FXOption( pairEURUSD, expiration_date3m, option_typeCall, strike_priceATMF, notional_amount10e6, premium0, ) with PricingContext(marketLiveMarket()): price_f option.dollar_price() # 异步求值 price price_f.result() # 基于实时行情快照的定价结果PricingContext采用异步/延迟求值模型上下文内的计算返回 future 对象离开上下文后调用.result()取回结果。组合层面同样适用from gs_quant.instrument import FXOption, FXForward from gs_quant.markets import PricingContext, LiveMarket from gs_quant.markets.portfolio import Portfolio from gs_quant.risk import DollarPrice portfolio Portfolio([ FXOption(pairEURUSD, expiration_date3m, option_typeCall, strike_priceATMF, notional_amount10e6, premium0, nameEUR Call), FXForward(pairUSDJPY, settlement_date6m, notional_amount10e6, nameJPY Fwd), ]) with PricingContext(marketLiveMarket()): result portfolio.calc(DollarPrice) prices result[DollarPrice]6.2 常用风险计量除DollarPrice、FairPremium外仓库 documentation 中提供了 FX 期权计量专题示例02_fx_option_trade_construction.ipynbFX 期权交易构建全流程05_calc_option_measures.ipynb计算期权各类计量09_fx_delta_measures.ipynbFX Delta 系列计量含 ATM 波动率行权价联动10_fx_gamma_measures.ipynbFX Gamma 系列计量。这些示例展示了option.calc(risk.DeltaSpot)、option.calc(risk.GammaSpot)、option.calc(risk.Vega)等希腊字母计量的标准用法它们与PricingContext配合可一次性求出一揽子风险敞口。七、策略与组合应用FXOption 在多工具场景中的角色7.1 与同族 FX 工具的协同FXOption是 FX 期权族的基础成员仓库中与之并列的还有FXOptionLeginstrument.py#L1215单腿期权用于FXOptionStrategy的组合腿FXOptionStrategyinstrument.py#L2133多腿期权策略可组合多条FXOptionLeg构建价差、跨式等结构FXForward、FXBinary、FXEuropeanKnockout、FXOneTouch、FXMultiCrossBinary等进阶结构。测试 gs_quant/test/api/backtests_xasset/json_encoders/test_request_encoders.py#L39-L40 展示了FXOption仅凭pair与name即可实例化并被请求编码器正确处理fx_leg_1 FXOption(pairEURUSD, nameleg_0) fx_leg_2 FXOption(pairGBPUSD)7.2 在回测框架中作为交易资产FXOption被广泛用于 GenericEngine 回测。官方 skills 文档 gs_quant/skills/gs-quant-overview/backtesting.md#L57 将FXOption列为 GenericEngine 支持的多资产 OTC 策略工具之一gs_quant/test/backtest/test_generic_engine.py#L87-L114 给出了完整范式call FXOption( buy_sellBuy, option_typeCall, pairUSDJPY, strike_priceATMF, notional_amount1e5, expiration_date2y, name2y_call, ) trig_req DateTriggerRequirements(dates[start_date]) actions AddTradeAction(call, 1m, nameAction1) triggers DateTrigger(trig_req, actions) strategy Strategy(None, triggers)即用FXOption定义交易模板 → 用AddTradeAction包装为动作 → 挂载到DateTrigger/PeriodicTrigger→ 交给Strategy驱动的回测引擎执行。同一测试文件中还通过FXOption(EURUSD, expiration_date3m, option_typeOptionType.Call)验证了纯位置参数构造的兼容性test_generic_engine.py#L1103-L1104。八、方法与继承Priceable 提供的公共能力根据原文档说明FXOption的方法非属性继承自gs_quant.base.Priceable包括resolve()解析未指定的经济参数如 ATMF 行权价、结算日calc(measure)在PricingContext内计算指定风险计量dollar_price()快捷计算美元价格to_frame()/from_frame()与 DataFrame 互转便于批量分析scale()/scale_in_place()缩放交易规模clone()等工具方法。属性与方法的完整划分可在 docs/classes/gs_quant.base.Priceable.rst 与FXOption类文档中进一步查阅。九、使用前提与限制说明gs-quant 的FXOption需要接入 Marquee 服务端环境完成解析与定价resolve、calc依赖服务端市场数据本地仅负责交易结构与请求的构建测试用例均使用MockCalc模拟计算后端如 test_generic_engine.py。实时行情定价LiveMarket仅对 FX 工具可用且使用调用时刻的市场快照与收盘定价结果可能不同。strike_price、notional_amount等字段接受字符串形式的相对表达如ATMF、25D、3m具体解析行为由服务端决定如需完全确定的行权价与名义请显式传入数值。十、参考资源索引类文档docs/classes/gs_quant.instrument.FXOption.rst基类方法docs/classes/gs_quant.base.Priceable.rst源码实现gs_quant/target/instrument.py#L1170-L1209FXOption、#L1215FXOptionLeg、#L2133FXOptionStrategy枚举定义gs_quant/target/common.pyBuySell#L317、OptionExerciseStyle#L4222、OptionSettlementMethod#L4240、OptionType#L4267官方指南instruments.md、pricing.md、backtesting.md实战 Notebook02_fx_option_trade_construction.ipynb、09_fx_delta_measures.ipynb测试用例test_generic_engine.py、test_risk.py、test_request_encoders.py【免费下载链接】gs-quantPython toolkit for quantitative finance项目地址: https://gitcode.com/GitHub_Trending/gs/gs-quant创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表