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
+234 -259
View File
@@ -1,346 +1,321 @@
# XAUBot AI — Feature Reference
# XAUBot AI — Referensi Fitur
## Overview
## Gambaran Umum
XAUBot AI is an automated XAUUSD (Gold) trading bot that combines **XGBoost Machine Learning**, **Smart Money Concepts (SMC)**, and **Hidden Markov Model (HMM)** regime detection. It operates on MetaTrader 5 via an asynchronous Python loop, executing trades on the M15 (15-minute) timeframe.
XAUBot AI adalah bot *trading* XAUUSD (Emas) otomatis yang menggabungkan **XGBoost *Machine Learning***, **Smart Money Concepts (SMC)**, dan **Hidden Markov Model (HMM)** untuk deteksi *regime*. Bot ini beroperasi di MetaTrader 5 melalui *loop* Python asinkron, mengeksekusi *trade* pada *timeframe* M15 (15 menit).
The bot follows a strict pipeline: data is fetched, features are engineered, market structure is analyzed, regime is classified, ML predictions are generated, and a series of 14 sequential filters determine whether a trade is executed. Once in a position, 12 exit conditions are monitored every 5-10 seconds.
Bot mengikuti *pipeline* yang ketat: data diambil, fitur direkayasa, struktur pasar dianalisis, *regime* diklasifikasikan, prediksi ML dihasilkan, dan serangkaian 14 *filter* berurutan menentukan apakah *trade* dieksekusi. Setelah posisi terbuka, 12 kondisi *exit* dipantau setiap 5-10 detik.
---
## Entry Filter Pipeline
## *Pipeline* 14 *Entry Filter*
There are **14 filters** that run in order during `_trading_iteration()`. A signal must pass **ALL** of them to execute a trade.
Terdapat **14 *filter*** yang berjalan secara berurutan selama `_trading_iteration()`. Sebuah sinyal harus melewati **SEMUA** *filter* untuk mengeksekusi *trade*.
### 1. Data Fetch
- Pulls **200 M15 bars** from MetaTrader 5.
- Data is converted to a **Polars DataFrame** (not Pandas).
### 1. Pengambilan Data
- Mengambil **200 *bar* M15** dari MetaTrader 5.
- Data dikonversi ke **Polars *DataFrame*** (bukan Pandas).
### 2. Feature Engineering
- Calculates **37 technical features** from the OHLCV data.
- Includes: RSI, ATR, MACD, Bollinger Bands, EMA (multiple periods), Stochastic, volume-based indicators, and more.
- All computations use Polars for performance.
### 2. Rekayasa Fitur (*Feature Engineering*)
- Menghitung **37 fitur teknikal** dari data OHLCV.
- Meliputi: *RSI*, *ATR*, *MACD*, *Bollinger Bands*, *EMA* (berbagai periode), *Stochastic*, indikator berbasis volume, dan lainnya.
- Semua komputasi menggunakan Polars untuk performa.
### 3. SMC Analysis
- Detects institutional **Smart Money Concepts** structures:
- **Order Blocks (OB)** — supply/demand zones from institutional activity.
- **Fair Value Gaps (FVG)** — imbalances in price action.
- **Break of Structure (BOS)** — continuation signals.
- **Change of Character (CHoCH)** — reversal signals.
### 3. Analisis *SMC*
- Mendeteksi struktur institusional *Smart Money Concepts*:
- ***Order Block* (OB)** — zona *supply/demand* dari aktivitas institusional.
- ***Fair Value Gap* (FVG)** — ketidakseimbangan dalam *price action*.
- ***Break of Structure* (BOS)** — sinyal kelanjutan tren.
- ***Change of Character* (CHoCH)** — sinyal pembalikan arah.
### 4. Regime Detection
- **HMM (Hidden Markov Model)** classifies the current market state:
- `TRENDING`directional movement, favorable for entries.
- `RANGING`sideways consolidation, reduced sizing.
- `HIGH_VOLATILITY` — erratic movement, caution required.
- `CRISIS`extreme conditions, trading blocked.
### 4. Deteksi *Regime*
- ***HMM* (*Hidden Markov Model*)** mengklasifikasikan kondisi pasar saat ini:
- `TRENDING`pergerakan searah, kondusif untuk *entry*.
- `RANGING`konsolidasi menyamping, ukuran posisi dikurangi.
- `HIGH_VOLATILITY`pergerakan tidak menentu, butuh kehati-hatian.
- `CRISIS`kondisi ekstrem, *trading* diblokir.
### 5. Flash Crash Guard
- Emergency protection: if price move exceeds a threshold percentage, **all positions are immediately closed**.
- Prevents catastrophic loss during sudden market dislocations.
### 5. Pelindung *Flash Crash*
- Proteksi darurat: jika pergerakan harga melebihi ambang persentase tertentu, **semua posisi langsung ditutup**.
- Mencegah kerugian katastropik saat dislokasi pasar mendadak.
### 6. Regime Filter
- Blocks trading entirely if the regime recommendation is `SLEEP`.
- Prevents entries during unfavorable market conditions identified by the HMM.
### 6. *Filter Regime*
- Memblokir *trading* sepenuhnya jika rekomendasi *regime* adalah `SLEEP`.
- Mencegah *entry* saat kondisi pasar tidak menguntungkan yang teridentifikasi oleh *HMM*.
### 7. Risk Check
- Blocks trading if:
- **Daily loss limit** has been reached (5% of capital).
- **Equity** is too low relative to required margin.
- **Total loss limit** has been breached (10% of capital).
### 7. Pemeriksaan Risiko
- Memblokir *trading* jika:
- **Batas kerugian harian** telah tercapai (5% dari kapital).
- ***Equity*** terlalu rendah relatif terhadap *margin* yang dibutuhkan.
- **Batas kerugian total** telah dilanggar (10% dari kapital).
### 8. Session Filter
- Filters based on **WIB (Western Indonesian Time)** trading sessions.
- Each session applies a **lot size multiplier** to control exposure:
- **Sydney** (06:00-13:00 WIB) — 0.5x multiplier (low volatility).
- **Tokyo** (07:00-16:00 WIB) — 0.7x multiplier (medium volatility).
- **London** (15:00-24:00 WIB) — 1.0x multiplier (high volatility).
- **New York** (20:00-24:00 WIB) — 1.0x multiplier (extreme volatility).
- **Off-Hours** (00:00-06:00 WIB) — **blocked entirely**.
### 8. *Filter* Sesi
- Memfilter berdasarkan sesi *trading* **WIB (Waktu Indonesia Barat)**.
- Setiap sesi menerapkan ***lot size multiplier*** untuk mengontrol eksposur:
- **Sydney** (06:00-13:00 WIB) — *multiplier* 0.5x (volatilitas rendah).
- **Tokyo** (07:00-16:00 WIB) — *multiplier* 0.7x (volatilitas sedang).
- **London** (15:00-24:00 WIB) — *multiplier* 1.0x (volatilitas tinggi).
- **New York** (20:00-24:00 WIB) — *multiplier* 1.0x (volatilitas ekstrem).
- ***Off-Hours*** (00:00-06:00 WIB) — **diblokir sepenuhnya**.
### 9. H1 Bias Filter (#31B)
- Multi-timeframe confirmation using **EMA20 on the H1 chart**.
- Price position relative to H1 EMA20 determines directional bias:
- **BULLISH** (price above EMA20) — only BUY signals allowed.
- **BEARISH** (price below EMA20) — only SELL signals allowed.
- **NEUTRAL** (price near EMA20) — **all signals blocked**.
- Backtest result: **+$343 improvement, 81.8% win rate, Sharpe 3.97**.
### 9. *Filter Bias* H1 (#31B)
- Konfirmasi *multi-timeframe* menggunakan ***EMA20* pada *chart* H1**.
- Posisi harga relatif terhadap *EMA20* H1 menentukan bias arah:
- **BULLISH** (harga di atas *EMA20*) — hanya sinyal *BUY* yang diizinkan.
- **BEARISH** (harga di bawah *EMA20*) — hanya sinyal *SELL* yang diizinkan.
- **NEUTRAL** (harga dekat *EMA20*) — **semua sinyal diblokir**.
- Hasil *backtest*: **+$343 peningkatan, *win rate* 81.8%, *Sharpe* 3.97**.
### 10. SMC Signal Generation
- Generates a **BUY or SELL signal** based on SMC structure analysis.
- Each signal includes a **confidence score** derived from the quality of the detected structures (OB proximity, FVG alignment, BOS/CHoCH context).
### 10. Generasi Sinyal *SMC*
- Menghasilkan sinyal ***BUY* atau *SELL*** berdasarkan analisis struktur *SMC*.
- Setiap sinyal memiliki ***confidence score*** yang berasal dari kualitas struktur yang terdeteksi (kedekatan *OB*, keselarasan *FVG*, konteks *BOS*/*CHoCH*).
### 11. Signal Combination
- Combines **SMC signal + ML (XGBoost) prediction**.
- Applies a **dynamic confidence threshold** that adapts based on:
- Current trading session.
- Market regime.
- Recent volatility.
- Both signals must agree on direction; combined confidence must exceed the threshold.
### 11. Kombinasi Sinyal
- Menggabungkan **sinyal *SMC* + prediksi *ML* (*XGBoost*)**.
- Menerapkan ***dynamic confidence threshold*** yang beradaptasi berdasarkan:
- Sesi *trading* saat ini.
- *Regime* pasar.
- Volatilitas terkini.
- Kedua sinyal harus sepakat arah; *confidence* gabungan harus melampaui *threshold*.
### 12. Time Filter (#34A)
- Skips specific WIB hours known for poor conditions:
- **Hour 9 WIB** — end of New York session, low liquidity.
- **Hour 21 WIB** — London-New York transition, prone to whipsaw.
- Backtest result: **+$356 improvement**.
### 12. *Filter* Waktu (#34A)
- Melewatkan jam WIB tertentu yang dikenal berkondisi buruk:
- **Jam 9 WIB** — akhir sesi *New York*, likuiditas rendah.
- **Jam 21 WIB** — transisi *London*-*New York*, rawan *whipsaw*.
- Hasil *backtest*: **+$356 peningkatan**.
### 13. Trade Cooldown
- Enforces a minimum **150 seconds (2.5 minutes)** between consecutive trades.
- Prevents overtrading and rapid-fire entries from noisy signals.
### 13. *Cooldown Trade*
- Memberlakukan jeda minimum **150 detik (2.5 menit)** antara *trade* berturut-turut.
- Mencegah *overtrading* dan *entry* bertubi-tubi dari sinyal yang noisy.
### 14. Smart Risk Gate
- Final gate before execution. Checks:
- **Trading mode**: `NORMAL`, `RECOVERY`, `PROTECTED`, or `STOPPED`.
- **Lot size calculation**: Based on ATR, capital mode, and session multiplier.
- **Position limit**: Maximum **2 concurrent positions** allowed.
- If mode is `STOPPED`, no trade is executed regardless of signal quality.
### 14. Gerbang Risiko Cerdas (*Smart Risk Gate*)
- Gerbang terakhir sebelum eksekusi. Memeriksa:
- **Mode *trading***: `NORMAL`, `RECOVERY`, `PROTECTED`, atau `STOPPED`.
- **Perhitungan *lot size***: Berdasarkan *ATR*, mode kapital, dan *multiplier* sesi.
- **Batas posisi**: Maksimal **2 posisi bersamaan** diizinkan.
- Jika mode `STOPPED`, tidak ada *trade* yang dieksekusi terlepas dari kualitas sinyal.
---
## Exit Conditions
## 12 Kondisi *Exit*
**12 exit conditions** are checked every **5-10 seconds** while a position is open.
**12 kondisi *exit*** diperiksa setiap **5-10 detik** selama posisi terbuka.
### 1. Take Profit (Broker-Level TP)
- TP is set at the broker level at entry time.
- Calculated using ATR-based risk-reward ratios.
### 1. *Take Profit* (TP Level Broker)
- *TP* dipasang di level broker saat *entry*.
- Dihitung menggunakan rasio *risk-reward* berbasis *ATR*.
### 2. Trailing Stop (#24B)
- **ATR-adaptive trailing stop**:
- Activation distance: **ATR x 4.0**.
- Step size: **ATR x 3.0**.
- Locks in profits as price moves favorably.
### 2. *Trailing Stop* (#24B)
- ***Trailing stop* adaptif berbasis *ATR***:
- Jarak aktivasi: ***ATR* x 4.0**.
- Ukuran langkah: ***ATR* x 3.0**.
- Mengunci keuntungan seiring harga bergerak menguntungkan.
### 3. Breakeven Move (#24B)
- Moves stop loss to **entry price** (breakeven) when unrealized profit exceeds **ATR x 2.0**.
- Eliminates risk on the trade after a favorable move.
### 3. Perpindahan *Breakeven* (#24B)
- Memindahkan *stop loss* ke **harga *entry*** (*breakeven*) saat keuntungan belum direalisasi melampaui ***ATR* x 2.0**.
- Menghilangkan risiko pada *trade* setelah pergerakan menguntungkan.
### 4. ML Reversal Exit
- Closes the position if the ML model's confidence **flips direction** with confidence exceeding **75%**.
- Responds to changing market conditions detected by XGBoost.
### 4. *Exit* Pembalikan *ML*
- Menutup posisi jika *confidence* model *ML* **berbalik arah** dengan *confidence* melebihi **75%**.
- Merespons perubahan kondisi pasar yang terdeteksi oleh *XGBoost*.
### 5. Max Loss Per Trade
- **Software-level stop loss** at **1% of capital**.
- Acts as a safety net in addition to broker SL.
### 5. Kerugian Maksimal Per *Trade*
- ***Stop loss* level perangkat lunak** sebesar **1% dari kapital**.
- Berfungsi sebagai jaring pengaman di samping *SL* broker.
### 6. Daily Loss Limit
- If cumulative daily loss reaches **5% of capital**, **all positions are closed** and trading halts for the day.
### 6. Batas Kerugian Harian
- Jika kerugian kumulatif harian mencapai **5% dari kapital**, **semua posisi ditutup** dan *trading* dihentikan untuk hari itu.
### 7. Total Loss Limit
- If cumulative total loss reaches **10% of capital**, **trading is stopped entirely** until manual intervention.
### 7. Batas Kerugian Total
- Jika kerugian kumulatif total mencapai **10% dari kapital**, ***trading* dihentikan sepenuhnya** sampai intervensi manual.
### 8. Market Close Handler
- Before daily close or weekend close:
- Takes profit on positions with unrealized profit **> $5**.
- Prevents gap risk from overnight/weekend holds.
### 8. Penanganan Penutupan Pasar
- Sebelum penutupan harian atau penutupan akhir pekan:
- Mengambil keuntungan pada posisi dengan *unrealized profit* **> $5**.
- Mencegah risiko *gap* dari posisi yang terbawa semalam/akhir pekan.
### 9. Flash Crash Emergency
- Triggered by sudden extreme price movement.
- **Immediately closes all open positions** without delay.
### 9. Darurat *Flash Crash*
- Dipicu oleh pergerakan harga ekstrem secara tiba-tiba.
- **Langsung menutup semua posisi terbuka** tanpa penundaan.
### 10. Drawdown Protection
- Monitors drawdown from equity peak.
- Closes all positions if drawdown exceeds **50%** from the peak.
### 10. Proteksi *Drawdown*
- Memantau *drawdown* dari puncak *equity*.
- Menutup semua posisi jika *drawdown* melebihi **50%** dari puncak.
### 11. Impulse Trail (#33B)
- Enhanced trailing stop using **impulse candle detection**.
- Identifies strong momentum candles and trails the stop behind them.
- More responsive than standard ATR trailing in trending conditions.
### 11. *Impulse Trail* (#33B)
- *Trailing stop* yang ditingkatkan menggunakan **deteksi *impulse candle***.
- Mengidentifikasi *candle* momentum kuat dan men-*trail* *stop* di belakangnya.
- Lebih responsif dibanding *trailing ATR* standar dalam kondisi tren.
### 12. Smart Breakeven (#28B)
- Enhanced breakeven logic with **ATR multiplier triggers**:
- Trigger: profit exceeds **ATR x 2.0**.
- Moves SL to entry + small buffer.
- More adaptive than fixed-pip breakeven.
### 12. *Smart Breakeven* (#28B)
- Logika *breakeven* yang ditingkatkan dengan **pemicu *ATR multiplier***:
- Pemicu: keuntungan melampaui ***ATR* x 2.0**.
- Memindahkan *SL* ke *entry* + *buffer* kecil.
- Lebih adaptif dibanding *breakeven* berbasis pip tetap.
---
## Backtest Optimization History
## Riwayat Optimasi *Backtest*
Summary of key optimizations applied to the live bot, tested and validated through backtesting.
Rangkuman optimasi utama yang diterapkan ke bot *live*, diuji dan divalidasi melalui *backtest*.
| # | Name | Key Change | Result |
|---|------|------------|--------|
| #24B | ATR-Adaptive Exit | ATR-based trailing (4.0x) and breakeven (2.0x) multipliers | Base optimization for exit logic |
| #28B | Smart Breakeven | Enhanced breakeven with ATR x 2.0 trigger | Improved exit timing on winning trades |
| #31B | H1 EMA20 Filter | H1 price vs EMA20 multi-timeframe filter | +$343, WR 81.8%, Sharpe 3.97 |
| #33B | Impulse Trail | Trail using impulse candle detection | Better trailing in trending markets |
| #34A | Skip Hours | Skip WIB hours 9 and 21 | +$356, reduced whipsaw losses |
| # | Nama | Perubahan Utama | Hasil |
|---|------|-----------------|-------|
| #24B | *ATR-Adaptive Exit* | *Trailing* berbasis *ATR* (4.0x) dan *breakeven* (2.0x) *multiplier* | Optimasi dasar untuk logika *exit* |
| #28B | *Smart Breakeven* | *Breakeven* yang ditingkatkan dengan pemicu *ATR* x 2.0 | Peningkatan waktu *exit* pada *trade* yang menang |
| #31B | *Filter* H1 *EMA20* | *Filter multi-timeframe* harga H1 vs *EMA20* | +$343, WR 81.8%, *Sharpe* 3.97 |
| #33B | *Impulse Trail* | *Trail* menggunakan deteksi *impulse candle* | *Trailing* lebih baik di pasar tren |
| #34A | Lewati Jam Tertentu | Lewati jam WIB 9 dan 21 | +$356, pengurangan kerugian *whipsaw* |
---
## Risk Management
## Manajemen Risiko
### Capital Modes
### Mode Kapital
Capital modes are auto-configured based on account balance. Each mode sets risk parameters appropriate for the account size.
Mode kapital dikonfigurasi otomatis berdasarkan saldo akun. Setiap mode mengatur parameter risiko yang sesuai untuk ukuran akun.
| Mode | Capital Range | Risk/Trade | Max Lot |
|------|--------------|------------|---------|
| Mode | Rentang Kapital | Risiko/*Trade* | *Lot* Maks |
|------|----------------|----------------|------------|
| MICRO | < $500 | 2% | 0.02 |
| SMALL | $500 - $10,000 | 1.5% | 0.05 |
| MEDIUM | $10,000 - $100,000 | 0.5% | 0.10 |
| LARGE | > $100,000 | 0.25% | 0.50 |
### Trading Modes
### Mode *Trading*
The Smart Risk Manager dynamically adjusts the trading mode based on recent performance.
*Smart Risk Manager* secara dinamis menyesuaikan mode *trading* berdasarkan performa terkini.
| Mode | Trigger | Lot Adjustment |
|------|---------|---------------|
| NORMAL | Default state | Base lot (0.01-0.03) |
| RECOVERY | After a losing trade | Recovery lot (0.01) |
| PROTECTED | Approaching daily loss limit | Minimum lot (0.01) |
| STOPPED | Daily or total loss limit hit | No trading allowed |
| Mode | Pemicu | Penyesuaian *Lot* |
|------|--------|-------------------|
| NORMAL | Kondisi *default* | *Lot* dasar (0.01-0.03) |
| RECOVERY | Setelah *trade* rugi | *Lot* pemulihan (0.01) |
| PROTECTED | Mendekati batas kerugian harian | *Lot* minimum (0.01) |
| STOPPED | Batas kerugian harian atau total tercapai | *Trading* tidak diizinkan |
### Risk Limits
### Batas Risiko
| Limit | Value | Action |
|-------|-------|--------|
| Max daily loss | 5% of capital | Close all positions, halt trading for the day |
| Max total loss | 10% of capital | Stop all trading until manual reset |
| Max loss per trade | 1% of capital | Software stop loss |
| Emergency broker SL | 2% of capital | Broker-level hard stop |
| Max concurrent positions | 2 | Reject new entries if at limit |
| Batas | Nilai | Aksi |
|-------|-------|------|
| Kerugian harian maks | 5% dari kapital | Tutup semua posisi, hentikan *trading* untuk hari itu |
| Kerugian total maks | 10% dari kapital | Hentikan semua *trading* sampai *reset* manual |
| Kerugian maks per *trade* | 1% dari kapital | *Stop loss* perangkat lunak |
| *SL* darurat broker | 2% dari kapital | *Hard stop* level broker |
| Posisi bersamaan maks | 2 | Tolak *entry* baru jika sudah di batas |
---
## Session Filter (WIB)
## *Filter* Sesi (WIB)
All session times are in **WIB (Western Indonesian Time, UTC+7)**.
Semua waktu sesi dalam **WIB (Waktu Indonesia Barat, UTC+7)**.
| Session | Hours (WIB) | Volatility | Lot Multiplier |
|---------|-------------|------------|----------------|
| Sydney | 06:00 - 13:00 | Low | 0.5x |
| Tokyo | 07:00 - 16:00 | Medium | 0.7x |
| London | 15:00 - 24:00 | High | 1.0x |
| New York | 20:00 - 24:00 | Extreme | 1.0x |
| Off-Hours | 00:00 - 06:00 | N/A | **Blocked** |
| Sesi | Jam (WIB) | Volatilitas | *Multiplier Lot* |
|------|-----------|-------------|-------------------|
| Sydney | 06:00 - 13:00 | Rendah | 0.5x |
| Tokyo | 07:00 - 16:00 | Sedang | 0.7x |
| London | 15:00 - 24:00 | Tinggi | 1.0x |
| New York | 20:00 - 24:00 | Ekstrem | 1.0x |
| *Off-Hours* | 00:00 - 06:00 | N/A | **Diblokir** |
### Golden Hour
- **19:00 - 23:00 WIB** (London-New York Overlap).
- Highest liquidity and volatility period for XAUUSD.
- Best trading conditions; full lot multiplier applied.
### *Golden Hour*
- **19:00 - 23:00 WIB** (*London*-*New York Overlap*).
- Periode likuiditas dan volatilitas tertinggi untuk XAUUSD.
- Kondisi *trading* terbaik; *multiplier lot* penuh diterapkan.
### Skip Hours (#34A)
- **Hour 9 WIB** — End of New York session; low liquidity leads to erratic fills.
- **Hour 21 WIB** — London-New York transition; prone to whipsaw and false breakouts.
### Jam yang Dilewati (#34A)
- **Jam 9 WIB** — Akhir sesi *New York*; likuiditas rendah menyebabkan *fill* yang tidak menentu.
- **Jam 21 WIB** — Transisi *London*-*New York*; rawan *whipsaw* dan *false breakout*.
---
## Auto-Trainer
## *Auto-Trainer*
The bot includes an automatic model retraining pipeline to keep the ML model current with market conditions.
Bot menyertakan *pipeline* pelatihan ulang model otomatis untuk menjaga model *ML* tetap mutakhir dengan kondisi pasar.
| Parameter | Value |
| Parameter | Nilai |
|-----------|-------|
| Check interval | Every 20 candles (~5 hours on M15) |
| Daily retrain | 05:00 WIB (during market close) |
| Weekend training | Deep training with expanded data window |
| Min AUC threshold | 0.65 |
| Rollback policy | If new model performs worse, revert to backup |
| Interval pemeriksaan | Setiap 20 *candle* (~5 jam pada M15) |
| Pelatihan ulang harian | 05:00 WIB (saat pasar tutup) |
| Pelatihan akhir pekan | Pelatihan mendalam dengan jendela data yang diperluas |
| *Threshold AUC* minimum | 0.65 |
| Kebijakan *rollback* | Jika model baru berkinerja lebih buruk, kembali ke *backup* |
### Retraining Flow
1. Every 20 candles, the auto-trainer checks model performance metrics.
2. If AUC drops below **0.65**, a retrain is triggered.
3. At **05:00 WIB daily** (market close), a scheduled retrain runs.
4. On **weekends**, deep training uses a larger historical dataset.
5. After training, the new model is validated against the previous one.
6. If the new model underperforms, the system **rolls back** to the backup model.
### Alur Pelatihan Ulang
1. Setiap 20 *candle*, *auto-trainer* memeriksa metrik performa model.
2. Jika *AUC* turun di bawah **0.65**, pelatihan ulang dipicu.
3. Pada **05:00 WIB setiap hari** (pasar tutup), pelatihan ulang terjadwal berjalan.
4. Pada **akhir pekan**, pelatihan mendalam menggunakan *dataset* historis yang lebih besar.
5. Setelah pelatihan, model baru divalidasi terhadap model sebelumnya.
6. Jika model baru berkinerja lebih buruk, sistem **melakukan *rollback*** ke model *backup*.
---
## ML Model
## Model *ML*
### Algorithm
- **XGBoost** gradient-boosted decision trees.
### Algoritma
- ***XGBoost* *gradient-boosted decision trees***.
### Features
- **37 technical indicators** computed by `src/feature_eng.py`:
- Trend: EMA (multiple periods), MACD, ADX.
- Momentum: RSI, Stochastic K/D.
- Volatility: ATR, Bollinger Bands (width, %B).
- Volume: Volume-weighted indicators.
- Custom: SMC-derived features, regime features.
### Fitur
- **37 indikator teknikal** dihitung oleh `src/feature_eng.py`:
- Tren: *EMA* (berbagai periode), *MACD*, *ADX*.
- Momentum: *RSI*, *Stochastic K/D*.
- Volatilitas: *ATR*, *Bollinger Bands* (*width*, *%B*).
- Volume: Indikator berbasis volume.
- Kustom: Fitur turunan *SMC*, fitur *regime*.
### Output
- **Signal**: BUY, SELL, or HOLD.
- **Confidence score**: 0.0 to 1.0, used in combination with SMC confidence.
### Keluaran
- **Sinyal**: *BUY*, *SELL*, atau *HOLD*.
- ***Confidence score***: 0.0 hingga 1.0, digunakan dalam kombinasi dengan *confidence SMC*.
### Dynamic Threshold
- The confidence threshold for trade execution is not fixed.
- It adjusts based on:
- **Session**: Higher threshold during low-volatility sessions.
- **Regime**: Higher threshold during ranging/volatile regimes.
- **Recent performance**: Tightens after losses, relaxes after wins.
### *Threshold* Dinamis
- *Threshold confidence* untuk eksekusi *trade* tidak tetap.
- Menyesuaikan berdasarkan:
- **Sesi**: *Threshold* lebih tinggi saat sesi volatilitas rendah.
- ***Regime***: *Threshold* lebih tinggi saat *regime ranging*/*volatile*.
- **Performa terkini**: Diperketat setelah kerugian, dilonggarkan setelah kemenangan.
---
## Active Components
## Komponen Aktif
| Component | File | Status | Description |
|-----------|------|--------|-------------|
| SMC Analyzer | `src/smc_polars.py` | Active | Order Block, FVG, BOS, CHoCH detection |
| XGBoost ML | `src/ml_model.py` | Active | Signal prediction with confidence |
| HMM Regime | `src/regime_detector.py` | Active | Market regime classification |
| Feature Engine | `src/feature_eng.py` | Active | 37 technical feature computation |
| Risk Engine | `src/risk_engine.py` | Active | ATR-based SL/TP, position sizing |
| Smart Risk Manager | `src/smart_risk_manager.py` | Active | Dynamic mode management |
| Position Manager | `src/position_manager.py` | Active | Exit condition monitoring |
| Session Filter | `src/session_filter.py` | Active | WIB session-based filtering |
| Dynamic Confidence | `src/dynamic_confidence.py` | Active | Adaptive threshold adjustment |
| Auto Trainer | `src/auto_trainer.py` | Active | Scheduled model retraining |
| Telegram Notifier | `src/telegram_notifier.py` | Active | Trade alerts via Telegram |
| Trade Logger | `src/trade_logger.py` | Active | PostgreSQL trade logging |
| News Agent | `src/news_agent.py` | **DISABLED** | Economic news filter (costs $178 profit in backtest) |
| Flash Crash Detector | `src/regime_detector.py` | Active | Emergency position closure |
| Komponen | File | Status | Deskripsi |
|----------|------|--------|-----------|
| Penganalisis *SMC* | `src/smc_polars.py` | Aktif | Deteksi *Order Block*, *FVG*, *BOS*, *CHoCH* |
| *ML XGBoost* | `src/ml_model.py` | Aktif | Prediksi sinyal dengan *confidence* |
| *Regime HMM* | `src/regime_detector.py` | Aktif | Klasifikasi *regime* pasar |
| Mesin Fitur | `src/feature_eng.py` | Aktif | Komputasi 37 fitur teknikal |
| Mesin Risiko | `src/risk_engine.py` | Aktif | *SL*/*TP* berbasis *ATR*, *position sizing* |
| *Smart Risk Manager* | `src/smart_risk_manager.py` | Aktif | Manajemen mode dinamis |
| Manajer Posisi | `src/position_manager.py` | Aktif | Pemantauan kondisi *exit* |
| *Filter* Sesi | `src/session_filter.py` | Aktif | *Filtering* berbasis sesi WIB |
| *Confidence* Dinamis | `src/dynamic_confidence.py` | Aktif | Penyesuaian *threshold* adaptif |
| *Auto Trainer* | `src/auto_trainer.py` | Aktif | Pelatihan ulang model terjadwal |
| Notifikasi Telegram | `src/telegram_notifier.py` | Aktif | Peringatan *trade* via Telegram |
| Pencatat *Trade* | `src/trade_logger.py` | Aktif | Pencatatan *trade* ke PostgreSQL |
| Agen Berita | `src/news_agent.py` | **NONAKTIF** | *Filter* berita ekonomi (mengurangi $178 profit di *backtest*) |
| Detektor *Flash Crash* | `src/regime_detector.py` | Aktif | Penutupan posisi darurat |
---
## Architecture Diagram
## Diagram Arsitektur
```
MT5 Broker
|
v
[Data Fetch] --> [Feature Eng (37)] --> [SMC Analysis] --> [Regime Detection (HMM)]
|
v
[Flash Crash Guard]
|
v
[Regime Filter]
|
v
[Risk Check]
|
v
[Session Filter]
|
v
[H1 Bias Filter (#31B)]
|
v
[SMC Signal Gen]
|
v
[Signal Combination (ML+SMC)]
|
v
[Time Filter (#34A)]
|
v
[Trade Cooldown]
|
v
[Smart Risk Gate]
|
v
[TRADE EXECUTION]
|
v
[Position Manager (12 exits)]
|
v
[Telegram + PostgreSQL Logging]
```mermaid
flowchart TD
MT5["MT5 Broker"] --> DF["Data Fetch"]
DF --> FE["Feature Eng (37 fitur)"]
FE --> SMC["SMC Analysis"]
SMC --> HMM["Regime Detection (HMM)"]
HMM --> FCG["Flash Crash Guard"]
FCG --> RF["Regime Filter"]
RF --> RC["Risk Check"]
RC --> SF["Session Filter"]
SF --> H1["H1 Bias Filter (#31B)"]
H1 --> SG["SMC Signal Gen"]
SG --> SC["Signal Combination (ML+SMC)"]
SC --> TF["Time Filter (#34A)"]
TF --> TC["Trade Cooldown"]
TC --> SRG["Smart Risk Gate"]
SRG --> TE["TRADE EXECUTION"]
TE --> PM["Position Manager (12 exits)"]
PM --> LOG["Telegram + PostgreSQL Logging"]
```