-
Notifications
You must be signed in to change notification settings - Fork 1
/
market_data.py
34 lines (31 loc) · 1.05 KB
/
market_data.py
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
import yfinance as yf
def fetch_index_data(symbol):
ticker = yf.Ticker(symbol)
hist = ticker.history(period="1d")
if not hist.empty:
return hist.iloc[-1]
else:
print(f"No data fetched for {symbol}")
return None
def get_index_summary():
indices = {
'^DJI': 'Dow Jones Industrial Average',
'^GSPC': 'S&P 500',
'^IXIC': 'Nasdaq Composite'
}
summary = []
for symbol, name in indices.items():
data = fetch_index_data(symbol)
if data is not None:
print(f"Data fetched for {symbol}: {data}") # Debug information
price = data['Close']
change = data['Close'] - data['Open']
change_pct = (change / data['Open']) * 100
summary.append(f"{name}: Closed {('up' if change > 0 else 'down')} at {price:.2f} points, {change_pct:.2f}% change")
else:
print(f"No data fetched for {symbol}")
return summary
if __name__ == "__main__":
summary = get_index_summary()
for line in summary:
print(line)