-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignal-bot.py
More file actions
executable file
·189 lines (155 loc) · 5.1 KB
/
signal-bot.py
File metadata and controls
executable file
·189 lines (155 loc) · 5.1 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import ccxt
import pandas as pd
import pandas_ta as ta
import time
from datetime import datetime
# Initialize the exchange
exchange = ccxt.binance({
'rateLimit': 1200,
'enableRateLimit': True,
})
def fetch_data(symbol, timeframe, limit=500):
"""Fetch historical data from Binance."""
ohlcv = exchange.fetch_ohlcv(symbol, timeframe, limit=limit)
df = pd.DataFrame(ohlcv, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
df.set_index('timestamp', inplace=True)
df.sort_index(inplace=True)
return df
def apply_indicators(df):
"""Apply a comprehensive set of technical indicators to the dataframe."""
# RSI
df['RSI'] = ta.rsi(df['close'], length=14)
# MACD
macd = ta.macd(df['close'], fast=12, slow=26, signal=9)
if isinstance(macd, pd.DataFrame):
df['MACD'] = macd['MACD_12_26_9']
df['MACD_signal'] = macd['MACDs_12_26_9']
df['MACD_hist'] = macd['MACDh_12_26_9']
# Bollinger Bands
bb = ta.bbands(df['close'], length=20, std=2)
if isinstance(bb, pd.DataFrame):
df['BB_upper'] = bb['BBU_20_2.0']
df['BB_middle'] = bb['BBM_20_2.0']
df['BB_lower'] = bb['BBL_20_2.0']
# ATR
df['ATR'] = ta.atr(df['high'], df['low'], df['close'], length=14)
# ADX
adx = ta.adx(df['high'], df['low'], df['close'], length=14)
if isinstance(adx, pd.DataFrame):
df['ADX'] = adx['ADX_14']
df['DI_plus'] = adx['DMP_14']
df['DI_minus'] = adx['DMN_14']
# Stochastic Oscillator
stoch = ta.stoch(df['high'], df['low'], df['close'])
if isinstance(stoch, pd.DataFrame):
df['Stoch_K'] = stoch['STOCHk_14_3_3']
df['Stoch_D'] = stoch['STOCHd_14_3_3']
# VWAP
df['VWAP'] = ta.vwap(df['high'], df['low'], df['close'], df['volume'])
# CCI
df['CCI'] = ta.cci(df['high'], df['low'], df['close'], length=20)
# ROC
df['ROC'] = ta.roc(df['close'], length=12)
# MFI
df['MFI'] = ta.mfi(df['high'], df['low'], df['close'], df['volume'], length=14)
# EMA
df['EMA_50'] = ta.ema(df['close'], length=50)
df['EMA_200'] = ta.ema(df['close'], length=200)
# Williams %R
df['Williams_%R'] = ta.willr(df['high'], df['low'], df['close'], length=14)
# Fill missing values
df.fillna(method='bfill', inplace=True)
df.fillna(method='ffill', inplace=True)
return df
def detect_trend(df):
"""Detect market trend using EMA crossover."""
if df['EMA_50'].iloc[-1] > df['EMA_200'].iloc[-1]:
return "UPTREND"
elif df['EMA_50'].iloc[-1] < df['EMA_200'].iloc[-1]:
return "DOWNTREND"
else:
return "RANGE"
def generate_composite_signal(df):
"""Generate a composite signal based on a scoring system."""
try:
trend = detect_trend(df)
close = df['close'].iloc[-1]
rsi = df['RSI'].iloc[-1]
macd = df['MACD'].iloc[-1]
macd_signal = df['MACD_signal'].iloc[-1]
bb_upper = df['BB_upper'].iloc[-1]
bb_lower = df['BB_lower'].iloc[-1]
atr = df['ATR'].iloc[-1]
adx = df['ADX'].iloc[-1]
stoch_k = df['Stoch_K'].iloc[-1]
stoch_d = df['Stoch_D'].iloc[-1]
cci = df['CCI'].iloc[-1]
roc = df['ROC'].iloc[-1]
mfi = df['MFI'].iloc[-1]
williams_r = df['Williams_%R'].iloc[-1]
score = 0
# RSI
if rsi < 30:
score += 1
elif rsi > 70:
score -= 1
# MACD
if macd > macd_signal:
score += 1
elif macd < macd_signal:
score -= 1
# Bollinger Bands
if close < bb_lower:
score += 1
elif close > bb_upper:
score -= 1
# Stochastic Oscillator
if stoch_k < 20 and stoch_d < 20:
score += 1
elif stoch_k > 80 and stoch_d > 80:
score -= 1
# CCI
if cci < -100:
score += 1
elif cci > 100:
score -= 1
# MFI
if mfi < 20:
score += 1
elif mfi > 80:
score -= 1
# Williams %R
if williams_r < -80:
score += 1
elif williams_r > -20:
score -= 1
# Trend adjustment
if trend == "UPTREND" and score > 0:
return "BUY"
elif trend == "DOWNTREND" and score < 0:
return "SELL"
else:
return "HOLD"
except Exception as e:
print(f"Error in signal generation: {e}")
return "HOLD"
def main():
symbol = 'BTC/USDT'
timeframe = '4h'
print("Starting the signal bot...")
while True:
try:
# Fetch and process data
df = fetch_data(symbol, timeframe)
df = apply_indicators(df)
# Generate signal
signal = generate_composite_signal(df)
# Print the latest signal with the current timestamp
print(f"{datetime.now()} - Latest Signal: {signal}")
except Exception as e:
print(f"Error: {e}")
# Sleep for 60 seconds
time.sleep(60)
if __name__ == "__main__":
main()