Example #1
0
def test_get_asset(mocker):
    marquee_id = 'MA1234567890'
    mock_response = GsAsset(AssetClass.Equity, GsAssetType.Single_Stock,
                            'Test Asset')

    # mock GsSession
    mocker.patch.object(GsSession.__class__,
                        'default_value',
                        return_value=GsSession.get(Environment.QA, 'client_id',
                                                   'secret'))
    mocker.patch.object(GsSession.current, '_get', return_value=mock_response)

    asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.MARQUEE_ID)

    assert asset.name == "Test Asset"
    assert asset.get_type() == AssetType.STOCK

    asset = SecurityMaster.get_asset(marquee_id,
                                     AssetIdentifier.MARQUEE_ID,
                                     as_of=dt.date.today())

    assert asset.name == "Test Asset"
    assert asset.get_type() == AssetType.STOCK

    asset = SecurityMaster.get_asset(marquee_id,
                                     AssetIdentifier.MARQUEE_ID,
                                     as_of=dt.datetime.utcnow())

    assert asset.name == "Test Asset"
    assert asset.get_type() == AssetType.STOCK

    mock_response = GsAsset(AssetClass.Equity, GsAssetType.Index, 'Test Asset')
    mocker.patch.object(GsSession.current, '_get', return_value=mock_response)

    asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.MARQUEE_ID)

    assert asset.name == "Test Asset"
    assert asset.get_type() == AssetType.INDEX

    mock_response = GsAsset(AssetClass.Equity, GsAssetType.Future,
                            'Test Asset')
    mocker.patch.object(GsSession.current, '_get', return_value=mock_response)

    asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.MARQUEE_ID)

    assert asset.name == "Test Asset"
    assert asset.get_type() == AssetType.FUTURE

    mock_response = GsAsset(AssetClass.Equity, GsAssetType.ETF, 'Test Asset')
    mocker.patch.object(GsSession.current, '_get', return_value=mock_response)

    asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.MARQUEE_ID)

    assert asset.name == "Test Asset"
    assert asset.get_type() == AssetType.ETF

    mock_response = GsAsset(AssetClass.Equity,
                            GsAssetType.Custom_Basket,
                            'Test Asset',
                            id_=marquee_id)
    mock_positions = PositionSet(positions=[{
        'assetId': 'asset_1',
        'quantity': 100
    }],
                                 position_date=dt.date(2021, 1, 1),
                                 divisor=100)
    mock_price = {'price': 100}
    mock_report = Report(marquee_id, 'Asset', 'Basket Create',
                         ReportParameters())
    mocker.patch.object(GsAssetApi,
                        'get_latest_positions',
                        return_value=mock_positions)
    mocker.patch.object(GsIndexApi, 'initial_price', return_value=mock_price)
    mocker.patch.object(GsReportApi, 'get_reports', return_value=mock_report)
    mocker.patch.object(GsSession.current, '_get', return_value=mock_response)

    asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.MARQUEE_ID)

    assert asset.name == "Test Asset"
    assert asset.get_type() == AssetType.CUSTOM_BASKET

    mock_response = {
        'results': (GsAsset(id=marquee_id,
                            assetClass='Equity',
                            type='Single Stock',
                            name='Test 1'), ),
    }

    mocker.patch.object(GsSession.current, '_post', return_value=mock_response)
    asset = SecurityMaster.get_asset('GS.N', AssetIdentifier.REUTERS_ID)
    assert asset.name == "Test 1"
    assert asset.get_type() == AssetType.STOCK

    asset = SecurityMaster.get_asset('GS',
                                     AssetIdentifier.TICKER,
                                     exchange_code=ExchangeCode.NYSE)
    assert asset.name == "Test 1"
    assert asset.get_type() == AssetType.STOCK

    asset = SecurityMaster.get_asset('GS',
                                     AssetIdentifier.TICKER,
                                     asset_type=AssetType.STOCK)
    assert asset.name == "Test 1"
    assert asset.get_type() == AssetType.STOCK

    mocker.patch.object(GsSession.current,
                        '_post',
                        return_value={'results': ()})
    asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.REUTERS_ID)
    assert asset is None
Example #2
0
def test_asset_identifiers(mocker):
    marquee_id = 'MA1234567890'

    mocker.patch.object(GsSession,
                        'default_value',
                        return_value=GsSession.get(Environment.QA, 'client_id',
                                                   'secret'))
    mock_response = GsAsset(AssetClass.Equity,
                            GsAssetType.Custom_Basket,
                            'Test Asset',
                            id_=marquee_id)
    mock_positions = PositionSet(positions=[{
        'assetId': 'asset_1',
        'quantity': 100
    }],
                                 position_date=dt.date(2021, 1, 1),
                                 divisor=100)
    mock_price = {'price': 100}
    mock_report = Report(marquee_id, 'Asset', 'Basket Create',
                         ReportParameters())
    mocker.patch.object(GsAssetApi,
                        'get_latest_positions',
                        return_value=mock_positions)
    mocker.patch.object(GsIndexApi, 'initial_price', return_value=mock_price)
    mocker.patch.object(GsReportApi, 'get_reports', return_value=mock_report)
    mocker.patch.object(GsSession.current, '_get', return_value=mock_response)

    asset = SecurityMaster.get_asset(marquee_id, AssetIdentifier.MARQUEE_ID)

    mock_response = {
        'xrefs': ({
            'startDate': '1952-01-01',
            'endDate': '2018-12-31',
            'identifiers': {
                'ric': '.GSTHHOLD',
                'bbid': 'GSTHHOLD',
                'cusip': '9EQ24FOLD',
                'ticker': 'GSTHHOLD'
            }
        }, {
            'startDate': '2019-01-01',
            'endDate': '2952-12-31',
            'identifiers': {
                'ric': '.GSTHHVIP',
                'bbid': 'GSTHHVIP',
                'cusip': '9EQ24FPE5',
                'ticker': 'GSTHHVIP',
            }
        })
    }

    mocker.patch.object(GsSession.current, '_get', return_value=mock_response)

    identifiers = asset.get_identifiers(dt.date.today())

    assert identifiers[AssetIdentifier.REUTERS_ID.value] == '.GSTHHVIP'
    assert identifiers[AssetIdentifier.BLOOMBERG_ID.value] == 'GSTHHVIP'
    assert identifiers[AssetIdentifier.CUSIP.value] == '9EQ24FPE5'
    assert identifiers[AssetIdentifier.TICKER.value] == 'GSTHHVIP'

    assert asset.get_identifier(AssetIdentifier.REUTERS_ID,
                                as_of=dt.date.today()) == '.GSTHHVIP'
    assert asset.get_identifier(AssetIdentifier.BLOOMBERG_ID,
                                as_of=dt.date.today()) == 'GSTHHVIP'
    assert asset.get_identifier(AssetIdentifier.CUSIP,
                                as_of=dt.date.today()) == '9EQ24FPE5'
    assert asset.get_identifier(AssetIdentifier.TICKER,
                                as_of=dt.date.today()) == 'GSTHHVIP'

    market = PricingContext(dt.date(2018, 3, 1))

    with market:
        identifiers = asset.get_identifiers()

    assert identifiers[AssetIdentifier.REUTERS_ID.value] == '.GSTHHOLD'
    assert identifiers[AssetIdentifier.BLOOMBERG_ID.value] == 'GSTHHOLD'
    assert identifiers[AssetIdentifier.CUSIP.value] == '9EQ24FOLD'
    assert identifiers[AssetIdentifier.TICKER.value] == 'GSTHHOLD'

    market = PricingContext(dt.date(2018, 3, 1))

    with market:
        identifiers = asset.get_identifiers()

    assert identifiers[AssetIdentifier.REUTERS_ID.value] == '.GSTHHOLD'
    assert identifiers[AssetIdentifier.BLOOMBERG_ID.value] == 'GSTHHOLD'
    assert identifiers[AssetIdentifier.CUSIP.value] == '9EQ24FOLD'
    assert identifiers[AssetIdentifier.TICKER.value] == 'GSTHHOLD'
Example #3
0
initial_price = {'price': 100}
mqid = 'MA1234567890'
name = 'Test Basket'
positions = [
    Position('bbid1', asset_id='id1', quantity=100),
    Position('bbid2', asset_id='id2', quantity=200)
]
positions_weighted = positions = [
    Position('bbid1', asset_id='id1', weight=0.4),
    Position('bbid2', asset_id='id2', weight=0.6)
]
position_set = PositionSet(positions, divisor=1000)
report = Report(mqid,
                'asset',
                'Basket Create',
                ReportParameters(),
                status='done')
resolved_asset = {'GSMBXXXX': [{'id': mqid}]}
target_positions = tuple([
    TargetPosition(asset_id='id1', quantity=100),
    TargetPosition(asset_id='id2', quantity=200)
])
target_position_set = TargetPositionSet(target_positions,
                                        dt.date(2021, 1, 7),
                                        divisor=1000)
ticker = 'GSMBXXXX'
user_ea = {
    **base_user, 'id': 'user_abc',
    'tokens': ['external', 'guid:user_abc']
}  # external, admin
user_ena = {
Example #4
0
 def save(self):
     """ Create a report in Marquee if it doesn't exist. Update the report if it does. """
     target_report = TargetReport(name=self.name,
                                  position_source_id=self.position_source_id,
                                  position_source_type=self.position_source_type,
                                  type_=self.type,
                                  parameters=self.parameters if self.parameters else ReportParameters())
     if self.id:
         target_report.id = self.id
         GsReportApi.update_report(target_report)
     else:
         report = GsReportApi.create_report(target_report)
         self.__id = report.id
Example #5
0
from gs_quant.target.risk_models import RiskModel, CoverageType, Term, UniverseIdentifier

risk_model = RiskModel(coverage=CoverageType.Country,
                       id_='model_id',
                       name='Fake Risk Model',
                       term=Term.Long,
                       universe_identifier=UniverseIdentifier.gsid,
                       vendor='GS',
                       version=1.0)

factor_risk_report = Report(
    position_source_id='position source id',
    position_source_type=PositionSourceType.Portfolio,
    type_=ReportType.Portfolio_Factor_Risk,
    id_='report_id',
    parameters=ReportParameters(risk_model='risk_model_id'))

ppa_report = Report(position_source_id='position source id',
                    position_source_type=PositionSourceType.Portfolio,
                    type_=ReportType.Portfolio_Performance_Analytics,
                    id_='report_id',
                    parameters=ReportParameters(risk_model='risk_model_id'))

factor_data = [{
    'date': '2020-11-23',
    'reportId': 'report_id',
    'factor': 'factor_id',
    'factorCategory': 'CNT',
    'pnl': 11.23,
    'exposure': -11.23,
    'proportionOfRisk': 1
Example #6
0
def test_normalized_performance_default_aum():
    replace = Replacer()
    idx = pd.date_range('2020-01-02', freq='D', periods=3)
    expected = pd.Series(data=[1, 1 + 2 / 1.2, 1 + 6 / 1.3],
                         index=idx,
                         name='normalizedPerformance',
                         dtype='float64')

    with DataContext(datetime.date(2020, 1, 1), datetime.date(2019, 1, 3)):
        df = MarketDataResponseFrame(data=ppa_data, dtype="float64")

        # mock GsPortfolioApi.get_reports()
        mock = replace('gs_quant.api.gs.portfolios.GsPortfolioApi.get_reports',
                       Mock())
        mock.return_value = [
            Report.from_dict({
                'id': 'RP1',
                'positionSourceType': 'Portfolio',
                'positionSourceId': 'MP1',
                'type': 'Portfolio Performance Analytics',
                'parameters': {
                    'transactionCostModel': 'FIXED'
                }
            })
        ]

        # mock PerformanceReport.get_portfolio_constituents()
        mock = replace(
            'gs_quant.markets.report.PerformanceReport.get_portfolio_constituents',
            Mock())
        mock.return_value = MarketDataResponseFrame(data=constituents_data,
                                                    dtype="float64")

        # mock PerformanceReport.get_many_measures()
        mock = replace(
            'gs_quant.markets.report.PerformanceReport.get_many_measures',
            Mock())
        mock.return_value = df

        # mock PerformanceReport.get_custom_aum()
        mock = replace(
            'gs_quant.api.gs.portfolios.GsPortfolioApi.get_custom_aum', Mock())
        mock.return_value = aum

        # mock PerformanceReport.get()
        mock = replace('gs_quant.markets.report.PerformanceReport.get', Mock())
        mock.return_value = PerformanceReport(
            report_id='RP1',
            position_source_type='Portfolio',
            position_source_id='MP1',
            report_type='Portfolio Performance Analytics',
            parameters=ReportParameters(transaction_cost_model='FIXED'))

        mock = replace(
            'gs_quant.api.gs.portfolios.GsPortfolioApi.get_portfolio', Mock())
        mock.return_value = Portfolio('USD', 'P1', id_='MP1')

        actual = mr.normalized_performance('MP1', None)
        print(actual)
        assert all(actual.values == expected.values)
    replace.restore()