Example #1
0
def create_instrument_quotes(quotes_effective_date, today, instrument_prices,
                             analyst_scope_code, api_factory):
    # Create prices via instrument, analytic
    # Set our quotes effective dates
    now = datetime.now(pytz.UTC).replace(hour=0,
                                         minute=0,
                                         second=0,
                                         microsecond=0)
    quotes_effective_date = now - timedelta(days=3)
    today = now

    instrument_quotes = {}

    # Create prices for all instruments except cash
    for index, instrument in instrument_prices.iterrows():

        if 'Cash' in instrument['instrument_name']:
            continue

        # Get our Lusid Instrument Id
        luid = api_factory.build(lusid.api.InstrumentsApi).get_instrument(
            identifier_type='Figi',
            identifier=instrument['figi']).lusid_instrument_id

        instrument_quotes[
            luid + str(quotes_effective_date)] = models.UpsertQuoteRequest(
                quote_id=models.QuoteId(quote_series_id=models.QuoteSeriesId(
                    provider='DataScope',
                    instrument_id=luid,
                    instrument_id_type='LusidInstrumentId',
                    quote_type='Price',
                    field='Mid'),
                                        effective_at=quotes_effective_date),
                metric_value=models.MetricValue(
                    value=instrument["price_original"],
                    unit=instrument["currency"]),
                lineage='InternalSystem')

        instrument_quotes[luid + str(today)] = models.UpsertQuoteRequest(
            quote_id=models.QuoteId(quote_series_id=models.QuoteSeriesId(
                provider='DataScope',
                instrument_id=luid,
                instrument_id_type='LusidInstrumentId',
                quote_type='Price',
                field='Mid'),
                                    effective_at=today),
            metric_value=models.MetricValue(value=instrument["price_current"],
                                            unit=instrument["currency"]),
            lineage='InternalSystem')

    response = api_factory.build(lusid.api.QuotesApi).upsert_quotes(
        scope=analyst_scope_code, request_body=instrument_quotes)

    prettyprint.upsert_quotes_response(response)
Example #2
0
def upsert_quotes(
    api_factory,
    scope,
    data_frame,
    instrument_identifier_mapping,
    instrument_identifier_heirarchy,
    required_mapping,
):
    """
    This function takes quotes from a data_frame and upserts them into LUSID
    
    param (lusid.utilities.ClientApiFactory) api_factory: The LUSID api factory to use
    param (str) scope: The LUSID scope to upsert the quotes into
    param (Pandas DataFrame) data_frame: The DataFrame that the quotes are in
    param (dict) instrument_identifier_mapping : The dictionary with the instrument identifier mapping between LUSID and the dataframe
    param (list[str]) instrument_identifier_heirarchy : The heirarchy to use for the LUSID instrument identifiers when upserting quotes
    param (dict) required_mapping: The mapping of the LUSID required quote fields to the dataframe fields
    
    
    returns (Pandas DataFrame): The succesfully upserted quotes
    """

    # Initialise an empty instrument quotes list to hold the quotes
    instrument_quotes = {}

    quote_type_values = {
        "mid_price": {
            "quote_type": "Price",
            "price_side": "Mid",
            "value": "price",
        },
        "mid_rate": {
            "quote_type": "Rate",
            "price_side": "Mid",
            "value": "rate",
        },
    }

    # Iterate over the quotes
    for index, quote in data_frame.iterrows():

        quote_type = quote_type_values[quote[
            required_mapping["quote_type"]]]["quote_type"]
        field = quote_type_values[quote[
            required_mapping["quote_type"]]]["price_side"]

        for identifier in instrument_identifier_heirarchy:

            identifier_value = quote[instrument_identifier_mapping[
                "identifier_mapping"][identifier]]

            if (identifier == "CurrencyPair") and (len(identifier_value) == 6):
                identifier_value = identifier_value[:
                                                    3] + "/" + identifier_value[
                                                        3:]

            if not pd.isna(identifier_value):
                break

        # Add the quote to the list of upsert quote requests
        effective_date = quote[required_mapping["effective_at"]]

        instrument_quotes[identifier + "_" + identifier_value + "_" +
                          effective_date] = models.UpsertQuoteRequest(
                              quote_id=models.QuoteId(
                                  quote_series_id=models.QuoteSeriesId(
                                      provider="DataScope",
                                      instrument_id=identifier_value,
                                      instrument_id_type=identifier,
                                      quote_type=quote_type,
                                      field=field,
                                  ),
                                  effective_at=effective_date,
                              ),
                              metric_value=models.MetricValue(
                                  value=quote[required_mapping["value"]],
                                  unit=quote[required_mapping["currency"]],
                              ),
                              lineage="InternalSystem",
                          )

    # Upsert the quotes into LUSID
    response = api_factory.build(lusid.api.QuotesApi).upsert_quotes(
        scope=scope, request_body=instrument_quotes)

    # Pretty print the response
    # prettyprint.upsert_quotes_response(response)
    return prettyprint.upsert_quotes_response(response)