menu
Pricing and Risk

Pricing Context

What is Pricing Context

PricingContext is a class used for controlling pricing, market data and computation behavior when pricing instruments and calculating risk. It can be used to provide a common context which can be reused for a number of different data access or manipulation functions.

More specifically, PricingContext can be used to define pricing date, date for sourcing market data, market data location as well as whether the calculation should be processed asynchronously or batched.

HistoricalPricingContext is also available to produce valuations over multiple dates. Both PricingContext and HistoricalPricingContext are distinct from the MarketDataContext which provides a market state as of a given date.

Evaluating Pricing and Risk Using A Context

Pricing date is used to compute the expiration date and discounting rules for a derivative instrument. For example, the expiration date of a 1-month forward will be 1 month from the current pricing date. pricing_date is different from market_data_as_of which is the date for sourcing market data and is defaulted to 1 business day before pricing_date.

info

Note

For information on how to define an instrument and compute price and risk for it, please refer to Instruments and Measures, respectively, for details.

Let's import PricingContext and examine the current pricing date. It is defaulted to today when pricing an instrument.

from gs_quant.markets import PricingContext

PricingContext.current.pricing_date

Output:

datetime.date(2019, 10, 2)

If you wish to change the default behaviour, you can change the default pricing context:

import datetime as dt
from gs_quant.datetime import business_day_offset

PricingContext.default=PricingContext(
    pricing_date=business_day_offset(dt.date.today(), -1, roll='preceding'),
    market_data_location='NYC')

A PricingContext may also be used as a context manager, to temporarily change pricing parameters for the scope of a computation:

custom_date = dt.date(2019, 5, 31)

with PricingContext(pricing_date=custom_date):
    swaption.resolve()
    price_f = swaption.price()

print(swaption.as_dict())
print(price_f.result())

Output:

{'floating_rate_spread': 0.0,
 'floating_rate_day_count_fraction': ACT/360,
 'asset_class': Rates,
 'floating_rate_designated_maturity': '3m',
 'fixed_rate_business_day_convention': Modified Following,
 'premium': 0.0,
 'expiration_date': '2020-11-02',
 'settlement': Phys.CLEARED,
 'termination_date': '2025-11-04',
 'notional_currency': USD,
 'pay_or_receive': 'Receive',
 'effective_date': '2020-11-04',
 'strike': 0.017845989434194312,
 'premium_payment_date': '2019-10-04',
 'fee': 0.0,
 'floating_rate_frequency': '3m',
 'fixed_rate_day_count_fraction': 30/360,
 'type': Swaption,
 'floating_rate_option': 'USD-LIBOR-BBA',
 'floating_rate_business_day_convention': Modified Following,
 'fixed_rate_frequency': '6m',
 'notional_amount': 100000000.0}
 1163436.0812983029

Note that using a PricingContext as a context manager has two extra effects:

  1. All calls to price(), calc() are dispatched as a single request, on context manager exit. This allows for the communication overhead to be borne only once for multiple calculations.
  2. The results of these calls will be futures, whose result is the original data type. Thus, calc(risk.IRDelta) will return a future whose result will be a pandas DataFrame and dollar_price(), will return a future whose result will be a float.

PricingContext has optional parameters of is_async and is_batch, which are discussed below.

Historical Pricing Context

HistoricalPricingContext can be used to evaluate instruments for a range of parameters, like the date range below.

from gs_quant.markets import HistoricalPricingContext

start_date = dt.date(2019, 5, 1)
end_date = dt.date(2019, 5, 31)

with HistoricalPricingContext(start_date, end_date):
    swaption_vega_f = swaption.calc(risk.IRVega)

swaption_vega = swaption_vega_f.result()
swaption_vega.head()

Output:

         date   marketDataType	      assetId	pointClass	point	     value
0	2019-05-01	        IR VOL	USD-LIBOR-BBA	SWAPTION	5Y;1Y	-872.258904
1	2019-05-01	        IR VOL	USD-LIBOR-BBA	SWAPTION	5Y;15M	19017.802128
2	2019-05-01	        IR VOL	USD-LIBOR-BBA	SWAPTION	5Y;18M	2974.140440
3	2019-05-01	        IR VOL	USD-LIBOR-BBA	SWAPTION	5Y;21M	-306.115675
4	2019-05-02	        IR VOL	USD-LIBOR-BBA	SWAPTION	5Y;1Y	-859.820134

Controlling Computation Behavior

There are two parameters of PricingContext which determine how the calculation call should be made: is_async and is_batch. There are several considerations when deciding what the preferred behavior is.

Async

is_async determines whether the request is processed asynchronously. This allows a unit of work to run separately from the primary application thread.

If False (the default) the __exit__ method of PricingContext will block until the results are returned. If True, it will return immediately and the user should check the status of the returned futures, or add a callback to them in order to handle results.

Setting is_async=True allows other work to be done while waiting for calculation results. It is best used for processing independent data; asynchronously updating records that are dependent or depended upon may lead to unexpected results.

Batch

Calculation requests are handled by HTTP calls. The gateway which processes these calls has a maximum timeout of 3 minutes. For calculations that are expected to last longer than 3 minutes, is_batch should be set to True. In this mode, the computation is run asynchronously on Goldman Sachs' servers and a thread is started to listen for the results.

is_batch is independent of is_async and may be used with is_async set either to True or False.

Currently is_batch=True adds a small overhead to calculations. This will be eliminated in the future, at which point we will likely to make all calculations execute this way, and eliminate the is_batch option.

Pricing Context with Async and Batch

In the example below we will examine the behavior of async and batch. We will price and calculate vega for an Interest Rate Swaption over May 2019 and do some 'work' while waiting for the result. Based on the output, one can see that some useful work can be done by setting is_async=True but changing is_batch=True doesn't offer any additional speed-up.

from gs_quant.common import PayReceive, Currency
from gs_quant.instrument import IRSwaption
from gs_quant.markets import HistoricalPricingContext
from time import sleep
import gs_quant.risk as risk
import datetime as dt

# create swaption
swaption = IRSwaption(PayReceive.Receive, '5y', Currency.USD, expiration_date='13m', strike='atm+40', notional_amount=1e8)

start_date = dt.date(2019, 5, 1)
end_date = dt.date(2019, 5, 31)

# price over date range with both async and batch = True
with HistoricalPricingContext(start_date, end_date, is_async=True, is_batch=True):
    swaption_price = swaption.price()
    swaption_vega  = swaption.calc(risk.IRVega)

# Do some work while waiting for results. All futures will be completed on exit of PricingContext
n = 0
while not swaption_price.done():
    print(n)
    n += 1
    sleep(1)

swaption_prices = swaption_price.result()
swaption_vegas  = swaption_vega.result()

Output with is_batch=True:

0
1
2
3
4

Output with is_batch=False:

0
1
2
3
4
5
6

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.