
gs-quant 指数最新收盘价获取Index.get_latest_close_price 全面解析【免费下载链接】gs-quantPython toolkit for quantitative finance项目地址: https://gitcode.com/GitHub_Trending/gs/gs-quant导读本文围绕 gs-quantGoldman Sachs 开源的 Python 量化金融工具包中Index.get_latest_close_price方法展开详细讲解如何通过该方法一次性获取指数Index的官方收盘价与STS 指数专有的指示性收盘价。读完本文你将掌握该方法的完整签名、PriceType枚举的取值语义、返回 DataFrame 的列结构、STS 指数的约束边界以及从 SDK 方法到数据坐标与底层查询 API 的完整调用链。方法签名与功能定位Index.get_latest_close_price定义于 gs_quant/markets/index.py是Index类继承自Asset与PositionedEntity提供的一组收盘价查询方法之一。其 Sphinx 文档页位于 docs/functions/gs_quant.markets.index.Index.get_latest_close_price.rst通过.. automethod::指令自动从源码 docstring 生成因此本文将以源码 docstring 与实现为双重依据展开。def get_latest_close_price(self, price_type: list[PriceType] None) - pd.DataFrame: Retrieve latest close prices for an index. Only STS indices support indicative prices. :param price_type: Type of prices to return. Default returns official close price :return: dataframe with latest close price 核心能力以 pandas DataFrame 形式返回指数最新一个交易日的收盘价默认只返回官方收盘价也可通过price_type参数同时请求官方收盘价与指示性收盘价。指示性收盘价indicative close price目前仅 STS 指数支持。参数说明price_type 与 PriceType 枚举price_type接收PriceType枚举组成的列表枚举定义于 gs_quant/markets/indices_utils.py枚举成员取值字符串语义PriceType.OFFICIAL_CLOSE_PRICEofficialClosePrice官方收盘价PriceType.INDICATIVE_CLOSE_PRICEindicativeClosePrice指示性收盘价仅 STS 指数PriceType继承自EnumBase, Enum并提供了to_list()类方法用于获取全部成员列表。参数行为遵循以下规则传入None默认等价于仅请求官方收盘价直接走基础Asset路径[PriceType.OFFICIAL_CLOSE_PRICE]与默认行为完全一致同时传入两种类型返回同时包含官方与指示性收盘价的 DataFrame仅传入INDICATIVE_CLOSE_PRICE对 STS 指数返回指示性收盘价对非 STS 指数抛出MqValueError。返回值DataFrame 的列结构方法的返回结构随请求的price_type组合不同而有所差异具体由 gs_quant/markets/index.py 的分支逻辑决定情况一默认或仅官方收盘价if (not price_type) or (price_type [PriceType.OFFICIAL_CLOSE_PRICE]): return super().get_latest_close_price()此时直接委托给父类Asset.get_latest_close_price()见 gs_quant/markets/securities.py返回的是标量float最新收盘价数值而非 DataFrame——这是该分支与其余分支在返回类型上的重要差异调用方需注意。情况二官方收盘价 指示性收盘价prices pd.DataFrame() if PriceType.OFFICIAL_CLOSE_PRICE in price_type: official_level super().get_latest_close_price() prices[date] official_level.index prices[closePrice] official_level[0] if PriceType.INDICATIVE_CLOSE_PRICE in price_type: if self.__is_sts_index(): ... prices[date] indicative_level[date].iat[0] prices[indicativeClosePrice] indicative_level[indicativeClosePrice].iat[0] else: raise MqValueError(PriceType.INDICATIVE_CLOSE_PRICE currently supports STS indices only)此时返回一个 DataFrame包含date、closePrice列若同时请求了指示性价格还会追加indicativeClosePrice列。快速上手完整示例按照 docstring 中的用法获取指数最新官方与指示性收盘价from gs_quant.markets.index import Index from gs_quant.markets.indices_utils import PriceType # 通过任意常见标识符RIC、ticker、GS 资产 ID 等获取 Index 对象 index Index.get(GSMBXXXX) # 方式一默认仅官方收盘价返回 float latest_close index.get_latest_close_price() # 方式二同时获取官方与指示性收盘价返回 DataFrame latest index.get_latest_close_price( [PriceType.OFFICIAL_CLOSE_PRICE, PriceType.INDICATIVE_CLOSE_PRICE] ) print(latest)需要说明Index.get()类方法gs_quant/markets/index.py负责根据标识符解析资产并校验其类型非 Index 类型如自定义篮子会抛出MqValueError且该方法内部会判断资产类型是否属于STSIndexType以决定是否构建 STS 树结构。底层调用链从 SDK 到数据坐标与查询 API官方收盘价路径默认分支调用super().get_latest_close_price()其实现位于 gs_quant/markets/securities.pydef get_latest_close_price(self) - float: coordinate self.get_data_coordinate(DataMeasure.CLOSE_PRICE, None, DataFrequency.DAILY) if coordinate is None: raise MqValueError(...) return coordinate.last_value()调用链为Asset.get_data_coordinate(DataMeasure.CLOSE_PRICE, None, DataFrequency.DAILY)—— 按「收盘价指标 日频」定位数据坐标DataCoordinate若坐标不存在资产无该数据覆盖抛出MqValueErrorcoordinate.last_value()—— 返回该坐标下最新一期观测值。这意味着官方收盘价本质上是DataMeasure.CLOSE_PRICE这一指标的日频最新值与Asset层其他资产类型如股票、篮子的取值逻辑完全一致。指示性收盘价路径指示性价格仅对 STS 指数开放其判断逻辑为__is_sts_index()gs_quant/markets/index.py检查get_type().value是否属于STSIndexType枚举成员列表。对 STS 指数方法构造DataQuery(where{assetId: self.id})后调用response GsDataApi.last_data( queryquery, dataset_idIndicesDatasets.STS_INDICATIVE_LEVELS.value, )其中数据集标识STS_INDICATIVE_LEVELS定义于 gs_quant/markets/indices_utils.py 的IndicesDatasets枚举。GsDataApi.last_data见 gs_quant/api/gs/data.py会向/data/{dataset_id}/last/query端点发起 POST 请求并返回最新一条数据记录随后取iloc[-1:]中的date与indicativeClosePrice字段填入结果 DataFrame。作为对比历史区间查询走的是__query_indicative_levels_dataset内部的GsDataApi.query_datags_quant/markets/index.py。约束边界与注意事项STS 指数专属PriceType.INDICATIVE_CLOSE_PRICE仅适用于 STS 指数对普通指数传入会抛出MqValueError(PriceType.INDICATIVE_CLOSE_PRICE currently supports STS indices only)调用前可通过index.get_type()与STSIndexType做前置校验。返回类型不一致默认分支返回float而多价格组合分支返回DataFrame。若下游代码统一按 DataFrame 处理建议显式传入price_type或对默认结果做包装。官方收盘价的坐标依赖官方价格依赖资产在CLOSE_PRICE日频坐标上的数据覆盖覆盖缺失时直接报错而非返回空值。数据新鲜度官方价格取last_value()最新一期指示性价格取last_data查询的最新记录。两者日期应一致但分别来自不同数据源价格坐标 vsSTS_INDICATIVE_LEVELS数据集。与其他收盘价方法的对比Index还提供两个同族方法可配合选择使用方法定位默认返回get_latest_close_price最新收盘价单点官方收盘价float 或 DataFrameget_close_price_for_date(date, price_type)指定日期的收盘价官方收盘价Seriesget_close_prices(start, end, price_type)区间内收盘价序列官方收盘价 DataFrame可合并指示性价格outer join见 gs_quant/markets/index.py三者的共同点在于INDICATIVE_CLOSE_PRICE分支都只对 STS 指数开放OFFICIAL_CLOSE_PRICE分支均委托给Asset基类的对应实现。若需要批量历史数据或按日期对齐官方与指示性价格可优先使用get_close_prices的合并逻辑。总结Index.get_latest_close_price是 gs-quant 中获取指数最新收盘价的入口方法默认路径与Asset层数据坐标体系完全对齐适合大多数指数显式传入PriceType列表则可进一步获取 STS 指数专有的指示性收盘价。理解其「默认 float / 组合 DataFrame」的双重返回形态以及 STS 专属约束可以避免在真实量化工作流中踩坑并为后续的指数定价、归因与回测提供准确的价格输入。【免费下载链接】gs-quantPython toolkit for quantitative finance项目地址: https://gitcode.com/GitHub_Trending/gs/gs-quant创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考