Spaces:
Sleeping
Sleeping
added date validation code
Browse files
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)->
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
Args:
|
| 18 |
-
symbol:
|
| 19 |
-
start_date:
|
| 20 |
-
end_date:
|
| 21 |
"""
|
| 22 |
-
FMP_KEY = os.getenv(
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
try:
|
| 27 |
response = requests.get(url)
|
| 28 |
if response.status_code != 200:
|
| 29 |
-
return
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
|
|
|
|
|
|
| 33 |
return df.to_dict(orient="records")
|
|
|
|
| 34 |
except requests.RequestException as e:
|
| 35 |
-
return f"Request failed for 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:
|