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:
co-authored by
Claude Opus 4.6
parent
b2dc2dacd7
commit
e8355b3f62
@@ -1,6 +1,6 @@
|
||||
# XAUBot AI
|
||||
|
||||
**AI-powered XAUUSD (Gold) trading bot** with XGBoost ML, Smart Money Concepts (SMC), and HMM regime detection for MetaTrader 5.
|
||||
**Bot trading XAUUSD (Emas) berbasis AI** dengan *XGBoost ML*, *Smart Money Concepts* (SMC), dan deteksi *regime* menggunakan *Hidden Markov Model* untuk *MetaTrader 5*.
|
||||
|
||||
[](https://www.python.org/downloads/)
|
||||
[](LICENSE)
|
||||
@@ -8,175 +8,152 @@
|
||||
|
||||
---
|
||||
|
||||
## Features
|
||||
## Fitur
|
||||
|
||||
| Feature | Description |
|
||||
|---------|-------------|
|
||||
| **XGBoost ML Model** | 37-feature model predicting BUY/SELL/HOLD with calibrated confidence |
|
||||
| **Smart Money Concepts** | Order Blocks, Fair Value Gaps, Break of Structure, Change of Character |
|
||||
| **HMM Regime Detection** | 3-state Hidden Markov Model classifying trending/ranging/volatile markets |
|
||||
| **Dynamic Risk Management** | ATR-based stop loss, Kelly criterion sizing, daily loss limits |
|
||||
| **Session-Aware Trading** | Optimized for Sydney, London, and New York sessions |
|
||||
| **Auto-Retraining** | Models automatically retrain when market conditions shift |
|
||||
| **Telegram Alerts** | Real-time trade notifications and daily summaries |
|
||||
| **Web Dashboard** | Next.js monitoring interface for live tracking |
|
||||
| Fitur | Deskripsi |
|
||||
|-------|-----------|
|
||||
| **Model *XGBoost ML*** | Model 37-fitur yang memprediksi BUY/SELL/HOLD dengan *confidence* terkalibrasi |
|
||||
| ***Smart Money Concepts*** | *Order Block*, *Fair Value Gap*, *Break of Structure*, *Change of Character* |
|
||||
| **Deteksi *Regime* HMM** | *Hidden Markov Model* 3-state yang mengklasifikasikan pasar *trending*/*ranging*/*volatile* |
|
||||
| **Manajemen Risiko Dinamis** | *Stop Loss* berbasis ATR, *position sizing* dengan *Kelly criterion*, batas kerugian harian |
|
||||
| **Kesadaran Sesi** | Dioptimalkan untuk sesi Sydney, London, dan New York |
|
||||
| **Pelatihan Ulang Otomatis** | Model secara otomatis dilatih ulang saat kondisi pasar berubah |
|
||||
| **Notifikasi Telegram** | Pemberitahuan *trade* secara *real-time* dan ringkasan harian |
|
||||
| ***Dashboard* Web** | Antarmuka pemantauan *Next.js* untuk pelacakan *live* |
|
||||
|
||||
## Architecture
|
||||
## Arsitektur
|
||||
|
||||
```
|
||||
┌─────────────────┐
|
||||
│ MetaTrader 5 │
|
||||
│ (XAUUSD M15) │
|
||||
└────────┬─────────┘
|
||||
│ OHLCV
|
||||
┌────────▼─────────┐
|
||||
│ Data Pipeline │
|
||||
│ (Polars Engine) │
|
||||
└────────┬─────────┘
|
||||
│
|
||||
┌─────────────────┼─────────────────┐
|
||||
│ │ │
|
||||
┌────────▼───────┐ ┌──────▼───────┐ ┌───────▼──────┐
|
||||
│ SMC Analyzer │ │ Feature Eng │ │ HMM Regime │
|
||||
│ (OB/FVG/BOS) │ │ (37 features) │ │ Detector │
|
||||
└────────┬───────┘ └──────┬───────┘ └───────┬──────┘
|
||||
│ │ │
|
||||
└─────────────────┼─────────────────┘
|
||||
│
|
||||
┌────────▼─────────┐
|
||||
│ XGBoost Model │
|
||||
│ (Signal + Conf) │
|
||||
└────────┬─────────┘
|
||||
│
|
||||
┌─────────────────┼─────────────────┐
|
||||
│ │ │
|
||||
┌────────▼───────┐ ┌──────▼───────┐ ┌───────▼──────┐
|
||||
│ 11 Entry │ │ Risk Engine │ │ Position │
|
||||
│ Filters │ │ (ATR + Kelly)│ │ Manager │
|
||||
└────────┬───────┘ └──────┬───────┘ └───────┬──────┘
|
||||
│ │ │
|
||||
└────────────────┼──────────────────┘
|
||||
│
|
||||
┌────────▼─────────┐
|
||||
│ Trade Execution │
|
||||
│ (MT5 + Logging) │
|
||||
└───────────────────┘
|
||||
```mermaid
|
||||
graph TD
|
||||
MT5["MetaTrader 5<br/>(XAUUSD M15)"] -->|OHLCV| DP["Data Pipeline<br/>(Polars Engine)"]
|
||||
DP --> SMC["SMC Analyzer<br/>(OB / FVG / BOS)"]
|
||||
DP --> FE["Feature Engineering<br/>(37 fitur)"]
|
||||
DP --> HMM["HMM Regime<br/>Detector"]
|
||||
SMC --> XGB["XGBoost Model<br/>(Signal + Confidence)"]
|
||||
FE --> XGB
|
||||
HMM --> XGB
|
||||
XGB --> EF["14 Entry<br/>Filters"]
|
||||
XGB --> RE["Risk Engine<br/>(ATR + Kelly)"]
|
||||
XGB --> PM["Position<br/>Manager"]
|
||||
EF --> TE["Trade Execution<br/>(MT5 + Logging)"]
|
||||
RE --> TE
|
||||
PM --> TE
|
||||
```
|
||||
|
||||
## Project Structure
|
||||
## Struktur Proyek
|
||||
|
||||
```
|
||||
xaubot-ai/
|
||||
├── main_live.py # Main async trading orchestrator
|
||||
├── train_models.py # Model training script
|
||||
├── src/ # Core modules
|
||||
│ ├── config.py # Trading configuration & capital modes
|
||||
│ ├── mt5_connector.py # MetaTrader 5 connection layer
|
||||
│ ├── smc_polars.py # Smart Money Concepts analyzer
|
||||
│ ├── ml_model.py # XGBoost trading model
|
||||
│ ├── feature_eng.py # Feature engineering (37 features)
|
||||
│ ├── regime_detector.py # HMM market regime detection
|
||||
│ ├── risk_engine.py # Risk calculations & validation
|
||||
│ ├── smart_risk_manager.py # Dynamic risk management
|
||||
│ ├── session_filter.py # Session filter (Sydney/London/NY)
|
||||
│ ├── position_manager.py # Open position management
|
||||
│ ├── dynamic_confidence.py # Adaptive confidence thresholds
|
||||
│ ├── auto_trainer.py # Auto-retraining pipeline
|
||||
│ ├── news_agent.py # Economic news filtering
|
||||
│ ├── telegram_notifier.py # Telegram alerts
|
||||
│ ├── trade_logger.py # Trade logging to DB
|
||||
│ └── utils.py # Utility functions
|
||||
├── main_live.py # Orkestrator trading async utama
|
||||
├── train_models.py # Skrip pelatihan model
|
||||
├── src/ # Modul inti
|
||||
│ ├── config.py # Konfigurasi trading & mode kapital
|
||||
│ ├── mt5_connector.py # Layer koneksi MetaTrader 5
|
||||
│ ├── smc_polars.py # Penganalisis Smart Money Concepts
|
||||
│ ├── ml_model.py # Model trading XGBoost
|
||||
│ ├── feature_eng.py # Feature engineering (37 fitur)
|
||||
│ ├── regime_detector.py # Deteksi regime pasar HMM
|
||||
│ ├── risk_engine.py # Kalkulasi & validasi risiko
|
||||
│ ├── smart_risk_manager.py # Manajemen risiko dinamis
|
||||
│ ├── session_filter.py # Filter sesi (Sydney/London/NY)
|
||||
│ ├── position_manager.py # Manajemen posisi terbuka
|
||||
│ ├── dynamic_confidence.py # Threshold confidence adaptif
|
||||
│ ├── auto_trainer.py # Pipeline pelatihan ulang otomatis
|
||||
│ ├── news_agent.py # Filter berita ekonomi
|
||||
│ ├── telegram_notifier.py # Notifikasi Telegram
|
||||
│ ├── trade_logger.py # Pencatatan trade ke DB
|
||||
│ └── utils.py # Fungsi utilitas
|
||||
├── backtests/ # Backtesting
|
||||
│ ├── backtest_live_sync.py # Main backtest (synced with live)
|
||||
│ └── archive/ # Historical versions
|
||||
├── scripts/ # Utility scripts
|
||||
│ ├── check_market.py # Quick SMC market analysis
|
||||
│ ├── check_positions.py # View open positions
|
||||
│ ├── check_status.py # Account status check
|
||||
│ ├── close_positions.py # Emergency close all
|
||||
│ ├── modify_tp.py # Modify take-profit levels
|
||||
│ └── get_trade_history.py # Pull trade history
|
||||
├── tests/ # Tests
|
||||
├── models/ # Trained models (.pkl)
|
||||
├── data/ # Market data & trade logs
|
||||
├── docs/ # Documentation
|
||||
│ ├── arsitektur-ai/ # Architecture docs (23 components)
|
||||
│ └── research/ # Research & analysis
|
||||
├── web-dashboard/ # Next.js monitoring dashboard
|
||||
├── docker/ # Docker configuration & scripts
|
||||
│ ├── scripts/ # Helper scripts (.bat/.sh)
|
||||
│ └── docs/ # Docker documentation
|
||||
└── archive/ # Deprecated files (gitignored)
|
||||
│ ├── backtest_live_sync.py # Backtest utama (sinkron dengan live)
|
||||
│ └── archive/ # Versi historis
|
||||
├── scripts/ # Skrip utilitas
|
||||
│ ├── check_market.py # Analisis cepat pasar SMC
|
||||
│ ├── check_positions.py # Lihat posisi terbuka
|
||||
│ ├── check_status.py # Cek status akun
|
||||
│ ├── close_positions.py # Tutup semua posisi darurat
|
||||
│ ├── modify_tp.py # Modifikasi level take-profit
|
||||
│ └── get_trade_history.py # Tarik riwayat trade
|
||||
├── tests/ # Pengujian
|
||||
├── models/ # Model terlatih (.pkl)
|
||||
├── data/ # Data pasar & catatan trade
|
||||
├── docs/ # Dokumentasi
|
||||
│ ├── arsitektur-ai/ # Dokumen arsitektur (23 komponen)
|
||||
│ └── research/ # Riset & analisis
|
||||
├── web-dashboard/ # Dashboard pemantauan Next.js
|
||||
├── docker/ # Konfigurasi & skrip Docker
|
||||
│ ├── scripts/ # Skrip pembantu (.bat/.sh)
|
||||
│ └── docs/ # Dokumentasi Docker
|
||||
└── archive/ # File usang (gitignored)
|
||||
```
|
||||
|
||||
## Backtest Results (Jan 2025 - Feb 2026)
|
||||
## Hasil *Backtest* (Jan 2025 - Feb 2026)
|
||||
|
||||
| Metric | Value |
|
||||
| Metrik | Nilai |
|
||||
|--------|-------|
|
||||
| Total Trades | 654 |
|
||||
| Win Rate | 63.9% |
|
||||
| Net P/L | $4,189.52 |
|
||||
| Profit Factor | 2.64 |
|
||||
| Max Drawdown | 2.2% |
|
||||
| Sharpe Ratio | 4.83 |
|
||||
| Total *Trade* | 654 |
|
||||
| *Win Rate* | 63.9% |
|
||||
| *Net P/L* | $4,189.52 |
|
||||
| *Profit Factor* | 2.64 |
|
||||
| *Max Drawdown* | 2.2% |
|
||||
| *Sharpe Ratio* | 4.83 |
|
||||
|
||||
## Installation
|
||||
## Instalasi
|
||||
|
||||
### 🐳 Docker Deployment (Recommended)
|
||||
### Deployment *Docker* (Direkomendasikan)
|
||||
|
||||
**Quick Start:**
|
||||
**Mulai Cepat:**
|
||||
|
||||
```bash
|
||||
# 1. Clone the repository
|
||||
# 1. Clone repositori
|
||||
git clone https://github.com/GifariKemal/xaubot-ai.git
|
||||
cd xaubot-ai
|
||||
|
||||
# 2. Configure environment
|
||||
# 2. Konfigurasi environment
|
||||
cp docker/.env.docker.example .env
|
||||
# Edit .env with your MT5 credentials
|
||||
# Edit .env dengan kredensial MT5 Anda
|
||||
|
||||
# 3. Start all services (Windows)
|
||||
# 3. Jalankan semua layanan (Windows)
|
||||
docker\scripts\docker-start.bat
|
||||
|
||||
# 3. Start all services (Linux/Mac)
|
||||
# 3. Jalankan semua layanan (Linux/Mac)
|
||||
./docker/scripts/docker-start.sh
|
||||
```
|
||||
|
||||
**Services will be available at:**
|
||||
- 📊 Dashboard: http://localhost:3000
|
||||
- 🔌 API: http://localhost:8000
|
||||
- 📚 API Docs: http://localhost:8000/docs
|
||||
- 🗄️ Database: localhost:5432
|
||||
**Layanan yang tersedia:**
|
||||
- *Dashboard*: http://localhost:3000
|
||||
- API: http://localhost:8000
|
||||
- Dokumentasi API: http://localhost:8000/docs
|
||||
- *Database*: localhost:5432
|
||||
|
||||
**Full Docker documentation:** See [docker/docs/DOCKER.md](docker/docs/DOCKER.md)
|
||||
**Dokumentasi *Docker* lengkap:** Lihat [docker/docs/DOCKER.md](docker/docs/DOCKER.md)
|
||||
|
||||
---
|
||||
|
||||
### 🐍 Manual Installation
|
||||
### Instalasi Manual
|
||||
|
||||
**Prerequisites:**
|
||||
**Prasyarat:**
|
||||
- Python 3.11+
|
||||
- MetaTrader 5 terminal (Windows)
|
||||
- PostgreSQL (optional, for trade logging)
|
||||
- Terminal *MetaTrader 5* (Windows)
|
||||
- PostgreSQL (opsional, untuk pencatatan *trade*)
|
||||
|
||||
**Setup:**
|
||||
**Persiapan:**
|
||||
|
||||
```bash
|
||||
# Clone the repository
|
||||
# Clone repositori
|
||||
git clone https://github.com/GifariKemal/xaubot-ai.git
|
||||
cd xaubot-ai
|
||||
|
||||
# Install dependencies
|
||||
# Instal dependensi
|
||||
pip install -r requirements.txt
|
||||
|
||||
# Configure environment
|
||||
# Konfigurasi environment
|
||||
cp .env.example .env
|
||||
# Edit .env with your MT5 credentials and Telegram token
|
||||
# Edit .env dengan kredensial MT5 dan token Telegram Anda
|
||||
```
|
||||
|
||||
### Configuration
|
||||
### Konfigurasi
|
||||
|
||||
Key settings in `.env`:
|
||||
Pengaturan utama di `.env`:
|
||||
|
||||
```env
|
||||
# MetaTrader 5
|
||||
@@ -185,7 +162,7 @@ MT5_PASSWORD=your_password
|
||||
MT5_SERVER=your_server
|
||||
MT5_PATH=C:/Program Files/MetaTrader 5/terminal64.exe
|
||||
|
||||
# Telegram Notifications
|
||||
# Notifikasi Telegram
|
||||
TELEGRAM_BOT_TOKEN=your_bot_token
|
||||
TELEGRAM_CHAT_ID=your_chat_id
|
||||
|
||||
@@ -194,49 +171,49 @@ CAPITAL=5000
|
||||
SYMBOL=XAUUSD
|
||||
```
|
||||
|
||||
### Run
|
||||
### Menjalankan
|
||||
|
||||
```bash
|
||||
# Train models first
|
||||
# Latih model terlebih dahulu
|
||||
python train_models.py
|
||||
|
||||
# Start the bot
|
||||
# Jalankan bot
|
||||
python main_live.py
|
||||
|
||||
# Run backtest
|
||||
# Jalankan backtest
|
||||
python backtests/backtest_live_sync.py --tune
|
||||
```
|
||||
|
||||
## Risk Management
|
||||
## Manajemen Risiko
|
||||
|
||||
| Protection | Details |
|
||||
|-----------|---------|
|
||||
| **ATR-Based Stop Loss** | Minimum 1.5x ATR distance |
|
||||
| **Broker-Level SL** | Emergency SL set at broker level |
|
||||
| **Position Sizing** | Kelly criterion with capital mode scaling |
|
||||
| **Daily Loss Limit** | 5% of capital per day |
|
||||
| **Total Loss Limit** | 10% of capital |
|
||||
| **Position Limit** | Max 2 concurrent positions |
|
||||
| **Time-Based Exit** | Max 6 hours per trade |
|
||||
| **Session Filter** | Only trades during active sessions |
|
||||
| **Spread Filter** | Rejects trades during high spread |
|
||||
| **Cooldown** | Minimum time between trades |
|
||||
| Proteksi | Detail |
|
||||
|----------|--------|
|
||||
| ***Stop Loss* Berbasis ATR** | Jarak minimum 1.5x ATR |
|
||||
| ***Stop Loss* Level Broker** | *Stop Loss* darurat diatur di level broker |
|
||||
| ***Position Sizing*** | *Kelly criterion* dengan penyesuaian mode kapital |
|
||||
| **Batas Kerugian Harian** | 5% dari kapital per hari |
|
||||
| **Batas Kerugian Total** | 10% dari kapital |
|
||||
| **Batas Posisi** | Maksimal 2 posisi bersamaan |
|
||||
| ***Exit* Berbasis Waktu** | Maksimal 6 jam per *trade* |
|
||||
| **Filter Sesi** | Hanya membuka *trade* saat sesi aktif |
|
||||
| **Filter *Spread*** | Menolak *trade* saat *spread* tinggi |
|
||||
| ***Cooldown*** | Waktu minimum antar *trade* |
|
||||
|
||||
## Tech Stack
|
||||
## Teknologi
|
||||
|
||||
- **Polars** — High-performance data engine (not Pandas)
|
||||
- **XGBoost** — Gradient boosted ML model
|
||||
- **hmmlearn** — Hidden Markov Model for regime detection
|
||||
- **MetaTrader5** — Broker connection API
|
||||
- **asyncio** — Async event loop for low-latency execution
|
||||
- **loguru** — Structured logging
|
||||
- **PostgreSQL** — Trade database
|
||||
- **Next.js** — Web dashboard
|
||||
- **Polars** — Mesin pemrosesan data performa tinggi (bukan Pandas)
|
||||
- ***XGBoost*** — Model *machine learning* berbasis *gradient boosting*
|
||||
- **hmmlearn** — *Hidden Markov Model* untuk deteksi *regime* pasar
|
||||
- ***MetaTrader5*** — API koneksi broker
|
||||
- **asyncio** — *Event loop* asinkron untuk eksekusi latensi rendah
|
||||
- **loguru** — *Logging* terstruktur
|
||||
- **PostgreSQL** — *Database* pencatatan *trade*
|
||||
- ***Next.js*** — *Dashboard* web
|
||||
|
||||
## Disclaimer
|
||||
## Peringatan
|
||||
|
||||
> This software is for **educational and research purposes only**. Trading foreign exchange (Forex) and commodities on margin carries a high level of risk and may not be suitable for all investors. Past performance is not indicative of future results. You could lose some or all of your investment. **Use at your own risk.**
|
||||
> Perangkat lunak ini dibuat **hanya untuk tujuan edukasi dan riset**. Trading valuta asing (Forex) dan komoditas dengan margin memiliki tingkat risiko yang tinggi dan mungkin tidak cocok untuk semua investor. Kinerja masa lalu bukan indikasi hasil di masa depan. Anda dapat kehilangan sebagian atau seluruh investasi Anda. **Gunakan dengan risiko Anda sendiri.**
|
||||
|
||||
## License
|
||||
## Lisensi
|
||||
|
||||
[MIT License](LICENSE) - Copyright (c) 2025-2026 Gifari Kemal
|
||||
[MIT License](LICENSE) - Hak Cipta (c) 2025-2026 Gifari Kemal
|
||||
|
||||
Reference in New Issue
Block a user