Spaces:
Runtime error
Runtime error
Update yfinance_data/fetcher.py
Browse files- yfinance_data/fetcher.py +26 -51
yfinance_data/fetcher.py
CHANGED
|
@@ -1,59 +1,34 @@
|
|
| 1 |
-
# yfinance_data/fetcher.py
|
| 2 |
-
|
| 3 |
import yfinance as yf
|
| 4 |
-
import pandas as pd
|
| 5 |
|
| 6 |
-
def fetch_stock_data(symbol, start_date, end_date, interval='
|
| 7 |
"""
|
| 8 |
-
|
| 9 |
-
|
| 10 |
Parameters:
|
| 11 |
-
- symbol:
|
| 12 |
-
- start_date:
|
| 13 |
-
- end_date:
|
| 14 |
-
- interval:
|
| 15 |
-
|
| 16 |
Returns:
|
| 17 |
-
- DataFrame: Historical stock data including date, open, high, low, close, volume.
|
| 18 |
-
"""
|
| 19 |
-
# Fetch the data
|
| 20 |
-
data = yf.download(symbol, start=start_date, end=end_date, interval=interval)
|
| 21 |
-
|
| 22 |
-
# Drop any NaNs (usually in case of missing data)
|
| 23 |
-
data.dropna(inplace=True)
|
| 24 |
-
|
| 25 |
-
return data
|
| 26 |
-
|
| 27 |
-
def fetch_data_for_indicators(symbol, days=30):
|
| 28 |
"""
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
"""
|
| 38 |
-
# Calculate start and end dates
|
| 39 |
-
end_date = pd.Timestamp.now()
|
| 40 |
-
start_date = end_date - pd.Timedelta(days=days)
|
| 41 |
-
|
| 42 |
-
# Convert dates to string format for the API call
|
| 43 |
-
start_date_str = start_date.strftime('%Y-%m-%d')
|
| 44 |
-
end_date_str = end_date.strftime('%Y-%m-%d')
|
| 45 |
-
|
| 46 |
-
# Fetch data for both intervals
|
| 47 |
-
data_4h = fetch_stock_data(symbol, start_date_str, end_date_str, interval='4h')
|
| 48 |
-
data_1h = fetch_stock_data(symbol, start_date_str, end_date_str, interval='1h')
|
| 49 |
-
|
| 50 |
-
return data_4h, data_1h
|
| 51 |
|
| 52 |
-
# Example usage
|
| 53 |
if __name__ == "__main__":
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import yfinance as yf
|
|
|
|
| 2 |
|
| 3 |
+
def fetch_stock_data(symbol, start_date, end_date, interval='1d'):
|
| 4 |
"""
|
| 5 |
+
Fetch historical stock data for a given symbol from Yahoo Finance.
|
| 6 |
+
|
| 7 |
Parameters:
|
| 8 |
+
- symbol (str): Stock symbol to fetch the data for.
|
| 9 |
+
- start_date (str): Start date in 'YYYY-MM-DD' format.
|
| 10 |
+
- end_date (str): End date in 'YYYY-MM-DD' format.
|
| 11 |
+
- interval (str): Data interval. Defaults to '1d'. Options: '1d', '1h', etc.
|
| 12 |
+
|
| 13 |
Returns:
|
| 14 |
+
- DataFrame: Historical stock data including date, open, high, low, close, and volume.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
"""
|
| 16 |
+
# Fetch the stock data using the yfinance library
|
| 17 |
+
stock_data = yf.download(symbol, start=start_date, end=end_date, interval=interval)
|
| 18 |
+
|
| 19 |
+
# Check if the DataFrame is empty
|
| 20 |
+
if stock_data.empty:
|
| 21 |
+
raise ValueError(f"No data found for {symbol} using interval {interval}.")
|
| 22 |
+
|
| 23 |
+
return stock_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
| 25 |
if __name__ == "__main__":
|
| 26 |
+
# Example usage
|
| 27 |
+
symbol = "AAPL"
|
| 28 |
+
start_date = "2023-01-01"
|
| 29 |
+
end_date = "2023-04-01"
|
| 30 |
+
interval = "1h" # For hourly data
|
| 31 |
+
|
| 32 |
+
# Fetch and print the data for demonstration
|
| 33 |
+
data = fetch_stock_data(symbol, start_date, end_date, interval)
|
| 34 |
+
print(data.head()) # Print the first few rows
|