How bad is inflation eating your returns?
![](https://cdn.prod.website-files.com/6606adcab6ed6464b674ad71/6661ebbf37d796ae3ec1da41_pyquant-newsletter-61.webp)
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.](https://cdn.prod.website-files.com/6606adcab6ed6464b674ad71/66c8fcca57644a8b6dfb56be_662df5adb2c5579c42454c81_Cover-1024x683.png)
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:
1pip install openbb==4.0.0a0
Once that's installed, open up your Jupyter Notebook and import it.
1from openbb import obb
Getting the data
Next, we load price performance data from the data provider and calculate the nominal returns.
1# Get the nominal price returns for 2022
2stocks_dataframe = obb.stocks.load(
3 "BMWYY",
4 start_date="2022-03-01",
5 end_date="2023-03-01"
6).to_dataframe()
7price_start = stocks_dataframe["close"].iloc[0]
8price_end = stocks_dataframe["close"].iloc[-1]
9
10nominal_returns = (price_end - price_start) / price_start
11print(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.
1# Get the inflation rate in percentage points for 2022
2inflation = obb.economy.cpi(
3 ["united_states"],
4 units="growth_same",
5 start_date="2022-03-01",
6 end_date="2023-03-01",
7 frequency="monthly"
8).to_dataframe()
9
10# We're only asking for one country, we can drop the country level index
11inflation.columns = inflation.columns.droplevel(0)
12inflation = inflation["value"].iloc[-1] / 100
13print(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.
1# Calculate inflation adjusted (real) returns
2real_returns = (1 + nominal_returns) / (1 + inflation) - 1
3print(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.
![Man with glasses and a wristwatch, wearing a white shirt, looking thoughtfully at a laptop with a data screen in the background.](https://cdn.prod.website-files.com/6604815bfffc39e4941bd158/66070439083cb3ca5c1d1c34_cta%20image.webp)