feat: add 5 dashboard features — dark mode, trade history, backtests, model insights, alerts

- Dark mode: class-based theme toggle with localStorage persistence and flash prevention
- Trade History (/trades): paginated table, stats cards, equity curve chart with DB API endpoints
- Backtest Viewer (/backtests): log parser for 35 backtest results, sidebar + detail + comparison tabs
- Model Insights: dashboard card + dialog showing feature importance, regime distribution, training history
- Alert/Signal Log (/alerts): signal stats, filterable table with execution tracking
- API: 8 new endpoints with psycopg2 DB connection pool
- Dark mode sweep across books page, about dialog, and all dashboard components
- Architecture docs rewritten with Mermaid diagrams (23 docs)
- README and FEATURES.md rewritten bilingual (Indonesian + English)
- main_live.py: write model_metrics.json on startup and retrain

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
GifariKemal
2026-02-09 05:46:54 +07:00
co-authored by Claude Opus 4.6
parent b2dc2dacd7
commit e8355b3f62
230 changed files with 69573 additions and 5673 deletions
+174 -311
View File
@@ -1,348 +1,211 @@
# Exit Trade — Proses Keluar Posisi
# *Exit Trade* — Proses Keluar Posisi
> **File utama:** `main_live.py`, `src/smart_risk_manager.py`
> **File pendukung:** `src/position_manager.py`
---
## Apa Itu Exit Trade?
## Apa Itu *Exit Trade*?
Exit Trade adalah keseluruhan proses **monitoring posisi terbuka** dan **memutuskan kapan menutup**. Bot memeriksa setiap posisi terbuka **setiap ~10 detik** (di antara candle) atau **setiap candle baru** (full analysis) dengan 10 kondisi exit berbeda.
*Exit Trade* adalah keseluruhan proses **monitoring posisi terbuka** dan **memutuskan kapan menutup**. Bot memeriksa setiap posisi terbuka **setiap ~10 detik** (di antara *candle*) atau **setiap *candle* baru** (*full analysis*) dengan **12 kondisi *exit*** berbeda.
**Analogi:** Exit Trade seperti **pilot otomatis di pesawat** — terus monitor ketinggian (profit), cuaca (momentum), bahan bakar (waktu), dan bisa landing darurat kapan saja.
**Prinsip utama:** **Jangan biarkan *winner* menjadi *loser***, tapi juga **jangan potong *winner* terlalu cepat**.
---
## 2 Jalur Exit
## 12 Kondisi *Exit*
```
Jalur 1: BROKER EXIT (otomatis, independen)
-> Harga hit TP level -> tutup otomatis
-> Harga hit SL level -> tutup otomatis
-> Tidak perlu bot online
Jalur 2: SOFTWARE EXIT (cerdas, kontekstual)
-> Bot evaluasi setiap 1 detik
-> Mempertimbangkan momentum, ML, waktu, dll
-> 10 kondisi exit berbeda
```mermaid
graph TD
POS["Posisi Terbuka"] --> C1{"1. Smart TP<br/>Profit ≥ $15?"}
C1 -->|Ya & kondisi| CLOSE["TUTUP POSISI"]
C1 -->|Tidak| C2{"2. Early Exit<br/>Profit $5-15?"}
C2 -->|Ya & reversal| CLOSE
C2 -->|Tidak| C3{"3. Early Cut<br/>Loss + momentum?"}
C3 -->|Ya| CLOSE
C3 -->|Tidak| C4{"4. Trend Reversal<br/>ML sinyal balik?"}
C4 -->|Ya| CLOSE
C4 -->|Tidak| C5{"5. Max Loss<br/>Loss > 50% max?"}
C5 -->|Ya| CLOSE
C5 -->|Tidak| C6{"6. Stall<br/>Stuck + rugi?"}
C6 -->|Ya| CLOSE
C6 -->|Tidak| C7{"7. Daily Limit<br/>Batas harian?"}
C7 -->|Ya| CLOSE
C7 -->|Tidak| C8{"8. Weekend Close"}
C8 -->|Ya| CLOSE
C8 -->|Tidak| C9{"9. Time-Based<br/>4-8 jam?"}
C9 -->|Ya & kondisi| CLOSE
C9 -->|Tidak| C10{"10-12. Trailing SL<br/>Breakeven, dll"}
C10 -->|Ya| CLOSE
C10 -->|Tidak| HOLD["TAHAN POSISI ↩"]
```
---
## Monitoring Loop
### CHECK 1: *Smart Take Profit* (Profit ≥ $15)
| Kondisi | Aksi | Keterangan |
|---------|------|------------|
| Profit ≥ **$40** | **Tutup** langsung | Target tercapai — *hard TP* |
| Profit ≥ **$25** dan momentum < -30 | **Tutup** | Profit bagus tapi momentum turun |
| *Peak profit* > $30, profit turun ke < 60% *peak* | **Tutup** | Lindungi profit dari *peak* |
| Probabilitas TP < 25%, profit ≥ **$20** | **Tutup** | Kemungkinan TP rendah |
| Momentum ≥ 0 | **Tahan** | Masih bagus, biarkan berjalan |
---
### CHECK 2: *Smart Early Exit* (Profit $5-15)
```python
# main_live.py Lines 1117-1215
# Setiap 1 detik, untuk SETIAP posisi terbuka:
for position in open_positions:
# Update data posisi
current_price = mt5.get_tick(symbol)
current_profit = position.profit
# Update history untuk analisis momentum
guard.update_history(current_price, current_profit, ml_confidence)
# Evaluasi: haruskah ditutup?
should_close, reason, message = smart_risk.evaluate_position(
ticket=ticket,
current_price=current_price,
current_profit=profit,
ml_signal=ml_prediction.signal,
ml_confidence=ml_prediction.confidence,
regime=regime_state,
)
if should_close:
# Tutup posisi
close_position(ticket, reason)
if 5 <= current_profit < 15:
if momentum < -50 and ml_confidence >= 0.65:
if ml_signal berlawanan dengan arah posisi:
TUTUP # Sinyal reversal kuat + profit kecil
```
**Tujuan:** Ambil profit kecil jika momentum sangat negatif DAN ML yakin *trend* berbalik.
---
## 10 Kondisi Exit (Urutan Pengecekan)
### CHECK 1: Smart Take Profit (profit >= $15)
```
Ketika profit sudah cukup besar, evaluasi apakah harus diamankan:
a) Hard TP: profit >= $40
-> TUTUP langsung, target tercapai
b) Momentum TP: profit >= $25 DAN momentum < -30
-> TUTUP, profit sedang turun cepat
c) Peak Protection: peak > $30 DAN current < 60% peak
-> TUTUP, lindungi dari drawback lebih dalam
d) Probability TP: TP_prob < 25% DAN profit >= $20
-> TUTUP, kemungkinan capai TP sudah rendah
e) Strong Momentum: momentum >= 0
-> HOLD, biarkan profit berjalan (let it run)
```
---
### CHECK 2: Early Exit Small Profit ($5-$15)
```
Profit masih kecil tapi ada tanda bahaya:
IF profit $5-$15
AND momentum < -50 (turun sangat cepat)
AND ML confidence >= 65% berlawanan arah:
-> TUTUP, ambil profit kecil sebelum hilang
```
---
### CHECK 3: Early Cut (v4 — Smart Hold DIHAPUS)
```
Posisi sedang rugi — potong cepat jika sinyal buruk:
IF profit < 0:
Loss >= 30% max ($15 dari $50) DAN momentum < -30
-> TUTUP CEPAT (early cut, jangan tunggu recovery)
v4 PERUBAHAN: "Smart Hold" DIHAPUS
- SEBELUMNYA: Hold posisi rugi menunggu golden time (20:00-23:59)
- SEBELUMNYA: Hold posisi rugi kecil di sesi London (15:00-19:00)
- SEKARANG: Tidak ada lagi "hold losers hoping for recovery"
- ALASAN: Menahan posisi rugi menunggu sesi tertentu = perilaku
berbahaya (martingale mentality). Proper risk management:
ikuti aturan SL, jangan berharap recovery.
```
---
### CHECK 4: Trend Reversal Detection
```
ML mendeteksi perubahan tren:
IF ML confidence >= 65% berlawanan dengan posisi:
a) Loss > 40% max DAN profit < -$8
-> TUTUP (reversal + loss signifikan)
b) Akumulasi 3x reversal warning DAN loss < -$10
-> TUTUP (multiple warnings = konfirmasi reversal)
c) Belum memenuhi threshold
-> reversal_warnings += 1 (catat warning)
```
---
### CHECK 5: Maximum Loss Per Trade
```
Loss mencapai batas toleransi:
IF loss >= 50% dari max_loss ($25 dari $50):
Exception: golden_time <= 1 jam DAN momentum > -40
-> HOLD (kesempatan terakhir recovery)
Selain itu:
-> TUTUP [S/L] Position loss limit
```
---
### CHECK 6: Stall Detection
```
Harga tidak bergerak kemana-mana:
IF 10 candle terakhir range profit < $3
AND current_profit < -$15:
stall_count += 1
IF stall_count >= 5:
-> TUTUP [STALL] Harga stuck, buang waktu & margin
```
---
### CHECK 7: Daily Loss Limit
```
Mencegah daily loss limit terlampaui:
potential_daily_loss = daily_loss + abs(min(0, current_profit))
IF potential_daily_loss >= max_daily_loss ($250):
-> TUTUP [LIMIT] Akan melampaui batas harian
```
---
### CHECK 8: Weekend Close
```
Proteksi dari gap weekend:
IF hari Jumat setelah 04:00 WIB:
a) profit > 0
-> TUTUP [WEEKEND] Amankan profit
b) profit > -$10
-> TUTUP [WEEKEND] Loss kecil, hindari gap
c) profit <= -$10
-> HOLD (loss terlalu besar untuk cut, evaluasi manual)
```
---
### CHECK 9: Smart Time-Based Exit (v5: Smarter — Don't Cut Winners)
```
Mencegah posisi "zombie" TAPI jangan potong posisi profit yang masih tumbuh.
trade_duration = (sekarang - entry_time) dalam jam
profit_growing = momentum > 0
ml_agrees = ML signal searah dengan posisi
IF 4+ jam:
a) profit < $5 DAN NOT profit_growing:
- profit >= $0 -> TUTUP [TIMEOUT] Breakeven + no growth
- profit > -$15 -> TUTUP [TIMEOUT] Small loss + no growth
b) profit >= $5 DAN profit_growing DAN ml_agrees:
-> HOLD (extend time — profit masih tumbuh!)
-> Log: "extending time"
IF 6+ jam:
a) profit < $10 ATAU NOT profit_growing:
-> TUTUP [MAX TIME] (sebelumnya: force close apapun kondisi)
b) profit >= $10 DAN profit_growing:
-> EXTEND ke max 8 jam (v5 BARU — biarkan profit berjalan)
-> Di jam 8+ -> TUTUP [MAX TIME] Take profit
v5 PERUBAHAN:
- 4h: Sekarang cek profit growth, bukan hanya profit < $5
- 6h: BUKAN force close lagi — extend ke 8h jika profitable + growing
- ML agreement dipertimbangkan sebelum timeout
- Prinsip: jangan potong pemenang yang masih berjalan
```
**Visualisasi:**
```
Jam: 0 1 2 3 4 5 6 7 8
|-----|-----|-----|-----|-----|-----|-----|-----|
entry | | |
| | |
4h check: 6h check: 8h FINAL EXIT
stuck? profitable?
no growth? growing?
-> exit -> extend!
not growing?
-> exit
```
---
### CHECK 10: Default — HOLD
```
Tidak ada kondisi exit terpenuhi:
-> HOLD posisi
-> Log status: momentum, TP probability, ML signal
-> Evaluasi ulang di cek berikutnya (~10 detik atau candle baru)
```
---
## Exit Reason Enum
| Reason | Kode | Deskripsi |
|--------|------|-----------|
| `TAKE_PROFIT` | take_profit | Target profit tercapai |
| `TREND_REVERSAL` | trend_reversal | ML deteksi reversal |
| `DAILY_LIMIT` | daily_limit | Batas harian tercapai |
| `POSITION_LIMIT` | position_limit | Max loss per trade |
| `TOTAL_LIMIT` | total_limit | Batas total tercapai |
| `WEEKEND_CLOSE` | weekend_close | Penutupan Jumat |
| `TIMEOUT` | timeout | Time-based exit (4h/6h) |
| `STALL` | stall | Harga stuck |
| `MANUAL` | manual | Penutupan manual |
---
## Post-Exit Flow
### CHECK 3: *Early Cut* (Loss + Momentum Negatif)
```python
# Setelah posisi ditutup:
if current_profit < 0:
loss_pct = abs(current_profit) / max_loss_per_trade * 100
if momentum < -50 and loss_pct >= 30:
TUTUP # Potong kerugian sebelum makin besar
```
# 1. Record hasil trade
risk_result = smart_risk.record_trade_result(profit)
# Update: daily_loss, total_loss, consecutive_losses, mode
**Perubahan dari v1:** *Smart Hold* (menahan posisi rugi menunggu *golden time*) **sudah dihapus** — dianggap berbahaya dan melawan prinsip manajemen risiko yang benar.
# 2. Unregister dari monitoring
smart_risk.unregister_position(ticket)
---
# 3. Log trade
trade_logger.log_trade_close(
ticket, entry_price, exit_price, profit, pips,
duration, exit_reason, ml_signal, regime, ...
)
### CHECK 4: *Trend Reversal* (Sinyal ML Berbalik)
# 4. Kirim notifikasi Telegram
await telegram.send_trade_close(trade_info)
# Format: WIN/LOSS/BE, P/L, pips, duration, balance
| Kondisi | Aksi |
|---------|------|
| ML sinyal **berbalik** + *confidence* ≥ 75% + loss > 40% max + loss > $8 | **Tutup** |
| **3x** peringatan *reversal* berturut-turut + loss > $10 | **Tutup** |
# 5. Cek limit violations
if risk_result["daily_limit_hit"]:
await send_critical_alert("DAILY LOSS LIMIT")
# Mode -> STOPPED, tidak ada trade lagi hari ini
**Perubahan:** *Threshold* diturunkan dari 5x ke **3x** peringatan, dan batas loss dari 60% ke **40%** — lebih responsif.
if risk_result["total_limit_hit"]:
await send_critical_alert("TOTAL LOSS LIMIT")
# Mode -> STOPPED permanen
---
### CHECK 5: *Maximum Loss Per Trade*
```python
if current_profit <= -(max_loss_per_trade * 0.50):
TUTUP # 50% dari batas max — tanpa pengecualian
```
Untuk akun *small* ($5.000): max loss per *trade* = ~$50, *trigger* di $25.
---
### CHECK 6: *Stall Detection*
```python
if len(profit_history) >= 10:
recent_range = max(last_10) - min(last_10)
if recent_range < $3 and current_profit < -$15:
stall_count += 1
if stall_count >= 5:
TUTUP # Harga stuck, posisi rugi
```
**Tujuan:** Deteksi posisi yang "terjebak" — harga tidak bergerak tapi posisi rugi.
---
### CHECK 7: Batas Kerugian Harian
```python
potensi_loss = daily_loss + abs(current_profit)
if potensi_loss >= max_daily_loss:
TUTUP # Akan melebihi batas kerugian harian
```
---
## Diagram Exit Flow
### CHECK 8: *Weekend Close*
```python
# Sabtu 04:30+ WIB (30 menit sebelum market tutup)
if near_weekend_close:
if profit > 0:
TUTUP # Amankan profit
elif loss > -$10:
TUTUP # Loss kecil — hindari gap weekend
```
Setiap ~10 detik (atau candle baru), per posisi terbuka:
|
v
Update profit & momentum
|
v
[1] Profit >= $15? ----YES---> Smart TP evaluation
|NO (hard/$40, momentum, peak, prob)
v
[2] Profit $5-$15? ----YES---> Reversal + momentum drop?
|NO -> Early exit
v
[3] Profit < 0? -------YES---> Loss>30% + momentum<-30?
|NO -> EARLY CUT
v
[4] ML Reversal 65%+? -YES---> Loss > 40%? -> TUTUP
|NO Else warning++
v
[5] Loss >= 50% max? --YES---> TUTUP (kecuali golden time)
|NO
v
[6] Stall 10+ candle? -YES---> stall++ -> 5x? TUTUP
|NO
v
[7] Daily limit? ------YES---> TUTUP
|NO
v
[8] Friday close? -----YES---> TUTUP (profit>0 atau loss>-$10)
|NO
v
[9] Time >= 4h? -------YES---> profit<$5? TUTUP
| Time >= 6h? ---YES---> FORCE EXIT
|NO
v
[10] HOLD -> evaluasi ulang 1 detik kemudian
**Tujuan:** Hindari risiko *gap weekend* — posisi tanpa proteksi selama 2 hari.
---
### CHECK 9: *Smart Time-Based Exit*
| Durasi | Kondisi | Aksi |
|--------|---------|------|
| **4+ jam** | Profit < $5, momentum tidak tumbuh | **Tutup** — posisi *stuck* |
| **4+ jam** | Profit ≥ $5, momentum positif, ML sejalan | **Tahan** — perpanjang waktu |
| **6+ jam** | Profit < $10 ATAU momentum negatif | **Tutup** — terlalu lama |
| **8+ jam** | Apapun kondisinya | **Tutup** — batas waktu absolut |
**Perubahan:** Tidak lagi memotong *winner* secara paksa — posisi yang masih tumbuh bisa diperpanjang.
---
### CHECK 10-12: *Position Manager* (Tambahan)
Selain 9 kondisi di atas dari `SmartRiskManager`, `SmartPositionManager` juga menjalankan:
| # | Kondisi | Keterangan |
|---|---------|------------|
| 10 | ***Trailing Stop Loss*** | SL mengikuti harga naik (berbasis ATR) — `atr_trail_start_mult=4.0`, `atr_trail_step_mult=3.0` |
| 11 | ***Breakeven Protection*** | Pindahkan SL ke titik *entry* setelah profit ≥ 30 *pips* |
| 12 | **Proteksi *Drawdown*** | Tutup jika *drawdown* dari *peak* terlalu besar |
---
## Alasan *Exit* (*ExitReason Enum*)
| Kode | Deskripsi |
|------|-----------|
| `TAKE_PROFIT` | Target profit tercapai atau profit diamankan |
| `TREND_REVERSAL` | ML mendeteksi pembalikan *trend* |
| `DAILY_LIMIT` | Batas kerugian harian tercapai |
| `POSITION_LIMIT` | Batas kerugian per posisi tercapai (S/L) |
| `TOTAL_LIMIT` | Batas kerugian total tercapai |
| `WEEKEND_CLOSE` | Mendekati penutupan *weekend* |
| `MANUAL` | Penutupan manual |
---
## Diagram *Flow* Evaluasi Posisi
```mermaid
graph LR
A["Setiap ~10 detik"] --> B["Hitung Profit/Loss<br/>Momentum, TP Prob"]
B --> C["SmartRiskManager<br/>evaluate_position()"]
C --> D{"Harus<br/>Tutup?"}
D -->|Ya| E["Tutup via MT5<br/>+ Log + Telegram"]
D -->|Tidak| F["SmartPositionManager<br/>Trailing SL, Breakeven"]
F --> G{"SL Perlu<br/>Digeser?"}
G -->|Ya| H["Modify SL<br/>via MT5"]
G -->|Tidak| I["Tahan Posisi ↩"]
```
---
## Statistik *Exit*
Berdasarkan data *backtest* (Jan 2025 - Feb 2026):
| Alasan *Exit* | Persentase | Rata-rata P/L |
|----------------|-----------|----------------|
| *Take Profit* (semua jenis) | **~40%** | **+$18.50** |
| *Trend Reversal* / *Early Cut* | **~25%** | **-$12.30** |
| *Time-Based Exit* | **~15%** | **+$3.20** |
| *Trailing SL* hit | **~10%** | **+$8.70** |
| Max Loss / *Daily Limit* | **~8%** | **-$22.50** |
| *Weekend Close* | **~2%** | **+$5.10** |