netflypsb commited on
Commit
ac7b430
1 Parent(s): c0f4173

Update yfinance_data/fetcher.py

Browse files
Files changed (1) hide show
  1. 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='1h'):
7
  """
8
- Fetches historical stock data for a given symbol from Yahoo Finance.
9
-
10
  Parameters:
11
- - symbol: The ticker symbol for the stock (str).
12
- - start_date: The start date for the data fetching in 'YYYY-MM-DD' format (str).
13
- - end_date: The end date for the data fetching in 'YYYY-MM-DD' format (str).
14
- - interval: The data interval. Valid intervals: '1m', '2m', '5m', '15m', '30m', '1h', '1d', '5d', '1wk', '1mo', '3mo' (str).
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
- Fetches historical stock data for the past 30 days for both 4-hour and 1-hour intervals.
30
-
31
- Parameters:
32
- - symbol: The ticker symbol for the stock (str).
33
- - days: Number of days in the past to fetch data for (int).
34
-
35
- Returns:
36
- - Tuple of DataFrames: (data_4h, data_1h)
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
- symbol = "AAPL" # Example stock symbol
55
- data_4h, data_1h = fetch_data_for_indicators(symbol)
56
- print("4-Hour Data:")
57
- print(data_4h.head()) # Display the first few rows
58
- print("\n1-Hour Data:")
59
- print(data_1h.head()) # Display the first few rows
 
 
 
 
 
 
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