How U.S. Elections Impact S&P 500 and Bitcoin Prices: Python for Investors (MatPlotLib and yfinance)

In the financial world, everything is interconnected. Geopolitical events often lead to significant shifts in financial assets. For instance, U.S. elections can impact traditional assets like the S&P 500, as well as cryptocurrencies like Bitcoin. Analyzing election-related dates and their effect on these assets offers valuable insight into market behavior and investor sentiment during politically sensitive periods. In this post, we’ll explore how Python can be used to visualize these impacts, highlighting key election dates and tracking their influence on both the S&P 500 and Bitcoin.

Plan

To do this, we plan to create a line chart in Python to track the daily closing prices of these assets. This chart will highlight key election dates, allowing us to observe any patterns or trends that may emerge around these events.

Defining relevant dates

Arbitrarily I chose some dates to work with.

Key Election Dates and Market Relevance

  • August 1, 2024 — Start of the election season. Campaigns intensify, and markets begin to react to early candidate proposals.
  • September 1, 2024 — Beginning of primaries and first debates. Markets respond to the emergence of key candidates.
  • September 29, 2024 — Final debates. Significant market movements often occur based on the candidates’ performances.
  • October 1, 2024 — Release of candidates’ fiscal plans. Markets start reacting to proposed policies on taxes and spending.
  • October 15, 2024 — Start of the final phase before the election. Markets adjust expectations based on campaign developments.
  • October 28, 2024 — Peak of the campaign. Last-minute announcements or changes can create strong reactions in the market.
  • November 1, 2024 — Final days of the campaign. Markets anticipate the likely election outcome and potential policy shifts.
  • November 5, 2024 — Election Day. Immediate market reactions occur as early results start coming in.
  • November 6, 2024 — The day after the election. Markets process the results, anticipating the new administration’s potential impact.

Once we have defined the dates, let’s start our code in Python

Code

First, we need to fetch the relevant data. I will use the yfinance library for fetching the S&P 500 and Bitcoin price change over time. I will also define some important libraries we will use later on

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

# Download S&P 500 data for months before and after the U.S. Election
sp500_data = yf.download('^GSPC', start='2024-07-01', end='2024-12-01')

bitcoin_data = yf.download('BTC-USD', start='2024-07-01', end='2024-12-01')

Once this is defined, let’s define in Python the dates I sent before

dates = [ "2024-08-01",  # Start of the election season. Campaigns begin to heat up, market reacts to early candidate proposals.
     "2024-09-01",  # Start of primaries and first debates. Market starts reacting to the selection of key candidates.
     "2024-09-29",  # Final debates before the election. Significant market movements due to performance of candidates in debates.
     "2024-10-01",  # Release of candidates' fiscal plans. Market begins to react to proposed policies on taxes and spending.
     "2024-10-15",  # Start of the final phase before the election. Market adjusts expectations based on key campaign developments.
     "2024-10-28",  # Peak of the campaign. Last-minute announcements or changes can create strong reactions in the market.
     "2024-11-01",  # Final days of the election campaign. Market anticipates the likely outcome and reactions to potential policy shifts.
     "2024-11-05",  # Election Day. Immediate market reactions to early results and expectations about the winner.
     "2024-11-06"]  # Day after the election. Market processes election results and anticipates the new administration's impact.

dates = pd.to_datetime(dates)

To make it easy, let’s work one asset at a time. Let’s start with the S&P 500:

stock_data = sp500_data

# Plot the adjusted close price for the S&P 500
plt.figure(figsize=(10, 6))
plt.plot(stock_data['Adj Close'], label="S&P 500 Adjusted Close", color='blue')

for idx, d in enumerate(dates):
    if idx == 0:
        plt.axvline(x=d, color='red', linestyle='--', label='Relevant day for election')
    else:
        plt.axvline(x=d, color='red', linestyle='--')

# Title and labels
plt.title('S&P 500 Performance around U.S. Election (2024)', fontsize=14)
plt.xlabel('Date', fontsize=12)
plt.ylabel('Adjusted Close Price (USD)', fontsize=12)
plt.legend()
plt.grid(True)
plt.show()

After running this code, we have a good chart to start analyzing:

We observed that the asset’s price increased steadily as key election dates approached, with this trend continuing even after Election Day. This pattern suggests that the market may have reacted positively to Trump’s victory, potentially viewing it as favorable for economic policy and investor confidence.

Now, let’s examine how the Bitcoin/USD price evolved.

stock_data = bitcoin_data

# Plot the adjusted close price for the Bitcoin
plt.figure(figsize=(10, 6))
plt.plot(stock_data['Adj Close'], label="Bitcoin Adjusted Close", color='blue')

for idx, d in enumerate(dates):
    if idx == 0:
        plt.axvline(x=d, color='red', linestyle='--', label='Relevant day for election')
    else:
        plt.axvline(x=d, color='red', linestyle='--')

# Title and labels
plt.title('Bitcoin Performance around U.S. Election (2024)', fontsize=14)
plt.xlabel('Date', fontsize=12)
plt.ylabel('Adjusted Close Price (USD)', fontsize=12)
plt.legend()
plt.grid(True)
plt.show()

After running this code, we have another chart to start analyzing:

As we can see, Bitcoin’s price also aligns with key election dates, showing a notable spike immediately after the election. This surge likely reflects investor optimism and enthusiasm, driven by Trump’s favorable stance on Bitcoin adoption within the U.S. The market’s positive response underscores the potential impact of supportive policies on the crypto space.

Conclusion

From this brief study, we can infer that elections play a notable role in intriguingly driving asset price fluctuations. Investors who can code to support this type of analysis gain a competitive edge, positioning themselves to anticipate and respond to key market events ahead of time.

That’s it for this post! I hope you like it.

Let me know in the comments what would like to see next!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top