def get_excess_returns_query(self) -> DataQueryInfo: marquee_id = SharpeAssets[self.currency.value].value entity = Stock(marquee_id, "", "") coordinate: DataCoordinate = DataCoordinate( measure=DataMeasure.CLOSE_PRICE, frequency=DataFrequency.DAILY) data_query: DataQuery = DataQuery(coordinate=coordinate, start=self.start, end=self.end) return DataQueryInfo('excess_returns', None, data_query, entity)
def get_test_entity(entity_id: str): entity = _read_entity(entity_id) return Stock(id_=entity_id, name=entity['name'], currency=Currency.USD, entity=entity)
def test_factor_zscore(): replace = Replacer() # mock getting risk model entity() mock = replace('gs_quant.api.gs.risk_models.GsRiskModelApi.get_risk_model', Mock()) mock.return_value = mock_risk_model_obj # mock getting risk model factor entity mock = replace( 'gs_quant.api.gs.risk_models.GsFactorRiskModelApi.get_risk_model_data', Mock()) mock.return_value = mock_risk_model_data # mock getting risk model factor entity mock = replace( 'gs_quant.api.gs.risk_models.GsFactorRiskModelApi.get_risk_model_factor_data', Mock()) mock.return_value = mock_risk_model_factor_data # mock getting asset gsid mock = replace('gs_quant.markets.securities.Asset.get_identifier', Mock()) mock.return_value = '12345' # mock getting risk model dates mock = replace( 'gs_quant.api.gs.risk_models.GsRiskModelApi.get_risk_model_dates', Mock()) mock.return_value = ['2020-01-01', '2020-01-02', '2020-01-03'] # mock getting risk model data mock = replace('gs_quant.models.risk_model.FactorRiskModel.get_data', Mock()) mock.return_value = { 'results': [{ 'date': '2020-01-01', 'assetData': { 'factorExposure': [{ 'factor_id': 1.01, 'factor_id_1': 1.23 }] } }, { 'date': '2020-01-02', 'assetData': { 'factorExposure': [{ 'factor_id': 1.02, 'factor_id_1': 1.23 }] } }, { 'date': '2020-01-03', 'assetData': { 'factorExposure': [{ 'factor_id': 1.03, 'factor_id_1': 1.23 }] } }] } with DataContext(datetime.date(2020, 1, 1), datetime.date(2020, 1, 3)): actual = mrm.factor_zscore(Stock(id_='id', name='Fake Asset'), 'model_id', 'Factor Name') assert all(actual.values == [1.01, 1.02, 1.03]) with pytest.raises(MqValueError): mrm.factor_zscore(Stock(id_='id', name='Fake Asset'), 'model_id', 'Wrong Factor Name') replace.restore()
def test_factor_exposure(): 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) risk_model_data = { 'results': [ { 'date': '2020-01-01', 'assetData': { 'factorExposure': [ { 'factor_id': 1.01, 'factor_id_1': 1.23 } ] } }, { 'date': '2020-01-02', 'assetData': { 'factorExposure': [ { 'factor_id': 1.02, 'factor_id_1': 1.23 } ] } }, { 'date': '2020-01-03', 'assetData': { 'factorExposure': [ { 'factor_id': 1.03, 'factor_id_1': 1.23 } ] } } ] } replace = Replacer() # mock getting risk model entity() mock = replace('gs_quant.api.gs.risk_models.GsRiskModelApi.get_risk_model', Mock()) mock.return_value = risk_model # mock getting risk model factor entity mock = replace('gs_quant.api.gs.risk_models.GsRiskModelApi.get_risk_model_factor_data', Mock()) mock.return_value = [{ 'identifier': 'factor_id', 'type': 'Factor', 'name': "Factor Name" }] # mock getting asset gsid mock = replace('gs_quant.markets.securities.Asset.get_identifiers', Mock()) mock.return_value = {'GSID': '12345'} # mock getting risk model dates mock = replace('gs_quant.api.gs.risk_models.GsRiskModelApi.get_risk_model_dates', Mock()) mock.return_value = ['2020-01-01', '2020-01-02', '2020-01-03'] # mock getting risk model data mock = replace('gs_quant.markets.risk_model.RiskModel.get_data', Mock()) mock.return_value = risk_model_data with DataContext(datetime.date(2020, 1, 1), datetime.date(2020, 1, 3)): actual = mrm.factor_exposure(Stock(id_='id', name='Fake Asset'), 'model_id', 'Factor Name') assert all(actual.values == [1.01, 1.02, 1.03]) with pytest.raises(MqValueError): mrm.factor_exposure(Stock(id_='id', name='Fake Asset'), 'model_id', 'Wrong Factor Name') replace.restore()