-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_client.py
More file actions
60 lines (49 loc) · 2.14 KB
/
api_client.py
File metadata and controls
60 lines (49 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import yfinance as yf
import time
from typing import List, Optional
class StockAPIClient:
def __init__(self, api_key: Optional[str] = None):
pass
def get_current_price(self, symbol: str) -> Optional[float]:
try:
ticker = yf.Ticker(symbol)
data = ticker.history(period="1d", interval="1m")
if not data.empty:
return float(data['Close'].iloc[-1])
info = ticker.info
if 'currentPrice' in info:
return float(info['currentPrice'])
elif 'regularMarketPrice' in info:
return float(info['regularMarketPrice'])
elif 'previousClose' in info:
return float(info['previousClose'])
return None
except Exception as e:
print(f"Error fetching price for {symbol}: {e}")
return None
def get_price_history(self, symbol: str, duration_seconds: int = 20) -> List[float]:
prices = []
start_time = time.time()
while time.time() - start_time < duration_seconds:
price = self.get_current_price(symbol)
if price is not None:
prices.append(price)
time.sleep(1)
if not prices:
ticker = yf.Ticker(symbol)
hist = ticker.history(period="1d", interval="1m")
if not hist.empty:
prices = hist['Close'].tail(duration_seconds).tolist()
prices = [float(p) for p in prices]
return prices
def get_historical_data(self, symbol: str, period: str = "1d", interval: str = "1m", max_points: int = 100) -> List[float]:
"""Get historical price data for different timeframes"""
try:
ticker = yf.Ticker(symbol)
hist = ticker.history(period=period, interval=interval)
if not hist.empty:
prices = hist['Close'].tail(max_points).tolist()
return [float(p) for p in prices]
except Exception as e:
print(f"Error fetching historical data for {symbol}: {e}")
return []