Spaces:
Running
Running
Create data_fetcher/yfinance_client.py
Browse files
data_fetcher/data_fetcher/yfinance_client.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import yfinance as yf
|
2 |
+
|
3 |
+
def fetch_intraday_data(symbol, start_date, end_date):
|
4 |
+
"""
|
5 |
+
Fetches 15-minute intraday stock data for the specified symbol between start_date and end_date.
|
6 |
+
|
7 |
+
Parameters:
|
8 |
+
- symbol (str): The stock symbol to fetch data for (e.g., 'AAPL').
|
9 |
+
- start_date (str): The start date for the data in 'YYYY-MM-DD' format.
|
10 |
+
- end_date (str): The end date for the data in 'YYYY-MM-DD' format.
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
- DataFrame: Pandas DataFrame containing the intraday stock data.
|
14 |
+
"""
|
15 |
+
# Define the ticker object
|
16 |
+
ticker = yf.Ticker(symbol)
|
17 |
+
|
18 |
+
# Fetch the historical market data
|
19 |
+
data = ticker.history(interval="15m", start=start_date, end=end_date)
|
20 |
+
|
21 |
+
# Return the data
|
22 |
+
return data
|
23 |
+
|
24 |
+
if __name__ == "__main__":
|
25 |
+
# Example usage
|
26 |
+
symbol = "AAPL" # Apple Inc.
|
27 |
+
start_date = "2023-01-01"
|
28 |
+
end_date = "2023-01-31"
|
29 |
+
|
30 |
+
# Fetch the data
|
31 |
+
data = fetch_intraday_data(symbol, start_date, end_date)
|
32 |
+
|
33 |
+
# Display the first few rows of the data
|
34 |
+
print(data.head())
|