-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
31 lines (26 loc) · 1.13 KB
/
app.py
File metadata and controls
31 lines (26 loc) · 1.13 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
import requests
import pandas as pd
import datetime
# Function to fetch stock market data from Yahoo Finance API
def fetch_stock_data(ticker, interval="1d", range_period="1mo"):
url = f"https://query1.finance.yahoo.com/v8/finance/chart/{ticker}?interval={interval}&range={range_period}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
timestamps = data["chart"]["result"][0]["timestamp"]
prices = data["chart"]["result"][0]["indicators"]["quote"][0]["close"]
# Convert timestamps to human-readable dates
dates = [datetime.datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d') for ts in timestamps]
# Create DataFrame
df = pd.DataFrame({"Date": dates, "Closing Price": prices})
return df
else:
print("Failed to fetch stock data.")
return None
# Example usage
if __name__ == "__main__":
ticker = "AAPL" # Apple Inc. stock
stock_data = fetch_stock_data(ticker)
if stock_data is not None:
print(stock_data.head())
stock_data.to_csv("stock_data.csv", index=False) # Save to CSV