0393087168
Gates 5-6 in the M5 filter: block 'high' vol regime entries (chop transition — 61% WR, negative total while low/normal/extreme all positive) and block SELLs with RSI<25 (dip already extended, mean- reverts before target). 365d: 419→311 trades, +41→+48.4R, DD 11.2→8.0R. Replace the fixed RR-0.5 take-profit with a trailing stop: arm at +0.6R favorable excursion, trail 0.75R behind best price, stop levels update at bar close (conservative). The RR-0.5 target was the binding constraint — winners kept running after TP. Whole trail parameter grid (arm 0.4-1.2 × dist 0.5-1.25) lands +90-103R, a plateau not a spike; also +91.7R on the pre-gate 419-trade population. TP1 stays advisory; Telegram alerts include the arm price and trail distance. max_hold 200→300 bars (trailing exits need room; +10R). 12mo M5, same 311 trades: +105.0R total, PF 2.27, DD 7.9R, 61% WR, train/val +54.6/+50.4, every quarter positive.
266 lines
9.0 KiB
Python
Executable File
266 lines
9.0 KiB
Python
Executable File
"""
|
||
ForexBot2 — Configuration (XAU/USD focused)
|
||
==========================================
|
||
"""
|
||
|
||
from dataclasses import dataclass, field
|
||
from enum import Enum
|
||
from typing import Dict, List
|
||
|
||
|
||
class Timeframe(Enum):
|
||
M1 = "1min"
|
||
M5 = "5min"
|
||
M15 = "15min"
|
||
M30 = "30min"
|
||
H1 = "1h"
|
||
H4 = "4h"
|
||
D1 = "1d"
|
||
W1 = "1wk"
|
||
|
||
|
||
class SignalStrength(Enum):
|
||
STRONG_BUY = 2
|
||
BUY = 1
|
||
NEUTRAL = 0
|
||
SELL = -1
|
||
STRONG_SELL = -2
|
||
|
||
|
||
class MarketSession(Enum):
|
||
SYDNEY = "sydney"
|
||
TOKYO = "tokyo"
|
||
LONDON = "london"
|
||
NEW_YORK = "new_york"
|
||
OVERLAP_LONDON_NY = "london_ny_overlap"
|
||
|
||
|
||
# ── Supported pairs ──────────────────────────────────────────────
|
||
XAUUSD = "XAU/USD"
|
||
|
||
PAIR_INFO = {
|
||
XAUUSD: {"display": "XAU/USD", "pip_value": 0.10, "is_jpy_pair": False},
|
||
}
|
||
|
||
|
||
@dataclass
|
||
class TechnicalConfig:
|
||
"""Technical indicator parameters."""
|
||
ema_fast: int = 9
|
||
ema_medium: int = 21
|
||
ema_slow: int = 50
|
||
ema_trend: int = 200
|
||
|
||
rsi_period: int = 14
|
||
rsi_overbought: float = 70.0
|
||
rsi_oversold: float = 30.0
|
||
rsi_neutral_upper: float = 60.0
|
||
rsi_neutral_lower: float = 40.0
|
||
|
||
macd_fast: int = 12
|
||
macd_slow: int = 26
|
||
macd_signal: int = 9
|
||
|
||
bb_period: int = 20
|
||
bb_std: float = 2.0
|
||
|
||
atr_period: int = 14
|
||
|
||
adx_period: int = 14
|
||
adx_strong_trend: float = 25.0
|
||
adx_very_strong: float = 50.0
|
||
|
||
divergence_lookback: int = 40
|
||
swing_sr_lookback: int = 50
|
||
|
||
disabled_indicators: List[str] = field(default_factory=list)
|
||
|
||
ema_mode: str = "4ema" # "4ema" (9/21/50/200) or "2ema" (50/200)
|
||
rsi_mode: str = "regime_aware" # "classic" or "regime_aware"
|
||
macd_as_pillar: bool = True # MACD histogram is a scored signal
|
||
|
||
# ROC (Rate of Change)
|
||
roc_period: int = 10
|
||
roc_threshold: float = 1.0 # % threshold for strong signal
|
||
|
||
# Keltner Channels
|
||
kc_period: int = 20
|
||
kc_atr_period: int = 10
|
||
kc_multiplier: float = 1.5
|
||
|
||
# ADX slope (trend acceleration detection)
|
||
adx_slope_lookback: int = 5
|
||
adx_slope_emerging_threshold: float = 3.0
|
||
|
||
regime_weights_override: Dict[str, Dict[str, float]] = field(default_factory=dict)
|
||
|
||
|
||
@dataclass
|
||
class SignalConfig:
|
||
"""Signal scoring & filtering."""
|
||
min_confidence: float = 55.0
|
||
|
||
weights: Dict[str, float] = field(default_factory=lambda: {
|
||
"ema_trend": 1.0,
|
||
"rsi": 1.5,
|
||
"bollinger": 1.5,
|
||
"macd": 1.5,
|
||
"keltner": 1.5,
|
||
"roc": 1.5,
|
||
"candlestick": 1.0,
|
||
"divergence": 1.5,
|
||
})
|
||
|
||
mtf_agreement_threshold: int = 2 # of 3 timeframes
|
||
cooldown_bars: int = 5
|
||
|
||
default_risk_pct: float = 1.0
|
||
default_rr_ratio: float = 2.0
|
||
atr_sl_multiplier: float = 1.5
|
||
min_rr_ratio: float = 0.5
|
||
# TP tightening (May 2026) was tuned on the pre-spread backtester.
|
||
# Re-tested 2026-07-08 with $0.30 spread + gap fills: 0.8 nearly
|
||
# halves total R (+36.1R vs +66.8R, worse in both train/val halves)
|
||
# — smaller wins make the fixed spread a larger share of every TP.
|
||
tp_tightening_factor: float = 1.0
|
||
|
||
# ── Trade geometry ────────────────────────────────────────────
|
||
# Round-trip transaction cost estimate in price units (XAU ~$0.30).
|
||
round_trip_cost: float = 0.30
|
||
# Spread gate: reject setups whose TP1 distance is below
|
||
# round_trip_cost × this multiple (caps cost at ~3% of target).
|
||
# Validated 2026-07: small-target trades lose in both data halves
|
||
# on M5 while TP≥$10 trades earn ~+0.08R/trade.
|
||
min_tp_cost_multiple: float = 30.0
|
||
# Fixed SL floor in pips (80 pips = $8 on XAU). 0 disables — SL then
|
||
# floors at ATR × v2_sl_atr_multiplier only.
|
||
min_sl_pips: float = 80.0
|
||
# TP1 floor as a fraction of SL distance (0.5 = historical RR 0.5).
|
||
tp_floor_rr: float = 0.5
|
||
|
||
# ── Trailing-stop exit (2026-07) ──────────────────────────────
|
||
# Once favorable excursion reaches trail_arm_r × risk, the stop
|
||
# trails trail_dist_r × risk behind the best price (updated at bar
|
||
# close). TP1 becomes advisory — the trail is the exit. Validated
|
||
# on 12mo M5: +105R vs +48R with the fixed RR-0.5 target, balanced
|
||
# train/val, positive every quarter, PF 2.27. The RR-0.5 target was
|
||
# the binding constraint — winners kept running after TP.
|
||
# Set trail_arm_r = 0 to disable and exit at TP1 as before.
|
||
trail_arm_r: float = 0.6
|
||
trail_dist_r: float = 0.75
|
||
|
||
|
||
@dataclass
|
||
class NewsConfig:
|
||
"""Economic calendar & sentiment."""
|
||
tracked_currencies: List[str] = field(default_factory=lambda: ["USD", "XAU"])
|
||
min_impact: str = "medium"
|
||
pre_event_buffer_minutes: int = 30
|
||
post_event_buffer_minutes: int = 15
|
||
|
||
rss_feeds: List[str] = field(default_factory=lambda: [
|
||
"https://investinglive.com/feed/",
|
||
"https://www.fxstreet.com/rss",
|
||
])
|
||
|
||
|
||
@dataclass
|
||
class XauStrategyConfig:
|
||
"""XAU/USD strategy config.
|
||
Legacy gates (H4 RSI, H1 MACD, M15 score) removed in favor of
|
||
new momentum indicators (ROC, Keltner squeeze, ADX slope).
|
||
"""
|
||
enabled: bool = True
|
||
rsi_entry_threshold: float = 45.0
|
||
tp_atr_multiplier: float = 1.0
|
||
sl_atr_multiplier: float = 2.0
|
||
min_ema200_tfs: int = 2
|
||
allow_generic_trades: bool = True
|
||
blocked_hours_utc: List[int] = field(default_factory=lambda: [0, 1, 2, 10, 11, 13, 16, 21])
|
||
weekend_filter: bool = True
|
||
min_atr_threshold: float = 1.0
|
||
v2_sl_atr_multiplier: float = 2.0
|
||
buy_threshold_boost: int = 0
|
||
m5_confirmation_bars: int = 0
|
||
h4_rsi_gate: bool = False
|
||
h1_macd_gate: bool = False
|
||
rsi_zone: tuple = (40.0, 65.0)
|
||
allowed_weekdays: List[int] = field(default_factory=lambda: [0, 1, 2, 3, 4, 6]) # Mon-Fri + Sun (Sydney open)
|
||
sell_min_quality: List[str] = field(default_factory=lambda: ["A+", "A", "B"])
|
||
# Volatility regimes in which SELL setups are allowed (low/normal-vol
|
||
# shorts lose consistently against gold's macro uptrend).
|
||
sell_vol_regimes: List[str] = field(default_factory=lambda: ["high", "extreme"])
|
||
|
||
# ── M5-specific filters ──────────────────────────────────────
|
||
# M5 signals are noisy; require stronger confirmation before
|
||
# generating trade setups. Without these, M5 is breakeven
|
||
# (+0.01R/trade) with 21% drawdown.
|
||
m5_require_mtf: bool = True
|
||
m5_min_abs_score: float = 30.0
|
||
m5_min_confirmation_score: float = 10.0
|
||
# A-grade M5 SELLs enter at the extreme of temporary dips and
|
||
# lose (-0.017R/trade on 163 trades) — maximum conviction at
|
||
# the worst entry. Cap SELL quality so A-grade can't trigger
|
||
# the ATR-based setup generator; B-grade swing setups survive
|
||
# because they catch moderate drops, not extremes.
|
||
m5_sell_max_quality: List[str] = field(default_factory=lambda: ["B", "C"])
|
||
# 'high' vol regime on M5 is chop transition — elevated noise
|
||
# without directional expansion. 61% WR / negative total across
|
||
# 12mo while low/normal/extreme are all positive (validated 2026-07).
|
||
m5_blocked_vol_regimes: List[str] = field(default_factory=lambda: ["high"])
|
||
# Block M5 SELLs when RSI is already < this floor: the dip is
|
||
# extended and mean-reverts before the target (negative in both
|
||
# halves of 6mo and 12mo backtests).
|
||
m5_sell_min_rsi: float = 25.0
|
||
|
||
|
||
@dataclass
|
||
class FilterConfig:
|
||
"""Regime and session filters for live signals."""
|
||
regime_filter: bool = False
|
||
regime_filter_timeframe: Timeframe = Timeframe.H4
|
||
regime_filter_min_adx: float = 25.0
|
||
|
||
session_filter: bool = True
|
||
session_start_hour: int = 3
|
||
session_end_hour: int = 23
|
||
blocked_hours_utc: List[int] = field(default_factory=lambda: [0, 1, 2, 10, 11, 13, 16, 21])
|
||
|
||
allowed_weekdays: List[int] = field(default_factory=lambda: [0, 1, 2, 3, 4, 6]) # Mon-Fri + Sun (Sydney open)
|
||
|
||
|
||
@dataclass
|
||
class PlatformConfig:
|
||
"""Top-level configuration."""
|
||
technical: TechnicalConfig = field(default_factory=TechnicalConfig)
|
||
signals: SignalConfig = field(default_factory=SignalConfig)
|
||
news: NewsConfig = field(default_factory=NewsConfig)
|
||
backtest: FilterConfig = field(default_factory=FilterConfig)
|
||
xau_strategy: XauStrategyConfig = field(default_factory=XauStrategyConfig)
|
||
|
||
symbol: str = XAUUSD
|
||
display_name: str = "XAU/USD"
|
||
pip_value: float = 0.10
|
||
|
||
primary_timeframe: Timeframe = Timeframe.H1
|
||
confirmation_timeframes: List[Timeframe] = field(
|
||
default_factory=lambda: [Timeframe.M15, Timeframe.H4]
|
||
)
|
||
|
||
data_lookback_days: int = 60
|
||
|
||
twelve_data_api_key: str = "f563b22ffdfd4924955decbe1a8c8701"
|
||
|
||
refresh_interval: int = 300
|
||
|
||
scan_pairs: List[str] = field(
|
||
default_factory=lambda: [XAUUSD]
|
||
)
|
||
scan_timeframes: List[Timeframe] = field(
|
||
default_factory=lambda: [Timeframe.M5, Timeframe.M15, Timeframe.H1, Timeframe.H4]
|
||
)
|
||
candle_buffer_seconds: int = 10
|
||
ws_cooldown_seconds: int = 300
|
||
|
||
log_level: str = "INFO"
|