File size: 1,362 Bytes
366ba1b
34e6ac9
 
366ba1b
 
 
 
 
 
 
34e6ac9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
366ba1b
34e6ac9
366ba1b
34e6ac9
 
 
366ba1b
34e6ac9
 
 
366ba1b
34e6ac9
 
 
366ba1b
34e6ac9
 
 
366ba1b
34e6ac9
 
 
366ba1b
34e6ac9
366ba1b
34e6ac9
366ba1b
34e6ac9
366ba1b
 
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
60
61
import ta
import pandas as pd


def analyze_technical(market_data):

    results = {}

    for ticker, df in market_data.items():

        try:

            df = df.copy()

            # Ensure 1D Series (fix for yfinance issue)
            close = pd.Series(df["Close"]).squeeze()
            volume = pd.Series(df["Volume"]).squeeze()

            score = 0

            rsi = ta.momentum.RSIIndicator(close).rsi()

            macd = ta.trend.MACD(close).macd()

            ma20 = close.rolling(20).mean()
            ma50 = close.rolling(50).mean()

            bb = ta.volatility.BollingerBands(close)

            bb_high = bb.bollinger_hband()
            bb_low = bb.bollinger_lband()

            last = len(close) - 1

            # RSI oversold
            if rsi.iloc[last] < 35:
                score += 1

            # MACD bullish
            if macd.iloc[last] > 0:
                score += 1

            # MA crossover
            if ma20.iloc[last] > ma50.iloc[last]:
                score += 1

            # Bollinger bounce
            if close.iloc[last] < bb_low.iloc[last]:
                score += 1

            # Volume spike
            if volume.iloc[last] > volume.rolling(20).mean().iloc[last]:
                score += 1

            results[ticker] = score

        except:

            results[ticker] = 0

    return results