Update technical_agent.py
Browse files- 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 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 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
if last["Close"] < last["bb_low"]:
|
| 36 |
-
score += 1
|
| 37 |
|
| 38 |
-
|
| 39 |
-
if df["Volume"].iloc[-1] > df["Volume"].rolling(20).mean().iloc[-1]:
|
| 40 |
-
score += 1
|
| 41 |
|
| 42 |
-
|
| 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
|