Spaces:
Paused
Paused
Update trade_analysis/data.py
Browse files- trade_analysis/data.py +36 -40
trade_analysis/data.py
CHANGED
|
@@ -54,52 +54,48 @@ class UnifiedDataProvider:
|
|
| 54 |
return data
|
| 55 |
return None
|
| 56 |
|
|
|
|
| 57 |
async def fetch_multi_timeframe_stock_data(self, symbol: str) -> dict:
|
| 58 |
"""
|
| 59 |
-
Fetches
|
| 60 |
-
|
| 61 |
"""
|
| 62 |
-
print(f"Fetching Finnhub
|
| 63 |
dfs = {}
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
"token": config.FINNHUB_KEY
|
| 85 |
-
}
|
| 86 |
-
|
| 87 |
-
try:
|
| 88 |
-
res = await self.client.get(base_url, params=params)
|
| 89 |
-
res.raise_for_status()
|
| 90 |
-
data = res.json()
|
| 91 |
-
processed_df = _process_finnhub_data(data, symbol)
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
return dfs
|
| 102 |
-
|
| 103 |
async def fetch_news(self, symbol: str, days: int = 3) -> tuple:
|
| 104 |
"""Fetches news from Finnhub, with local file fallback."""
|
| 105 |
local_data = self._load_local_data(symbol)
|
|
|
|
| 54 |
return data
|
| 55 |
return None
|
| 56 |
|
| 57 |
+
|
| 58 |
async def fetch_multi_timeframe_stock_data(self, symbol: str) -> dict:
|
| 59 |
"""
|
| 60 |
+
REWRITTEN FOR FREE TIER: Fetches the current day's quote data from Finnhub.
|
| 61 |
+
Historical candle data is a premium feature.
|
| 62 |
"""
|
| 63 |
+
print(f"Fetching Finnhub REAL-TIME QUOTE for {symbol}...")
|
| 64 |
dfs = {}
|
| 65 |
+
base_url = "https://finnhub.io/api/v1/quote"
|
| 66 |
+
params = {"symbol": symbol, "token": config.FINNHUB_KEY}
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
res = await self.client.get(base_url, params=params)
|
| 70 |
+
res.raise_for_status()
|
| 71 |
+
data = res.json()
|
| 72 |
+
|
| 73 |
+
if data and 'c' in data and data['c'] != 0:
|
| 74 |
+
# Create a single-row DataFrame
|
| 75 |
+
df = pd.DataFrame([{
|
| 76 |
+
'Open': data['o'],
|
| 77 |
+
'High': data['h'],
|
| 78 |
+
'Low': data['l'],
|
| 79 |
+
'Close': data['c'],
|
| 80 |
+
# Volume is not in the quote endpoint, so we put a placeholder
|
| 81 |
+
'Volume': 0
|
| 82 |
+
}])
|
| 83 |
+
# Use the current time for the index
|
| 84 |
+
df.index = pd.to_datetime([datetime.now(tz=pytz.UTC)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
# Since we only have one row, we can't do multi-timeframe.
|
| 87 |
+
# We will return this single row for the 'daily' key.
|
| 88 |
+
dfs["daily"] = df
|
| 89 |
+
print(f" - Fetched real-time quote for {symbol} from Finnhub.")
|
| 90 |
+
else:
|
| 91 |
+
dfs["daily"] = pd.DataFrame()
|
| 92 |
+
|
| 93 |
+
except (httpx.RequestError, httpx.HTTPStatusError) as e:
|
| 94 |
+
print(f"ERROR: Finnhub Quote request failed for {symbol}: {e}")
|
| 95 |
+
dfs["daily"] = pd.DataFrame()
|
| 96 |
+
|
| 97 |
return dfs
|
| 98 |
+
|
| 99 |
async def fetch_news(self, symbol: str, days: int = 3) -> tuple:
|
| 100 |
"""Fetches news from Finnhub, with local file fallback."""
|
| 101 |
local_data = self._load_local_data(symbol)
|