-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalyzer.py
More file actions
781 lines (628 loc) · 32 KB
/
Copy pathanalyzer.py
File metadata and controls
781 lines (628 loc) · 32 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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
"""
Moduł analizy rynku
Zawiera funkcje do analizy danych, predykcji i generowania sygnałów
"""
import os
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import Sequential, load_model
from sklearn.preprocessing import MinMaxScaler
import ccxt
import logging
import requests
import random
from datetime import datetime
import ta
import time
import config
logger = logging.getLogger("analyzer")
class MarketAnalyzer:
"""
Klasa do analizy rynku kryptowalut:
- Pobieranie danych
- Analiza techniczna
- Analiza sentymentu
- Predykcja cen modelem LSTM
"""
def __init__(self):
"""Inicjalizacja analizatora rynku."""
logger.info("Inicjalizacja analizatora rynku")
self.exchange = self._initialize_exchange()
self.model = None
self.scaler_price = MinMaxScaler(feature_range=(0, 1))
self.scaler_features = MinMaxScaler(feature_range=(0, 1))
def _initialize_exchange(self):
"""Inicjalizacja połączenia z giełdą."""
try:
exchange_id = config.EXCHANGE["name"]
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
'apiKey': config.EXCHANGE["api_key"],
'secret': config.EXCHANGE["api_secret"],
'enableRateLimit': True,
'options': {'defaultType': 'future' if config.EXCHANGE["use_futures"] else 'spot'}
})
# Testnet
if config.EXCHANGE["testnet"]:
if hasattr(exchange, 'set_sandbox_mode'):
exchange.set_sandbox_mode(True)
logger.info(f"Połączono z giełdą {exchange_id}")
return exchange
except Exception as e:
logger.error(f"Błąd podczas inicjalizacji giełdy: {e}")
return None
def fetch_market_data(self, symbol=None, timeframe=None, limit=1000, since=None):
"""
Pobieranie danych historycznych z giełdy.
Args:
symbol (str): Symbol instrumentu
timeframe (str): Interwał czasowy
limit (int): Liczba świec do pobrania
since (int): Timestamp początkowy w ms
Returns:
DataFrame: DataFrame z danymi historycznymi
"""
try:
if symbol is None:
symbol = config.SYMBOL
if timeframe is None:
timeframe = config.TIMEFRAME
if self.exchange is None:
raise Exception("Brak połączenia z giełdą")
logger.info(f"Pobieranie {limit} świec dla {symbol} na interwale {timeframe}")
ohlcv = self.exchange.fetch_ohlcv(symbol, timeframe, limit=limit, since=since)
df = pd.DataFrame(ohlcv, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
# Dodanie wskaźników technicznych
df = self.add_technical_indicators(df)
logger.info(f"Pobrano {len(df)} świec historycznych")
return df
except Exception as e:
logger.error(f"Błąd podczas pobierania danych: {e}")
return pd.DataFrame()
def fetch_historical_data(self, symbol=None, timeframe=None, start_date=None, end_date=None, save_path=None):
"""
Pobieranie danych historycznych z giełdy z implementacją paginacji dla dłuższych zakresów czasowych.
Args:
symbol (str): Symbol instrumentu (np. BTC/USDT)
timeframe (str): Interwał czasowy (np. 1h, 4h, 1d)
start_date (str): Data początkowa w formacie 'YYYY-MM-DD'
end_date (str): Data końcowa w formacie 'YYYY-MM-DD'
save_path (str): Ścieżka do zapisu danych w formacie CSV (opcjonalnie)
Returns:
DataFrame: DataFrame z danymi historycznymi
"""
try:
if symbol is None:
symbol = config.SYMBOL
if timeframe is None:
timeframe = config.TIMEFRAME
# Konwersja dat na timestamp (ms)
if start_date:
start_ts = int(datetime.strptime(start_date, '%Y-%m-%d').timestamp() * 1000)
else:
start_ts = None
if end_date:
end_ts = int(datetime.strptime(end_date, '%Y-%m-%d').timestamp() * 1000)
else:
end_ts = int(datetime.now().timestamp() * 1000)
logger.info(f"Pobieranie danych historycznych dla {symbol} na interwale {timeframe} "
f"od {start_date if start_date else 'początku'} "
f"do {end_date if end_date else 'teraz'}")
# Sprawdzenie czy plik z danymi już istnieje
if save_path and os.path.exists(save_path):
logger.info(f"Znaleziono istniejący plik z danymi: {save_path}")
df = pd.read_csv(save_path)
# Sprawdzenie czy plik zawiera wszystkie potrzebne dane
if 'timestamp' in df.columns:
df['timestamp'] = pd.to_datetime(df['timestamp'])
min_date = df['timestamp'].min()
max_date = df['timestamp'].max()
logger.info(f"Zakres dat w pliku: od {min_date} do {max_date}")
# Jeśli plik zawiera wymagany zakres dat, zwracamy go
if ((not start_date or min_date <= pd.to_datetime(start_date)) and
(not end_date or max_date >= pd.to_datetime(end_date))):
logger.info("Plik zawiera wszystkie potrzebne dane. Używam istniejących danych.")
# Filtracja według żądanego zakresu
if start_date:
df = df[df['timestamp'] >= pd.to_datetime(start_date)]
if end_date:
df = df[df['timestamp'] <= pd.to_datetime(end_date)]
# Dodanie wskaźników technicznych
df = self.add_technical_indicators(df)
return df
# Inicjalizacja połączenia z giełdą, jeśli nie jest jeszcze ustanowione
if self.exchange is None:
self.exchange = self._initialize_exchange()
if self.exchange is None:
raise Exception("Brak połączenia z giełdą")
# Maksymalna liczba świec na jedno zapytanie
max_limit = 1000
# Lista do przechowywania wszystkich pobranych danych
all_candles = []
# Początkowy timestamp dla paginacji
current_ts = start_ts
# Pętla paginacji - pobieramy dane w porcjach
while True:
# Jeśli dotarliśmy do końca zakresu, kończymy
if current_ts and end_ts and current_ts >= end_ts:
break
try:
# Określenie końcowego timestamp dla tego zapytania
temp_end_ts = None
if end_ts:
temp_end_ts = min(end_ts, current_ts + (max_limit * self._get_timeframe_ms(timeframe)))
logger.info(f"Pobieranie porcji danych od {datetime.fromtimestamp(current_ts/1000).strftime('%Y-%m-%d %H:%M') if current_ts else 'początku'} "
f"do {datetime.fromtimestamp(temp_end_ts/1000).strftime('%Y-%m-%d %H:%M') if temp_end_ts else 'teraz'}")
# Pobieranie porcji danych
candles = self.exchange.fetch_ohlcv(
symbol,
timeframe,
limit=max_limit,
since=current_ts
)
if not candles:
logger.info("Brak dalszych danych do pobrania.")
break
logger.info(f"Pobrano {len(candles)} świec.")
all_candles.extend(candles)
# Przygotowanie do następnej iteracji - pobieramy timestamp ostatniej świecy + 1ms
if candles:
last_candle_ts = candles[-1][0]
current_ts = last_candle_ts + 1
else:
break
# Sprawdzenie czy dotarliśmy do końca zakresu
if end_ts and current_ts >= end_ts:
break
# Dodanie opóźnienia między zapytaniami, aby nie przekroczyć limitów API
time.sleep(0.5) # 500ms opóźnienia
except Exception as e:
logger.error(f"Błąd podczas pobierania porcji danych: {e}")
# Próbujemy kontynuować mimo błędu
time.sleep(2) # Dłuższe opóźnienie po błędzie
continue
# Konwersja wszystkich pobranych danych do DataFrame
if not all_candles:
logger.warning("Nie pobrano żadnych danych.")
return pd.DataFrame()
df = pd.DataFrame(all_candles, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
# Usunięcie ewentualnych duplikatów (może się zdarzyć przy paginacji)
df = df.drop_duplicates(subset=['timestamp'])
# Sortowanie według timestampu
df = df.sort_values('timestamp')
# Filtracja według żądanego zakresu (na wszelki wypadek)
if start_date:
df = df[df['timestamp'] >= pd.to_datetime(start_date)]
if end_date:
df = df[df['timestamp'] <= pd.to_datetime(end_date)]
# Zapisanie danych do pliku CSV, jeśli podano ścieżkę
if save_path:
os.makedirs(os.path.dirname(os.path.abspath(save_path)), exist_ok=True)
df.to_csv(save_path, index=False)
logger.info(f"Zapisano dane do pliku: {save_path}")
# Dodanie wskaźników technicznych
df = self.add_technical_indicators(df)
logger.info(f"Zakończono pobieranie danych. Łącznie pozyskano {len(df)} świec.")
return df
except Exception as e:
logger.error(f"Błąd podczas pobierania danych historycznych: {e}")
return pd.DataFrame()
def _get_timeframe_ms(self, timeframe):
"""
Konwersja interwału czasowego na milisekundy.
Args:
timeframe (str): Interwał w formacie np. '1h', '4h', '1d'
Returns:
int: Liczba milisekund odpowiadająca interwałowi
"""
# Parsowanie wartości i jednostki
if timeframe.endswith('m'):
return int(timeframe[:-1]) * 60 * 1000
elif timeframe.endswith('h'):
return int(timeframe[:-1]) * 60 * 60 * 1000
elif timeframe.endswith('d'):
return int(timeframe[:-1]) * 24 * 60 * 60 * 1000
elif timeframe.endswith('w'):
return int(timeframe[:-1]) * 7 * 24 * 60 * 60 * 1000
else:
# Domyślnie zakładamy minuty
return int(timeframe) * 60 * 1000
def add_technical_indicators(self, df):
"""
Dodanie wskaźników technicznych do danych.
Args:
df (DataFrame): DataFrame z danymi OHLCV
Returns:
DataFrame: DataFrame z dodanymi wskaźnikami
"""
try:
if df.empty:
return df
# Upewnienie się, że dane są typu float64 do obliczeń
for col in ['open', 'high', 'low', 'close', 'volume']:
if col in df.columns:
df[col] = df[col].astype('float64')
# RSI
if len(df) >= 14:
df['rsi'] = ta.momentum.RSIIndicator(df['close'], window=14).rsi()
# MACD
if len(df) >= 26:
macd_indicator = ta.trend.MACD(
df['close'],
window_slow=26,
window_fast=12,
window_sign=9
)
df['macd'] = macd_indicator.macd()
df['macdsignal'] = macd_indicator.macd_signal()
df['macdhist'] = macd_indicator.macd_diff()
# Bollinger Bands
if len(df) >= 20:
bollinger = ta.volatility.BollingerBands(
df['close'],
window=20,
window_dev=2
)
df['bb_upper'] = bollinger.bollinger_hband()
df['bb_middle'] = bollinger.bollinger_mavg()
df['bb_lower'] = bollinger.bollinger_lband()
# EMA
if len(df) >= 9:
df['ema9'] = ta.trend.EMAIndicator(df['close'], window=9).ema_indicator()
if len(df) >= 20:
df['ema20'] = ta.trend.EMAIndicator(df['close'], window=20).ema_indicator()
if len(df) >= 50:
df['ema50'] = ta.trend.EMAIndicator(df['close'], window=50).ema_indicator()
if len(df) >= 200:
df['ema200'] = ta.trend.EMAIndicator(df['close'], window=200).ema_indicator()
# ATR - Average True Range
if len(df) >= 14:
df['atr'] = ta.volatility.AverageTrueRange(df['high'], df['low'], df['close'], window=14).average_true_range()
# Stochastic Oscillator
if len(df) >= 14:
stoch = ta.momentum.StochasticOscillator(
df['high'],
df['low'],
df['close'],
window=14,
smooth_window=3
)
df['stoch_k'] = stoch.stoch()
df['stoch_d'] = stoch.stoch_signal()
# ADX - Average Directional Index
if len(df) >= 14:
adx = ta.trend.ADXIndicator(df['high'], df['low'], df['close'], window=14)
df['adx'] = adx.adx()
df['adx_pos'] = adx.adx_pos()
df['adx_neg'] = adx.adx_neg()
# CCI - Commodity Channel Index
if len(df) >= 20:
df['cci'] = ta.trend.CCIIndicator(df['high'], df['low'], df['close'], window=20).cci()
# Williams %R
if len(df) >= 14:
df['willr'] = ta.momentum.WilliamsRIndicator(df['high'], df['low'], df['close'], lbp=14).williams_r()
# OBV - On Balance Volume
df['obv'] = ta.volume.OnBalanceVolumeIndicator(df['close'], df['volume']).on_balance_volume()
# Procentowa zmiana ceny
df['pct_change'] = df['close'].pct_change()
# Dodatkowe cechy pochodne
if all(col in df.columns for col in ['close', 'bb_upper', 'bb_lower']):
# Pozycja ceny w kanale Bollinger Bands (0-1)
df['bb_position'] = (df['close'] - df['bb_lower']) / (df['bb_upper'] - df['bb_lower'])
if all(col in df.columns for col in ['close', 'ema20']):
# Odległość ceny od EMA20
df['ema20_distance'] = (df['close'] - df['ema20']) / df['ema20']
return df
except Exception as e:
logger.error(f"Błąd podczas dodawania wskaźników technicznych: {e}")
return df
def load_model(self, model_path=None):
"""
Wczytanie modelu LSTM.
Args:
model_path (str): Ścieżka do modelu
Returns:
bool: True jeśli wczytano pomyślnie
"""
try:
if model_path is None:
# Szukaj najnowszego modelu
models = [f for f in os.listdir(config.MODELS_DIR) if f.endswith('.h5')]
if not models:
logger.warning("Nie znaleziono żadnego modelu")
return False
# Wybierz najnowszy model
model_path = os.path.join(config.MODELS_DIR,
max(models, key=lambda x: os.path.getmtime(os.path.join(config.MODELS_DIR, x))))
self.model = load_model(model_path)
# Wczytanie skaler'ów jeśli istnieją
try:
import pickle
scaler_price_path = model_path.replace('.h5', '_price_scaler.pkl')
scaler_features_path = model_path.replace('.h5', '_features_scaler.pkl')
if os.path.exists(scaler_price_path):
with open(scaler_price_path, 'rb') as f:
self.scaler_price = pickle.load(f)
if os.path.exists(scaler_features_path):
with open(scaler_features_path, 'rb') as f:
self.scaler_features = pickle.load(f)
except Exception as e:
logger.warning(f"Nie udało się wczytać scalerów: {e}")
logger.info(f"Model wczytany: {model_path}")
return True
except Exception as e:
logger.error(f"Błąd podczas wczytywania modelu: {e}")
return False
def get_feature_list(self):
"""Zwraca listę cech używanych przez model."""
base_features = ['open', 'high', 'low', 'close', 'volume']
# Dodatkowe cechy zależne od konfiguracji
technical_features = []
if "rsi" in config.TECHNICAL_INDICATORS:
technical_features.append('rsi')
if "macd" in config.TECHNICAL_INDICATORS:
technical_features.extend(['macd', 'macdsignal', 'macdhist'])
if "bollinger_bands" in config.TECHNICAL_INDICATORS:
technical_features.extend(['bb_upper', 'bb_middle', 'bb_lower'])
if "ema" in config.TECHNICAL_INDICATORS:
technical_features.extend(['ema9', 'ema20', 'ema50', 'ema200'])
if "atr" in config.TECHNICAL_INDICATORS:
technical_features.append('atr')
# Jeśli nie ma żadnych wskaźników w konfiguracji, dodaj domyślne
if not technical_features:
technical_features = ['rsi', 'macd', 'bb_upper', 'bb_lower', 'ema20']
return base_features + technical_features
def preprocess_data(self, df):
"""
Przetwarzanie danych dla modelu LSTM.
Args:
df (DataFrame): DataFrame z danymi historycznymi
Returns:
tuple: X, y dane dla modelu
"""
try:
# Sprawdzenie czy DataFrame ma wystarczającą liczbę wierszy
if len(df) < config.MODEL["sequence_length"]:
logger.error(f"Za mało danych do przetworzenia: {len(df)} < {config.MODEL['sequence_length']}")
return None, None
# Usunięcie wierszy z NaN
df = df.dropna()
# Lista cech
feature_columns = self.get_feature_list()
# Separujemy cenę zamknięcia do predykcji
price_data = df['close'].values.reshape(-1, 1)
scaled_price = self.scaler_price.fit_transform(price_data)
# Sprawdzenie czy wszystkie kolumny istnieją
missing_columns = [col for col in feature_columns if col not in df.columns]
if missing_columns:
logger.warning(f"Brakujące kolumny: {missing_columns}")
feature_columns = [col for col in feature_columns if col in df.columns]
# Skalujemy cechy
feature_data = df[feature_columns].values
scaled_features = self.scaler_features.fit_transform(feature_data)
X, y = [], []
sequence_length = config.MODEL["sequence_length"]
for i in range(len(scaled_features) - sequence_length):
X.append(scaled_features[i:i + sequence_length])
y.append(scaled_price[i + sequence_length])
return np.array(X), np.array(y)
except Exception as e:
logger.error(f"Błąd podczas przetwarzania danych: {e}")
return None, None
def predict_price_movement(self, df):
"""
Przewidywanie ruchu ceny na podstawie modelu LSTM.
Args:
df (DataFrame): DataFrame z danymi historycznymi
Returns:
dict: Słownik z predykcją
"""
try:
# Sprawdzenie czy model jest wczytany
if self.model is None:
if not self.load_model():
logger.error("Brak modelu do predykcji")
return None
# Aktualna cena
current_price = df['close'].iloc[-1]
# Przetworzenie danych dla modelu
X, _ = self.preprocess_data(df)
if X is None:
logger.error("Błąd podczas przetwarzania danych dla predykcji")
return None
# Predykcja
X_pred = X[-1:] # Ostatnia sekwencja
scaled_prediction = self.model.predict(X_pred)
# Odwrócenie skalowania
predicted_price = self.scaler_price.inverse_transform(scaled_prediction)[0][0]
# Obliczanie procentowej zmiany
percent_change = ((predicted_price - current_price) / current_price) * 100
# Określenie kierunku ruchu
threshold = config.MODEL["prediction_threshold"]
if abs(percent_change) < threshold:
direction = "FLAT"
elif percent_change > 0:
direction = "UP"
else:
direction = "DOWN"
# Przygotowanie wyniku
prediction = {
'current_price': current_price,
'predicted_price': predicted_price,
'percent_change': percent_change,
'direction': direction
}
logger.info(f"Predykcja: Aktualna cena={current_price:.2f}, "
f"Przewidywana={predicted_price:.2f}, Zmiana={percent_change:.2f}%, "
f"Kierunek={direction}")
return prediction
except Exception as e:
logger.error(f"Błąd podczas predykcji ceny: {e}")
return None
def generate_ta_signal(self, df):
"""
Generowanie sygnału na podstawie analizy technicznej.
Args:
df (DataFrame): DataFrame z danymi i wskaźnikami
Returns:
str: Sygnał ('LONG', 'SHORT', 'FLAT')
"""
try:
if df.empty or len(df) < 14: # Minimalna liczba punktów
return "FLAT"
# Zliczanie głosów dla każdego sygnału
votes = {"LONG": 0, "SHORT": 0, "FLAT": 0}
# 1. RSI
if 'rsi' in df.columns:
rsi = df['rsi'].iloc[-1]
if rsi < 30:
votes["LONG"] += 1
elif rsi > 70:
votes["SHORT"] += 1
else:
votes["FLAT"] += 1
# 2. MACD
if all(col in df.columns for col in ['macd', 'macdsignal']):
if len(df) >= 2:
macd_current = df['macd'].iloc[-1]
macd_prev = df['macd'].iloc[-2]
signal_current = df['macdsignal'].iloc[-1]
signal_prev = df['macdsignal'].iloc[-2]
# Przecięcie w górę (byczy)
if macd_prev < signal_prev and macd_current > signal_current:
votes["LONG"] += 1
# Przecięcie w dół (niedźwiedzi)
elif macd_prev > signal_prev and macd_current < signal_current:
votes["SHORT"] += 1
else:
# Trend na podstawie wartości MACD
if macd_current > 0:
votes["LONG"] += 0.5
elif macd_current < 0:
votes["SHORT"] += 0.5
else:
votes["FLAT"] += 0.5
# 3. Bollinger Bands
if all(col in df.columns for col in ['bb_upper', 'bb_lower']):
current_price = df['close'].iloc[-1]
upper_band = df['bb_upper'].iloc[-1]
lower_band = df['bb_lower'].iloc[-1]
if current_price > upper_band:
votes["SHORT"] += 1 # Potencjalne przewartościowanie
elif current_price < lower_band:
votes["LONG"] += 1 # Potencjalne niedowartościowanie
else:
# Miejsce w kanale Bollingera
width = upper_band - lower_band
if width > 0:
position = (current_price - lower_band) / width
if position < 0.3:
votes["LONG"] += 0.5
elif position > 0.7:
votes["SHORT"] += 0.5
else:
votes["FLAT"] += 0.5
# 4. EMA
if all(col in df.columns for col in ['ema9', 'ema20']):
ema9 = df['ema9'].iloc[-1]
ema20 = df['ema20'].iloc[-1]
price = df['close'].iloc[-1]
# Trend wzrostowy: EMA9 > EMA20 i cena > EMA9
if ema9 > ema20 and price > ema9:
votes["LONG"] += 1
# Trend spadkowy: EMA9 < EMA20 i cena < EMA9
elif ema9 < ema20 and price < ema9:
votes["SHORT"] += 1
else:
votes["FLAT"] += 0.5
# 5. Stochastic Oscillator
if all(col in df.columns for col in ['stoch_k', 'stoch_d']):
stoch_k = df['stoch_k'].iloc[-1]
stoch_d = df['stoch_d'].iloc[-1]
if stoch_k < 20 and stoch_d < 20:
votes["LONG"] += 1 # Wyprzedanie
elif stoch_k > 80 and stoch_d > 80:
votes["SHORT"] += 1 # Wykupienie
elif stoch_k > stoch_d and stoch_k < 80 and stoch_d < 80:
votes["LONG"] += 0.5 # Przecięcie w górę w normalnym zakresie
elif stoch_k < stoch_d and stoch_k > 20 and stoch_d > 20:
votes["SHORT"] += 0.5 # Przecięcie w dół w normalnym zakresie
# 6. ADX
if 'adx' in df.columns and 'adx_pos' in df.columns and 'adx_neg' in df.columns:
adx = df['adx'].iloc[-1]
adx_pos = df['adx_pos'].iloc[-1]
adx_neg = df['adx_neg'].iloc[-1]
if adx > 25: # Silny trend
if adx_pos > adx_neg:
votes["LONG"] += 1 # Silny trend wzrostowy
else:
votes["SHORT"] += 1 # Silny trend spadkowy
else:
votes["FLAT"] += 0.5 # Słaby trend
# 7. CCI
if 'cci' in df.columns:
cci = df['cci'].iloc[-1]
if cci < -100:
votes["LONG"] += 0.5 # Wyprzedanie
elif cci > 100:
votes["SHORT"] += 0.5 # Wykupienie
# Wybór sygnału z największą liczbą głosów
signal = max(votes.items(), key=lambda x: x[1])[0]
logger.info(f"Sygnał TA: {signal} (głosy: LONG={votes['LONG']}, "
f"SHORT={votes['SHORT']}, FLAT={votes['FLAT']})")
return signal
except Exception as e:
logger.error(f"Błąd podczas generowania sygnału TA: {e}")
return "FLAT" # W przypadku błędu zwracamy neutralny sygnał
def analyze_market_sentiment(self):
"""
Analiza sentymentu rynkowego (uproszczona).
Returns:
float: Wartość sentymentu w zakresie [-1, 1]
"""
try:
# W tej uproszczonej wersji używamy tylko Fear & Greed Index
# i losowego szumu dla symulacji innych źródeł
# Fear & Greed Index
try:
url = "https://api.alternative.me/fng/"
response = requests.get(url, timeout=5)
if response.status_code == 200:
data = response.json()
if 'data' in data and len(data['data']) > 0:
fear_greed_value = int(data['data'][0]['value'])
# Normalizacja do [-1, 1]
normalized_fear_greed = (fear_greed_value - 50) / 50
logger.info(f"Fear & Greed Index: {fear_greed_value} -> {normalized_fear_greed:.2f}")
# Dodanie niewielkiego losowego szumu
noise = random.uniform(-0.1, 0.1)
sentiment = normalized_fear_greed + noise
sentiment = max(min(sentiment, 1.0), -1.0) # Ograniczenie do [-1, 1]
return sentiment
except Exception as e:
logger.warning(f"Błąd podczas pobierania Fear & Greed Index: {e}")
# 2. Próba pobierania danych o rynku BTC
try:
# Pobranie aktualnej ceny i 24h change z CoinGecko
cg_url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd&include_24hr_change=true"
response = requests.get(cg_url, timeout=5)
if response.status_code == 200:
data = response.json()
if 'bitcoin' in data and 'usd_24h_change' in data['bitcoin']:
change_24h = data['bitcoin']['usd_24h_change']
# Normalizacja 24h change do [-1, 1]
# Zakładamy, że ±20% to ekstremalne przypadki
normalized_change = max(min(change_24h / 20, 1.0), -1.0)
logger.info(f"BTC 24h change: {change_24h:.2f}% -> {normalized_change:.2f}")
# W przypadku braku F&G Index, zwróć zmianę 24h jako sentyment
return normalized_change
except Exception as e:
logger.warning(f"Błąd podczas pobierania danych z CoinGecko: {e}")
# Jeśli nie udało się pobrać żadnych danych, zwracamy losową wartość
return random.uniform(-0.3, 0.3) # Losowy sentyment z przewagą neutralnego
except Exception as e:
logger.error(f"Błąd podczas analizy sentymentu: {e}")
return 0.0 # Neutralny sentyment w przypadku błędu