-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
1199 lines (1044 loc) · 45.1 KB
/
Copy pathdatabase.py
File metadata and controls
1199 lines (1044 loc) · 45.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
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
"""
database.py - SQLite persistence untuk trade history NERA QUANT
Menyimpan semua posisi yang sudah close dengan data lengkap dari Binance.
"""
import sqlite3
import hmac
import hashlib
import time
import logging
import threading
import requests
from urllib.parse import urlencode
from typing import List, Dict, Optional
from config import API_KEY, API_SECRET, BINANCE_BASE_URL
logger = logging.getLogger(__name__)
DB_PATH = '/home/ajiekusumadhany.me/public_html/nera-quant/trades.db'
# Hanya catat PnL dari tanggal ini ke depan (2026-05-23 04:00 WIB = 2026-05-22 21:00:00 UTC)
SYNC_START_TS = 1779569100000
_db_lock = threading.Lock()
# ── Schema ────────────────────────────────────────────────────────────
SCHEMA = """
CREATE TABLE IF NOT EXISTS trades (
id INTEGER PRIMARY KEY AUTOINCREMENT,
symbol TEXT NOT NULL,
side TEXT NOT NULL, -- LONG / SHORT
qty REAL NOT NULL,
entry_price REAL NOT NULL,
exit_price REAL,
open_time INTEGER NOT NULL, -- ms timestamp
close_time INTEGER, -- ms timestamp, NULL jika masih open
realized_pnl REAL DEFAULT 0.0,
commission REAL DEFAULT 0.0,
net_pnl REAL DEFAULT 0.0, -- realized_pnl - commission
leverage INTEGER DEFAULT 1,
margin_used REAL DEFAULT 0.0,
status TEXT DEFAULT 'OPEN', -- OPEN / CLOSED / PARTIAL
binance_order_id TEXT, -- orderId dari market order awal
UNIQUE(symbol, open_time, side) -- cegah duplikat posisi
);
CREATE TABLE IF NOT EXISTS income_log (
trade_id TEXT PRIMARY KEY, -- tradeId dari Binance income
symbol TEXT NOT NULL,
income_type TEXT NOT NULL, -- REALIZED_PNL / COMMISSION / dll
income REAL NOT NULL,
timestamp INTEGER NOT NULL,
asset TEXT DEFAULT 'USDT'
);
CREATE TABLE IF NOT EXISTS sync_state (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
);
-- ── Decision Intelligence System Tables ──────────────────────────────
CREATE TABLE IF NOT EXISTS trade_intelligence (
id INTEGER PRIMARY KEY AUTOINCREMENT,
trade_ref TEXT UNIQUE, -- {symbol}_{entry_time_utc} unique ref
binance_order_id TEXT, -- link ke trades.binance_order_id
symbol TEXT NOT NULL,
direction TEXT NOT NULL, -- LONG / SHORT
entry_time_utc TEXT NOT NULL, -- ISO8601
close_time_utc TEXT, -- diisi saat close
timeframe TEXT NOT NULL, -- 5m / 15m / 1h
session TEXT, -- ASIA / LONDON / NY / OFF
entry_hour_utc INTEGER, -- 0-23
entry_weekday INTEGER, -- 0=Mon … 6=Sun
setup_type TEXT, -- SMC_OB_PULLBACK / INSTANT / OI_DIVERGENCE / PENDING_TRIGGER
smc_signals TEXT, -- JSON: {bos, choch, fvg_dir, bull_ob_top, ...}
mc_confidence REAL,
mc_win_prob REAL,
signal_score REAL,
risk_reward REAL,
atr REAL,
atr_pct REAL,
funding_rate REAL,
oi_change REAL,
htf_bias TEXT, -- BULLISH / BEARISH / NEUTRAL
bb_pct REAL,
rsi REAL,
macd_cross INTEGER, -- 1 / 0
vol_spike INTEGER, -- 1 / 0
entry_price REAL,
take_profit REAL,
stop_loss REAL,
exit_price REAL,
result_pnl REAL,
result_rr_achieved REAL,
outcome TEXT DEFAULT 'OPEN', -- WIN / LOSS / BE / OPEN
trade_duration_mins INTEGER,
mae REAL DEFAULT 0.0, -- Max Adverse Excursion
mfe REAL DEFAULT 0.0, -- Max Favorable Excursion
leverage INTEGER,
margin_used REAL,
pending_duration_mins INTEGER DEFAULT 0, -- 0 = instant entry
consecutive_losses_at_entry INTEGER DEFAULT 0
);
CREATE TABLE IF NOT EXISTS pair_stats (
symbol TEXT PRIMARY KEY,
total_trades INTEGER DEFAULT 0,
win_trades INTEGER DEFAULT 0,
loss_trades INTEGER DEFAULT 0,
win_rate REAL DEFAULT 0.0,
avg_rr REAL DEFAULT 0.0,
avg_rr_achieved REAL DEFAULT 0.0,
avg_duration_mins REAL DEFAULT 0.0,
best_session TEXT,
best_timeframe TEXT,
avg_atr_pct REAL DEFAULT 0.0,
recommended_risk_pct REAL DEFAULT 0.02,
last_updated TEXT
);
CREATE TABLE IF NOT EXISTS session_stats (
session TEXT NOT NULL,
timeframe TEXT NOT NULL,
total_trades INTEGER DEFAULT 0,
win_trades INTEGER DEFAULT 0,
win_rate REAL DEFAULT 0.0,
avg_rr REAL DEFAULT 0.0,
last_updated TEXT,
PRIMARY KEY (session, timeframe)
);
CREATE TABLE IF NOT EXISTS setup_stats (
setup_type TEXT PRIMARY KEY,
total_trades INTEGER DEFAULT 0,
win_trades INTEGER DEFAULT 0,
win_rate REAL DEFAULT 0.0,
avg_rr REAL DEFAULT 0.0,
last_updated TEXT
);
-- ── Feature 3: Standing Orders / Auto-Blacklist ───────────────────────
CREATE TABLE IF NOT EXISTS auto_blacklist (
id INTEGER PRIMARY KEY AUTOINCREMENT,
symbol TEXT NOT NULL,
reason TEXT NOT NULL, -- e.g. "win_rate=0.20 over 15 trades"
blacklist_type TEXT NOT NULL, -- 'PAIR' | 'PAIR_SESSION'
session TEXT DEFAULT '', -- '' for full pair blacklist
win_rate REAL,
total_trades INTEGER,
created_at TEXT NOT NULL,
expires_at TEXT, -- NULL = permanent until manually cleared
active INTEGER DEFAULT 1, -- 1=active, 0=cleared
UNIQUE (symbol, blacklist_type, session)
);
-- ── OI vs Price Change Stats ─────────────────────────────────────────
CREATE TABLE IF NOT EXISTS oi_price_stats (
oi_bucket TEXT NOT NULL, -- e.g. 'STRONG_RISE', 'RISE', 'FLAT', 'DROP', 'STRONG_DROP'
price_direction TEXT NOT NULL, -- 'LONG' | 'SHORT'
total_trades INTEGER DEFAULT 0,
win_trades INTEGER DEFAULT 0,
win_rate REAL DEFAULT 0.0,
avg_rr REAL DEFAULT 0.0,
avg_mc_win_prob REAL DEFAULT 0.0, -- rata-rata mc_win_prob saat entry
avg_oi_change REAL DEFAULT 0.0, -- rata-rata oi_change aktual
last_updated TEXT,
PRIMARY KEY (oi_bucket, price_direction)
);
-- ── Feature 4: L3 Meta-Feedback column (added via ALTER if missing) ───
-- meta_feedback TEXT added to trade_intelligence via migration below
"""
# ── DB Connection ─────────────────────────────────────────────────────
def get_conn() -> sqlite3.Connection:
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
conn.row_factory = sqlite3.Row
return conn
def init_db():
"""Buat tabel jika belum ada."""
with _db_lock:
conn = get_conn()
conn.executescript(SCHEMA)
conn.commit()
# ── Migration: add meta_feedback column if not exists ──────────
try:
conn.execute("ALTER TABLE trade_intelligence ADD COLUMN meta_feedback TEXT")
conn.commit()
logger.info("[DB] Migration: added meta_feedback column to trade_intelligence")
except Exception:
pass # Column already exists
# ── Migration: add cio_bull / cio_bear columns ─────────────────
for col in ('cio_bull_reasoning TEXT', 'cio_bear_reasoning TEXT', 'cio_verdict TEXT'):
try:
conn.execute(f"ALTER TABLE trade_intelligence ADD COLUMN {col}")
conn.commit()
except Exception:
pass
# ── Migration: create oi_price_stats if not exists ─────────────
conn.execute("""
CREATE TABLE IF NOT EXISTS oi_price_stats (
oi_bucket TEXT NOT NULL,
price_direction TEXT NOT NULL,
total_trades INTEGER DEFAULT 0,
win_trades INTEGER DEFAULT 0,
win_rate REAL DEFAULT 0.0,
avg_rr REAL DEFAULT 0.0,
avg_mc_win_prob REAL DEFAULT 0.0,
avg_oi_change REAL DEFAULT 0.0,
last_updated TEXT,
PRIMARY KEY (oi_bucket, price_direction)
)
""")
conn.commit()
conn.close()
# Init RAG schema
try:
import rag_memory
rag_memory.init_rag_schema()
except Exception as e:
logger.warning(f"[DB] RAG schema init warning: {e}")
logger.info(f"[DB] Initialized: {DB_PATH}")
# ── Binance API helper ────────────────────────────────────────────────
def _signed_get(path: str, params: dict = None) -> any:
params = params or {}
params['timestamp'] = int(time.time() * 1000)
params['recvWindow'] = 60000
query = urlencode(params)
sig = hmac.new(API_SECRET.encode(), query.encode(), hashlib.sha256).hexdigest()
url = f"{BINANCE_BASE_URL}{path}?{query}&signature={sig}"
resp = requests.get(url, headers={'X-MBX-APIKEY': API_KEY}, timeout=15)
resp.raise_for_status()
return resp.json()
# ── Sync State (last fetched timestamp) ──────────────────────────────
def _get_sync_time(key: str) -> int:
"""Ambil timestamp terakhir sync dari DB."""
with _db_lock:
conn = get_conn()
row = conn.execute(
"SELECT value FROM sync_state WHERE key = ?", (key,)
).fetchone()
conn.close()
# Default: mulai dari waktu saat ini agar tidak mengambil history lama
return int(row['value']) if row else int(time.time() * 1000)
def _set_sync_time(key: str, ts: int):
"""Simpan timestamp terakhir sync ke DB."""
with _db_lock:
conn = get_conn()
conn.execute(
"INSERT OR REPLACE INTO sync_state (key, value) VALUES (?, ?)",
(key, str(ts))
)
conn.commit()
conn.close()
# ── Income Log Sync ───────────────────────────────────────────────────
def sync_income_log() -> int:
"""
Fetch semua REALIZED_PNL + COMMISSION dari Binance income history.
Simpan ke income_log, deduplikasi by trade_id.
Returns jumlah record baru yang disimpan.
"""
total_new = 0
income_types = ['REALIZED_PNL', 'COMMISSION']
for income_type in income_types:
sync_key = f'income_last_time_{income_type}'
# Guard: jangan pernah ambil data sebelum SYNC_START_TS
last_time = _get_sync_time(sync_key)
# Fallback ke key lama jika key spesifik tipe belum ada
if last_time == int(time.time() * 1000) or last_time == 0:
legacy_time = _get_sync_time('income_last_time')
if legacy_time > 0:
last_time = legacy_time
else:
# Default 24 jam ke belakang
last_time = int(time.time() * 1000) - 24 * 3600 * 1000
# Look back 12 jam untuk menyembuhkan gap, tapi tidak boleh sebelum SYNC_START_TS
fetch_start = max(SYNC_START_TS, last_time - 12 * 3600 * 1000)
latest_fetched_time = last_time
while True:
params = {
'incomeType': income_type,
'limit': 1000,
}
if fetch_start:
params['startTime'] = fetch_start
try:
params['recvWindow'] = 60000
data = _signed_get('/fapi/v1/income', params)
except Exception as e:
logger.error(f"[DB] Income fetch error ({income_type}): {e}")
break
if not isinstance(data, list) or not data:
_set_sync_time(sync_key, max(latest_fetched_time, int(time.time() * 1000)))
break
rows = []
for item in data:
item_time = int(item.get('time', 0))
if item_time < SYNC_START_TS:
continue
base_trade_id = str(item.get('tradeId', f"{item['symbol']}_{item['time']}"))
db_trade_id = f"{base_trade_id}_{income_type}"
rows.append((
db_trade_id,
item.get('symbol', ''),
income_type,
float(item.get('income', 0)),
item_time,
item.get('asset', 'USDT'),
))
if item_time > latest_fetched_time:
latest_fetched_time = item_time
if rows:
with _db_lock:
conn = get_conn()
conn.executemany(
"""INSERT OR IGNORE INTO income_log
(trade_id, symbol, income_type, income, timestamp, asset)
VALUES (?, ?, ?, ?, ?, ?)""",
rows
)
inserted = conn.total_changes
conn.commit()
conn.close()
total_new += inserted
if len(data) < 1000:
_set_sync_time(sync_key, max(latest_fetched_time, int(time.time() * 1000)))
break
fetch_start = int(data[-1]['time']) + 1
if total_new > 0:
logger.info(f"[DB] Income sync: +{total_new} records baru")
return total_new
# ── Trade Position Sync ───────────────────────────────────────────────
def sync_closed_trades() -> int:
"""
Rekonstruksi posisi closed dari income_log.
Cocokkan REALIZED_PNL + COMMISSION per symbol per waktu close.
Simpan ke tabel trades.
Returns jumlah posisi baru yang disimpan.
"""
with _db_lock:
conn = get_conn()
# Ambil semua REALIZED_PNL sejak SYNC_START_TS yang belum ada di trades
rows = conn.execute("""
SELECT il.symbol, il.income AS pnl, il.timestamp AS close_time, il.trade_id
FROM income_log il
WHERE il.income_type = 'REALIZED_PNL'
AND il.income != 0
AND il.timestamp >= ?
ORDER BY il.timestamp ASC
""", (SYNC_START_TS,)).fetchall()
existing_trade_ids = set(
r[0] for r in conn.execute(
"SELECT binance_order_id FROM trades WHERE binance_order_id IS NOT NULL"
).fetchall()
)
conn.close()
if not rows:
return 0
# Fetch user trades dari Binance untuk dapat entry/exit price
# Group by symbol untuk efisiensi
symbols = list({r['symbol'] for r in rows if r['symbol']})
user_trades_map: Dict[str, List] = {}
# Dapatkan startTime dinamis: 5 menit sebelum close_time unsynced paling awal
min_close_time = min(int(r['close_time']) for r in rows)
start_time_param = max(min_close_time - 300000, int(time.time() * 1000) - 7 * 24 * 3600 * 1000)
for symbol in symbols:
try:
params = {
'symbol': symbol,
'limit': 1000,
'startTime': start_time_param
}
data = _signed_get('/fapi/v1/userTrades', params)
if isinstance(data, list):
user_trades_map[symbol] = data
except Exception as e:
logger.warning(f"[DB] userTrades fetch error {symbol}: {e}")
user_trades_map[symbol] = []
# Rekonstruksi posisi: match income record dengan user trade yang sesuai
new_count = 0
with _db_lock:
conn = get_conn()
for row in rows:
trade_id = str(row['trade_id'])
# Dapatkan raw trade_id numerik jika menggunakan format bersuffix
actual_trade_id = trade_id.split('_')[0]
if actual_trade_id in existing_trade_ids or trade_id in existing_trade_ids:
continue
symbol = row['symbol']
pnl = float(row['pnl'])
close_time = int(row['close_time'])
# Cari commission untuk trade ini (coba via ID langsung dulu, fallback ke timestamp)
commission = 0.0
commission_row = conn.execute("""
SELECT income FROM income_log
WHERE trade_id = ?
""", (f"{actual_trade_id}_COMMISSION",)).fetchone()
if commission_row:
commission = abs(float(commission_row['income']))
else:
commission = conn.execute("""
SELECT COALESCE(SUM(ABS(income)), 0) as fee
FROM income_log
WHERE symbol = ? AND income_type = 'COMMISSION'
AND timestamp BETWEEN ? AND ?
""", (symbol, close_time - 1000, close_time + 1000)).fetchone()['fee']
net_pnl = pnl - abs(commission)
# Cari user trade yang cocok
ut_list = user_trades_map.get(symbol, [])
closest = None
# 1. Cari exact match menggunakan trade ID (id)
for ut in ut_list:
if str(ut.get('id')) == actual_trade_id:
closest = ut
break
# 2. Fallback ke fuzzy match by timestamp jika exact match tidak ditemukan
if not closest:
min_diff = float('inf')
for ut in ut_list:
diff = abs(int(ut.get('time', 0)) - close_time)
if diff < min_diff:
min_diff = diff
closest = ut
if closest and min_diff >= 60000:
closest = None
if closest:
side = 'LONG' if closest.get('buyer') else 'SHORT'
exit_price = float(closest.get('price', 0))
qty = float(closest.get('qty', 0))
# Estimasi entry dari PnL: entry = exit ± (pnl / qty)
if qty > 0:
if side == 'LONG':
entry_price = exit_price - (pnl / qty)
else:
entry_price = exit_price + (pnl / qty)
else:
entry_price = exit_price
else:
# Fallback jika tidak ada user trade yang cocok
side = 'UNKNOWN'
exit_price = 0.0
entry_price = 0.0
qty = 0.0
# Gunakan actual_trade_id sebagai binance_order_id agar tidak terjadi
# duplikat saat sync berikutnya (karena existing_trade_ids mengecek ini).
order_id = actual_trade_id
# Tambahkan offset kecil unik ke open_time untuk menghindari UNIQUE(symbol, open_time, side)
# constraint violation jika ada beberapa trade di detik/milidetik yang sama.
try:
unique_offset = int(actual_trade_id) % 1000
except ValueError:
unique_offset = 0
unique_open_time = close_time - 60000 + unique_offset
try:
conn.execute("""
INSERT OR IGNORE INTO trades
(symbol, side, qty, entry_price, exit_price,
open_time, close_time, realized_pnl, commission, net_pnl,
status, binance_order_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'CLOSED', ?)
""", (
symbol, side, qty, entry_price, exit_price,
unique_open_time,
close_time,
pnl, commission, net_pnl,
order_id,
))
new_count += conn.total_changes
except Exception as e:
logger.warning(f"[DB] Insert trade error {symbol}: {e}")
conn.commit()
conn.close()
if new_count > 0:
logger.info(f"[DB] Closed trades sync: +{new_count} posisi baru")
return new_count
# ── Query helpers untuk dashboard ────────────────────────────────────
def get_stats() -> Dict:
"""Hitung statistik dari semua closed trades di DB (teragregasi by orderId)."""
with _db_lock:
conn = get_conn()
row = conn.execute("""
WITH lagged AS (
SELECT symbol, side, net_pnl, realized_pnl, commission,
LAG(close_time) OVER (PARTITION BY symbol, side ORDER BY close_time) as prev_close,
close_time
FROM trades
WHERE status = 'CLOSED' AND close_time >= ?
),
marked AS (
SELECT *, CASE WHEN prev_close IS NULL OR (close_time - prev_close) > 7200000 THEN 1 ELSE 0 END as is_new_group
FROM lagged
),
grouped AS (
SELECT *, SUM(is_new_group) OVER (PARTITION BY symbol, side ORDER BY close_time) as group_id
FROM marked
),
aggregated_trades AS (
SELECT
symbol,
side,
group_id,
SUM(net_pnl) AS net_pnl,
SUM(realized_pnl) AS realized_pnl,
SUM(commission) AS commission
FROM grouped
GROUP BY symbol, side, group_id
)
SELECT
COUNT(*) AS total_trades,
SUM(CASE WHEN net_pnl > 0 THEN 1 ELSE 0 END) AS win_trades,
SUM(CASE WHEN net_pnl < 0 THEN 1 ELSE 0 END) AS loss_trades,
COALESCE(SUM(net_pnl), 0) AS total_pnl,
COALESCE(SUM(realized_pnl), 0) AS total_realized,
COALESCE(SUM(commission), 0) AS total_commission
FROM aggregated_trades
""", (SYNC_START_TS,)).fetchone()
conn.close()
total = row['total_trades'] or 0
wins = row['win_trades'] or 0
losses = row['loss_trades'] or 0
return {
'total_trades': total,
'win_trades': wins,
'loss_trades': losses,
'total_pnl': round(row['total_pnl'] or 0, 4),
'total_realized': round(row['total_realized'] or 0, 4),
'total_commission': round(row['total_commission'] or 0, 4),
'win_rate': round(wins / total * 100, 2) if total > 0 else 0.0,
}
def get_recent_trades(limit: int = 50) -> List[Dict]:
"""Ambil trade terbaru dari DB (teragregasi by orderId — 1 order = 1 baris)."""
with _db_lock:
conn = get_conn()
rows = conn.execute("""
WITH lagged AS (
SELECT *, LAG(close_time) OVER (PARTITION BY symbol, side ORDER BY close_time) as prev_close
FROM trades
WHERE status = 'CLOSED' AND close_time >= ?
),
marked AS (
SELECT *, CASE WHEN prev_close IS NULL OR (close_time - prev_close) > 7200000 THEN 1 ELSE 0 END as is_new_group
FROM lagged
),
grouped AS (
SELECT *, SUM(is_new_group) OVER (PARTITION BY symbol, side ORDER BY close_time) as group_id
FROM marked
)
SELECT
symbol,
side,
SUM(qty) AS qty,
CASE WHEN SUM(qty) > 0 THEN SUM(entry_price * qty) / SUM(qty) ELSE AVG(entry_price) END AS entry_price,
CASE WHEN SUM(qty) > 0 THEN SUM(exit_price * qty) / SUM(qty) ELSE AVG(exit_price) END AS exit_price,
MIN(open_time) AS open_time,
MAX(close_time) AS close_time,
SUM(realized_pnl) AS realized_pnl,
SUM(commission) AS commission,
SUM(net_pnl) AS net_pnl,
MAX(leverage) AS leverage,
'CLOSED' AS status
FROM grouped
GROUP BY symbol, side, group_id
ORDER BY close_time DESC
LIMIT ?
""", (SYNC_START_TS, limit)).fetchall()
conn.close()
return [dict(r) for r in rows]
def get_pnl_curve(limit: int = 200) -> List[Dict]:
"""Ambil data untuk PnL curve chart (teragregasi by close_time)."""
with _db_lock:
conn = get_conn()
rows = conn.execute("""
WITH lagged AS (
SELECT *, LAG(close_time) OVER (PARTITION BY symbol, side ORDER BY close_time) as prev_close
FROM trades
WHERE status = 'CLOSED' AND close_time >= ?
),
marked AS (
SELECT *, CASE WHEN prev_close IS NULL OR (close_time - prev_close) > 7200000 THEN 1 ELSE 0 END as is_new_group
FROM lagged
),
grouped AS (
SELECT *, SUM(is_new_group) OVER (PARTITION BY symbol, side ORDER BY close_time) as group_id
FROM marked
),
aggregated_trades AS (
SELECT MAX(close_time) as close_time, SUM(net_pnl) AS net_pnl
FROM grouped
GROUP BY symbol, side, group_id
)
SELECT close_time, net_pnl,
SUM(net_pnl) OVER (ORDER BY close_time) AS cumulative_pnl
FROM aggregated_trades
ORDER BY close_time DESC
LIMIT ?
""", (SYNC_START_TS, limit)).fetchall()
conn.close()
return [dict(r) for r in reversed(rows)]
# ── Trade Intelligence CRUD ──────────────────────────────────────────
import json as _json
from datetime import datetime as _dt
def log_trade_open(
trade_ref: str,
symbol: str,
direction: str,
entry_time_utc: str,
timeframe: str,
session: str,
entry_hour_utc: int,
entry_weekday: int,
setup_type: str,
smc_signals: dict,
mc_confidence: float,
mc_win_prob: float,
signal_score: float,
risk_reward: float,
atr: float,
atr_pct: float,
funding_rate: float,
oi_change: float,
htf_bias: str,
bb_pct: float,
rsi: float,
macd_cross: int,
vol_spike: int,
entry_price: float,
take_profit: float,
stop_loss: float,
leverage: int,
margin_used: float,
pending_duration_mins: int = 0,
consecutive_losses_at_entry: int = 0,
binance_order_id: str = None,
):
"""Catat pembukaan trade baru ke tabel trade_intelligence."""
with _db_lock:
conn = get_conn()
try:
conn.execute("""
INSERT OR IGNORE INTO trade_intelligence (
trade_ref, binance_order_id, symbol, direction,
entry_time_utc, timeframe, session, entry_hour_utc, entry_weekday,
setup_type, smc_signals, mc_confidence, mc_win_prob, signal_score,
risk_reward, atr, atr_pct, funding_rate, oi_change, htf_bias,
bb_pct, rsi, macd_cross, vol_spike,
entry_price, take_profit, stop_loss,
leverage, margin_used, pending_duration_mins,
consecutive_losses_at_entry, outcome
) VALUES (
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
)
""", (
trade_ref, binance_order_id, symbol, direction,
entry_time_utc, timeframe, session, entry_hour_utc, entry_weekday,
setup_type, _json.dumps(smc_signals), mc_confidence, mc_win_prob, signal_score,
risk_reward, atr, atr_pct, funding_rate, oi_change, htf_bias,
bb_pct, rsi, macd_cross, vol_spike,
entry_price, take_profit, stop_loss,
leverage, margin_used, pending_duration_mins,
consecutive_losses_at_entry, 'OPEN'
))
conn.commit()
logger.info(f"[DI] Logged trade open: {trade_ref}")
except Exception as e:
logger.error(f"[DI] log_trade_open error: {e}")
finally:
conn.close()
def log_trade_close(
trade_ref: str,
exit_price: float,
result_pnl: float,
close_time_utc: str,
binance_order_id: str = None,
):
"""Update trade_intelligence saat posisi close."""
with _db_lock:
conn = get_conn()
try:
row = conn.execute(
"SELECT entry_price, stop_loss, take_profit, entry_time_utc, direction FROM trade_intelligence WHERE trade_ref = ?",
(trade_ref,)
).fetchone()
if not row:
# Jika tidak ada trade_ref, coba cari by binance_order_id
if binance_order_id:
row = conn.execute(
"SELECT entry_price, stop_loss, take_profit, entry_time_utc, direction, trade_ref FROM trade_intelligence WHERE binance_order_id = ?",
(binance_order_id,)
).fetchone()
if row:
trade_ref = row['trade_ref']
if not row:
logger.warning(f"[DI] log_trade_close: trade_ref '{trade_ref}' not found")
conn.close()
return
entry_price = float(row['entry_price'])
stop_loss = float(row['stop_loss'])
take_profit = float(row['take_profit'])
direction = row['direction']
entry_time = row['entry_time_utc']
# Hitung RR yang tercapai
sl_dist = abs(entry_price - stop_loss)
if sl_dist > 0:
price_move = exit_price - entry_price if direction == 'LONG' else entry_price - exit_price
result_rr_achieved = price_move / sl_dist
else:
result_rr_achieved = 0.0
# Tentukan outcome
if result_pnl > 0:
outcome = 'WIN'
elif result_pnl < 0:
outcome = 'LOSS'
else:
outcome = 'BE'
# Hitung durasi
try:
t_open = _dt.fromisoformat(entry_time.replace('Z', ''))
t_close = _dt.fromisoformat(close_time_utc.replace('Z', ''))
duration_mins = int((t_close - t_open).total_seconds() / 60)
except Exception:
duration_mins = 0
conn.execute("""
UPDATE trade_intelligence
SET exit_price = ?, result_pnl = ?, result_rr_achieved = ?,
close_time_utc = ?, outcome = ?, trade_duration_mins = ?
WHERE trade_ref = ?
""", (
exit_price, result_pnl, result_rr_achieved,
close_time_utc, outcome, duration_mins,
trade_ref
))
conn.commit()
logger.info(f"[DI] Logged trade close: {trade_ref} → {outcome} | PnL: {result_pnl:.4f}")
except Exception as e:
logger.error(f"[DI] log_trade_close error: {e}")
finally:
conn.close()
def update_mae_mfe(trade_ref: str, mark_price: float):
"""Update MAE dan MFE untuk trade yang masih open."""
with _db_lock:
conn = get_conn()
try:
row = conn.execute(
"SELECT entry_price, stop_loss, direction, mae, mfe FROM trade_intelligence WHERE trade_ref = ? AND outcome = 'OPEN'",
(trade_ref,)
).fetchone()
if not row:
conn.close()
return
entry = float(row['entry_price'])
sl = float(row['stop_loss'])
sl_dist = abs(entry - sl)
if sl_dist == 0:
conn.close()
return
direction = row['direction']
cur_mae = float(row['mae'] or 0)
cur_mfe = float(row['mfe'] or 0)
if direction == 'LONG':
excursion = (mark_price - entry) / sl_dist # positive = profit, negative = adverse
else:
excursion = (entry - mark_price) / sl_dist
new_mae = min(cur_mae, excursion) # most negative
new_mfe = max(cur_mfe, excursion) # most positive
if new_mae != cur_mae or new_mfe != cur_mfe:
conn.execute(
"UPDATE trade_intelligence SET mae = ?, mfe = ? WHERE trade_ref = ?",
(new_mae, new_mfe, trade_ref)
)
conn.commit()
except Exception as e:
logger.error(f"[DI] update_mae_mfe error: {e}")
finally:
conn.close()
def get_consecutive_losses(symbol: str = None) -> int:
"""Hitung jumlah losses berturut-turut terbaru (opsional filter per pair)."""
with _db_lock:
conn = get_conn()
try:
if symbol:
rows = conn.execute(
"SELECT outcome FROM trade_intelligence WHERE outcome IN ('WIN','LOSS','BE') AND symbol = ? ORDER BY entry_time_utc DESC LIMIT 20",
(symbol,)
).fetchall()
else:
rows = conn.execute(
"SELECT outcome FROM trade_intelligence WHERE outcome IN ('WIN','LOSS','BE') ORDER BY entry_time_utc DESC LIMIT 20"
).fetchall()
conn.close()
count = 0
for r in rows:
if r['outcome'] == 'LOSS':
count += 1
else:
break
return count
except Exception as e:
logger.error(f"[DI] get_consecutive_losses error: {e}")
conn.close()
return 0
def get_last_loss_close_time() -> Optional[str]:
"""Ambil close_time_utc dari loss terakhir."""
with _db_lock:
conn = get_conn()
try:
row = conn.execute(
"SELECT close_time_utc FROM trade_intelligence WHERE outcome = 'LOSS' AND close_time_utc IS NOT NULL ORDER BY close_time_utc DESC LIMIT 1"
).fetchone()
conn.close()
return row['close_time_utc'] if row else None
except Exception as e:
logger.error(f"[DI] get_last_loss_close_time error: {e}")
conn.close()
return None
def get_pair_stats(symbol: str = None) -> List[Dict]:
"""Ambil statistik per pair dari tabel pair_stats."""
with _db_lock:
conn = get_conn()
if symbol:
rows = conn.execute("SELECT * FROM pair_stats WHERE symbol = ?", (symbol,)).fetchall()
else:
rows = conn.execute("SELECT * FROM pair_stats ORDER BY win_rate DESC").fetchall()
conn.close()
return [dict(r) for r in rows]
def get_session_stats() -> List[Dict]:
"""Ambil statistik per session dari tabel session_stats."""
with _db_lock:
conn = get_conn()
rows = conn.execute("SELECT * FROM session_stats ORDER BY win_rate DESC").fetchall()
conn.close()
return [dict(r) for r in rows]
def get_setup_stats() -> List[Dict]:
"""Ambil statistik per setup dari tabel setup_stats."""
with _db_lock:
conn = get_conn()
rows = conn.execute("SELECT * FROM setup_stats ORDER BY win_rate DESC").fetchall()
conn.close()
return [dict(r) for r in rows]
def get_oi_price_stats() -> List[Dict]:
"""Ambil statistik OI vs Price Change dari tabel oi_price_stats."""
with _db_lock:
conn = get_conn()
rows = conn.execute("""
SELECT * FROM oi_price_stats
ORDER BY
CASE oi_bucket
WHEN 'STRONG_RISE' THEN 1
WHEN 'RISE' THEN 2
WHEN 'FLAT' THEN 3
WHEN 'DROP' THEN 4
WHEN 'STRONG_DROP' THEN 5
ELSE 6
END,
price_direction
""").fetchall()
conn.close()
return [dict(r) for r in rows]
def get_intelligence_by_symbol(symbol: str, limit: int = 100) -> List[Dict]:
"""Ambil histori trade_intelligence untuk satu pair."""
with _db_lock:
conn = get_conn()
rows = conn.execute(
"SELECT * FROM trade_intelligence WHERE symbol = ? ORDER BY entry_time_utc DESC LIMIT ?",
(symbol, limit)
).fetchall()
conn.close()
return [dict(r) for r in rows]
def get_hourly_stats() -> List[Dict]:
"""Hitung win rate per jam UTC dari trade_intelligence."""
with _db_lock:
conn = get_conn()
rows = conn.execute("""
SELECT entry_hour_utc,
COUNT(*) as total,
SUM(CASE WHEN outcome='WIN' THEN 1 ELSE 0 END) as wins,
ROUND(1.0 * SUM(CASE WHEN outcome='WIN' THEN 1 ELSE 0 END) / COUNT(*), 4) as win_rate
FROM trade_intelligence
WHERE outcome IN ('WIN','LOSS','BE')
GROUP BY entry_hour_utc
ORDER BY entry_hour_utc
""").fetchall()
conn.close()
return [dict(r) for r in rows]
# ── Background sync loop ──────────────────────────────────────────────
def run_sync_loop():