File size: 1,154 Bytes
2293f58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# analysis/technical.py
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'
        }