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,164 +1,160 @@
|
||||
# Configuration — Pusat Pengaturan Bot
|
||||
# Konfigurasi — Pusat Pengaturan Bot
|
||||
|
||||
> **File:** `src/config.py`
|
||||
> **Class:** `TradingConfig`, `RiskConfig`, `SMCConfig`, `MLConfig`, `ThresholdsConfig`, `RegimeConfig`
|
||||
> **Sumber:** Environment variables (`.env`)
|
||||
> **Sumber:** *Environment variables* (`.env`)
|
||||
|
||||
---
|
||||
|
||||
## Apa Itu Configuration?
|
||||
## Struktur Konfigurasi
|
||||
|
||||
Configuration adalah **pusat pengaturan** seluruh parameter bot — dari kredensial MT5 hingga threshold AI. Semua pengaturan otomatis menyesuaikan berdasarkan ukuran modal (small/medium).
|
||||
|
||||
**Analogi:** Configuration seperti **kokpit pesawat** — semua tombol dan dial pengaturan ada di satu tempat, dan bisa diubah sebelum "terbang" (trading).
|
||||
|
||||
---
|
||||
|
||||
## Capital Mode (Otomatis)
|
||||
|
||||
| Mode | Modal | Risk/Trade | Max Daily Loss | Leverage | Max Lot | Max Posisi | Timeframe |
|
||||
|------|-------|-----------|----------------|----------|---------|-----------|-----------|
|
||||
| **SMALL** | ≤ $10K | 1% | 3% | 1:100 | 0.05 | 3 | M15 |
|
||||
| **MEDIUM** | > $10K | 0.5% | 2% | 1:30 | 2.0 | 5 | H1 |
|
||||
|
||||
```python
|
||||
# Otomatis berdasarkan capital
|
||||
if capital <= 10000:
|
||||
mode = SMALL # Growth mode
|
||||
else:
|
||||
mode = MEDIUM # Preservation mode
|
||||
```mermaid
|
||||
graph TD
|
||||
A[".env File"] --> B["TradingConfig.from_env()"]
|
||||
B --> C["CapitalMode<br/>SMALL / MEDIUM"]
|
||||
C -->|"≤ $10.000"| D["SMALL<br/>Risk 1%, Max Lot 0.05"]
|
||||
C -->|"> $10.000"| E["MEDIUM<br/>Risk 0.5%, Max Lot 2.0"]
|
||||
B --> F["RiskConfig"]
|
||||
B --> G["SMCConfig"]
|
||||
B --> H["MLConfig"]
|
||||
B --> I["ThresholdsConfig"]
|
||||
B --> J["RegimeConfig"]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6 Sub-Konfigurasi
|
||||
## 2 Mode Kapital
|
||||
|
||||
### 1. RiskConfig
|
||||
### Mode SMALL (≤ $10.000) — *Growth Mode*
|
||||
|
||||
```python
|
||||
RiskConfig(
|
||||
risk_per_trade=1.0, # 1% per trade ($50 dari $5K)
|
||||
max_daily_loss=3.0, # 3% max daily loss ($150)
|
||||
max_leverage=100, # 1:100
|
||||
max_positions=3, # Max 3 posisi bersamaan
|
||||
max_lot_size=0.05, # Max 0.05 lot
|
||||
min_lot_size=0.01, # Min 0.01 lot
|
||||
lot_step=0.01, # Increment 0.01
|
||||
)
|
||||
```
|
||||
| Parameter | Nilai | Keterangan |
|
||||
|-----------|-------|------------|
|
||||
| `risk_per_trade` | **1.0%** | $50 risiko per *trade* (akun $5.000) |
|
||||
| `max_daily_loss` | **3.0%** | Batas kerugian harian $150 |
|
||||
| `max_leverage` | **1:100** | *Leverage* tinggi untuk akun kecil |
|
||||
| `max_positions` | **3** | Maksimal 3 posisi bersamaan |
|
||||
| `max_lot_size` | **0.05** | Batas atas *lot* per *trade* |
|
||||
| `min_lot_size` | **0.01** | *Lot* minimum |
|
||||
| `execution_timeframe` | **M15** | *Scalping* / *day trading* |
|
||||
|
||||
### 2. SMCConfig
|
||||
### Mode MEDIUM (> $10.000) — *Preservation Mode*
|
||||
|
||||
```python
|
||||
SMCConfig(
|
||||
swing_length=5, # 5 bar untuk swing detection
|
||||
fvg_min_gap_pips=2.0, # Min gap FVG: 2 pips
|
||||
ob_lookback=10, # Order block lookback: 10 bar
|
||||
bos_close_break=True, # Butuh close break untuk BOS
|
||||
)
|
||||
```
|
||||
| Parameter | Nilai | Keterangan |
|
||||
|-----------|-------|------------|
|
||||
| `risk_per_trade` | **0.5%** | $250 risiko per *trade* (akun $50.000) |
|
||||
| `max_daily_loss` | **2.0%** | Batas kerugian harian $1.000 |
|
||||
| `max_leverage` | **1:30** | *Leverage* konservatif |
|
||||
| `max_positions` | **5** | Lebih banyak diversifikasi |
|
||||
| `max_lot_size` | **2.0** | Batas atas *lot* |
|
||||
| `execution_timeframe` | **H1** | *Swing trading* |
|
||||
| `trend_timeframe` | **H4** | Analisis *trend* jangka menengah |
|
||||
|
||||
### 3. MLConfig
|
||||
---
|
||||
|
||||
```python
|
||||
MLConfig(
|
||||
model_path="models/xgboost_model.json",
|
||||
confidence_threshold=0.65, # Min confidence untuk entry
|
||||
retrain_frequency_days=7, # Retrain setiap 7 hari
|
||||
lookback_periods=1000, # Data lookback
|
||||
)
|
||||
```
|
||||
## *Thresholds* (Ambang Batas)
|
||||
|
||||
### 4. ThresholdsConfig
|
||||
### *ML Confidence*
|
||||
|
||||
```python
|
||||
ThresholdsConfig(
|
||||
# ML Confidence
|
||||
ml_min_confidence=0.65, # Minimum confidence
|
||||
ml_entry_confidence=0.70, # Default entry
|
||||
ml_high_confidence=0.75, # High confidence
|
||||
ml_very_high_confidence=0.80, # Lot multiplier trigger
|
||||
| Parameter | Nilai | Fungsi |
|
||||
|-----------|-------|--------|
|
||||
| `ml_min_confidence` | **0.65** | Minimum *confidence* untuk pertimbangkan sinyal |
|
||||
| `ml_entry_confidence` | **0.70** | *Default confidence* untuk *entry* |
|
||||
| `ml_high_confidence` | **0.75** | *High confidence* — sinyal kuat |
|
||||
| `ml_very_high_confidence` | **0.80** | Sangat yakin — *lot multiplier* aktif |
|
||||
|
||||
# Risk
|
||||
trend_reversal_confidence=0.75, # Trigger reversal close
|
||||
protected_mode_threshold=0.80, # Enter protected mode
|
||||
### *Dynamic Threshold*
|
||||
|
||||
# Profit/Loss (USD)
|
||||
min_profit_to_secure=15.0, # Min profit to consider secure
|
||||
good_profit_level=25.0, # Good profit
|
||||
great_profit_level=40.0, # Take it!
|
||||
| Parameter | Nilai | Kondisi |
|
||||
|-----------|-------|---------|
|
||||
| `dynamic_threshold_aggressive` | **0.65** | Pasar *trending* kuat |
|
||||
| `dynamic_threshold_moderate` | **0.70** | Kondisi normal |
|
||||
| `dynamic_threshold_conservative` | **0.75** | Pasar bergejolak |
|
||||
|
||||
# Timing
|
||||
trade_cooldown_seconds=300, # 5 menit antar trade
|
||||
loop_interval_seconds=30.0, # Main loop interval
|
||||
### *Profit/Loss* ($)
|
||||
|
||||
# Session
|
||||
sydney_lot_multiplier=0.5, # Sydney lot reduction
|
||||
)
|
||||
```
|
||||
| Parameter | Nilai | Keterangan |
|
||||
|-----------|-------|------------|
|
||||
| `min_profit_to_secure` | **$15** | Mulai pertimbangkan *take profit* |
|
||||
| `good_profit_level` | **$25** | Level profit bagus |
|
||||
| `great_profit_level` | **$40** | *Hard take profit* — ambil profit |
|
||||
|
||||
### 5. RegimeConfig
|
||||
### *Trading Timing*
|
||||
|
||||
```python
|
||||
RegimeConfig(
|
||||
n_regimes=3, # 3 HMM states
|
||||
lookback_periods=500, # HMM training lookback
|
||||
retrain_frequency=20, # Retrain setiap 20 bar
|
||||
)
|
||||
| Parameter | Nilai | Keterangan |
|
||||
|-----------|-------|------------|
|
||||
| `trade_cooldown_seconds` | **300** | 5 menit antar *trade* |
|
||||
| `loop_interval_seconds` | **30** | Interval *main loop* |
|
||||
| `sydney_lot_multiplier` | **0.5** | *Lot* dikurangi 50% saat Sydney |
|
||||
|
||||
---
|
||||
|
||||
## *Environment Variables* (`.env`)
|
||||
|
||||
```env
|
||||
# MetaTrader 5 — WAJIB
|
||||
MT5_LOGIN=12345678
|
||||
MT5_PASSWORD=your_password
|
||||
MT5_SERVER=YourBroker-Server
|
||||
MT5_PATH=C:/Program Files/MetaTrader 5/terminal64.exe
|
||||
|
||||
# Telegram — OPSIONAL
|
||||
TELEGRAM_BOT_TOKEN=bot123:ABC-DEF
|
||||
TELEGRAM_CHAT_ID=123456789
|
||||
|
||||
# Trading
|
||||
CAPITAL=5000 # Menentukan mode (SMALL/MEDIUM)
|
||||
SYMBOL=XAUUSD # Pair yang diperdagangkan
|
||||
|
||||
# Override (opsional)
|
||||
RISK_PER_TRADE=1.0 # Override risk per trade (%)
|
||||
MAX_DAILY_LOSS_PERCENT=3.0
|
||||
MAX_POSITION_SIZE=0.05
|
||||
AI_CONFIDENCE_THRESHOLD=0.65
|
||||
FLASH_CRASH_THRESHOLD=2.5
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Environment Variables (.env)
|
||||
## Kalkulasi *Position Sizing*
|
||||
|
||||
| Variable | Contoh | Wajib | Keterangan |
|
||||
|----------|--------|-------|------------|
|
||||
| `MT5_LOGIN` | `12345678` | Ya | Akun MT5 |
|
||||
| `MT5_PASSWORD` | `p@ssw0rd` | Ya | Password MT5 |
|
||||
| `MT5_SERVER` | `BrokerName-Live` | Ya | Server broker |
|
||||
| `MT5_PATH` | `C:\...\terminal64.exe` | Tidak | Path MT5 |
|
||||
| `CAPITAL` | `5000` | Tidak | Modal ($5000 default) |
|
||||
| `SYMBOL` | `XAUUSD` | Tidak | Simbol trading |
|
||||
| `RISK_PER_TRADE` | `1.0` | Tidak | Override risk % |
|
||||
| `MAX_DAILY_LOSS_PERCENT` | `3.0` | Tidak | Override daily loss |
|
||||
| `AI_CONFIDENCE_THRESHOLD` | `0.65` | Tidak | Override ML threshold |
|
||||
| `TELEGRAM_BOT_TOKEN` | `123:ABC...` | Tidak | Token Telegram |
|
||||
| `TELEGRAM_CHAT_ID` | `-1001234...` | Tidak | Chat ID Telegram |
|
||||
| `DB_HOST` | `localhost` | Tidak | PostgreSQL host |
|
||||
| `DB_NAME` | `trading_db` | Tidak | Database name |
|
||||
|
||||
---
|
||||
|
||||
## Position Sizing (Kelly Criterion)
|
||||
Bot menggunakan **metode *Half-Kelly Criterion*** untuk menghitung ukuran *lot*:
|
||||
|
||||
```python
|
||||
def calculate_position_size(entry_price, stop_loss_price, balance):
|
||||
"""
|
||||
Risk-Constrained Kelly Criterion:
|
||||
def calculate_position_size(self, entry_price, stop_loss_price, account_balance=None):
|
||||
# 1. Hitung jumlah risiko dalam $
|
||||
risk_amount = balance * (risk_per_trade / 100)
|
||||
|
||||
risk_amount = balance × risk% ($5000 × 1% = $50)
|
||||
sl_pips = |entry - SL| / 0.1
|
||||
lot = risk_amount / (sl_pips × pip_value)
|
||||
lot × 0.5 (Half-Kelly untuk safety)
|
||||
# 2. Hitung jarak SL dalam pips
|
||||
sl_distance = abs(entry_price - stop_loss_price)
|
||||
sl_pips = sl_distance / 0.1 # XAUUSD: 1 pip = $0.1
|
||||
|
||||
Clamp: min_lot ≤ lot ≤ max_lot
|
||||
"""
|
||||
# 3. Hitung lot size
|
||||
lot_size = risk_amount / (sl_pips * pip_value_per_lot)
|
||||
|
||||
# 4. Apply Half-Kelly (keamanan)
|
||||
lot_size *= 0.5
|
||||
|
||||
# 5. Round dan batasi
|
||||
lot_size = round(lot_size / lot_step) * lot_step
|
||||
lot_size = max(min_lot, min(lot_size, max_lot))
|
||||
```
|
||||
|
||||
**Contoh:** Akun $5.000, SL 50 *pips*, risiko 1%:
|
||||
- Risiko = $50
|
||||
- *Lot* = $50 / (50 × $1) = 0.01 *lot* (setelah *Half-Kelly*)
|
||||
|
||||
---
|
||||
|
||||
## Validasi Otomatis
|
||||
## Validasi Konfigurasi
|
||||
|
||||
```python
|
||||
def _validate_required_settings(self):
|
||||
"""Validasi environment variables wajib saat startup."""
|
||||
# MT5_LOGIN harus ada dan > 0
|
||||
# MT5_PASSWORD harus ada dan tidak kosong
|
||||
# MT5_SERVER harus ada dan tidak kosong
|
||||
# CAPITAL harus > 0
|
||||
# Jika ada yang hilang → ValueError dengan pesan jelas
|
||||
```
|
||||
Saat TradingConfig dibuat:
|
||||
|
|
||||
v
|
||||
_validate_required_settings():
|
||||
├── MT5_LOGIN != 0?
|
||||
├── MT5_PASSWORD tidak kosong?
|
||||
├── MT5_SERVER tidak kosong?
|
||||
└── Capital > 0?
|
||||
|
|
||||
├── Ada yang gagal → ValueError
|
||||
└── Semua OK → _configure_by_capital()
|
||||
```
|
||||
|
||||
Bot **tidak bisa berjalan** tanpa kredensial MT5 yang valid.
|
||||
|
||||
Reference in New Issue
Block a user