Spaces:
Sleeping
Sleeping
Create stock_utils.py
Browse files- stock_utils.py +25 -0
stock_utils.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import yfinance as yf
|
| 2 |
+
|
| 3 |
+
def get_stock_data(ticker):
|
| 4 |
+
try:
|
| 5 |
+
stock = yf.Ticker(ticker)
|
| 6 |
+
hist = stock.history(period="5d")
|
| 7 |
+
info = stock.info
|
| 8 |
+
|
| 9 |
+
data = {
|
| 10 |
+
"ticker": ticker,
|
| 11 |
+
"company_name": info.get("longName", "N/A"),
|
| 12 |
+
"current_price": info.get("currentPrice", "N/A"),
|
| 13 |
+
"pe_ratio": info.get("trailingPE", "N/A"),
|
| 14 |
+
"market_cap": info.get("marketCap", "N/A"),
|
| 15 |
+
"day_low": info.get("dayLow", "N/A"),
|
| 16 |
+
"day_high": info.get("dayHigh", "N/A"),
|
| 17 |
+
"volume": info.get("volume", "N/A"),
|
| 18 |
+
"52_week_high": info.get("fiftyTwoWeekHigh", "N/A"),
|
| 19 |
+
"52_week_low": info.get("fiftyTwoWeekLow", "N/A"),
|
| 20 |
+
"recent_close_prices": hist['Close'].tail(3).to_list()
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
return data
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return {"error": str(e)}
|