Esempio n. 1
0
def create_aggregation_request(analyst_scope_code, today, quotes_date):

    # Create our aggregation request
    inline_recipe = models.ConfigurationRecipe(
        scope="User",
        code='quotes_recipe',
        market=models.MarketContext(
            market_rules=[
                models.MarketDataKeyRule(
                    key='Equity.LusidInstrumentId.*',
                    supplier='DataScope',
                    data_scope=analyst_scope_code,
                    quote_type='Price',
                    field='Mid',
                    quote_interval=quotes_date.strftime("%Y-%m-%d"))
            ],
            suppliers=models.MarketContextSuppliers(commodity='DataScope',
                                                    credit='DataScope',
                                                    equity='DataScope',
                                                    fx='DataScope',
                                                    rates='DataScope'),
            options=models.MarketOptions(
                default_supplier='DataScope',
                default_instrument_code_type='LusidInstrumentId',
                default_scope=analyst_scope_code)),
    )

    aggregation_request = models.AggregationRequest(
        inline_recipe=inline_recipe,
        effective_at=today,
        metrics=[
            models.AggregateSpec(key='Instrument/default/LusidInstrumentId',
                                 op='Value'),
            models.AggregateSpec(key='Holding/default/Units', op='Sum'),
            models.AggregateSpec(key='Holding/default/Cost', op='Sum'),
            models.AggregateSpec(key='Holding/default/PV', op='Sum'),
            models.AggregateSpec(key='Holding/default/Price', op='Sum')
        ],
        group_by=['Instrument/default/LusidInstrumentId'])

    return aggregation_request
Esempio n. 2
0
def valuation(api_factory, marketdata_scope, portfolio_group, time):

    time_parts = [time[:10], time[11:]]

    if time_parts[1] == "LSE_market_close":
        time_parts[1] = "16:30:00.000000+00:00"
    elif time_parts[1] == "NYSE_market_close":
        time_parts[1] = "21:00:00.000000+00:00"

    time = "T".join(time_parts)

    # Create a recipe to perform a valuation
    configuration_recipe = models.ConfigurationRecipe(
        scope="User",
        code="quotes_recipe",
        market=models.MarketContext(
            market_rules=[
                models.MarketDataKeyRule(
                    key="Equity.Figi.*",
                    supplier="DataScope",
                    data_scope=marketdata_scope,
                    quote_type="Price",
                    field="Mid",
                ),
                models.MarketDataKeyRule(
                    key="Equity.Isin.*",
                    supplier="DataScope",
                    data_scope=marketdata_scope,
                    quote_type="Price",
                    field="Mid",
                ),
                models.MarketDataKeyRule(
                    key="Equity.LusidInstrumentId.*",
                    supplier="DataScope",
                    data_scope=marketdata_scope,
                    quote_type="Price",
                    field="Mid",
                ),
                models.MarketDataKeyRule(
                    key="Fx.CurrencyPair.*",
                    supplier="DataScope",
                    data_scope=marketdata_scope,
                    quote_type="Rate",
                    field="Mid",
                ),
            ],
            suppliers=models.MarketContextSuppliers(
                commodity="DataScope",
                credit="DataScope",
                equity="DataScope",
                fx="DataScope",
                rates="DataScope",
            ),
            options=models.MarketOptions(
                default_supplier="DataScope",
                default_instrument_code_type="Figi",
                default_scope=marketdata_scope,
            ),
        ),
    )

    upsert_configuration_recipe_response = api_factory.build(
        lusid.api.ConfigurationRecipeApi).upsert_configuration_recipe(
            upsert_recipe_request=models.UpsertRecipeRequest(
                configuration_recipe=configuration_recipe))

    # Create the valuation request
    valuation_request = models.ValuationRequest(
        recipe_id=models.ResourceId(scope="User", code="quotes_recipe"),
        metrics=[
            models.AggregateSpec(key="Instrument/default/LusidInstrumentId",
                                 op="Value"),
            models.AggregateSpec(key="Instrument/default/Name", op="Value"),
            models.AggregateSpec(key="Holding/default/Units", op="Sum"),
            models.AggregateSpec(key="Holding/default/Cost", op="Sum"),
            models.AggregateSpec(key="Holding/default/PV", op="Sum"),
            models.AggregateSpec(key="Holding/default/PV", op="Proportion"),
        ],
        group_by=["Instrument/default/LusidInstrumentId"],
        portfolio_entity_ids=[
            models.PortfolioEntityId(
                scope=portfolio_group.scope,
                code=portfolio_group.code,
                portfolio_entity_type="GroupPortfolio",
            )
        ],
        valuation_schedule=models.ValuationSchedule(effective_at=time),
    )

    # Perform a valuation
    response = api_factory.build(lusid.api.AggregationApi).get_valuation(
        valuation_request=valuation_request)

    dataframe = prettyprint.aggregation_responses_generic_df([response])
    dataframe = dataframe.append(dataframe.sum(numeric_only=True),
                                 ignore_index=True)
    return dataframe