Spaces:
Sleeping
Sleeping
File size: 4,930 Bytes
af0538f 786285f af0538f 786285f af0538f 786285f af0538f | 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | """
Technical signal scoring.
Converts raw indicator values into a directional score (-1 to +1).
Each signal has an individual sub-score; the aggregate is weighted.
"""
from __future__ import annotations
import numpy as np
import pandas as pd
def score_technical_signals(df: pd.DataFrame) -> dict:
"""
Analyze technical indicators and return a composite score.
Returns:
{
"score": float (-1 to +1),
"direction": "bullish" | "bearish" | "neutral",
"signals": {name: {"score": float, "value": float, "interpretation": str}},
}
"""
latest = df.iloc[-1]
signals = {}
# ββ 1. RSI (weight: 25%) ββββββββββββββββββββββββββ
rsi = float(latest.get("RSI", 50))
if rsi >= 75:
rsi_score = -0.8 # overbought β bearish
rsi_interp = "Overbought"
elif rsi >= 65:
rsi_score = -0.3
rsi_interp = "Slightly overbought"
elif rsi <= 25:
rsi_score = 0.8 # oversold β bullish
rsi_interp = "Oversold"
elif rsi <= 35:
rsi_score = 0.3
rsi_interp = "Slightly oversold"
else:
rsi_score = 0.0
rsi_interp = "Neutral"
signals["RSI"] = {"score": rsi_score, "value": round(rsi, 2), "interpretation": rsi_interp}
# ββ 2. MACD Crossover (weight: 25%) βββββββββββββββ
macd = float(latest.get("MACD", 0))
macd_signal = float(latest.get("MACD_Signal", 0))
macd_diff = macd - macd_signal
if abs(macd_diff) < 0.01:
macd_score = 0.0
macd_interp = "Neutral"
elif macd_diff > 0:
macd_score = min(0.79, macd_diff * 10) # bullish crossover β capped at 0.79
macd_interp = "Bullish crossover"
else:
macd_score = max(-0.79, macd_diff * 10) # bearish crossover β capped at -0.79
macd_interp = "Bearish crossover"
signals["MACD"] = {"score": round(macd_score, 3), "value": round(macd_diff, 4), "interpretation": macd_interp}
# ββ 3. SMA Trend (weight: 20%) ββββββββββββββββββββ
price = float(latest["Close"])
sma20 = float(latest.get("SMA_20", price))
sma50 = float(latest.get("SMA_50", price))
above_sma20 = price > sma20
above_sma50 = price > sma50
sma20_above_50 = sma20 > sma50 # golden cross
if above_sma20 and above_sma50 and sma20_above_50:
sma_score = 0.7 # strong uptrend
sma_interp = "Strong uptrend (Golden Cross)"
elif above_sma20 and above_sma50:
sma_score = 0.4
sma_interp = "Uptrend"
elif not above_sma20 and not above_sma50 and not sma20_above_50:
sma_score = -0.7 # strong downtrend
sma_interp = "Strong downtrend (Death Cross)"
elif not above_sma20 and not above_sma50:
sma_score = -0.4
sma_interp = "Downtrend"
else:
sma_score = 0.0
sma_interp = "Mixed trend"
signals["SMA_Trend"] = {"score": sma_score, "value": round(price - sma20, 2), "interpretation": sma_interp}
# ββ 4. Bollinger Band Width (weight: 15%) βββββββββ
bb_width = float(latest.get("BB_Width", 0))
bb_upper = float(latest.get("BB_Upper", price))
bb_lower = float(latest.get("BB_Lower", price))
if price >= bb_upper * 0.99:
bb_score = -0.5 # near upper band β reversal risk
bb_interp = "Near upper band"
elif price <= bb_lower * 1.01:
bb_score = 0.5 # near lower band β bounce potential
bb_interp = "Near lower band"
else:
bb_score = 0.0
bb_interp = "Within bands"
signals["Bollinger"] = {"score": bb_score, "value": round(bb_width, 4), "interpretation": bb_interp}
# ββ 5. ATR Volatility (weight: 15%) βββββββββββββββ
atr = float(latest.get("ATR", 0))
atr_pct = (atr / price * 100) if price > 0 else 0
if atr_pct > 3.0:
atr_score = -0.3 # high volatility = risk
atr_interp = "High volatility"
elif atr_pct > 2.0:
atr_score = -0.1
atr_interp = "Moderate volatility"
else:
atr_score = 0.1
atr_interp = "Low volatility"
signals["ATR"] = {"score": atr_score, "value": round(atr_pct, 2), "interpretation": atr_interp}
# ββ Weighted composite ββββββββββββββββββββββββββββ
weights = {"RSI": 0.25, "MACD": 0.25, "SMA_Trend": 0.20, "Bollinger": 0.15, "ATR": 0.15}
composite = sum(signals[k]["score"] * weights[k] for k in weights)
composite = max(-0.99, min(0.99, composite))
if composite > 0.15:
direction = "bullish"
elif composite < -0.15:
direction = "bearish"
else:
direction = "neutral"
return {
"score": round(composite, 3),
"direction": direction,
"signals": signals,
}
|