Support

Handling multiple stock selections

Overview

The following example demonstrate how to use the Reference client to retrieve all relevant adjustment factors for corporate actions affecting a selection of stocks. These factors are then applied to daily close prices (using UTC date boundaries) to create a smoother time series that accounts for the impact of the capital events.

See also
See also

Adjustment factors dataset guide for further details and considerations for applying adjustment factors.

Procedure

We'll use the same procedure as outlined in the Applying adjustment factors tutorial. The only difference is that we’ll now apply the factors to multiple listings simultaneously.

Selection

The selected stocks are prominent representatives of the tech sector, chosen to illustrate various scenarios where adjustment factors are applied:

  • Microsoft Corp. (MSFT): Pays regular quarterly cash dividends.
  • Apple Inc. (AAPL): Pays regular quarterly cash dividends.
  • Tesla Inc. (TSLA): Underwent two forward stock splits on 2020-08-31 and 2022-08-25.
  • Amazon.com Inc. (AMZN): Underwent a forward stock split on 2022-06-06, and issued a cash dividend on 2022-12-27.
import databento as db
import matplotlib.pyplot as plt
import pandas as pd

# Set parameters
dataset = "XNAS.ITCH"
symbols = ["MSFT", "AAPL", "TSLA", "AMZN"]
start = "2018-06"
end = "2025-04"

# Create a reference client
ref_client = db.Reference(key="$YOUR_API_KEY")

# Request adjustment factors data for the relevant time range
df_factors = ref_client.adjustment_factors.get_range(
    symbols=symbols,
    start=start,
    end=end,
    countries=["US"],
)

# Filter listings on Nasdaq/NGS (Global Select Market)
df_factors = df_factors[df_factors["operating_mic"] == "XNGS"]

# Filter factors with (A)pply status
df_factors = df_factors[df_factors["status"] == "A"]

# Filter option 1 (default) for all factors
df_factors = df_factors[(df_factors["option"] == 1)]

# Convert the ex_dates index to UTC timestamps
df_factors.index = pd.to_datetime(df_factors.index, utc=True)

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

# Request daily bars data to obtain close prices
data = hist_client.timeseries.get_range(
    dataset=dataset,
    schema="ohlcv-1d",
    symbols=symbols,
    start=start,
    end=end,
)
df_bars = data.to_df()


def apply_factors(df_adjusted, df_bars, factors_symbol, symbol):
    for ex_date, factor in zip(factors_symbol.index, factors_symbol["factor"]):
        mask = (df_adjusted.index < ex_date) & (df_bars["symbol"] == symbol)
        df_adjusted.loc[mask, "close_adjusted"] *= factor


# Apply the adjustment factors:
# Create a new dataframe for the adjusted prices
df_adjusted = pd.DataFrame(index=df_bars.index)
df_adjusted["symbol"] = df_bars["symbol"]
df_adjusted["close_adjusted"] = df_bars["close"].copy()

for symbol in symbols:
    # Filter the bars and factors for the current symbol
    bars_symbol = df_bars[df_bars["symbol"] == symbol]
    factors_symbol = df_factors[df_factors["symbol"] == symbol]

    # Apply the factors using the `apply_factors` function defined above
    apply_factors(df_adjusted, df_bars, factors_symbol, symbol)

# Plot the results
for symbol in symbols:
    df_symbol = df_adjusted[df_adjusted["symbol"] == symbol]
    plt.plot(df_symbol.index, df_symbol["close_adjusted"], label=f"{symbol} adjusted")

plt.legend()
plt.xlabel("Date (UTC)")
plt.ylabel("Daily Close Price (USD)")
plt.title("Adjusted Close Prices for Selected Stocks")
plt.show()

Multiple adjusted close