The AI rally looks like the dot-com bubble. The companies do not.
Back to Tutorials
techTutorialintermediate

The AI rally looks like the dot-com bubble. The companies do not.

April 25, 20266 views4 min read

Learn to analyze stock market valuations using Python to compare current AI stock market conditions with the dot-com bubble using the CAPE ratio.

Introduction

In the wake of the AI boom, investors are drawing comparisons between current market conditions and the dot-com bubble of 2000. This tutorial will teach you how to analyze stock market data using Python to evaluate whether AI stocks are showing bubble-like characteristics. We'll build a tool that calculates the Cyclically Adjusted Price-to-Earnings (CAPE) ratio and compares it to historical data, helping you understand market valuation trends.

Prerequisites

  • Basic Python knowledge
  • Installed Python 3.7+ with pandas, yfinance, and matplotlib libraries
  • Understanding of stock market fundamentals

Step-by-Step Instructions

Step 1: Set Up Your Python Environment

First, we need to install the required libraries. Open your terminal or command prompt and run:

pip install pandas yfinance matplotlib

This installs the necessary packages to fetch stock data and perform calculations.

Step 2: Import Required Libraries

Let's create our main Python script and import the necessary modules:

import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
import numpy as np

These libraries provide data fetching capabilities (yfinance), data manipulation (pandas), and visualization (matplotlib).

Step 3: Fetch S&P 500 Data

We need to get the S&P 500 index data to calculate the CAPE ratio:

# Fetch S&P 500 data
sp500 = yf.download('^GSPC', start='2000-01-01', end='2024-12-31')
print(sp500.head())

This retrieves historical data for the S&P 500 index from 2000 to 2024, which is essential for historical comparison.

Step 4: Calculate Earnings Data

The CAPE ratio requires both price and earnings data. We need to fetch earnings data for the S&P 500:

# Fetch earnings data for S&P 500
# Note: yfinance doesn't directly provide earnings, so we'll simulate
# In practice, you'd use a financial data provider like Quandl or Alpha Vantage
sp500['Earnings'] = sp500['Close'].rolling(window=10).mean() * 0.08  # Simulated 8% earnings
sp500['CAPE'] = sp500['Close'] / sp500['Earnings']

We're simulating earnings data since direct access to comprehensive earnings data requires premium services. This approach demonstrates the calculation method.

Step 5: Calculate the 10-Year Average Earnings

The CAPE ratio uses a 10-year average of earnings to smooth out business cycles:

# Calculate 10-year average earnings
sp500['Earnings_10Y'] = sp500['Earnings'].rolling(window=252*10).mean()
sp500['CAPE_10Y'] = sp500['Close'] / sp500['Earnings_10Y']

This calculation smooths out short-term earnings volatility to better assess long-term valuation.

Step 6: Visualize the CAPE Ratio

Now let's create a chart to visualize how the CAPE ratio compares to historical values:

# Create visualization
plt.figure(figsize=(12, 6))
plt.plot(sp500.index, sp500['CAPE_10Y'], label='CAPE Ratio')
plt.axhline(y=44.19, color='red', linestyle='--', label='2000 Dot-Com Peak')
plt.axhline(y=38, color='orange', linestyle='--', label='Current CAPE')
plt.title('S&P 500 CAPE Ratio vs Historical Values')
plt.xlabel('Date')
plt.ylabel('CAPE Ratio')
plt.legend()
plt.grid(True)
plt.show()

This visualization helps us understand how current market valuations compare to historical peaks, including the dot-com bubble.

Step 7: Analyze Current Market Conditions

Let's add logic to determine if current conditions resemble the dot-com bubble:

# Get current CAPE ratio
latest_cape = sp500['CAPE_10Y'].iloc[-1]
print(f'Current CAPE Ratio: {latest_cape:.2f}')

# Compare to 2000 peak
if latest_cape > 44.19:
    print('Warning: Current CAPE ratio exceeds 2000 dot-com peak')
elif latest_cape > 38:
    print('Current CAPE ratio is elevated but not at dot-com bubble levels')
else:
    print('Current CAPE ratio is within historical norms')

This comparison helps investors understand whether current market conditions are approaching bubble-like valuations.

Step 8: Add Additional Market Indicators

Enhance our analysis by adding more market indicators:

# Add moving averages for trend analysis
sp500['MA_50'] = sp500['Close'].rolling(window=50).mean()
sp500['MA_200'] = sp500['Close'].rolling(window=200).mean()

# Calculate volatility
sp500['Volatility'] = sp500['Close'].pct_change().rolling(window=20).std()

# Display recent analysis
recent_data = sp500.tail(252)  # Last year of data
print('Recent Market Analysis:')
print(f'Average CAPE: {recent_data["CAPE_10Y"].mean():.2f}')
print(f'Max CAPE: {recent_data["CAPE_10Y"].max():.2f}')
print(f'Current Volatility: {sp500["Volatility"].iloc[-1]:.4f}')

This enhanced analysis provides a more comprehensive view of market conditions, including trends and volatility.

Step 9: Export Analysis Results

Save our findings for future reference:

# Export analysis to CSV
sp500[['Close', 'CAPE_10Y', 'MA_50', 'MA_200', 'Volatility']].to_csv('sp500_analysis.csv')
print('Analysis exported to sp500_analysis.csv')

Storing results allows for future comparison and detailed backtesting of market conditions.

Step 10: Create a Summary Report

Generate a final summary of our analysis:

# Generate summary report
summary = {
    'Current CAPE': f'{latest_cape:.2f}',
    'Dot-Com Peak Comparison': 'Exceeds' if latest_cape > 44.19 else 'Below',
    'Market Status': 'Bubble-like' if latest_cape > 44.19 else 'Normal' if latest_cape > 38 else 'Safe',
    'Volatility': f'{sp500["Volatility"].iloc[-1]:.4f}',
    'Trend': 'Bullish' if sp500['MA_50'].iloc[-1] > sp500['MA_200'].iloc[-1] else 'Bearish'
}

print('\n=== MARKET ANALYSIS SUMMARY ===')
for key, value in summary.items():
    print(f'{key}: {value}')

This summary provides a quick overview of market conditions, helping investors make informed decisions.

Summary

This tutorial demonstrated how to build a market analysis tool that calculates and visualizes the CAPE ratio for the S&P 500 index. By comparing current valuations to historical peaks including the dot-com bubble, investors can better understand market conditions. The tool provides insights into whether current market conditions resemble historical bubbles, helping guide investment decisions. While this example uses simulated earnings data for demonstration, real-world applications would require premium financial data services for accurate earnings figures.

Source: TNW Neural

Related Articles