menu
Data

Data Contexts

DataContexts are objects which provide a set of parameters which describe how to query data. These can be used to provide a common context which can be reused for a number of different data access or manipulation functions. The GS Quant market data apis use DataContext to determine how to query data (e.g. what date range to use) if no specific parameters are provided.

Creating a DataContext

Creating a DataContext is straightforward given a start and end date:

from gs_quant.data import DataContext
from datetime import date

data_ctx = DataContext(start=date(2018, 1, 1), end=date(2018, 12, 31))       # Create data context

This example creates a context covering 2018.

Usage

DataContexts can be used within a scoped block to provide a common reference for subsequent operations. Similar to Sessions, there are two ways to use a DataContext:

  • Set as global
  • Use within a block

Global Context

Any request which is not scoped will use the global data context automatically. The global context can be accessed through current:

current = DataContext.current     # Get current data context
DataContext.current = data_ctx    # Set current data context

Scoped Context

DataContext objects can be used within blocks. This allows the same data query parameters shared by multiple requests within a defined scope.

data_ctx_2017 = DataContext(start=date(2017, 1, 1), end=date(2017, 12, 31))       # Create 2017 data context
data_ctx_2018 = DataContext(start=date(2018, 1, 1), end=date(2018, 12, 31))       # Create 2018 data context

with data_ctx_2017:

    # query data for 2017

with data_ctx_2018:

    # query data for 2018

Example

The following example shows how to access historical implied volatility level for SPX using a DataContext:

info

Note

Requires an initialized GsSession and data subscription. Please refer to Sessions for details.

from datetime import date
from gs_quant.data import DataContext
from gs_quant.markets.securities import SecurityMaster, AssetIdentifier, ExchangeCode
import gs_quant.timeseries as ts

data_ctx = DataContext(start=date(2018, 1, 1), end=date(2018, 12, 31))      # Create a data context covering 2018
spx = SecurityMaster.get_asset('SPX', AssetIdentifier.TICKER, exchange_code=ExchangeCode.NYSE)               # Lookup S&P 500 Index via Security Master

with data_ctx:                                                              # Use the data context we setup
    vol = ts.implied_volatility(spx, '1m', ts.VolReference.DELTA_CALL, 25)  # Get 25 delta call implied volatility

vol.tail()

Output:

Out[1]:
2021-12-20 26.108257
2021-12-21 21.794968
2021-12-22 22.398788
2021-12-23 20.985507
2021-12-24 19.574263
dtype: float64

Related Content


GS DAP® is owned and operated by Goldman Sachs. This site is for informational purposes only and does not constitute an offer to provide, or the solicitation of an offer to provide access to or use of GS DAP®. Any subsequent commitment by Goldman Sachs to provide access to and / or use of GS DAP® would be subject to various conditions, including, amongst others, (i) satisfactory determination and legal review of the structure of any potential product or activity, (ii) receipt of all internal and external approvals (including potentially regulatory approvals); (iii) execution of any relevant documentation in a form satisfactory to Goldman Sachs; and (iv) completion of any relevant system / technology / platform build or adaptation required or desired to support the structure of any potential product or activity. All GS DAP® features may not be available in certain jurisdictions. Not all features of GS DAP® will apply to all use cases. Use of terms (e.g., "account") on GS DAP® are for convenience only and does not imply any regulatory or legal status by such term.
Certain solutions and Institutional Services described herein are provided via our Marquee platform. The Marquee platform is for institutional and professional clients only. This site is for informational purposes only and does not constitute an offer to provide the Marquee platform services described, nor an offer to sell, or the solicitation of an offer to buy, any security. Some of the services and products described herein may not be available in certain jurisdictions or to certain types of clients. Please contact your Goldman Sachs sales representative with any questions. Any data or market information presented on the site is solely for illustrative purposes. There is no representation that any transaction can or could have been effected on such terms or at such prices. Please see https://www.goldmansachs.com/disclaimer/sec-div-disclaimers-for-electronic-comms.html for additional information.
Transaction Banking services are offered by Goldman Sachs Bank USA (“GS Bank”). GS Bank is a New York State chartered bank, a member of the Federal Reserve System and a Member FDIC.
Mosaic is a service mark of Goldman Sachs & Co. LLC. This service is made available in the United States by Goldman Sachs & Co. LLC and outside of the United States by Goldman Sachs International, or its local affiliates in accordance with applicable law and regulations. Goldman Sachs International and Goldman Sachs & Co. LLC are the distributors of the Goldman Sachs Funds. Depending upon the jurisdiction in which you are located, transactions in non-Goldman Sachs money market funds are affected by either Goldman Sachs & Co. LLC, a member of FINRA, SIPC and NYSE, or Goldman Sachs International. For additional information contact your Goldman Sachs representative. Goldman Sachs & Co. LLC, Goldman Sachs International, Goldman Sachs Liquidity Solutions, Goldman Sachs Asset Management, L.P., and the Goldman Sachs funds available through Goldman Sachs Liquidity Solutions and other affiliated entities, are under the common control of the Goldman Sachs Group, Inc.
© 2025 Goldman Sachs. All rights reserved.