-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.py
More file actions
1423 lines (1265 loc) · 71.6 KB
/
Copy pathscanner.py
File metadata and controls
1423 lines (1265 loc) · 71.6 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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
scanner.py - Main scanner engine: orchestrate semua komponen
"""
import logging
import time
import concurrent.futures
from typing import List, Optional, Dict, Tuple
from datetime import datetime, timedelta
from config import (
MC_CONFIDENCE_THRESHOLD, MIN_SIGNAL_SCORE,
SCAN_INTERVAL_SECONDS, TOP_PAIRS_COUNT,
AUTO_TRADE, MAX_OPEN_POSITIONS,
ENABLE_CIO_AGENT, ENABLE_VISUAL_CHECK,
SIGNAL_COOLDOWN_MINUTES, TRADE_COOLDOWN_MINUTES,
MC_MIN_WIN_PROBABILITY, MC_MIN_EXPECTED_RETURN,
MAX_MARGIN_USAGE_PCT, SCAN_TIMEFRAMES,
EARLY_CLOSE_CONFIDENCE_THRESHOLD, EARLY_CLOSE_WIN_PROB_THRESHOLD,
SMC_MODE, SMC_MC_CONFIDENCE_THRESHOLD, EARLY_CLOSE_ON_DECAY,
SMC_OB_RETEST_ENTRY,
ADAPTIVE_RISK, MIN_PAIR_TRADES_FOR_STATS,
CIRCUIT_BREAKER_ENABLED, CIRCUIT_BREAKER_LOSSES,
CIRCUIT_BREAKER_PAUSE_HOURS, RISK_REDUCTION_LOSSES,
RISK_REDUCTION_PCT, TRACK_MAE_MFE,
NEWS_BLACKOUT_ENABLED, BLACKOUT_MOVE_SL_TO_BE,
)
from market_data import MarketData
from indicators import TechnicalIndicators
from monte_carlo import MonteCarloEngine, SimulationResult
from notifier import TelegramNotifier
from trader import BinanceTrader
import database as db
from analytics_engine import get_pair_personality, run_all_analytics, get_setup_weight, get_timeframe_weight, get_blacklisted_symbols, get_blacklisted_pair_sessions
import sqlite3
import os
def get_backtest_blocked_pairs() -> set:
try:
db_path = os.path.join(os.path.dirname(__file__), 'pair_statistics.db')
if not os.path.exists(db_path):
return set()
conn = sqlite3.connect(db_path)
rows = conn.execute("SELECT symbol FROM pair_stats WHERE win_rate < 0.40 AND total_trades >= 10").fetchall()
conn.close()
return {r[0] for r in rows}
except Exception as e:
logger.error(f"Error fetching backtest blocked pairs: {e}")
return set()
from market_context import get_full_context
from news_filter import get_news_filter
import gemini_client
import charting_engine
import rag_memory
logger = logging.getLogger(__name__)
class CircuitBreaker:
def __init__(self):
pass
def check(self) -> Tuple[bool, float, str]:
"""
Check circuit breaker status based on consecutive losses.
Returns:
is_paused: bool (True if trading should be fully paused)
risk_multiplier: float (multiplier for risk sizing, e.g. 0.5 or 1.0)
reason: str (status description or reason for pause/reduction)
"""
if not CIRCUIT_BREAKER_ENABLED:
return False, 1.0, ""
consecutive_losses = db.get_consecutive_losses()
if consecutive_losses == 0:
return False, 1.0, ""
last_loss_str = db.get_last_loss_close_time()
if not last_loss_str:
return False, 1.0, ""
try:
clean_str = last_loss_str.replace('Z', '+00:00')
last_loss_time = datetime.fromisoformat(clean_str).replace(tzinfo=None)
except Exception as e:
logger.error(f"[CircuitBreaker] Failed to parse last loss close time '{last_loss_str}': {e}")
return False, 1.0, ""
time_since_loss = datetime.utcnow() - last_loss_time
# 5 losses in a row -> pause 4 hours
if consecutive_losses >= CIRCUIT_BREAKER_LOSSES:
pause_duration = timedelta(hours=CIRCUIT_BREAKER_PAUSE_HOURS)
if time_since_loss < pause_duration:
remaining_secs = (pause_duration - time_since_loss).total_seconds()
remaining_str = f"{int(remaining_secs // 3600)}j {int((remaining_secs % 3600) // 60)}m"
return True, 0.0, f"Circuit Breaker Aktif: {consecutive_losses} loss berturut-turut. Pause trading selama {CIRCUIT_BREAKER_PAUSE_HOURS} jam. Sisa pause: {remaining_str}."
# 3 losses in a row -> risk -50% for 2 hours
if consecutive_losses >= RISK_REDUCTION_LOSSES:
reduction_duration = timedelta(hours=2)
if time_since_loss < reduction_duration:
remaining_secs = (reduction_duration - time_since_loss).total_seconds()
remaining_str = f"{int(remaining_secs // 3600)}j {int((remaining_secs % 3600) // 60)}m"
return False, RISK_REDUCTION_PCT, f"Risk Reduction Aktif: {consecutive_losses} loss berturut-turut. Risk dikurangi {int((1-RISK_REDUCTION_PCT)*100)}% selama 2 jam. Sisa pengurangan: {remaining_str}."
return False, 1.0, ""
class NeraScanner:
"""
Main scanner: scan top 50 pairs, jalankan Monte Carlo,
filter sinyal kuat, eksekusi order, kirim notifikasi Telegram.
"""
def __init__(self):
self.market = MarketData()
self.indicator = TechnicalIndicators()
self.mc_engine = MonteCarloEngine()
self.notifier = TelegramNotifier()
self.trader = BinanceTrader()
self.scan_count = 0
self._circuit_breaker = CircuitBreaker()
# News Blackout Filter (singleton, starts background refresh thread)
if NEWS_BLACKOUT_ENABLED:
self._news_filter = get_news_filter()
logger.info("[NewsFilter] News Blackout Filter: AKTIF ✅")
else:
self._news_filter = None
logger.info("[NewsFilter] News Blackout Filter: NONAKTIF")
# Cooldown trackers: symbol -> datetime terakhir sinyal/trade
self._signal_cooldown: Dict[str, datetime] = {}
self._trade_cooldown: Dict[str, datetime] = {}
# Blacklist dikelola oleh MarketData (persist ke file)
# Active trade persistence
self._active_trades_file = '/home/ajiekusumadhany.me/public_html/nera-quant/active_trades.json'
self.active_trades = self._load_active_trades()
# Pending setups persistence
self._pending_setups_file = '/home/ajiekusumadhany.me/public_html/nera-quant/pending_setups.json'
self.pending_setups = self._load_pending_setups()
def _get_adaptive_risk_pct(self, symbol: str, risk_multiplier: float) -> float:
from config import RISK_PER_TRADE, ADAPTIVE_RISK
if not ADAPTIVE_RISK:
return RISK_PER_TRADE * risk_multiplier
p = get_pair_personality(symbol)
base_risk = p.get('recommended_risk_pct', RISK_PER_TRADE)
return base_risk * risk_multiplier
def _log_trade_intelligence(self, signal: SimulationResult, trade, setup_type: str, pending_duration_mins: int = 0) -> str:
try:
utcnow = datetime.utcnow()
entry_time_utc = utcnow.strftime('%Y-%m-%dT%H:%M:%SZ')
trade_ref = f"{signal.symbol}_{utcnow.strftime('%Y%m%d_%H%M%S')}"
# Session info
from market_context import get_session_and_meta
session, hour_utc, weekday = get_session_and_meta(utcnow)
# SMC signals info
smc_signals = {
'bull_ob_top': getattr(signal, 'bull_ob_top', 0.0),
'bull_ob_bot': getattr(signal, 'bull_ob_bot', 0.0),
'bear_ob_top': getattr(signal, 'bear_ob_top', 0.0),
'bear_ob_bot': getattr(signal, 'bear_ob_bot', 0.0)
}
if getattr(signal, 'indicator_breakdown', None):
smc_signals.update(signal.indicator_breakdown)
# Consecutive losses
consecutive_losses = db.get_consecutive_losses()
db.log_trade_open(
trade_ref=trade_ref,
symbol=signal.symbol,
direction=signal.direction,
entry_time_utc=entry_time_utc,
timeframe=signal.timeframe,
session=session,
entry_hour_utc=hour_utc,
entry_weekday=weekday,
setup_type=setup_type,
smc_signals=smc_signals,
mc_confidence=signal.confidence,
mc_win_prob=signal.win_probability,
signal_score=signal.signal_score,
risk_reward=signal.risk_reward,
atr=getattr(signal, 'atr', 0.0),
atr_pct=getattr(signal, 'atr_pct', 0.0),
funding_rate=getattr(signal, 'funding_rate', 0.0),
oi_change=getattr(signal, 'oi_change', 0.0),
htf_bias=getattr(signal, 'htf_bias', 'NEUTRAL'),
bb_pct=getattr(signal, 'bb_pct', 0.5),
rsi=getattr(signal, 'rsi', 50.0),
macd_cross=getattr(signal, 'macd_cross', 0),
vol_spike=getattr(signal, 'vol_spike', 0),
entry_price=trade.entry_price if trade and trade.entry_price else signal.entry_price,
take_profit=trade.take_profit if trade and trade.take_profit else signal.take_profit,
stop_loss=trade.stop_loss if trade and trade.stop_loss else signal.stop_loss,
leverage=trade.leverage_used if trade and trade.leverage_used else 1,
margin_used=trade.margin_used if trade and trade.margin_used else 0.0,
pending_duration_mins=pending_duration_mins,
consecutive_losses_at_entry=consecutive_losses,
binance_order_id=str(trade.order_id) if trade and trade.order_id else None
)
return trade_ref
except Exception as e:
logger.error(f"Error logging trade intelligence: {e}", exc_info=True)
return ""
def _log_trade_close_details(self, symbol: str):
active_trade = self.active_trades.get(symbol)
if not active_trade:
return
trade_ref = active_trade.get('trade_ref')
if not trade_ref:
return
# Fetch latest user trades from Binance to get exact exit price and PnL
try:
# Parse open time to ms
open_time_ms = 0
trade_timestamp = active_trade.get('timestamp')
if trade_timestamp:
try:
from datetime import timezone
open_dt = datetime.fromisoformat(trade_timestamp.replace('Z', ''))
open_time_ms = int(open_dt.replace(tzinfo=timezone.utc).timestamp() * 1000)
except Exception as te:
logger.warning(f"[{symbol}] Failed to parse active trade timestamp '{trade_timestamp}': {te}")
trades_data = self.trader._signed_get('/fapi/v1/userTrades', {'symbol': symbol, 'limit': 10})
if isinstance(trades_data, list) and len(trades_data) > 0:
# Find the most recent trade with non-zero realized PnL and time >= open_time_ms (with 1 min tolerance)
closed_trades = [
t for t in trades_data
if float(t.get('realizedPnl', 0.0)) != 0.0
and int(t.get('time', 0)) >= (open_time_ms - 60000)
]
if closed_trades:
closed_trades.sort(key=lambda x: int(x.get('time', 0)), reverse=True)
latest_close = closed_trades[0]
exit_price = float(latest_close.get('price'))
realized_pnl = float(latest_close.get('realizedPnl'))
close_time_ms = int(latest_close.get('time'))
close_time_utc = datetime.utcfromtimestamp(close_time_ms / 1000.0).strftime('%Y-%m-%dT%H:%M:%SZ')
binance_order_id = str(latest_close.get('orderId'))
db.log_trade_close(
trade_ref=trade_ref,
exit_price=exit_price,
result_pnl=realized_pnl,
close_time_utc=close_time_utc,
binance_order_id=binance_order_id
)
logger.info(f"[DI] Position exit captured from userTrades for {symbol}: exit_price={exit_price}, pnl={realized_pnl}")
try:
run_all_analytics()
except Exception as ae:
logger.error(f"Gagal memicu run_all_analytics() setelah userTrades close: {ae}")
# Feature 5: Store pattern in RAG memory
try:
_features = active_trade.get('indicator_breakdown') or {}
_outcome_str = 'WIN' if realized_pnl > 0 else ('LOSS' if realized_pnl < 0 else 'BE')
rag_memory.store_pattern(
trade_ref=trade_ref,
symbol=symbol,
direction=active_trade.get('direction', ''),
features=_features,
outcome=_outcome_str,
result_pnl=realized_pnl,
risk_reward=active_trade.get('risk_reward', 0.0),
session=active_trade.get('session'),
timeframe=active_trade.get('timeframe'),
)
except Exception as re:
logger.debug(f"[RAG] store_pattern skipped (no features): {re}")
# Feature 4: Trigger L3 meta-feedback (async-safe, best-effort)
try:
from analytics_engine import run_meta_feedback_loop
run_meta_feedback_loop(limit=5)
except Exception as me:
logger.debug(f"[MetaFeedback] Skipped: {me}")
return
else:
logger.info(f"[{symbol}] No recent exit trades found in userTrades since open_time_ms={open_time_ms}. Fallback to estimation.")
except Exception as e:
logger.warning(f"[{symbol}] Failed to fetch exact exit details from userTrades: {e}. Fallback to estimation.")
# Fallback to estimated exit parameters if Binance fetch fails or finds nothing
mark_price = self.market.get_ticker_price(symbol) or active_trade['entry_price']
entry_price = active_trade['entry_price']
direction = active_trade['direction']
quantity = active_trade['quantity']
# Estimate PnL
est_pnl = 0.0
if direction == 'LONG':
est_pnl = (mark_price - entry_price) * quantity
else:
est_pnl = (entry_price - mark_price) * quantity
close_time_utc = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
db.log_trade_close(
trade_ref=trade_ref,
exit_price=mark_price,
result_pnl=est_pnl,
close_time_utc=close_time_utc
)
logger.info(f"[DI] Position exit estimated for {symbol}: exit_price={mark_price}, pnl={est_pnl}")
try:
run_all_analytics()
except Exception as ae:
logger.error(f"Gagal memicu run_all_analytics() setelah estimated close: {ae}")
# Feature 5: Store pattern in RAG memory (estimated close)
try:
_features = {}
_outcome_str = 'WIN' if est_pnl > 0 else ('LOSS' if est_pnl < 0 else 'BE')
rag_memory.store_pattern(
trade_ref=trade_ref,
symbol=symbol,
direction=active_trade.get('direction', ''),
features=_features,
outcome=_outcome_str,
result_pnl=est_pnl,
risk_reward=active_trade.get('risk_reward', 0.0),
session=active_trade.get('session'),
timeframe=active_trade.get('timeframe'),
)
except Exception as re:
logger.debug(f"[RAG] store_pattern (estimated) skipped: {re}")
# Feature 4: Trigger L3 meta-feedback
try:
from analytics_engine import run_meta_feedback_loop
run_meta_feedback_loop(limit=5)
except Exception as me:
logger.debug(f"[MetaFeedback] Skipped: {me}")
def _load_active_trades(self) -> dict:
import os
import json
if os.path.exists(self._active_trades_file):
try:
with open(self._active_trades_file, 'r') as f:
return json.load(f)
except Exception as e:
logger.error(f"Gagal me-load active_trades.json: {e}")
return {}
def _save_active_trades(self):
import json
try:
with open(self._active_trades_file, 'w') as f:
json.dump(self.active_trades, f, indent=4)
except Exception as e:
logger.error(f"Gagal menyimpan active_trades.json: {e}")
def _load_pending_setups(self) -> dict:
import os
import json
if os.path.exists(self._pending_setups_file):
try:
with open(self._pending_setups_file, 'r') as f:
return json.load(f)
except Exception as e:
logger.error(f"Gagal me-load pending_setups.json: {e}")
return {}
def _save_pending_setups(self):
import json
try:
with open(self._pending_setups_file, 'w') as f:
json.dump(self.pending_setups, f, indent=4)
except Exception as e:
logger.error(f"Gagal menyimpan pending_setups.json: {e}")
def run_forever(self):
"""Loop utama: scan terus menerus setiap SCAN_INTERVAL_SECONDS."""
logger.info("=" * 60)
logger.info(" NERA QUANT - Trading AI Scanner")
logger.info(" Monte Carlo Probability Engine")
logger.info(f" AUTO TRADE: {'ON ✅' if AUTO_TRADE else 'OFF (signal only)'}")
logger.info("=" * 60)
self.notifier.send_startup()
while True:
try:
self.scan_count += 1
logger.info(f"\n{'='*50}")
logger.info(f"SCAN #{self.scan_count} | {datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')}")
logger.info(f"{'='*50}")
signals = self.run_scan()
# Kirim summary setiap 10 scan
if self.scan_count % 10 == 0:
self.notifier.send_scan_summary(
total_pairs=TOP_PAIRS_COUNT,
signals_found=len(signals),
top_signals=signals[:5]
)
logger.info(f"Scan selesai. {len(signals)} sinyal ditemukan.")
logger.info(f"Menunggu {SCAN_INTERVAL_SECONDS}s untuk scan berikutnya...")
time.sleep(SCAN_INTERVAL_SECONDS)
except KeyboardInterrupt:
logger.info("Scanner dihentikan oleh user.")
break
except Exception as e:
logger.error(f"Error di main loop: {e}", exc_info=True)
self.notifier.send_error(str(e))
time.sleep(30)
def run_scan(self) -> List[SimulationResult]:
"""
Jalankan satu siklus scan lengkap.
Returns list sinyal yang memenuhi threshold, sorted by confidence.
"""
# Step 1: Ambil top 50 pairs (sudah difilter tradeable oleh MarketData)
symbols = self.market.get_top_pairs()
# Pastikan semua symbol yang sedang di-hold ada di daftar scan agar MC diupdate
for active_symbol in list(self.active_trades.keys()):
if active_symbol not in symbols:
symbols.append(active_symbol)
# ─── BACKTEST BLACKLIST CHECK ──────────────────────────────────────────
backtest_blocked = get_backtest_blocked_pairs()
if backtest_blocked:
symbols = [s for s in symbols if s not in backtest_blocked or s in self.active_trades]
logger.info(f"Scanning {len(symbols)} pairs... (Blocked by backtest: {len(backtest_blocked)})")
# ─── NEWS BLACKOUT CHECK ───────────────────────────────────────────────
# Jika blackout aktif (30m sebelum / 15m setelah berita High Impact),
# suspend semua pembukaan posisi baru dan (opsional) geser SL ke Breakeven.
_news_blackout_active = (
self._news_filter is not None
and self._news_filter.is_blackout_active()
)
if _news_blackout_active:
logger.warning(
"🚨 [NewsFilter] BLACKOUT AKTIF — Semua pembukaan posisi baru DITANGGUHKAN! "
"Scan tetap berjalan untuk monitoring posisi aktif."
)
self.notifier._send_message(
"🚨 *NEWS BLACKOUT AKTIF*\n"
"Berita High Impact akan segera rilis.\n"
"Bot MENGHENTIKAN pembukaan posisi baru untuk menghindari spike.\n"
"Monitoring posisi aktif tetap berjalan."
)
# Geser SL semua posisi aktif ke Breakeven (jika diaktifkan)
if BLACKOUT_MOVE_SL_TO_BE and self.active_trades:
logger.info("[NewsFilter] Mencoba pindahkan SL ke Breakeven untuk semua posisi aktif...")
for sym, trade_data in list(self.active_trades.items()):
try:
if trade_data.get('status') != 'OPEN':
continue
entry_price = float(trade_data.get('entry_price', 0))
current_sl = float(trade_data.get('stop_loss', 0))
direction = trade_data.get('direction', '')
if entry_price <= 0 or not direction:
continue
# Cek apakah SL sudah di breakeven atau lebih baik
if direction == 'LONG' and current_sl >= entry_price:
logger.info(f"[{sym}] SL sudah di/melewati breakeven ({current_sl:.4f} >= {entry_price:.4f}), skip.")
continue
if direction == 'SHORT' and current_sl <= entry_price:
logger.info(f"[{sym}] SL sudah di/melewati breakeven ({current_sl:.4f} <= {entry_price:.4f}), skip.")
continue
# Hitung TP2 price (pakai nilai TP aktif dari tracker)
tp2_price = float(trade_data.get('take_profit', 0))
quantity = float(trade_data.get('quantity', 0))
logger.info(
f"[{sym}] Blackout: memindahkan SL ke Breakeven "
f"(entry={entry_price:.4f}, SL_lama={current_sl:.4f})"
)
success, new_tp_id, new_sl_id, err = self.trader.execute_partial_close(
symbol = sym,
quantity = quantity,
direction = direction,
entry_price= entry_price,
tp2_price = tp2_price,
)
if success:
self.active_trades[sym]['stop_loss'] = entry_price
if new_sl_id:
self.active_trades[sym]['sl_order_id'] = new_sl_id
self._save_active_trades()
logger.info(f"[{sym}] ✅ SL Breakeven terpasang (blackout protection).")
else:
logger.warning(f"[{sym}] ⚠️ Gagal geser SL ke BE: {err}")
except Exception as be_err:
logger.error(f"[{sym}] Error saat blackout BE move: {be_err}")
results = []
with concurrent.futures.ThreadPoolExecutor(max_workers=30) as executor:
futures = {}
for symbol in symbols:
for tf in SCAN_TIMEFRAMES:
futures[executor.submit(self._analyze_pair, symbol, tf)] = (symbol, tf)
for future in concurrent.futures.as_completed(futures):
symbol, tf = futures[future]
try:
result = future.result(timeout=30)
if result is not None:
results.append(result)
except Exception as e:
logger.error(f"Error analyzing {symbol} on {tf}: {e}")
# Step 2.5: Sinkronisasi active_trades dengan posisi riil di Binance (auto-heal)
try:
open_positions = self.trader.get_open_positions()
open_symbols = {p['symbol'] for p in open_positions}
# Ambil sisa quantity riil di Binance
binance_qtys = {}
for p in open_positions:
binance_qtys[p['symbol']] = float(p.get('positionAmt', 0))
# Hapus active trades yang sudah tidak ada di Binance
for active_symbol in list(self.active_trades.keys()):
if active_symbol not in open_symbols:
logger.info(f"[{active_symbol}] Posisi sudah ditutup di Binance. Hapus dari active_trades dan cancel order.")
try:
self._log_trade_close_details(active_symbol)
except Exception as e:
logger.error(f"[{active_symbol}] Failed to log trade close: {e}")
self.trader._cancel_all_algo_orders(active_symbol)
del self.active_trades[active_symbol]
self._save_active_trades()
# Account-wide auto-heal untuk membersihkan orphaned algo orders di Binance
try:
# Query semua open algo orders di akun
open_algo_data = self.trader._signed_get('/fapi/v1/openAlgoOrders')
open_algo_symbols = set()
if isinstance(open_algo_data, list):
open_algo_symbols = {o['symbol'] for o in open_algo_data if 'symbol' in o}
elif isinstance(open_algo_data, dict):
orders = open_algo_data.get('orders', [])
open_algo_symbols = {o['symbol'] for o in orders if 'symbol' in o}
# Jika ada symbol yang punya open algo orders tapi tidak ada posisi terbuka
for symbol in open_algo_symbols:
if symbol not in open_symbols:
logger.warning(f"[{symbol}] Deteksi open algo orders tanpa posisi aktif. Melakukan auto-heal cleanup...")
self.trader._cancel_all_algo_orders(symbol)
except Exception as he:
logger.debug(f"Gagal menjalankan auto-heal open algo orders: {he}")
except Exception as e:
logger.error(f"Gagal melakukan sinkronisasi active_trades dengan Binance: {e}")
binance_qtys = {}
# Step 2.6: Active Position Monitoring Loop (TP1 / Breakeven / Early Close)
analysis_map = {(r.symbol, r.timeframe): r for r in results if r is not None}
for symbol, active_trade in list(self.active_trades.items()):
try:
# Ambil mark price terkini
mark_price = self.market.get_ticker_price(symbol)
if not mark_price:
continue
# Update MAE / MFE
trade_ref = active_trade.get('trade_ref')
if TRACK_MAE_MFE and trade_ref:
db.update_mae_mfe(trade_ref, mark_price)
direction = active_trade['direction']
entry_price = active_trade['entry_price']
status = active_trade['status']
quantity = abs(binance_qtys.get(symbol, active_trade['quantity']))
# A. Pengecekan TP1 (Partial Close & Breakeven)
if active_trade.get('is_partial') and status == 'OPEN':
tp1_price = active_trade['tp1_price']
tp2_price = active_trade['take_profit'] # Final TP
trigger_partial = False
if direction == 'LONG' and mark_price >= tp1_price:
trigger_partial = True
elif direction == 'SHORT' and mark_price <= tp1_price:
trigger_partial = True
if trigger_partial:
logger.info(f"[{symbol}] 🔥 Target TP1 ({tp1_price:.4f}) tercapai di harga {mark_price:.4f}!")
success, new_tp_id, new_sl_id, err = self.trader.execute_partial_close(
symbol=symbol,
quantity=quantity,
direction=direction,
entry_price=entry_price,
tp2_price=tp2_price
)
if success:
# Partial close sukses + SL BE terpasang
active_trade['status'] = 'PARTIAL_CLOSED'
active_trade['sl_order_id'] = new_sl_id
active_trade['tp2_order_id'] = new_tp_id
active_trade['tp1_order_id'] = None
self._save_active_trades()
self.notifier.send_partial_tp_executed(
symbol=symbol,
direction=direction,
qty=quantity * 0.5,
price=mark_price,
remaining_qty=quantity - (quantity * 0.5)
)
elif new_tp_id is not None:
# Partial close berhasil tapi SL BE gagal dipasang
# Tetap mark PARTIAL_CLOSED agar tidak re-trigger market close
logger.error(f"[{symbol}] ⚠️ SL Breakeven GAGAL dipasang! TP2 id={new_tp_id}. Posisi tidak terlindungi.")
active_trade['status'] = 'PARTIAL_CLOSED'
active_trade['sl_order_id'] = None # SL tidak ada!
active_trade['tp2_order_id'] = new_tp_id
active_trade['tp1_order_id'] = None
self._save_active_trades()
self.notifier.send_error(
f"⚠️ [{symbol}] SL Breakeven GAGAL dipasang setelah TP1! Error: {err}\n"
f"Posisi sisa masih terbuka TANPA SL. Cek manual!"
)
# B. Pengecekan Early Close (Confidence / Win Prob drop / Reversal)
trade_timeframe = active_trade.get('timeframe', '15m')
result = analysis_map.get((symbol, trade_timeframe))
if result:
is_reversal = (result.direction != 'NEUTRAL' and result.direction != direction)
is_decay = False
if EARLY_CLOSE_ON_DECAY:
is_decay = (result.confidence < EARLY_CLOSE_CONFIDENCE_THRESHOLD) or (result.win_probability < EARLY_CLOSE_WIN_PROB_THRESHOLD)
if is_reversal or is_decay:
if is_reversal:
logger.warning(
f"[{symbol}] ⚠️ Reversal terdeteksi! "
f"Direction Baru: {result.direction} (Arah Posisi: {direction}), "
f"Confidence Baru: {result.confidence*100:.1f}%, "
f"Win Prob Baru: {result.win_probability*100:.1f}%"
)
else:
logger.warning(
f"[{symbol}] ⚠️ Decay terdeteksi! "
f"Confidence Baru: {result.confidence*100:.1f}% (Threshold: {EARLY_CLOSE_CONFIDENCE_THRESHOLD*100:.1f}%), "
f"Win Prob Baru: {result.win_probability*100:.1f}% (Threshold: {EARLY_CLOSE_WIN_PROB_THRESHOLD*100:.1f}%)"
)
est_pnl = ((mark_price - entry_price) / entry_price * 100) if direction == 'LONG' else ((entry_price - mark_price) / entry_price * 100)
success, err = self.trader.execute_complete_close(symbol, direction)
if success:
if symbol in self.active_trades:
try:
self._log_trade_close_details(symbol)
except Exception as e:
logger.error(f"[{symbol}] Failed to log early close details: {e}")
del self.active_trades[symbol]
self._save_active_trades()
self.notifier.send_early_close_executed(
symbol=symbol,
direction=direction,
price=mark_price,
pnl=est_pnl,
confidence=result.confidence,
win_prob=result.win_probability
)
except Exception as e:
logger.error(f"[{symbol}] Error saat monitoring aktif: {e}", exc_info=True)
# Step 2.7: Pending SMC Setups Monitoring Loop (Trigger or Invalidation)
for symbol, setup in list(self.pending_setups.items()):
try:
# 1. Check expiration (2 hours maximum age)
setup_time = datetime.fromisoformat(setup['timestamp'].replace('Z', '+00:00')).replace(tzinfo=None)
if datetime.utcnow() - setup_time > timedelta(hours=2):
logger.info(f"[{symbol}] ⏳ SMC Pending Setup expired (older than 2 hours). Cancelling.")
mark_price = self.market.get_ticker_price(symbol) or setup['entry_price']
self.notifier.send_pending_setup_invalidated(
symbol=symbol,
direction=setup['direction'],
price=mark_price,
reason="Setup expired (older than 2 hours)"
)
del self.pending_setups[symbol]
self._save_pending_setups()
continue
# 2. Get current mark price
mark_price = self.market.get_ticker_price(symbol)
if not mark_price:
continue
direction = setup['direction']
trigger_price = setup['trigger_price']
invalidation_price = setup['invalidation_price']
# 3. Check invalidation and triggers
triggered = False
invalidated = False
invalidation_reason = ""
if direction == 'LONG':
if mark_price < invalidation_price:
invalidated = True
invalidation_reason = f"Price broke below invalidation price ({invalidation_price:.4f}) (Stop Loss boundary / OB bottom)"
elif mark_price <= trigger_price:
triggered = True
elif direction == 'SHORT':
if mark_price > invalidation_price:
invalidated = True
invalidation_reason = f"Price broke above invalidation price ({invalidation_price:.4f}) (Stop Loss boundary / OB top)"
elif mark_price >= trigger_price:
triggered = True
if invalidated:
logger.info(f"[{symbol}] ❌ Pending setup invalidated at {mark_price:.4f} | Reason: {invalidation_reason}")
self.notifier.send_pending_setup_invalidated(
symbol=symbol,
direction=direction,
price=mark_price,
reason=invalidation_reason
)
del self.pending_setups[symbol]
self._save_pending_setups()
elif triggered:
logger.info(f"[{symbol}] 🔥 Pending setup triggered at {mark_price:.4f} (Trigger price: {trigger_price:.4f})")
# Check position limits
open_count = self.trader.count_open_positions()
if open_count >= MAX_OPEN_POSITIONS:
logger.warning(f"[{symbol}] Cannot execute triggered setup because MAX_OPEN_POSITIONS ({MAX_OPEN_POSITIONS}) is reached. Skipping this cycle.")
continue
margin_usage_pct = self.trader.get_margin_usage_pct()
if margin_usage_pct >= MAX_MARGIN_USAGE_PCT:
logger.warning(
f"[{symbol}] Batas total margin tercapai! "
f"Margin terpakai: {margin_usage_pct * 100:.1f}% (Batas: {MAX_MARGIN_USAGE_PCT * 100:.1f}%). "
f"Skipping triggered setup this cycle."
)
continue
# Cek News Blackout — tunda eksekusi pending setup
if _news_blackout_active:
logger.warning(
f"[{symbol}] 🚫 Pending setup DITUNDA — News Blackout aktif. "
f"Setup tetap tersimpan dan akan dicoba di siklus berikutnya."
)
continue
# Reconstruct SimulationResult
from monte_carlo import SimulationResult
signal = SimulationResult(
symbol=setup['symbol'],
direction=setup['direction'],
confidence=setup['confidence'],
win_probability=setup['win_probability'],
expected_return=setup.get('expected_return', 0.0),
risk_reward=setup['risk_reward'],
entry_price=mark_price,
take_profit=setup['take_profit'],
stop_loss=setup['stop_loss'],
simulations_run=setup.get('simulations_run', 5000),
profitable_paths=setup.get('profitable_paths', 0),
signal_score=setup['signal_score'],
tp_multiplier=setup.get('tp_multiplier', 2.5),
sl_multiplier=setup.get('sl_multiplier', 1.5),
timeframe=setup.get('timeframe', '15m'),
bull_ob_top=setup.get('bull_ob_top', 0.0),
bull_ob_bot=setup.get('bull_ob_bot', 0.0),
bear_ob_top=setup.get('bear_ob_top', 0.0),
bear_ob_bot=setup.get('bear_ob_bot', 0.0)
)
# Execute trade
is_paused, risk_mult, cb_reason = self._circuit_breaker.check()
if is_paused:
logger.warning(f"[{symbol}] Circuit Breaker aktif. Batal mengeksekusi pending setup. Detail: {cb_reason}")
continue
chart_path = None
if ENABLE_VISUAL_CHECK:
chart_path = charting_engine.generate_chart(
symbol=signal.symbol, timeframe=signal.timeframe,
entry_price=mark_price, tp=signal.take_profit, sl=signal.stop_loss,
ob_top=getattr(signal, 'bull_ob_top', getattr(signal, 'bear_ob_top', 0)),
ob_bot=getattr(signal, 'bull_ob_bot', getattr(signal, 'bear_ob_bot', 0))
)
if ENABLE_CIO_AGENT:
# Feature 1: Multi-analyst debate for pending setup
# Feature 5: RAG context enrichment
_rag_patterns = rag_memory.find_similar_patterns(
features=getattr(signal, 'indicator_breakdown', {}) or {},
top_k=5,
)
_rag_str = rag_memory.format_similar_patterns_for_context(_rag_patterns)
_ctx_str = (
f"Pending setup triggered. "
f"Confidence={signal.confidence:.2f} WinProb={signal.win_probability:.2f} "
f"Score={signal.signal_score:.2f} RR={signal.risk_reward:.2f} "
f"Entry={mark_price:.4f} TP={signal.take_profit:.4f} SL={signal.stop_loss:.4f}"
)
debate = gemini_client.ask_gemini_debate(
symbol=signal.symbol,
direction=signal.direction,
context_str=_ctx_str,
chart_path=chart_path,
similar_patterns_str=_rag_str,
)
if debate['verdict'] == 'REJECT':
logger.warning(
f"[{symbol}] CIO Debate REJECTED pending setup execution. "
f"Bull={debate.get('bull_strength')} Bear={debate.get('bear_strength')} | {debate['reasoning']}"
)
continue
_cio_verdict = debate['verdict']
_cio_bull = debate.get('bull', '')
_cio_bear = debate.get('bear', '')
else:
_cio_verdict = None
_cio_bull = ''
_cio_bear = ''
risk_pct = self._get_adaptive_risk_pct(symbol, risk_mult)
trade = self.trader.execute(signal, risk_pct=risk_pct)
self.notifier.send_pending_setup_triggered(signal, trade, chart_path, debate if ENABLE_CIO_AGENT else None)
# Clean up pending setup regardless of execution success to prevent duplicate trigger loops
del self.pending_setups[symbol]
self._save_pending_setups()
if trade.success:
self._trade_cooldown[(symbol, setup['timeframe'])] = datetime.utcnow()
setup_dur_mins = int((datetime.utcnow() - setup_time).total_seconds() / 60)
# Pending setups saat ini hanya dibuat untuk SMC_OB_PULLBACK
# tapi jika di masa depan OI_DIVERGENCE juga punya pending mode,
# cek oi_divergence di sini juga
_pend_setup_type = 'SMC_OB_PULLBACK'
if getattr(signal, 'oi_divergence', 0) != 0:
_pend_setup_type = 'OI_DIVERGENCE'
trade_ref = self._log_trade_intelligence(signal, trade, _pend_setup_type, setup_dur_mins)
# Feature 4: Save CIO debate details for later meta-eval
if _cio_verdict and trade_ref:
db.save_meta_feedback(
trade_ref=trade_ref,
meta_feedback='',
cio_verdict=_cio_verdict,
cio_bull_reasoning=_cio_bull[:500],
cio_bear_reasoning=_cio_bear[:500],
)
# Add to active_trades tracker
self.active_trades[trade.symbol] = {
'symbol': trade.symbol,
'direction': trade.direction,
'quantity': trade.quantity,
'entry_price': trade.entry_price,
'take_profit': trade.take_profit,
'stop_loss': trade.stop_loss,
'tp1_price': getattr(trade, 'tp1_price', 0.0),
'is_partial': getattr(trade, 'is_partial', False),
'status': 'OPEN',
'tp1_order_id': getattr(trade, 'tp1_order_id', None),
'tp2_order_id': getattr(trade, 'tp2_order_id', None),
'sl_order_id': trade.sl_order_id,
'timestamp': datetime.utcnow().isoformat(),
'timeframe': setup['timeframe'],
'trade_ref': trade_ref,
'indicator_breakdown': getattr(signal, 'indicator_breakdown', {}) or {},
'risk_reward': signal.risk_reward,
'session': setup.get('session', ''),
}
self._save_active_trades()
import api_server as api
api.append_trade({
'symbol': trade.symbol,
'direction': trade.direction,
'entry': trade.entry_price,
'tp': trade.take_profit,
'sl': trade.stop_loss,
'leverage': trade.leverage_used,
'margin': trade.margin_used,
'pnl': 0,
'timestamp': datetime.utcnow().isoformat(),
'timeframe': setup['timeframe'],
})
except Exception as e:
logger.error(f"[{symbol}] Error saat monitoring pending setup: {e}", exc_info=True)
# Step 2.8: Circuit Breaker Pre-Scan Validation
is_paused, risk_mult, cb_reason = self._circuit_breaker.check()
if is_paused:
logger.warning(f"Circuit Breaker AKTIF: New trading signals will be bypassed. Reason: {cb_reason}")
if not getattr(self, '_cb_alert_sent', False):
last_loss_str = db.get_last_loss_close_time()
resume_at = "Unknown"
if last_loss_str:
try:
clean_str = last_loss_str.replace('Z', '+00:00')
last_loss_time = datetime.fromisoformat(clean_str).replace(tzinfo=None)
resume_dt = last_loss_time + timedelta(hours=CIRCUIT_BREAKER_PAUSE_HOURS)
resume_at = resume_dt.strftime('%H:%M:%S UTC')
except Exception:
pass
self.notifier.send_circuit_breaker_alert(cb_reason, resume_at)
self._cb_alert_sent = True
strong_signals = []
else:
self._cb_alert_sent = False
# Step 3: Filter sinyal yang memenuhi threshold
target_conf_threshold = SMC_MC_CONFIDENCE_THRESHOLD if SMC_MODE else MC_CONFIDENCE_THRESHOLD
# Feature 3: Load auto-blacklist (Standing Orders)
_blacklisted_pairs = get_blacklisted_symbols()
_blacklisted_sessions = get_blacklisted_pair_sessions()
from market_context import get_session
_current_session = get_session()
# Feature 2: Apply ε-greedy setup & timeframe weighting to signal_score
for r in results:
if r.direction == 'NEUTRAL':
continue
# Determine setup type for this signal
_setup_type = 'INSTANT'
if SMC_MODE and (getattr(r, 'bull_ob_top', 0) > 0 or getattr(r, 'bear_ob_bot', 0) > 0):
_setup_type = 'SMC_OB_PULLBACK'
elif getattr(r, 'oi_divergence', 0) != 0:
_setup_type = 'OI_DIVERGENCE'
_sw = get_setup_weight(_setup_type)
_tw = get_timeframe_weight(r.timeframe)
r.signal_score = round(r.signal_score * _sw * _tw, 4)
from config import OI_DIVERGENCE_CONF_THRESHOLD, OI_DIVERGENCE_MIN_SCORE
def _get_conf_threshold(r) -> float:
"""Threshold confidence per setup type."""
if getattr(r, 'oi_divergence', 0) != 0:
return OI_DIVERGENCE_CONF_THRESHOLD
return target_conf_threshold
def _get_min_score(r) -> float:
"""Min signal score per setup type."""
if getattr(r, 'oi_divergence', 0) != 0:
return OI_DIVERGENCE_MIN_SCORE
return MIN_SIGNAL_SCORE
strong_signals = [
r for r in results
if r.direction != 'NEUTRAL'
and r.confidence >= _get_conf_threshold(r)
and r.signal_score >= _get_min_score(r)
and r.win_probability >= MC_MIN_WIN_PROBABILITY
and r.expected_return >= MC_MIN_EXPECTED_RETURN
# Feature 3: Skip fully blacklisted pairs
and r.symbol not in _blacklisted_pairs
# Feature 3: Skip pair+session combos that are blacklisted
and (r.symbol, _current_session) not in _blacklisted_sessions
]
# Step 4: Sort by confidence descending
strong_signals.sort(key=lambda x: x.confidence, reverse=True)
# Step 5: Eksekusi & notifikasi
open_count = self.trader.count_open_positions()
logger.info(f"Posisi terbuka saat ini: {open_count}/{MAX_OPEN_POSITIONS}")
for signal in strong_signals:
now = datetime.utcnow()
# ── Cek signal cooldown ───────────────────────────────────
last_signal = self._signal_cooldown.get((signal.symbol, signal.timeframe))
if last_signal and (now - last_signal) < timedelta(minutes=SIGNAL_COOLDOWN_MINUTES):
remaining = SIGNAL_COOLDOWN_MINUTES - int((now - last_signal).total_seconds() / 60)
logger.info(f" ⏳ {signal.symbol} | {signal.timeframe} cooldown: {remaining}m tersisa, skip.")
continue
self._log_signal(signal)