-
-
Save amalbansode/3393d88090ea042646cca2eb8fca16a3 to your computer and use it in GitHub Desktop.
spring scaries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import yfinance as yf | |
| import pandas as pd | |
| from datetime import datetime, timedelta | |
| from scipy import stats | |
| def get_um_break_dates(year): | |
| first_day_ny = pd.Timestamp(year, 1, 1) | |
| # Break starts on the Saturday after seventh week | |
| break_start_sat = first_day_ny + timedelta(weeks=7) | |
| while break_start_sat.weekday() != 5: | |
| break_start_sat += timedelta(days=1) | |
| day_before_break = break_start_sat - timedelta(days=1) | |
| # This is a two week window to account for UM sliding its start date around | |
| # Could narrow this if we have a programmatic way to tell when semester starts | |
| # Swap to 9 for 1 week. | |
| day_after_break = break_start_sat + timedelta(days=16) | |
| return day_before_break, day_after_break | |
| def analyze_sp500_swing(ticker, years): | |
| ticker = yf.Ticker(ticker) | |
| results = [] | |
| for year in years: | |
| start, end = get_um_break_dates(year) | |
| # Download data for the range (add a buffer to ensure we catch the end day) | |
| data = ticker.history(start=start.strftime('%Y-%m-%d'), | |
| end=(end + timedelta(days=1)).strftime('%Y-%m-%d'), | |
| interval="1d") | |
| try: | |
| # Extract specific prices | |
| start_price = data.iloc[0]['Close'] | |
| end_price = data.iloc[-1]['Open'] | |
| pct_change = ((end_price - start_price) / start_price) * 100 | |
| results.append({ | |
| "Year": year, | |
| "Start Date": start.date(), | |
| "End Date": end.date(), | |
| "Start Price": round(start_price, 2), | |
| "End Price": round(end_price, 2), | |
| "Change %": round(pct_change, 2) | |
| }) | |
| except IndexError as e: | |
| # This happens if the ticker wasn't listed or no data? | |
| continue | |
| except KeyError as e: | |
| # This happens if the market was closed on those dates? | |
| continue | |
| return pd.DataFrame(results) | |
| tickers = { | |
| "^VIX": "Volatility Index", | |
| } | |
| for ticker, desc in tickers.items(): | |
| skip_years = [2007, 2020] | |
| df = analyze_sp500_swing(ticker, [i for i in range(1990, 2027) if i not in skip_years]) | |
| print(df.to_string(index=False)) | |
| median_change = df['Change %'].median() | |
| t_stat, p_value = stats.ttest_1samp(df['Change %'], 0) | |
| print(f"{ticker} ({desc}) Median Change: {median_change:,.2f}%") | |
| print(f"T-Statistic: {t_stat:.4f}") | |
| print(f"P-Value: {p_value:.4f}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment