|
|
|
import numpy as np |
|
from data.api_client import YahooFinanceClient |
|
|
|
class TechnicalAnalyzer: |
|
@staticmethod |
|
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)) |
|
|
|
@staticmethod |
|
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' |
|
} |