diff --git a/gs_quant/documentation/02_pricing_and_risk/01_scenarios_and_contexts/examples/07_curve_overlay/010700_discount_curve_overlay.ipynb b/gs_quant/documentation/02_pricing_and_risk/01_scenarios_and_contexts/examples/07_curve_overlay/010700_discount_curve_overlay.ipynb index 760763d4..9ae44207 100644 --- a/gs_quant/documentation/02_pricing_and_risk/01_scenarios_and_contexts/examples/07_curve_overlay/010700_discount_curve_overlay.ipynb +++ b/gs_quant/documentation/02_pricing_and_risk/01_scenarios_and_contexts/examples/07_curve_overlay/010700_discount_curve_overlay.ipynb @@ -10,7 +10,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -23,7 +23,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -33,17 +33,9 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Original price: 605,255.51\n" - ] - } - ], + "outputs": [], "source": [ "# Create and price a swaption\n", "swaption = IRSwaption('Pay', '5y', 'USD', expiration_date='3m')\n", @@ -68,20 +60,13 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Scenario price: 635,392.60\n" - ] - } - ], + "outputs": [], "source": [ "import pandas as pd\n", - "\n", + "from gs_quant.markets import PricingContext\n", + "from gs_quant.common import MarketBehaviour\n", "# Read discount factors from a csv file\n", "df = pd.read_csv (r'CurveExample.csv', sep=\",\" )\n", "dates = df.MaturityDate.tolist()\n", @@ -95,16 +80,24 @@ " csa_term = \"USD-SOFR\",\n", " curve_type = \"Discount Curve\"\n", ")\n", - "with curve_overlay_scenario:\n", - " price_with_overlay = swaption.price()\n", - "\n", - "print('Scenario price: {:,.2f}'.format(price_with_overlay))" + "with PricingContext(market_behaviour=MarketBehaviour.Calibrated):\n", + " with curve_overlay_scenario:\n", + " price_with_overlay = swaption.price()\n", + "p = price_with_overlay.result()\n", + "print('Scenario price: {:,.2f}'.format(p))" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -118,7 +111,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.1" + "version": "3.10.10" } }, "nbformat": 4, diff --git a/gs_quant/risk/results.py b/gs_quant/risk/results.py index d80d6711..553e09b9 100644 --- a/gs_quant/risk/results.py +++ b/gs_quant/risk/results.py @@ -127,7 +127,10 @@ def _value_for_date(result: Union[DataFrameWithInfo, SeriesWithInfo], date: Unio if result.empty: return result - raw_value = result.loc[date] + # if the result is a dataframe try to preserve the type + # if a dataframe has only 1 row selected it otherwise gets turned into a series + raw_value = result.loc[[date]] if isinstance(result, DataFrameWithInfo) and isinstance(date, dt.date) \ + else result.loc[date] key = result.risk_key risk_key = RiskKey( diff --git a/gs_quant/session.py b/gs_quant/session.py index 94570852..9ee1e326 100644 --- a/gs_quant/session.py +++ b/gs_quant/session.py @@ -190,7 +190,8 @@ def init(self): self._session.headers.update({'X-Application': self.application}) self._session.headers.update({'X-Version': self.application_version}) self._authenticate() - self.post_to_activity_service() + if self.domain == Domain.APP: + self.post_to_activity_service() def close(self): self._session: requests.Session @@ -527,12 +528,15 @@ def post_to_activity_service(self): params = {'featureApplication': self.application, 'gsQuantVersion': self.application_version, 'pythonVersion': f'{sys.version_info.major}.{sys.version_info.minor}'} - self._session.post(f'{self.domain}/{self.api_version}/activities', - verify=self.verify, - headers={'Content-Type': 'application/json; charset=utf-8'}, - data=json.dumps({'action': 'Initiated', 'kpis': [{'id': 'gsqInitiated', 'value': 1}], - 'resource': 'GSQuant', - 'parameters': params})) + try: + self._session.post(f'{self.domain}/{self.api_version}/activities', + verify=self.verify, + headers={'Content-Type': 'application/json; charset=utf-8'}, + data=json.dumps({'action': 'Initiated', 'kpis': [{'id': 'gsqInitiated', 'value': 1}], + 'resource': 'GSQuant', + 'parameters': params})) + except Exception: + pass @classmethod def get( diff --git a/gs_quant/target/workflow_quote.py b/gs_quant/target/workflow_quote.py index df28ab8a..1d04e7b8 100644 --- a/gs_quant/target/workflow_quote.py +++ b/gs_quant/target/workflow_quote.py @@ -248,6 +248,7 @@ class WorkflowPosition(Base): description: Optional[str] = field(default=None, metadata=field_metadata) entitlements: Optional[WorkflowEntitlements] = field(default=None, metadata=field_metadata) creator: Optional[str] = field(default=None, metadata=field_metadata) + originating_system: Optional[str] = field(default=None, metadata=field_metadata) is_read_only: Optional[bool] = field(default=None, metadata=field_metadata) name: Optional[str] = field(default=None, metadata=name_metadata) diff --git a/gs_quant/test/calc_cache/request1d4539a3acd22c90869eab6aa89349e3.json b/gs_quant/test/calc_cache/request1d4539a3acd22c90869eab6aa89349e3.json new file mode 100644 index 00000000..567f7ca1 --- /dev/null +++ b/gs_quant/test/calc_cache/request1d4539a3acd22c90869eab6aa89349e3.json @@ -0,0 +1,50 @@ +[ + [ + [ + [ + { + "$type": "RiskVector", + "points": [ + { + "path": "", + "type": "CMD NRG", + "asset": "WTI", + "class_": "FUTURE", + "point": "Z24", + "quoteStyle": "Price" + } + ], + "asset": [ + -1630.8292216583252 + ], + "calculationTime": 2170, + "queueingTime": -10164 + } + ] + ] + ], + [ + [ + [ + { + "$type": "RiskVector", + "points": [ + { + "path": "", + "type": "CMD NRG", + "asset": "WTI", + "class_": "FUTURE", + "point": "Z24", + "quoteStyle": "Price" + } + ], + "asset": [ + -1630.8292216583252 + ], + "calculationTime": 2170, + "queueingTime": -10164 + } + ] + ] + ] +] \ No newline at end of file diff --git a/gs_quant/test/risk/test_results.py b/gs_quant/test/risk/test_results.py index 0a31595e..0e4cea75 100644 --- a/gs_quant/test/risk/test_results.py +++ b/gs_quant/test/risk/test_results.py @@ -19,7 +19,7 @@ import gs_quant.risk as risk import numpy as np import pytest -from gs_quant.instrument import IRSwap, IRBasisSwap, IRSwaption, FXMultiCrossBinary, FXMultiCrossBinaryLeg +from gs_quant.instrument import IRSwap, IRBasisSwap, IRSwaption, FXMultiCrossBinary, FXMultiCrossBinaryLeg, CommodSwap from gs_quant.markets import HistoricalPricingContext, PricingContext, CloseMarket, MarketDataCoordinate from gs_quant.markets.portfolio import Portfolio from gs_quant.risk import MultiScenario, ResolvedInstrumentValues @@ -70,12 +70,14 @@ def get_attributes(p, risks, ctx='PricingCtx1', resolve=False, no_frame=False): swap_6 = IRSwap("Pay", "10y", "GBP", fixed_rate=-0.005, name="10y") swap_7 = IRSwap("Pay", "5y", "JPY", fixed_rate=-0.005, name="5y") swap_8 = IRSwap("Pay", "10y", "JPY", fixed_rate=-0.005, name="10y") +commod_swap = CommodSwap(name="Test") eur_port = Portfolio([swap_1, swap_2], name="EUR") usd_port = Portfolio([swap_3, swap_4], name="USD") gbp_port = Portfolio([swap_5, swap_6], name="GBP") jpy_port = Portfolio([swap_7, swap_8], name='JPY') port1 = Portfolio([eur_port, gbp_port], name='EURGBP') port2 = Portfolio([jpy_port, usd_port], name='USDJPY') +commod_port = Portfolio([commod_swap]) port = Portfolio([port1, port2]) swaption_port = Portfolio([IRSwaption("Receive", '5y', 'USD', expiration_date='2m', strike='atm', name='Swaption1'), IRSwaption("Receive", '10y', 'USD', expiration_date='3m', strike='atm', name='Swaption2')]) @@ -302,6 +304,7 @@ def test_bucketed_risks(mocker): _, res4, frame4 = get_attributes(port1, risk.IRDelta, 'Multiple') _, res5, frame5 = get_attributes(bs_port, risk.IRBasis(aggregation_level=AggregationLevel.Asset)) _, res6, frame6 = get_attributes(jpy_port, risk.IRBasis(aggregation_level=AggregationLevel.Asset), 'Multiple') + _, res7, frame7 = get_attributes(commod_port, risk.CommodDelta, "Multiple") def check_depth(res, f, with_dates=''): temp_res = list(res)[0].drop('value', axis=1) @@ -316,6 +319,7 @@ def check_depth(res, f, with_dates=''): check_depth(res4, frame4, 'dated') check_depth(res5, frame5) check_depth(res6, frame6, 'dated') + check_depth(res7, frame7, 'dated') default_pivot_table_test(res1, 'has_bucketed') default_pivot_table_test(res2, 'has_bucketed') @@ -323,6 +327,7 @@ def check_depth(res, f, with_dates=''): default_pivot_table_test(res4, 'has_bucketed') default_pivot_table_test(res5, 'has_bucketed') default_pivot_table_test(res6, 'has_bucketed') + default_pivot_table_test(res7, 'has_bucketed') # test slicing # slice one portfolio @@ -349,6 +354,9 @@ def filter_lambda(x): manual_agg_f6 = frame6.loc[frame6.apply(filter_lambda, axis=1)]['value'].values.sum() np.testing.assert_almost_equal(filter_agg_r6, manual_agg_f6, 8) + assert isinstance(res7[date(2020, 1, 14)].result().futures[0].result(), DataFrameWithInfo) + assert res7[date(2020, 1, 14)].to_frame()["mkt_type"][0] == "CMD NRG" + def test_cashflows_risk(mocker): with MockCalc(mocker):