Spaces:
Runtime error
Runtime error
Update yfinance_data/fetcher.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859# yfinance_data/fetcher.pyimport yfinance as yfimport pandas as pddef fetch_stock_data(symbol, start_date, end_date, interval='1h'):"""Fetches historical stock data for a given symbol from Yahoo Finance.Parameters:- symbol: The ticker symbol for the stock (str).- start_date: The start date for the data fetching in 'YYYY-MM-DD' format (str).- end_date: The end date for the data fetching in 'YYYY-MM-DD' format (str).- interval: The data interval. Valid intervals: '1m', '2m', '5m', '15m', '30m', '1h', '1d', '5d', '1wk', '1mo', '3mo' (str).Returns:- DataFrame: Historical stock data including date, open, high, low, close, volume."""# Fetch the datadata = yf.download(symbol, start=start_date, end=end_date, interval=interval)# Drop any NaNs (usually in case of missing data)data.dropna(inplace=True)return datadef fetch_data_for_indicators(symbol, days=30):"""Fetches historical stock data for the past 30 days for both 4-hour and 1-hour intervals.Parameters:- symbol: The ticker symbol for the stock (str).- days: Number of days in the past to fetch data for (int).Returns:- Tuple of DataFrames: (data_4h, data_1h)"""# Calculate start and end datesend_date = pd.Timestamp.now()start_date = end_date - pd.Timedelta(days=days)# Convert dates to string format for the API callstart_date_str = start_date.strftime('%Y-%m-%d')end_date_str = end_date.strftime('%Y-%m-%d')# Fetch data for both intervalsdata_4h = fetch_stock_data(symbol, start_date_str, end_date_str, interval='4h')data_1h = fetch_stock_data(symbol, start_date_str, end_date_str, interval='1h')return data_4h, data_1h# Example usageif __name__ == "__main__":symbol = "AAPL" # Example stock symboldata_4h, data_1h = fetch_data_for_indicators(symbol)print("4-Hour Data:")print(data_4h.head()) # Display the first few rowsprint("\n1-Hour Data:")print(data_1h.head()) # Display the first few rows12345678910111213141516171819202122232425262728293031323334import yfinance as yfdef fetch_stock_data(symbol, start_date, end_date, interval='1d'):"""Fetch historical stock data for a given symbol from Yahoo Finance.Parameters:- symbol (str): Stock symbol to fetch the data for.- start_date (str): Start date in 'YYYY-MM-DD' format.- end_date (str): End date in 'YYYY-MM-DD' format.- interval (str): Data interval. Defaults to '1d'. Options: '1d', '1h', etc.Returns:- DataFrame: Historical stock data including date, open, high, low, close, and volume."""# Fetch the stock data using the yfinance librarystock_data = yf.download(symbol, start=start_date, end=end_date, interval=interval)# Check if the DataFrame is emptyif stock_data.empty:raise ValueError(f"No data found for {symbol} using interval {interval}.")return stock_dataif __name__ == "__main__":# Example usagesymbol = "AAPL"start_date = "2023-01-01"end_date = "2023-04-01"interval = "1h" # For hourly data# Fetch and print the data for demonstrationdata = fetch_stock_data(symbol, start_date, end_date, interval)print(data.head()) # Print the first few rows
yfinance_data/fetcher.py+26-51