Uploaded investment_signal script
Browse files- investment_signal.py +45 -0
investment_signal.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# investment_signal.py
|
| 2 |
+
def get_investment_signal(ma, close_price, rsi, macd, lower_band, upper_band):
|
| 3 |
+
"""
|
| 4 |
+
Simple rule-based decision:
|
| 5 |
+
- RSI < 30 => Buy, RSI > 70 => Sell
|
| 6 |
+
- MACD > 0 bullish, < 0 bearish
|
| 7 |
+
- Price vs MA trend confirmation
|
| 8 |
+
- Price vs Bollinger Bands for extremes
|
| 9 |
+
Returns: (signal: 'Buy'|'Hold'|'Sell', reason: str)
|
| 10 |
+
"""
|
| 11 |
+
reasons = []
|
| 12 |
+
score = 0
|
| 13 |
+
|
| 14 |
+
# RSI
|
| 15 |
+
if rsi < 30:
|
| 16 |
+
score += 2; reasons.append("RSI indicates oversold (<30)")
|
| 17 |
+
elif rsi > 70:
|
| 18 |
+
score -= 2; reasons.append("RSI indicates overbought (>70)")
|
| 19 |
+
else:
|
| 20 |
+
reasons.append("RSI is neutral (30–70)")
|
| 21 |
+
|
| 22 |
+
# MACD
|
| 23 |
+
if macd > 0:
|
| 24 |
+
score += 1; reasons.append("MACD shows bullish momentum (>0)")
|
| 25 |
+
else:
|
| 26 |
+
score -= 1; reasons.append("MACD shows bearish momentum (<0)")
|
| 27 |
+
|
| 28 |
+
# Trend vs MA
|
| 29 |
+
if close_price > ma:
|
| 30 |
+
score += 1; reasons.append("Price above MA (uptrend)")
|
| 31 |
+
else:
|
| 32 |
+
score -= 1; reasons.append("Price below MA (downtrend)")
|
| 33 |
+
|
| 34 |
+
# Bollinger extremes
|
| 35 |
+
if close_price < lower_band:
|
| 36 |
+
score += 1; reasons.append("Price below lower Bollinger Band (potential rebound)")
|
| 37 |
+
elif close_price > upper_band:
|
| 38 |
+
score -= 1; reasons.append("Price above upper Bollinger Band (potential pullback)")
|
| 39 |
+
|
| 40 |
+
if score >= 2:
|
| 41 |
+
return "Buy", "; ".join(reasons)
|
| 42 |
+
elif score <= -2:
|
| 43 |
+
return "Sell", "; ".join(reasons)
|
| 44 |
+
else:
|
| 45 |
+
return "Hold", "; ".join(reasons)
|