Download analysis/technical.py from feliponi/stock: direct link, hf CLI and curl.
- Browser
- Download file 1.15 kB
-
https://huggingface.co/spaces/feliponi/stock/resolve/main/analysis/technical.py
- Command line
-
hf download hf://spaces/feliponi/stock/analysis/technical.py
-
curl -L -o technical.py https://huggingface.co/spaces/feliponi/stock/resolve/main/analysis/technical.py
1.15 kB
| # analysis/technical.py | |
| import numpy as np | |
| from data.api_client import YahooFinanceClient | |
| class TechnicalAnalyzer: | |
| def calculate_rsi(data, window=14): | |
| delta = data['Close'].diff() | |
| gain = (delta.where(delta > 0, 0)).rolling(window=window).mean() | |
| loss = (-delta.where(delta < 0, 0)).rolling(window=window).mean() | |
| rs = gain / loss | |
| return 100 - (100 / (1 + rs)) | |
| def calculate_sma(data, window): | |
| return data['Close'].rolling(window=window).mean() | |
| def analyze(self, ticker): | |
| data = YahooFinanceClient.download_data(ticker, period='1y') | |
| if data.empty or data.shape[0] < 50: | |
| return None | |
| sma_50 = self.calculate_sma(data, 50).iloc[-1].item() | |
| current_price = data['Close'].iloc[-1].item() | |
| rsi = self.calculate_rsi(data).iloc[-1].item() | |
| return { | |
| 'price': current_price, | |
| 'sma_50': sma_50, | |
| 'price_vs_sma': (current_price / sma_50) - 1, | |
| 'rsi': rsi if not np.isnan(rsi) else 50, | |
| 'trend': 'bullish' if current_price > sma_50 else 'bearish' | |
| } |