-
Notifications
You must be signed in to change notification settings - Fork 8
/
btcmapx.py
395 lines (298 loc) · 18 KB
/
btcmapx.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python3
import numpy as np
import talib
from binance.client import Client as BinanceClient
from binance.exceptions import BinanceAPIException
from scipy.stats import linregress
from numpy.polynomial.polynomial import Polynomial
import time
import gc
# Load credentials from file
with open("credentials.txt", "r") as f:
api_key = f.readline().strip()
api_secret = f.readline().strip()
# Instantiate Binance client
client = BinanceClient(api_key, api_secret)
symbol = "BTCUSDC"
timeframes = ["1m", "3m", "5m", "15m", "30m", "1h", "2h", "4h", "6h", "8h", "12h", "1d"]
candle_map = {}
# Define a function to get candles
def get_candles(symbol, timeframe, limit=1000):
try:
klines = client.get_klines(symbol=symbol, interval=timeframe, limit=limit)
return [{
"time": k[0] / 1000,
"open": float(k[1]),
"high": float(k[2]),
"low": float(k[3]),
"close": float(k[4]),
"volume": float(k[5])
} for k in klines]
except BinanceAPIException as e:
print(f"Error fetching candles for {symbol} at {timeframe}: {e}")
return []
# Helper function to remove NaNs and zeros from arrays
def remove_nans_and_zeros(*arrays):
arrays = [np.array(array) for array in arrays]
valid_mask = ~np.isnan(np.column_stack(arrays)).any(axis=1) & (np.column_stack(arrays) != 0).all(axis=1)
return [array[valid_mask] for array in arrays]
# Function to calculate the Volume Weighted Average Price (VWAP)
def calculate_vwap(candles):
close_prices = np.array([c["close"] for c in candles])
volumes = np.array([c["volume"] for c in candles])
close_prices, volumes = remove_nans_and_zeros(close_prices, volumes)
return np.sum(close_prices * volumes) / np.sum(volumes) if np.sum(volumes) != 0 else np.nan
# Define more functions for indicators (all previous functions here)
def calculate_ema(candles, timeperiod):
close_prices = np.array([c["close"] for c in candles])
close_prices, = remove_nans_and_zeros(close_prices)
ema = talib.EMA(close_prices, timeperiod=timeperiod)
return ema[-1] if len(ema) > 0 and not np.isnan(ema[-1]) and ema[-1] != 0 else np.nan
def calculate_rsi(candles, timeperiod=14):
close_prices = np.array([c["close"] for c in candles])
close_prices, = remove_nans_and_zeros(close_prices)
rsi = talib.RSI(close_prices, timeperiod=timeperiod)
return rsi[-1] if len(rsi) > 0 and not np.isnan(rsi[-1]) and rsi[-1] != 0 else np.nan
def calculate_macd(candles, fastperiod=12, slowperiod=26, signalperiod=9):
close_prices = np.array([c["close"] for c in candles])
close_prices, = remove_nans_and_zeros(close_prices)
macd, macdsignal, macdhist = talib.MACD(close_prices, fastperiod=fastperiod, slowperiod=slowperiod, signalperiod=signalperiod)
macd, macdsignal, macdhist = remove_nans_and_zeros(macd, macdsignal, macdhist)
return (macd[-1] if len(macd) > 0 and not np.isnan(macd[-1]) and macd[-1] != 0 else np.nan,
macdsignal[-1] if len(macdsignal) > 0 and not np.isnan(macdsignal[-1]) and macdsignal[-1] != 0 else np.nan,
macdhist[-1] if len(macdhist) > 0 and not np.isnan(macdhist[-1]) and macdhist[-1] != 0 else np.nan)
def calculate_momentum(candles, timeperiod=10):
close_prices = np.array([c["close"] for c in candles])
close_prices, = remove_nans_and_zeros(close_prices)
momentum = talib.MOM(close_prices, timeperiod=timeperiod)
return momentum[-1] if len(momentum) > 0 and not np.isnan(momentum[-1]) and momentum[-1] != 0 else np.nan
def calculate_regression_channels(candles):
if len(candles) < 50:
print("Not enough data for regression channel calculation")
return None, None, None, None
close_prices = np.array([c["close"] for c in candles])
x = np.arange(len(close_prices))
close_prices, x = remove_nans_and_zeros(close_prices, x)
if len(x) < 2:
print("Not enough valid data points for regression channel calculation")
return None, None, None, None
try:
slope, intercept, _, _, _ = linregress(x, close_prices)
regression_line = intercept + slope * x
deviation = close_prices - regression_line
std_dev = np.std(deviation)
regression_upper = regression_line + std_dev
regression_lower = regression_line - std_dev
regression_upper_value = regression_upper[-1] if not np.isnan(regression_upper[-1]) and regression_upper[-1] != 0 else None
regression_lower_value = regression_lower[-1] if not np.isnan(regression_lower[-1]) and regression_lower[-1] != 0 else None
current_close_value = close_prices[-1] if len(close_prices) > 0 and not np.isnan(close_prices[-1]) and close_prices[-1] != 0 else None
return regression_lower_value, regression_upper_value, (regression_upper_value + regression_lower_value) / 2, current_close_value
except Exception as e:
print(f"Error calculating regression channels: {e}")
return None, None, None, None
def calculate_polynomial_regression_channels(candles, degree=2):
if len(candles) < 50:
print("Not enough data for polynomial regression channel calculation")
return None, None, None, None
close_prices = np.array([c["close"] for c in candles])
x = np.arange(len(close_prices))
close_prices, x = remove_nans_and_zeros(close_prices, x)
if len(x) < 2:
print("Not enough valid data points for polynomial regression channel calculation")
return None, None, None, None
try:
coeffs = Polynomial.fit(x, close_prices, degree).convert().coef
poly = Polynomial(coeffs)
regression_line = poly(x)
deviation = close_prices - regression_line
std_dev = np.std(deviation)
regression_upper = regression_line + std_dev
regression_lower = regression_line - std_dev
regression_upper_value = regression_upper[-1] if not np.isnan(regression_upper[-1]) and regression_upper[-1] != 0 else None
regression_lower_value = regression_lower[-1] if not np.isnan(regression_lower[-1]) and regression_lower[-1] != 0 else None
current_close_value = close_prices[-1] if len(close_prices) > 0 and not np.isnan(close_prices[-1]) and close_prices[-1] != 0 else None
return regression_lower_value, regression_upper_value, (regression_upper_value + regression_lower_value) / 2, current_close_value
except Exception as e:
print(f"Error calculating polynomial regression channels: {e}")
return None, None, None, None
def calculate_fibonacci_retracement(high, low):
diff = high - low
retracement_levels = {
"0.0%": high,
"23.6%": high - 0.236 * diff,
"38.2%": high - 0.382 * diff,
"50.0%": high - 0.5 * diff,
"61.8%": high - 0.618 * diff,
"76.4%": high - 0.764 * diff,
"100.0%": low,
}
return retracement_levels
def calculate_zigzag_forecast(candles, depth=12, deviation=5, backstep=3):
highs = np.array([c['high'] for c in candles])
lows = np.array([c['low'] for c in candles])
highs, lows = remove_nans_and_zeros(highs, lows)
if len(highs) < depth:
return None, None, None
def zigzag_indicator(highs, lows, depth, deviation, backstep):
zigzag = np.zeros_like(highs)
last_pivot_low = 0
last_pivot_high = 0
current_trend = None
for i in range(depth, len(highs) - depth):
if highs[i] == max(highs[i - depth:i + depth]):
if highs[i] - lows[i] > deviation:
if last_pivot_high and highs[i] <= highs[last_pivot_high]:
continue
zigzag[i] = highs[i]
last_pivot_high = i
current_trend = 'down'
if lows[i] == min(lows[i - depth:i + depth]):
if highs[i] - lows[i] > deviation:
if last_pivot_low and lows[i] >= lows[last_pivot_low]:
continue
zigzag[i] = lows[i]
last_pivot_low = i
current_trend = 'up'
return zigzag, current_trend
zigzag, current_trend = zigzag_indicator(highs, lows, depth, deviation, backstep)
pivot_points = zigzag[zigzag != 0]
if len(pivot_points) < 2:
return None, None, None
high = max(pivot_points[-2:])
low = min(pivot_points[-2:])
fibonacci_levels = calculate_fibonacci_retracement(high, low)
first_incoming_value = None
if current_trend == 'up':
first_incoming_value = fibonacci_levels['23.6%']
elif current_trend == 'down':
first_incoming_value = fibonacci_levels['76.4%']
return first_incoming_value, current_trend, pivot_points
def scale_to_sine(timeframe):
close_prices = np.array([c["close"] for c in candle_map[timeframe]])
close_prices, = remove_nans_and_zeros(close_prices)
current_close = close_prices[-1]
sine_wave, leadsine = talib.HT_SINE(close_prices)
sine_wave = np.nan_to_num(sine_wave)
sine_wave = -sine_wave
current_sine = sine_wave[-1]
sine_wave_min = np.min(sine_wave)
sine_wave_max = np.max(sine_wave)
dist_min = ((current_sine - sine_wave_min) / (sine_wave_max - sine_wave_min)) * 100
dist_max = ((sine_wave_max - current_sine) / (sine_wave_max - sine_wave_min)) * 100
return dist_min, dist_max, current_sine
def calculate_thresholds(close_prices, period=14, minimum_percentage=3, maximum_percentage=3, range_distance=0.05):
close_prices = np.array(close_prices)
min_close = np.nanmin(close_prices)
max_close = np.nanmax(close_prices)
momentum = talib.MOM(close_prices, timeperiod=period)
min_momentum = np.nanmin(momentum)
max_momentum = np.nanmax(momentum)
min_percentage_custom = minimum_percentage / 100
max_percentage_custom = maximum_percentage / 100
min_threshold = np.minimum(min_close - (max_close - min_close) * min_percentage_custom, close_prices[-1])
max_threshold = np.maximum(max_close + (max_close - min_close) * max_percentage_custom, close_prices[-1])
range_price = np.linspace(close_prices[-1] * (1 - range_distance), close_prices[-1] * (1 + range_distance), num=50)
with np.errstate(invalid='ignore'):
filtered_close = np.where(close_prices < min_threshold, min_threshold, close_prices)
filtered_close = np.where(filtered_close > max_threshold, max_threshold, filtered_close)
avg_mtf = np.nanmean(filtered_close)
current_momentum = momentum[-1]
return min_threshold, max_threshold, avg_mtf, current_momentum, range_price
def calculate_distances_and_ratios(candles):
if len(candles) < 2:
return None, None, None
current_close = candles[-1]['close']
highs = np.array([c["high"] for c in candles])
lows = np.array([c["low"] for c in candles])
last_major_high = np.max(highs)
last_major_low = np.min(lows)
distance_to_high = last_major_high - current_close
distance_to_low = current_close - last_major_low
total_range = last_major_high - last_major_low
percent_to_high = (distance_to_high / total_range) * 100 if total_range != 0 else np.nan
percent_to_low = (distance_to_low / total_range) * 100 if total_range != 0 else np.nan
return distance_to_high, distance_to_low, percent_to_high, percent_to_low, last_major_high, last_major_low
# Function to calculate min/max thresholds based on the last 500 close prices
def calculate_min_max_thresholds(candles):
if len(candles) < 500:
close_prices = np.array([c["close"] for c in candles])
else:
close_prices = np.array([c["close"] for c in candles[-500:]]) # Limit to last 500
close_prices, = remove_nans_and_zeros(close_prices)
min_close = np.nanmin(close_prices)
max_close = np.nanmax(close_prices)
return min_close, max_close
# Main function to analyze timeframes
def analyze_timeframes():
global candle_map
while True:
double_bottom_detection_summary = {}
last_major_reversal_summary = {}
# Fetch new candles for all timeframes
for timeframe in timeframes:
candle_map[timeframe] = get_candles(symbol, timeframe)
for timeframe in timeframes:
candles = candle_map[timeframe]
close_prices = np.array([c["close"] for c in candles])
print(f"\nAnalyzing {timeframe}...")
vwap = calculate_vwap(candles)
print(f"VWAP: {vwap:.2f}")
ema_50 = calculate_ema(candles, timeperiod=50)
print(f"EMA 50: {ema_50:.2f}")
rsi = calculate_rsi(candles, timeperiod=14)
print(f"RSI 14: {rsi:.2f}")
macd, macdsignal, macdhist = calculate_macd(candles)
print(f"MACD: {macd:.2f}, Signal: {macdsignal:.2f}, Histogram: {macdhist:.2f}")
momentum = calculate_momentum(candles, timeperiod=10)
print(f"Momentum: {momentum:.2f}")
reg_lower, reg_upper, reg_avg, current_close_value = calculate_regression_channels(candles)
print(f"Regression Lower: {reg_lower:.2f}, Upper: {reg_upper:.2f}, Avg: {reg_avg:.2f}")
poly_reg_lower, poly_reg_upper, poly_reg_avg, current_close_value = calculate_polynomial_regression_channels(candles)
print(f"Polynomial Regression Lower: {poly_reg_lower:.2f}, Upper: {poly_reg_upper:.2f}, Avg: {poly_reg_avg:.2f}")
fib_high = np.max([c["high"] for c in candles])
fib_low = np.min([c["low"] for c in candles])
fibonacci_levels = calculate_fibonacci_retracement(fib_high, fib_low)
print(f"Fibonacci Retracement Levels: {fibonacci_levels}")
first_incoming_value, trend, pivots = calculate_zigzag_forecast(candles)
print(f"ZigZag Forecast First Incoming Value: {first_incoming_value:.2f}, Trend: {trend}, Pivot Points: {pivots}")
dist_min, dist_max, current_sine = scale_to_sine(timeframe)
print(f"Sine Scaling Distance to Min: {dist_min:.2f}%, Max: {dist_max:.2f}%, Current Sine: {current_sine:.2f}")
# Calculate min and max thresholds based on the last 500 close prices
min_threshold, max_threshold, avg_mtf, current_momentum, range_price = calculate_thresholds(close_prices, period=14, minimum_percentage=2, maximum_percentage=2, range_distance=0.05)
print(f"Thresholds: Min {min_threshold:.2f}, Max {max_threshold:.2f}, Avg MTF: {avg_mtf:.2f}, Momentum Signal: {current_momentum:.2f}")
distance_to_high, distance_to_low, percent_to_high, percent_to_low, last_major_high, last_major_low = calculate_distances_and_ratios(candles)
print(f"Distance to Last Major High: {distance_to_high:.2f}, Low: {distance_to_low:.2f}, Percent High: {percent_to_high:.2f}%, Low: {percent_to_low:.2f}%")
distance_between_thresholds = max_threshold - min_threshold
last_major_reversal = last_major_high if last_major_high > close_prices[-1] else last_major_low
distance_from_last_reversal = abs(last_major_reversal - close_prices[-1])
reversal_type = "TOP" if last_major_high == last_major_reversal else "DIP"
last_major_reversal_summary[timeframe] = reversal_type
total_distance = distance_between_thresholds + distance_from_last_reversal
symmetrical_percentage_thresholds = (distance_between_thresholds / total_distance) * 100 if total_distance != 0 else np.nan
symmetrical_percentage_last_reversal = (distance_from_last_reversal / total_distance) * 100 if total_distance != 0 else np.nan
print(f"Distance Between Thresholds: {distance_between_thresholds:.2f}")
print(f"Distance from Last Major Reversal to Current Close: {distance_from_last_reversal:.2f}")
print(f"Symmetrical Percentage: {symmetrical_percentage_thresholds:.2f}% (Thresholds), {symmetrical_percentage_last_reversal:.2f}% (Last Major Reversal)")
print(f"Last Major Reversal Found at: {reversal_type}")
closest_threshold = min(min_threshold, max_threshold, key=lambda x: abs(x - close_prices[-1]))
is_closest_max = (closest_threshold == max_threshold)
is_closest_min = (closest_threshold == min_threshold)
print(f"Is the last maximum value closest to current close? {'True' if is_closest_max else 'False'}")
print(f"Is the last minimum value closest to current close? {'True' if is_closest_min else 'False'}")
# Check for double bottom detection for the current timeframe
double_bottom_detected = False
if symmetrical_percentage_thresholds <= symmetrical_percentage_last_reversal and reversal_type == "DIP":
print("Potential double bottom detected.")
double_bottom_detected = True
elif symmetrical_percentage_thresholds > symmetrical_percentage_last_reversal and reversal_type == "TOP":
print("Potential double bottom not yet detected.")
double_bottom_detection_summary[timeframe] = double_bottom_detected
# Print overall results for each iteration
print(f"\nOverall Double Bottom Pattern Detected: {'Yes' if any(double_bottom_detection_summary.values()) else 'No'}")
print(f"Overall Last Major Reversal Found: {last_major_reversal_summary}")
for timeframe in timeframes:
print(f"Timeframe: {timeframe} - Double Bottom Detected: {'Yes' if double_bottom_detection_summary[timeframe] else 'No'}, Last Major Reversal: {last_major_reversal_summary[timeframe]}")
gc.collect()
time.sleep(5)
if __name__ == "__main__":
analyze_timeframes()