Rewritten financial data fetching so it finally works again.
This commit is contained in:
38
fetch_close_price.py
Normal file
38
fetch_close_price.py
Normal file
@@ -0,0 +1,38 @@
|
||||
# fetch_close_price.py
|
||||
# This script does ONLY ONE THING: gets the closing price using the stable Ticker.history() method.
|
||||
import sys
|
||||
import json
|
||||
import yfinance as yf
|
||||
import pandas as pd
|
||||
import logging
|
||||
|
||||
# Suppress verbose yfinance logging in this isolated process
|
||||
logging.getLogger("yfinance").setLevel(logging.CRITICAL)
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
# Exit with an error code if no ticker is provided
|
||||
sys.exit(1)
|
||||
|
||||
ticker_symbol = sys.argv[1]
|
||||
|
||||
try:
|
||||
# Instead of the global yf.download(), we use the Ticker object's .history() method.
|
||||
# This uses a different internal code path that we have proven is stable.
|
||||
ticker = yf.Ticker(ticker_symbol)
|
||||
data = ticker.history(period="2d", auto_adjust=False)
|
||||
# --- END OF FIX ---
|
||||
|
||||
closing_price = None
|
||||
if not data.empty:
|
||||
last_close_raw = data['Close'].iloc[-1]
|
||||
if pd.notna(last_close_raw):
|
||||
closing_price = float(last_close_raw)
|
||||
|
||||
# On success, print JSON to stdout and exit cleanly
|
||||
print(json.dumps({"closing_price": closing_price}))
|
||||
sys.exit(0)
|
||||
except Exception:
|
||||
# If any error occurs, print an empty JSON and exit with an error code
|
||||
print(json.dumps({"closing_price": None}))
|
||||
sys.exit(1)
|
Reference in New Issue
Block a user