-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignals.py
132 lines (96 loc) · 3.88 KB
/
signals.py
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
import ccxt
import time
# Initialize Binance API (No API Key needed for public data)
exchange = ccxt.binance()
# List of cryptocurrency pairs
COINS = ["BTC/USDT", "ETH/USDT", "SOL/USDT", "ADA/USDT", "XRP/USDT", "BNB/USDT", "DOGE/USDT", "SHIB/USDT", "TRX/USDT", "TRUMP/USDT"]
# RSI Parameters
TIMEFRAME = "3m" # Binance supports 8-hour timeframe
RSI_PERIOD = 14 # RSI calculation period
OVERBOUGHT = 70
OVERSOLD = 30
def fetch_ohlcv(symbol):
"""Fetch OHLCV data from Binance."""
try:
return exchange.fetch_ohlcv(symbol, timeframe=TIMEFRAME, limit=RSI_PERIOD + 5)
except Exception as e:
print(f"Error fetching OHLCV for {symbol}: {e}")
return None
def calculate_rsi(prices):
"""Calculate RSI based on closing prices."""
if len(prices) < RSI_PERIOD + 1:
return None # Not enough data
gains, losses = [], []
for i in range(1, RSI_PERIOD + 1):
change = prices[i] - prices[i - 1]
gains.append(max(change, 0))
losses.append(abs(min(change, 0)))
avg_gain = sum(gains) / RSI_PERIOD
avg_loss = sum(losses) / RSI_PERIOD
if avg_loss == 0:
return 100 # Avoid division by zero, assume max RSI
rs = avg_gain / avg_loss
rsi = 100 - (100 / (1 + rs))
return round(rsi, 2)
def fetch_rsi(symbol):
"""Fetch RSI indicator for a given cryptocurrency pair from Binance."""
ohlcv = fetch_ohlcv(symbol)
if not ohlcv:
return None
prices = [candle[4] for candle in ohlcv] # Closing prices
return calculate_rsi(prices)
def fetch_price(symbol):
"""Fetch the latest price for a given cryptocurrency pair."""
try:
ticker = exchange.fetch_ticker(symbol)
return ticker['last']
except Exception as e:
print(f"Error fetching price for {symbol}: {e}")
return None
def detect_divergence(symbol):
"""Определяет дивергенцию и ее силу"""
ohlcv = fetch_ohlcv(symbol)
if not ohlcv:
return None
prices = [candle[4] for candle in ohlcv] # Закрытые цены
rsi_values = [calculate_rsi(prices[i:i + RSI_PERIOD + 1]) for i in range(len(prices) - RSI_PERIOD)]
rsi_values = [rsi for rsi in rsi_values if rsi is not None]
if len(rsi_values) < 3:
return None
recent_prices = prices[-3:]
recent_rsi = rsi_values[-3:]
price_change = abs(recent_prices[2] - recent_prices[0])
rsi_change = abs(recent_rsi[2] - recent_rsi[0])
# Классификация по разнице RSI
if rsi_change >= 10:
strength = "Strong 💪"
elif rsi_change >= 5:
strength = "Moderate ⚖"
else:
strength = "Weak ❌"
# Бычья дивергенция
if recent_prices[0] > recent_prices[2] and recent_rsi[0] < recent_rsi[2]:
return f"Bullish Divergence 🟢 ({strength})"
# Медвежья дивергенция
if recent_prices[0] < recent_prices[2] and recent_rsi[0] > recent_rsi[2]:
return f"Bearish Divergence 🔴 ({strength})"
return "No Divergence"
def check_rsi_signals():
"""Fetch RSI and price, then print buy/sell signals and divergence."""
print("\n=== RSI SIGNAL BOT (BINANCE) ===\n")
for symbol in COINS:
rsi = fetch_rsi(symbol)
price = fetch_price(symbol)
divergence = detect_divergence(symbol)
if rsi is None or price is None:
print(f"{symbol}: Unable to retrieve RSI or price data\n")
continue
signal = "NEUTRAL"
if rsi < OVERSOLD:
signal = "BUY 📈 (Oversold)"
elif rsi > OVERBOUGHT:
signal = "SELL 📉 (Overbought)"
print(f"{symbol} - Price: ${price:.2f} | RSI: {rsi} | Signal: {signal} | {divergence}\n")
while True:
check_rsi_signals()
time.sleep(20)