nitishkarvekar commited on
Commit
34e6ac9
·
verified ·
1 Parent(s): d99678e

Update technical_agent.py

Browse files
Files changed (1) hide show
  1. technical_agent.py +42 -25
technical_agent.py CHANGED
@@ -1,4 +1,6 @@
1
  import ta
 
 
2
 
3
  def analyze_technical(market_data):
4
 
@@ -6,39 +8,54 @@ def analyze_technical(market_data):
6
 
7
  for ticker, df in market_data.items():
8
 
9
- score = 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- df["rsi"] = ta.momentum.RSIIndicator(df["Close"]).rsi()
12
- df["macd"] = ta.trend.MACD(df["Close"]).macd()
13
- df["ma20"] = df["Close"].rolling(20).mean()
14
- df["ma50"] = df["Close"].rolling(50).mean()
15
 
16
- bb = ta.volatility.BollingerBands(df["Close"])
17
- df["bb_high"] = bb.bollinger_hband()
18
- df["bb_low"] = bb.bollinger_lband()
19
 
20
- last = df.iloc[-1]
 
 
21
 
22
- # RSI oversold
23
- if last["rsi"] < 35:
24
- score += 1
25
 
26
- # MACD bullish
27
- if last["macd"] > 0:
28
- score += 1
29
 
30
- # MA crossover
31
- if last["ma20"] > last["ma50"]:
32
- score += 1
33
 
34
- # Bollinger bounce
35
- if last["Close"] < last["bb_low"]:
36
- score += 1
37
 
38
- # Volume spike
39
- if df["Volume"].iloc[-1] > df["Volume"].rolling(20).mean().iloc[-1]:
40
- score += 1
41
 
42
- results[ticker] = score
43
 
44
  return results
 
1
  import ta
2
+ import pandas as pd
3
+
4
 
5
  def analyze_technical(market_data):
6
 
 
8
 
9
  for ticker, df in market_data.items():
10
 
11
+ try:
12
+
13
+ df = df.copy()
14
+
15
+ # Ensure 1D Series (fix for yfinance issue)
16
+ close = pd.Series(df["Close"]).squeeze()
17
+ volume = pd.Series(df["Volume"]).squeeze()
18
+
19
+ score = 0
20
+
21
+ rsi = ta.momentum.RSIIndicator(close).rsi()
22
+
23
+ macd = ta.trend.MACD(close).macd()
24
+
25
+ ma20 = close.rolling(20).mean()
26
+ ma50 = close.rolling(50).mean()
27
+
28
+ bb = ta.volatility.BollingerBands(close)
29
+
30
+ bb_high = bb.bollinger_hband()
31
+ bb_low = bb.bollinger_lband()
32
 
33
+ last = len(close) - 1
 
 
 
34
 
35
+ # RSI oversold
36
+ if rsi.iloc[last] < 35:
37
+ score += 1
38
 
39
+ # MACD bullish
40
+ if macd.iloc[last] > 0:
41
+ score += 1
42
 
43
+ # MA crossover
44
+ if ma20.iloc[last] > ma50.iloc[last]:
45
+ score += 1
46
 
47
+ # Bollinger bounce
48
+ if close.iloc[last] < bb_low.iloc[last]:
49
+ score += 1
50
 
51
+ # Volume spike
52
+ if volume.iloc[last] > volume.rolling(20).mean().iloc[last]:
53
+ score += 1
54
 
55
+ results[ticker] = score
 
 
56
 
57
+ except:
 
 
58
 
59
+ results[ticker] = 0
60
 
61
  return results