💱 Detect Thị Trường Sideway Trong FOREX (OHLCV – Python)
1️⃣ Đặc thù Sideway trong Forex
👉 Forex sideway dễ bị fake breakout → cần filter kỹ
2️⃣ Threshold khuyến nghị cho Forex (Thực chiến)
| Indicator |
M15–H1 |
H4 |
| ADX |
< 18 |
< 20 |
| ATR / Close |
< 0.003 |
< 0.002 |
| Range % |
< 1.5% |
< 2% |
| BB Width |
< 0.01 |
< 0.015 |
3️⃣ Cách 1: ADX thấp + ATR thấp (Chuẩn Forex Bot)
📌 Logic
-
Không có xu hướng
-
Biên độ nhỏ theo pip
🧪 Code
import ta defdetect_sideway_forex_adx_atr( df, adx_threshold=18, atr_ratio_threshold=0.003 ): df['adx'] = ta.trend.ADXIndicator( df['High'], df['Low'], df['Close'] ).adx() df['atr'] = ta.volatility.AverageTrueRange( df['High'], df['Low'], df['Close'] ).average_true_range() df['atr_ratio'] = df['atr'] / df['Close'] df['sideway'] = ( (df['adx'] < adx_threshold) & (df['atr_ratio'] < atr_ratio_threshold) ) return df
✅ Dùng tốt cho EURUSD, GBPUSD, USDJPY, XAUUSD
4️⃣ Cách 2: Sideway theo Range giá (HH–LL)
📌 Logic
-
Giá bị nhốt trong box
-
Không phá đỉnh / đáy
🧪 Code
defdetect_sideway_forex_range(df, window=24, range_pct=0.015): df['hh'] = df['High'].rolling(window).max() df['ll'] = df['Low'].rolling(window).min() df['range_pct'] = (df['hh'] - df['ll']) / df['Close'] df['sideway'] = df['range_pct'] < range_pct return df
✅ Chuẩn cho scalping – range trading
5️⃣ Cách 3: MA phẳng (Loại trend giả)
📌 Logic
🧪 Code
defdetect_sideway_forex_ma_slope( df, ma_period=50, slope_threshold=0.00005 ): df['ema'] = df['Close'].ewm(span=ma_period).mean() df['ema_slope'] = df['ema'].diff() df['sideway'] = abs(df['ema_slope']) < slope_threshold return df
⚠️ Không dùng đơn lẻ – chỉ để filter
6️⃣ Cách 4: Bollinger Band Squeeze (Forex hay dùng)
📌 Logic
🧪 Code
defdetect_sideway_forex_bb( df, window=20, width_threshold=0.01 ): ma = df['Close'].rolling(window).mean() std = df['Close'].rolling(window).std() upper = ma + 2 * std lower = ma - 2 * std df['bb_width'] = (upper - lower) / ma df['sideway'] = df['bb_width'] < width_threshold return df
7️⃣ Cách PRO (Khuyến nghị dùng trong BOT Forex)
📌 Market Regime Detector
df['sideway'] = ( (df['adx'] < 18) & (df['atr_ratio'] < 0.003) & (df['bb_width'] < 0.012) )
🎯 Giảm fake breakout
🎯 Chuẩn cho auto-switch strategy
8️⃣ Mapping Strategy cho Forex
| Market State |
Strategy |
| Sideway |
RSI 30–70, Grid, Mean Reversion |
| Squeeze |
Breakout London / NY |
| Trend |
Pullback EMA, Trend-following |
9️⃣ Gợi ý Timeframe cho Forex Bot
| Timeframe |
Gợi ý |
| M5 |
Scalping (lọc sideway cực kỹ) |
| M15 |
Bot intraday |
| H1 |
Swing ngắn |
| H4 |
Regime lớn – lọc nhiễu |
🔚 Kết luận
Forex sideway = thời điểm nguy hiểm nhất cho bot trend.
Detect đúng sideway giúp: