Support

End-of-day pricing and portfolio valuation

Overview

This example shows how to use the Historical client to calculate the end-of-day (EOD) value of a hypothetical portfolio. To do this we will request the closing price for each symbol on a given date and use this price to determine the value of our portfolio.

OHLCV-1d schema

We'll demonstrate this example using the OHLCV-1d schema. The OHLCV family of schemas contain the opening, high, low and closing prices as well as the aggregated volume of trades within a time interval. Since we are interested in end-of-day evaluation, we'll use an interval of one day, which is specified by the suffix -1d.

Many users will prefer to use the official daily settlement prices found in the statistics schema for this purpose.

Example

import databento as db

# A hypothetical portfolio mapping symbols to a quantity of shares
portfolio = {
    "AAPL": 200,
    "AMZN": 200,
    "GOOG": 100,
    "META": 100,
    "NFLX": 114,
}

# Create a historical client
client = db.Historical("YOUR_API_KEY")

# Request OHLCV-1d data
data = client.timeseries.get_range(
    dataset="XNAS.ITCH",
    start="2022-09-19",
    symbols=list(portfolio.keys()),
    stype_in="raw_symbol",
    schema="ohlcv-1d",
)

# Convert to DataFrame
eod_data = data.to_df()
eod_data = eod_data.set_index("symbol")

# Sum the products of the close prices and portfolio quantities
eod_evaluation = sum(
    eod_data.at[symbol, "close"] * quantity for symbol, quantity in portfolio.items()
)
print(f"${eod_evaluation:,.2f}")

Result

$109,235.90