forked from Kowsi/SP500-Performance-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_data_loading.py
More file actions
164 lines (128 loc) · 5.37 KB
/
Copy pathtest_data_loading.py
File metadata and controls
164 lines (128 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
"""
Test script to verify data loading functionality
"""
import sys
import pandas as pd
import yfinance as yf
import datetime
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def test_stock_data_loading():
"""Test the stock data loading functionality"""
print("🧪 Testing Stock Data Loading...")
# Test parameters
test_ticker = "AAPL"
years_back = 2
frequency = '1d'
try:
# Use dynamic date range
end_date = datetime.datetime.now()
start_date = datetime.datetime(end_date.year - years_back, end_date.month, end_date.day)
# Format dates for yfinance
start = start_date.strftime('%Y-%m-%d')
end = end_date.strftime('%Y-%m-%d')
print(f"📅 Fetching {test_ticker} data from {start} to {end} with {frequency} frequency...")
# Download data
df = yf.download(test_ticker, start=start, end=end, interval=frequency)
if df.empty:
print(f"❌ No data returned for {test_ticker}")
return False
print(f"✅ Successfully fetched {test_ticker} data:")
print(f" 📊 Shape: {df.shape}")
print(f" 📈 Date range: {df.index.min()} to {df.index.max()}")
print(f" 💰 Price range: ${df['Close'].min():.2f} - ${df['Close'].max():.2f}")
# Process data like in the app
df = df.reset_index()[['Date', 'Close']]
df = df.rename(columns={'Close': test_ticker})
print(f" 🔄 Processed data shape: {df.shape}")
print(f" 📋 Columns: {list(df.columns)}")
print(f" 🎯 Data points: {len(df):,}")
# Show sample data
print("\n📋 Sample data:")
print(df.head(3).to_string(index=False))
print("...")
print(df.tail(3).to_string(index=False))
return True
except Exception as e:
print(f"❌ Error fetching data for {test_ticker}: {e}")
return False
def test_multiple_stocks():
"""Test loading multiple stocks"""
print("\n🧪 Testing Multiple Stock Loading...")
test_tickers = ["AAPL", "MSFT", "GOOGL"]
years_back = 1
frequency = '1d'
results = {}
for ticker in test_tickers:
try:
end_date = datetime.datetime.now()
start_date = datetime.datetime(end_date.year - years_back, end_date.month, end_date.day)
start = start_date.strftime('%Y-%m-%d')
end = end_date.strftime('%Y-%m-%d')
df = yf.download(ticker, start=start, end=end, interval=frequency)
if not df.empty:
df = df.reset_index()[['Date', 'Close']]
df = df.rename(columns={'Close': ticker})
results[ticker] = df
print(f"✅ {ticker}: {len(df):,} data points")
else:
print(f"❌ {ticker}: No data")
except Exception as e:
print(f"❌ {ticker}: Error - {e}")
if results:
total_points = sum(len(df) for df in results.values())
print(f"\n📊 Summary:")
print(f" ✅ Successfully loaded: {len(results)}/{len(test_tickers)} stocks")
print(f" 🎯 Total data points: {total_points:,}")
# Test different frequencies
print(f"\n🔄 Testing different frequencies for AAPL...")
frequencies = ['1d', '1wk', '1mo']
for freq in frequencies:
try:
df = yf.download("AAPL", start=start, end=end, interval=freq)
if not df.empty:
print(f" {freq}: {len(df):,} data points")
else:
print(f" {freq}: No data")
except Exception as e:
print(f" {freq}: Error - {e}")
return True
else:
print("❌ Failed to load any stock data")
return False
def test_sp500_file():
"""Test loading S&P 500 characteristics file"""
print("\n🧪 Testing S&P 500 File Loading...")
try:
sp = pd.read_csv('SP_500/Characteristics.csv', index_col='Ticker')
print(f"✅ Successfully loaded S&P 500 data:")
print(f" 📊 Shape: {sp.shape}")
print(f" 📋 Columns: {list(sp.columns)}")
# Check sectors
sectors = sp.Sector.dropna().unique()
print(f" 🏢 Sectors: {len(sectors)}")
print(f" 📈 Sample tickers: {sp.index[:10].tolist()}")
return True
except Exception as e:
print(f"❌ Error loading S&P 500 file: {e}")
return False
if __name__ == "__main__":
print("🚀 Starting Data Loading Tests...\n")
# Run tests
test1 = test_sp500_file()
test2 = test_stock_data_loading()
test3 = test_multiple_stocks()
# Summary
print(f"\n📋 Test Results:")
print(f" S&P 500 File: {'✅ PASS' if test1 else '❌ FAIL'}")
print(f" Single Stock: {'✅ PASS' if test2 else '❌ FAIL'}")
print(f" Multiple Stocks: {'✅ PASS' if test3 else '❌ FAIL'}")
if all([test1, test2, test3]):
print(f"\n🎉 All tests passed! The dashboard should work correctly.")
sys.exit(0)
else:
print(f"\n⚠️ Some tests failed. Check the errors above.")
sys.exit(1)