def create_ratings_property(self, *ratings):

        ratings = [*ratings]
        global scope
        scope = "Derived"

        for rating in ratings:
            property_definition = models.CreatePropertyDefinitionRequest(
                domain="Instrument",
                scope=scope,
                code=f"{rating}Rating",
                display_name=f"{rating}Rating",
                data_type_id=lusid.ResourceId(scope="system", code="number"),
            )

            try:
                # create property definition
                self.property_definitions_api.create_property_definition(
                    create_property_definition_request=property_definition
                )
            except lusid.ApiException as e:
                if json.loads(e.body)["name"] == "PropertyAlreadyExists":
                    logging.info(
                        f"Property {property_definition.domain}/{property_definition.scope}/{property_definition.display_name} already exists"
                    )
Exemple #2
0
    def test_derived_property(self):

        self.create_ratings_property("Fitch", "Moodys")

        # create instrument property edge cases and upsert (using arbitrary numeric ratings)
        self.upsert_ratings_property("BBG00KTDTF73",
                                     fitch_value=10,
                                     moodys_value=5)
        self.upsert_ratings_property("BBG00Y271826")
        self.upsert_ratings_property("BBG00L7XVNP1", moodys_value=5)
        self.upsert_ratings_property("BBG005D5KGM0", fitch_value=10)

        # create derived property using the 'Coalesce' derivation formula
        code = "DerivedRating"
        derivation_formula = f"Coalesce(Properties[Instrument/{scope}/MoodysRating], Properties[Instrument/{scope}/FitchRating], 0)"

        # create derived property request
        derived_prop_definition_req = models.CreateDerivedPropertyDefinitionRequest(
            domain="Instrument",
            scope=scope,
            code=code,
            display_name=code,
            data_type_id=lusid.ResourceId(scope="system", code="number"),
            derivation_formula=derivation_formula,
        )

        try:
            # create property definition
            self.property_definitions_api.create_derived_property_definition(
                derived_prop_definition_req)
        except lusid.ApiException as e:
            if json.loads(e.body)["name"] == "PropertyAlreadyExists":
                logging.info(
                    f"Property {derived_prop_definition_req.domain}{derived_prop_definition_req.scope}/{derived_prop_definition_req.display_name} already exists"
                )
        finally:
            self.id_generator.add_scope_and_code(
                "property_definition", derived_prop_definition_req.scope,
                derived_prop_definition_req.code, ["Instrument"])

        # test case for derived property with both ratings
        moodys_then_fitch = self.get_instruments_with_derived_prop(
            "BBG00KTDTF73")
        self.assertEqual(moodys_then_fitch, 5.0)

        # test case for derived property with no ratings
        no_ratings = self.get_instruments_with_derived_prop("BBG00Y271826")
        self.assertEqual(no_ratings, 0.0)

        # # test case for derived property with fitch only
        fitch_only = self.get_instruments_with_derived_prop("BBG005D5KGM0")
        self.assertEqual(fitch_only, 10.0)

        # test case for derived property with moodys only
        moodys_only = self.get_instruments_with_derived_prop("BBG00L7XVNP1")
        self.assertEqual(moodys_only, 5.0)
Exemple #3
0
 def load_properties(properties_api, id_generator, scopes, codes):
     for scope in scopes:
         for code in codes:
             try:
                 properties_api.create_property_definition(
                     create_property_definition_request=models.CreatePropertyDefinitionRequest(
                         domain="Order",
                         scope=scope,
                         code=code,
                         display_name=code,
                         constraint_style="Property",
                         data_type_id=lusid.ResourceId(scope="system", code="string"),
                     )
                 )
             except lusid.ApiException as e:
                 if json.loads(e.body)["name"] == "PropertyAlreadyExists":
                     pass  # ignore if the property definition exists
             finally:
                 id_generator.add_scope_and_code("property_definition", scope, code, ["Order"])
Exemple #4
0
    def create_transaction_property(self):
        # Details of the property
        property_definition = models.CreatePropertyDefinitionRequest(
            domain="Transaction",
            scope=self.scope,
            code=self.code,
            display_name=self.code,
            data_type_id=lusid.ResourceId(scope="system", code="string"),
        )

        # create property definition
        try:
            self.property_definitions_api.create_property_definition(
                create_property_definition_request=property_definition
            )
        except lusid.ApiException as e:
            if json.loads(e.body)["name"] == "PropertyAlreadyExists":
                self.root_logger.info(
                    f"Property {property_definition.domain}/{property_definition.scope}/{property_definition.code} already exists"
                )
    def test_create_portfolio_with_mv_property(self):
        # Details of property to be created
        effective_date = datetime(year=2018, month=1, day=1, tzinfo=pytz.utc)
        scope = "MultiValueProperties"
        code = "MorningstarQuarterlyRating"
        portfolio_code = "Portfolio-MVP"

        multi_value_property_definition = models.CreatePropertyDefinitionRequest(
            domain="Portfolio",
            scope=scope,
            code=code,
            display_name=code,
            constraint_style="Collection",
            data_type_id=lusid.ResourceId(scope="system", code="string"),
        )

        # create property definition
        try:
            self.property_definitions_api.create_property_definition(
                create_property_definition_request=
                multi_value_property_definition)
        except lusid.ApiException as e:
            if json.loads(e.body)["name"] == "PropertyAlreadyExists":
                logging.info(
                    f"Property {multi_value_property_definition.domain}/{multi_value_property_definition.scope}/{multi_value_property_definition.display_name} already exists"
                )
        finally:
            self.id_generator.add_scope_and_code(
                "property_definition", multi_value_property_definition.scope,
                multi_value_property_definition.code, ["Portfolio"])

        schedule = [
            '{ "2019-12-31" : "5"}',
            '{ "2020-03-31" : "4"}',
            '{ "2020-06-30" : "3"}',
            '{ "2020-09-30" : "3"}',
        ]

        # Details of new portfolio to be created
        create_portfolio_request = models.CreateTransactionPortfolioRequest(
            code=portfolio_code,
            display_name=portfolio_code,
            base_currency="GBP",
            created=effective_date,
        )

        # create portfolio
        try:
            self.transaction_portfolios_api.create_portfolio(
                scope=scope,
                create_transaction_portfolio_request=create_portfolio_request,
            )
        except lusid.ApiException as e:
            if json.loads(e.body)["name"] == "PortfolioWithIdAlreadyExists":
                logging.info(
                    f"Portfolio {create_portfolio_request.code} already exists"
                )
        finally:
            self.id_generator.add_scope_and_code("portfolio", scope,
                                                 portfolio_code)

        self.portfolios_api.upsert_portfolio_properties(
            scope=scope,
            code=portfolio_code,
            request_body={
                f"Portfolio/{scope}/{code}":
                models.ModelProperty(
                    key=f"Portfolio/{scope}/{code}",
                    value=models.PropertyValue(
                        label_value_set=models.LabelValueSet(values=schedule)),
                )
            },
        )

        # get properties for assertions
        portfolio_properties = self.portfolios_api.get_portfolio_properties(
            scope=scope, code=portfolio_code).properties
        label_value_set = portfolio_properties[
            f"Portfolio/MultiValueProperties/{code}"].value.label_value_set.values
        self.assertCountEqual(label_value_set, schedule)