tradingAI / trend_agent.py
yougandar's picture
Update trend_agent.py
d3ca120 verified
raw
history blame contribute delete
641 Bytes
from indicators import calculate_ema
class TrendWatcherAgent:
def run(self, df):
# Sort by time (required by ta-lib)
df = df.sort_index()
# Calculate EMA20 and EMA50
df = calculate_ema(df, 20)
df = calculate_ema(df, 50)
# Drop rows with NaN due to initial window
df.dropna(subset=["ema_20", "ema_50"], inplace=True)
# Get the latest row
latest = df.iloc[-1]
# Trend logic
if latest["ema_20"] > latest["ema_50"]:
return {"trend": "Uptrend", "confidence": 80}
else:
return {"trend": "Downtrend", "confidence": 70}