Update trend_agent.py
Browse files- trend_agent.py +10 -0
trend_agent.py
CHANGED
|
@@ -2,10 +2,20 @@ from indicators import calculate_ema
|
|
| 2 |
|
| 3 |
class TrendWatcherAgent:
|
| 4 |
def run(self, df):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
df = calculate_ema(df, 20)
|
| 6 |
df = calculate_ema(df, 50)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
latest = df.iloc[-1]
|
| 8 |
|
|
|
|
| 9 |
if latest["ema_20"] > latest["ema_50"]:
|
| 10 |
return {"trend": "Uptrend", "confidence": 80}
|
| 11 |
else:
|
|
|
|
| 2 |
|
| 3 |
class TrendWatcherAgent:
|
| 4 |
def run(self, df):
|
| 5 |
+
# Sort by time (required by ta-lib)
|
| 6 |
+
df = df.sort_index()
|
| 7 |
+
|
| 8 |
+
# Calculate EMA20 and EMA50
|
| 9 |
df = calculate_ema(df, 20)
|
| 10 |
df = calculate_ema(df, 50)
|
| 11 |
+
|
| 12 |
+
# Drop rows with NaN due to initial window
|
| 13 |
+
df.dropna(subset=["ema_20", "ema_50"], inplace=True)
|
| 14 |
+
|
| 15 |
+
# Get the latest row
|
| 16 |
latest = df.iloc[-1]
|
| 17 |
|
| 18 |
+
# Trend logic
|
| 19 |
if latest["ema_20"] > latest["ema_50"]:
|
| 20 |
return {"trend": "Uptrend", "confidence": 80}
|
| 21 |
else:
|