May cohort is now open: How to secure your spot:

How bad is inflation eating your returns?

When inflation is high, central banks all over the world increase their rates to get it under control. Higher rates makes everything more expensive and erodes returns on your assets.

When interest rates are high, it’s important to incorporate the impact into your portfolio return calculations.

In today’s guest post, we’ll use the brand new OpenBB Platform to compute inflation-adjusted returns.

OpenBB is free! Sign up for free code, exclusive tutorials, and a centralized way to configure API keys, update chart colors, and more.

Let’s go!

How bad is inflation eating your returns?

How bad is inflation eating your returns? When inflation is high, central banks all over the world increase their rates to get it under control.

This example will show you how to easily integrate asset price data from a stock data provider and information about inflation from the Federal Reserve Economic Data (FRED) through the OpenBB Platform.

With OpenBB Platform, fetching and harmonizing data from multiple sources has never been easier.

We’ll use OpenBB’s Python API to pull the price for an asset from a data provider. Then, we’ll obtain CPI growth information, and using this data, we’ll calculate the inflation-adjusted returns on an asset.

Here’s what we’ll use:

• Asset: ADRs of BMW Group, the legendary German automaker with symbol: BMWYY.

• Provider: Financial Modelling Prep, a financial market data provider

Provider: FRED, the Federal Reserve Economic Data

Imports and set up

We’ll use the OpenBB pre-release for today’s newsletter. You can use pip to easily install it:

pip install openbb==4.0.0a0

Once that’s installed, open up your Jupyter Notebook and import it.

from openbb import obb

Getting the data

Next, we load price performance data from the data provider and calculate the nominal returns.

# Get the nominal price returns for 2022
stocks_dataframe = obb.stocks.load(
    "BMWYY",
    start_date="2022-03-01",
    end_date="2023-03-01"
).to_dataframe()
price_start = stocks_dataframe["close"].iloc[0]
price_end = stocks_dataframe["close"].iloc[-1]

nominal_returns = (price_end - price_start) / price_start
print(f"Nominal price returns: {nominal_returns}")

This code uses the OpenBB platform to download stock data for symbol BMWYY between March 2022 and 2023. We then slice the resulting DataFrame to get the opening and closing price on the first and last days of the range. Finally, we compute the nominal returns which is around 11.9%.

Next we load the inflation data. FRED’s API allows us to get CPI growth compared to the same period last year.

# Get the inflation rate in percentage points for 2022
inflation = obb.economy.cpi(
    ["united_states"], 
    units="growth_same", 
    start_date="2022-03-01", 
    end_date="2023-03-01", 
    frequency="monthly"
).to_dataframe()

# We're only asking for one country, we can drop the country level index
inflation.columns = inflation.columns.droplevel(0)
inflation = inflation["value"].iloc[-1] / 100
print(f"Inflation: {inflation}")

We follow similar steps using OpenBB. We do some DataFrame manipulation and get the last CPI data point from the DataFrame. Running this code should give us something around 4.9%.

Finally, let’s calculate the real returns, adjusting for inflation.

# Calculate inflation adjusted (real) returns
real_returns = (1 + nominal_returns) / (1 + inflation) - 1
print(f"Real returns: {real_returns}")

To calculate the real return, divide 1 plus nominal returns by 1 plus inflation and subtract 1. Running this should give us something around 6.6%—a lot worse than the 11.9% nominal return!

Next steps

To experiment further, run the same code using the shares of BMW that are traded in Frankfurt (Symbol: BMW.DE) and the inflation data for Germany.

How about calculating total returns? Hint: You can get information on the historical dividends from the obb.sotcks.fa.divs command.