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
.
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:
- 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. - 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 anddollar_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
Previous - Measures
arrow_forwardNext - Portfolios
arrow_forwardWas this page useful?
Give feedback to help us improve developer.gs.com and serve you better.