LUSID API


Keywords
OpenAPI, OpenAPI-Generator, LUSID, API, lusid-sdk, bi-temporal, data-platform, finbourne, fintech, python
License
MIT
Install
pip install lusid-sdk==2.1.65

Documentation

LUSID® Python SDK

This is the Python SDK for LUSID by FINBOURNE, a bi-temporal investment management data platform with portfolio accounting capabilities. To use it you'll need a LUSID account. Sign up for free at lusid.com

LUSID_by_Finbourne

Installation

The PyPi package for the LUSID SDK can installed using the following:

pip install lusid-sdk

For more details on API endpoint categories, see What is the LUSID feature release lifecycle?. To find out which category an API endpoint falls into, see LUSID API Documentation.

Usage

Create API Client

First, import the following LUSID modules:

import lusid
import lusid.models as models
from lusid import (
    ApiClientFactory,
    EnvironmentVariablesConfigurationLoader,
    SecretsFileConfigurationLoader,
    ArgsConfigurationLoader
)

And construct the API factory:

secrets_file_path = "/path/to/secrets.json"
config_loaders = [
	EnvironmentVariablesConfigurationLoader(),
	SecretsFileConfigurationLoader(api_secrets_file=secrets_path)
]
api_client_factory = ApiClientFactory(config_loaders=config_loaders)

Accessing the LUSID API endpoints requires an authenticated request. An authentication token can be obtained following the instructions Getting started with the LUSID API and SDKs

Now that the API client is ready, you are ready to use the various API endpoints. You can list all the API endpoints by running the following:

[api for api in dir(lusid.api) if "API" in api]

An API endpoint can be constructed by calling api_factory.build(lusid.api.<className>) for any of the returned classes.

Code Samples

Before running the examples, import the following modules:

import uuid
import datetime
import pytz

The examples below should be run in order, as they assume that the preceding code has been executed.

Create portfolio

tx_portfolios_api = api_factory.build(lusid.api.TransactionPortfoliosApi)

scope = "GettingStartedScope"
guid = uuid.uuid4()

portfolio_request = models.CreateTransactionPortfolioRequest(
    display_name=f"Portfolio-{guid}",
    code=f"Id-{guid}",
    base_currency="GBP",
    created=datetime.datetime(2021, 3, 20, tzinfo=pytz.utc).isoformat()
)
portfolio = await tx_portfolios_api.create_portfolio(scope, create_transaction_portfolio_request=portfolio_request)
portfolio_code = portfolio.id.code
print("Porfolio Code:", portfolio_code)

Upsert instruments

instruments_api = api_factory.build(lusid.api.InstrumentsApi)

# FIGI is from https://www.openfigi.com/id/BBG000C6K6G9
figis_to_create = {
    "BBG000C6K6G9": models.InstrumentDefinition(name="VODAFONE GROUP PLC",
        identifiers={"Figi": models.InstrumentIdValue(value="BBG000C6K6G9")}
    )
}

await instruments_api.upsert_instruments(request_body=figis_to_create)
    

Get instruments

instruments_response = await instruments_api.get_instruments(
    identifier_type="Figi", request_body=list(figis_to_create.keys()))
name_to_luid = {
    value.name: value.lusid_instrument_id
    for _, value in instruments_response.values.items()
}
luid_to_name = {v: k for k, v in name_to_luid.items()}

Upsert transactions

tx_portfolios_api = api_factory.build(lusid.api.TransactionPortfoliosApi)

tx1 = models.TransactionRequest(
    transaction_id=f"Transaction-{uuid.uuid4()}",
    type="StockIn",
    instrument_identifiers={"Instrument/default/LusidInstrumentId": name_to_luid["VODAFONE GROUP PLC"]},
    transaction_date=datetime.datetime(2021, 3, 27, tzinfo=pytz.utc).isoformat(),
    settlement_date=datetime.datetime(2021, 3, 28, tzinfo=pytz.utc).isoformat(),
    units=100,
    transaction_price=models.TransactionPrice(price=103),
    total_consideration=models.CurrencyAndAmount(amount=103 * 100, currency="GBP"),
    source="Broker"
)

await tx_portfolios_api.upsert_transactions(scope=scope, code=portfolio_code, transaction_request=[tx1])

Get holdings

tx_portfolios_api = api_factory.build(lusid.api.TransactionPortfoliosApi)

holdings_response = await tx_portfolios_api.get_holdings(
    scope=scope, code=portfolio_code, property_keys=["Instrument/default/Name"]).values

print("Holdings:")
for holding in holdings_response:
    print(luid_to_name[holding.instrument_uid], holding.units, holding.cost.amount)

Manually building the SDK

A pre-generated version of the latest SDK is included in the sdk folder. This is based on the OpenAPI specification specification named lusid.json in the root folder. The most up to date version of the OpenAPI specification can be downloaded from api.lusid.com/swagger/v0/swagger.json.

If you want to generate the Python SDK locally from the FINBOURNE OpenAPI specification, see github.com/finbourne/lusid-sdk-generator-python.

Build status

branch status
master PyPI run-sdk-tests Quality Gate Status
develop run-sdk-tests