def test_market_data_object(): coord_val_pair = [ { 'coordinate': { 'mkt_type': 'IR', 'mkt_asset': 'USD', 'mkt_class': 'Swap', 'mkt_point': ('5y', ), 'mkt_quoting_style': 'ATMRate' }, 'value': 0.9973194889 }, { 'coordinate': { 'mkt_type': 'IR', 'mkt_asset': 'USD', 'mkt_class': 'Swap', 'mkt_point': ('40y', ), 'mkt_quoting_style': 'ATMRate' }, 'value': 'redacted' }, ] coordinates = { MarketDataCoordinate.from_dict(dic['coordinate']): dic['value'] for dic in coord_val_pair } overlay_market = OverlayMarket(base_market=CloseMarket( market_data=coordinates)) assert overlay_market.coordinates[0] == MarketDataCoordinate.from_dict( coord_val_pair[0]['coordinate']) assert overlay_market.redacted_coordinates[ 0] == MarketDataCoordinate.from_dict(coord_val_pair[1]['coordinate'])
def handle_result(result: Optional[Union[DataFrameWithInfo, ErrorValue, PricingFuture]]) -> \ [OverlayMarket, dict]: if isinstance(result, ErrorValue): return result properties = MarketDataCoordinate.properties() is_historical = result.index.name == 'date' location = PricingContext.current.market_data_location def extract_market_data(this_result: DataFrameWithInfo): market_data = {} for _, row in this_result.iterrows(): coordinate_values = {p: row.get(p) for p in properties} mkt_point = coordinate_values.get('mkt_point') if mkt_point is not None: coordinate_values['mkt_point'] = tuple(coordinate_values['mkt_point'].split(';')) # return 'redacted' as coordinate value if its a redacted coordinate market_data[MarketDataCoordinate.from_dict(coordinate_values)] = row['value'] if \ row['permissions'] == 'Granted' else 'redacted' return market_data if is_historical: return {date: OverlayMarket( base_market=CloseMarket(date=date, location=location), market_data=extract_market_data(result.loc[date])) for date in set(result.index)} else: return OverlayMarket(base_market=result.risk_key.market, market_data=extract_market_data(result))
def handle_result(result: Optional[Union[DataFrameWithInfo, ErrorValue, PricingFuture]]) ->\ [OverlayMarket, dict]: properties = MarketDataCoordinate.properties() is_historical = isinstance(PricingContext.current, HistoricalPricingContext) location = PricingContext.current.market_data_location def extract_market_data(this_result: DataFrameWithInfo): market_data = {} for _, row in result.iterrows(): coordinate_values = {p: row.get(p) for p in properties} if 'mkt_point' in coordinate_values: coordinate_values['mkt_point'] = tuple( coordinate_values['mkt_point'].split(';')) market_data[MarketDataCoordinate.from_dict( coordinate_values)] = row['value'] return market_data if is_historical: return { date: OverlayMarket( base_market=CloseMarket(date=date, location=location), market_data=extract_market_data(result.loc[date])) for date in set(result.index) } else: return OverlayMarket(base_market=result.risk_key.market, market_data=extract_market_data(result))
def extract_market_data(this_result: DataFrameWithInfo): market_data = {} for _, row in result.iterrows(): coordinate_values = {p: row.get(p) for p in properties} if 'mkt_point' in coordinate_values: coordinate_values['mkt_point'] = tuple( coordinate_values['mkt_point'].split(';')) market_data[MarketDataCoordinate.from_dict( coordinate_values)] = row['value'] return market_data
def extract_market_data(this_result: DataFrameWithInfo): market_data = {} for _, row in this_result.iterrows(): coordinate_values = {p: row.get(p) for p in properties} mkt_point = coordinate_values.get('mkt_point') if mkt_point is not None: coordinate_values['mkt_point'] = tuple(coordinate_values['mkt_point'].split(';')) # return 'redacted' as coordinate value if its a redacted coordinate market_data[MarketDataCoordinate.from_dict(coordinate_values)] = row['value'] if \ row['permissions'] == 'Granted' else 'redacted' return market_data
def _coordinate_from_str(cls, coordinate_str: str) -> MarketDataCoordinate: tmp = coordinate_str.rsplit(".", 1) dimensions = tmp[0].split("_") if len(dimensions) < 2: raise MqValueError('invalid coordinate ' + coordinate_str) kwargs = { 'mkt_type': dimensions[0], 'mkt_asset': dimensions[1] or None, 'mkt_quoting_style': tmp[-1] if len(tmp) > 1 else None} if len(dimensions) > 2: kwargs['mkt_class'] = dimensions[2] or None if len(dimensions) > 3: kwargs['mkt_point'] = tuple(dimensions[3:]) or None return MarketDataCoordinate(**kwargs)
def get_many_coordinates( cls, mkt_type: str = None, mkt_asset: str = None, mkt_class: str = None, mkt_point: Tuple[str, ...] = (), *, limit: int = 100, return_type: type = str, ) -> Union[Tuple[str, ...], Tuple[MarketDataCoordinate, ...]]: where = FieldFilterMap( mkt_type=mkt_type.upper() if mkt_type is not None else None, mkt_asset=mkt_asset.upper() if mkt_asset is not None else None, mkt_class=mkt_class.upper() if mkt_class is not None else None, ) for index, point in enumerate(mkt_point): setattr(where, 'mkt_point' + str(index + 1), point.upper()) query = EntityQuery( where=where, limit=limit ) results = GsSession.current._post('/data/mdapi/query', query)['results'] if return_type is str: return tuple(coordinate['name'] for coordinate in results) elif return_type is MarketDataCoordinate: return tuple( MarketDataCoordinate( mkt_type=coordinate['dimensions']['mktType'], mkt_asset=coordinate['dimensions']['mktAsset'], mkt_class=coordinate['dimensions']['mktClass'], mkt_point=tuple(coordinate['dimensions']['mktPoint'].values()), mkt_quoting_style=coordinate['dimensions']['mktQuotingStyle'] ) for coordinate in results) else: raise NotImplementedError('Unsupported return type')
import datetime as dt import pandas as pd import pytest from pandas.util.testing import assert_frame_equal, assert_series_equal from gs_quant.api.gs.data import GsDataApi from gs_quant.context_base import ContextMeta from gs_quant.errors import MqValueError from gs_quant.markets import MarketDataCoordinate from gs_quant.session import GsSession, Environment from gs_quant.target.data import MDAPIDataQuery, MarketDataVendor, DataSetEntity test_coordinates = ( MarketDataCoordinate(mkt_type='Prime', mkt_quoting_style='price', mkt_asset='335320934'), MarketDataCoordinate(mkt_type='IR', mkt_asset='USD', mkt_class='Swap', mkt_point=('2Y', )), ) test_str_coordinates = ('Prime_335320934_.price', 'IR_USD_Swap_2Y') test_defn_dict = { 'id': 'EXAMPLE_FROM_SLANG', 'name': 'Example DataSet', 'description': 'This is a test.', 'shortDescription': '', 'vendor': 'Goldman Sachs',
""" import pytest from pandas.util.testing import assert_frame_equal from gs_quant.api.gs.data import GsDataApi from gs_quant.markets import MarketDataCoordinate from gs_quant.risk import sort_risk import datetime as dt import pandas as pd from gs_quant.session import GsSession, Environment from gs_quant.context_base import ContextMeta test_coordinates = ( MarketDataCoordinate('Prime', field='price', marketDataAsset='335320934'), MarketDataCoordinate('IR', marketDataAsset='USD', pointClass='Swap', marketDataPoint=('2Y', ), quotingStyle='ATMRate'), ) def test_coordinates_data(mocker): bond_data = [{ 'marketDataType': 'Prime', 'marketDataAsset': '335320934', 'field': 'price', 'price': 1.0139, 'time': pd.to_datetime('2019-01-20T01:03:00Z')