cpatino10 commited on
Commit
3b46fcb
·
verified ·
1 Parent(s): 54355fd

added date validation code

Browse files
Files changed (1) hide show
  1. app.py +37 -13
app.py CHANGED
@@ -11,28 +11,52 @@ from Gradio_UI import GradioUI
11
 
12
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
13
  @tool
14
- def fetch_historical_price_data(symbol:str, start_date:str, end_date:str)-> pd.DataFrame: #it's import to specify the return type
15
- #Keep this format for the description / args / args description but feel free to modify the tool
16
- """Fetch daily historical stock price data for a given stock symbol from start_date to today
 
 
17
  Args:
18
- symbol: stock market symbol such as 'AAPL', 'MSFT', 'NVDA'
19
- start_date: start date in YYYY-MM-DD format. Example - '2025-05-10'
20
- end_date: end date in YYYY-MM-DD fomrat. Example '2025-11-03'
21
  """
22
- FMP_KEY = os.getenv('FMP_KEY')
23
-
24
- url = f"https://financialmodelingprep.com/api/v3/historical-price-full/{symbol}?from={start_date}&to={end_date}&apikey={FMP_KEY}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  try:
27
  response = requests.get(url)
28
  if response.status_code != 200:
29
- return None
30
- df = pd.DataFrame(response.json())
31
- df['symbol'] = symbol
 
 
32
 
 
 
33
  return df.to_dict(orient="records")
 
34
  except requests.RequestException as e:
35
- return f"Request failed for symbol {symbol}"
 
36
 
37
  @tool
38
  def get_current_time_in_timezone(timezone: str) -> str:
 
11
 
12
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
13
  @tool
14
+ def fetch_historical_price_data(symbol: str, start_date: str, end_date: str) -> list[dict]:
15
+ """
16
+ Fetch daily historical stock price data for a given stock symbol between start_date and end_date. Use
17
+ toda=datetime.now() to confirm the dates are valid dates prior to calling the API.
18
+
19
  Args:
20
+ symbol: Stock market symbol such as 'AAPL', 'MSFT', 'NVDA'
21
+ start_date: Start date in 'YYYY-MM-DD' format
22
+ end_date: End date in 'YYYY-MM-DD' format
23
  """
24
+ FMP_KEY = os.getenv("FMP_KEY")
25
+ if not FMP_KEY:
26
+ return [{"error": "Missing FMP_KEY environment variable"}]
27
+
28
+ # Validate and adjust date inputs
29
+ try:
30
+ start_dt = datetime.strptime(start_date, "%Y-%m-%d")
31
+ end_dt = datetime.strptime(end_date, "%Y-%m-%d")
32
+ today = datetime.now()
33
+ if end_dt > today:
34
+ end_dt = today
35
+ end_date = today.strftime("%Y-%m-%d")
36
+ except ValueError:
37
+ return [{"error": "Invalid date format. Use 'YYYY-MM-DD'."}]
38
+
39
+ url = (
40
+ f"https://financialmodelingprep.com/api/v3/historical-price-full/"
41
+ f"{symbol}?from={start_date}&to={end_date}&apikey={FMP_KEY}"
42
+ )
43
 
44
  try:
45
  response = requests.get(url)
46
  if response.status_code != 200:
47
+ return [{"error": f"HTTP {response.status_code} from FMP API"}]
48
+
49
+ data = response.json().get("historical", [])
50
+ if not data:
51
+ return [{"message": f"No data available for {symbol} between {start_date} and {end_date}."}]
52
 
53
+ df = pd.DataFrame(data)
54
+ df["symbol"] = symbol
55
  return df.to_dict(orient="records")
56
+
57
  except requests.RequestException as e:
58
+ return [{"error": f"Request failed for {symbol}: {str(e)}"}]
59
+
60
 
61
  @tool
62
  def get_current_time_in_timezone(timezone: str) -> str: