⏰ Time:
🌡️ Temp:
Loading...
📌 กลยุทธ์การเทรดตามเทรนด์ (Trend-Following Strategy)
โดย puk
•2025-09-02 03:39
1. ตัวชี้วัดที่ใช้เพื่อให้กลยุทธ์เหมาะกับการตั้งค่า timeframe_sl_tp_multipliers และลดปัญหาการขาดทุนจาก log เดิม ตัวชี้วัดจะถูกเลือกและปรับแต่งดังนี้:ADX (Average Directional Index):ใช้ยืนยันความแข็งแกร่งของแนวโน้ม
เกณฑ์: ADX > 25 สำหรับ timeframe 1h ขึ้นไป, ADX > 20 สำหรับ 1m, 5m
ถ้า ADX < 20 (ใน 1m, 5m) หรือ ADX < 25 (ใน 1h, 4h, 8h, 1d) ข้ามการเทรด
SMA (Simple Moving Average):ใช้ SMA Fast (10) และ SMA Slow (20) เพื่อยืนยันทิศทางแนวโน้ม
BUY: ราคา > SMA Slow และ SMA Fast > SMA Slow
SELL: ราคา < SMA Slow และ SMA Fast < SMA Slow
MACD (Moving Average Convergence Divergence):ใช้ MACD Line และ Signal Line เพื่อยืนยันการเปลี่ยนแปลงแนวโน้ม
BUY: MACD > Signal Line และ MACD Histogram > 0
SELL: MACD < Signal Line และ MACD Histogram < 0
VWAP (Volume Weighted Average Price):ใช้กรองสัญญาณในช่วงที่ไม่มีแนวโน้ม
BUY: ราคา > VWAP
SELL: ราคา < VWAP
ATR (Average True Range):ใช้กำหนด SL, TP และ Trailing Stop ตาม timeframe_sl_tp_multipliers
ใช้ ATR Trend (ATR Increasing) เพื่อยืนยันความผันผวนที่สูงขึ้นในแนวโน้ม
Stochastic Oscillator (ปรับลดน้ำหนัก):ใช้เป็นตัวชี้วัดเสริมใน timeframe สั้น (1m, 5m)
BUY: %K < 25 และ %K > %D
SELL: %K > 75 และ %K < %D
2. เงื่อนไขสัญญาณเพื่อลดสัญญาณหลอกและจับแนวโน้มที่แข็งแกร่ง:BUY Signal:1m, 5m:ADX > 20
ราคา > SMA Slow
SMA Fast > SMA Slow
MACD > Signal Line หรือ MACD Histogram > 0
ราคา > VWAP
Stochastic %K < 25 และ %K > %D
ATR > 0.8 หรือ ATR Increasing
จำนวนสัญญาณขั้นต่ำ: 3
15m, 30m:ADX > 20
ราคา > SMA Slow
SMA Fast > SMA Slow
MACD > Signal Line
ราคา > VWAP
ATR > 0.8
จำนวนสัญญาณขั้นต่ำ: 4
1h, 4h, 8h, 1d:ADX > 25
ราคา > SMA Slow
SMA Fast > SMA Slow
MACD > Signal Line และ MACD Histogram > 0
ราคา > VWAP
ATR > 1.0
จำนวนสัญญาณขั้นต่ำ: 5
SELL Signal:1m, 5m:ADX > 20
ราคา < SMA Slow
SMA Fast < SMA Slow
MACD < Signal Line หรือ MACD Histogram < 0
ราคา < VWAP
Stochastic %K > 75 และ %K < %D
ATR > 0.8 หรือ ATR Increasing
จำนวนสัญญาณขั้นต่ำ: 3
15m, 30m:ADX > 20
ราคา < SMA Slow
SMA Fast < SMA Slow
MACD < Signal Line
ราคา < VWAP
ATR > 0.8
จำนวนสัญญาณขั้นต่ำ: 4
1h, 4h, 8h, 1d:ADX > 25
ราคา < SMA Slow
SMA Fast < SMA Slow
MACD < Signal Line และ MACD Histogram < 0
ราคา < VWAP
ATR > 1.0
จำนวนสัญญาณขั้นต่ำ: 5
No Trade:ADX < 20 (1m, 5m) หรือ ADX < 25 (1h, 4h, 8h, 1d)
ATR < 0.8 (1m, 5m, 15m, 30m) หรือ ATR < 1.0 (1h, 4h, 8h, 1d)
ราคาอยู่ในช่วง Sideways (เช่น ราคาอยู่ระหว่าง SMA Fast และ SMA Slow)
3. การจัดการ PositionEntry:ใช้ investment_percentage = 0.25 (25% ของ balance ต่อ position)
เปิด Long เมื่อมี BUY Signal และ Short เมื่อมี SELL Signal
คำนวณ SL/TP จาก timeframe_sl_tp_multipliers:python
atr = last_row['atr']
sl_price = entry_price - (atr * multipliers['sl']) # Long
tp_price = entry_price + (atr * multipliers['tp']) # Long
trailing_stop = entry_price - (atr * multipliers['trailing']) # Long
Trailing Stop:อัปเดต Trailing Stop ในแต่ละ candle:python
if position['type'] == 'Long':
profit = (current_price - position['entry_price']) / position['entry_price']
trailing_multiplier = multipliers['trailing'] * (1 + profit * 0.5) # Dynamic Trailing
new_trailing_stop = current_price - (last_row['atr'] * trailing_multiplier)
position['trailing_stop'] = max(position['trailing_stop'], new_trailing_stop)
elif position['type'] == 'Short':
profit = (position['entry_price'] - current_price) / position['entry_price']
trailing_multiplier = multipliers['trailing'] * (1 + profit * 0.5)
new_trailing_stop = current_price + (last_row['atr'] * trailing_multiplier)
position['trailing_stop'] = min(position['trailing_stop'], new_trailing_stop)
Exit:ปิด position เมื่อ:ราคาถึง SL
ราคาถึง TP
ราคาถึง Trailing Stop
เกิดสัญญาณตรงข้าม (เช่น SELL Signal ใน Long position)
บันทึกเหตุผลการปิดใน trade_analysis:python
close_reason = "SL" if current_price <= position['sl_price'] else "TP" if current_price >= position['tp_price'] else "Trailing Stop" if current_price <= position['trailing_stop'] else "Opposite Signal"
Cooldown Period:หลังปิด position รอ 3 candles ก่อนตรวจสัญญาณใหม่:python
cooldown_count = 3
if cooldown_count > 0:
cooldown_count -= 1
continue
4. การจัดการความเสี่ยงSL/TP จาก timeframe_sl_tp_multipliers:python
timeframe_sl_tp_multipliers = {
'1m': {'sl': 1.2, 'tp': 0.8, 'trailing': 1.0},
'5m': {'sl': 1.2, 'tp': 0.8, 'trailing': 1.0},
'15m': {'sl': 1.8, 'tp': 1.2, 'trailing': 1.2},
'30m': {'sl': 1.8, 'tp': 1.2, 'trailing': 1.2},
'1h': {'sl': 3.2, 'tp': 1.8, 'trailing': 1.5},
'4h': {'sl': 4.2, 'tp': 1.8, 'trailing': 1.5},
'8h': {'sl': 5.8, 'tp': 2.2, 'trailing': 1.8},
'1d': {'sl': 6.8, 'tp': 2.2, 'trailing': 1.8}
}
ปรับ RRR:เนื่องจาก SL ใน timeframe ยาว (4h, 8h, 1d) กว้างมาก (4.2–6.8) ปรับ TP เพื่อให้ RRR ดีขึ้น:python
timeframe_sl_tp_multipliers = {
'1m': {'sl': 1.2, 'tp': 0.8, 'trailing': 1.0},
'5m': {'sl': 1.2, 'tp': 0.8, 'trailing': 1.0},
'15m': {'sl': 1.8, 'tp': 1.2, 'trailing': 1.2},
'30m': {'sl': 1.8, 'tp': 1.2, 'trailing': 1.2},
'1h': {'sl': 3.2, 'tp': 2.4, 'trailing': 1.5}, # RRR ~1:0.75
'4h': {'sl': 4.2, 'tp': 3.0, 'trailing': 1.5}, # RRR ~1:0.71
'8h': {'sl': 5.8, 'tp': 4.0, 'trailing': 1.8}, # RRR ~1:0.69
'1d': {'sl': 6.8, 'tp': 4.5, 'trailing': 1.8} # RRR ~1:0.66
}
จำกัด Drawdown:หยุดการเทรดชั่วคราวหาก balance ลดลงเกิน 20%:python
if balance < initial_capital * 0.8:
log_status("⚠️ Max Drawdown Reached (20%). Pausing trading for 1 hour...", Fore.RED)
await asyncio.sleep(3600)
5. การปรับโค้ดสำหรับกลยุทธ์ต่อไปนี้เป็นการปรับปรุงฟังก์ชัน evaluate_signals ในโค้ด V.2.3 เพื่อให้สอดคล้องกับกลยุทธ์การเทรดตามเทรนด์:python
def evaluate_signals(df, params, exchange_name, symbol, timeframe, mode='live', last_signal=None):
last_row = df.iloc[-1]
prev_row = df.iloc[-2] if len(df) > 1 else last_row
signals = []
signal_desc = []
indicators = timeframe_indicators.get(timeframe, ['sma', 'macd', 'vwap', 'adx', 'stoch'])
# ADX: Confirm Trend Strength
if 'adx' in indicators:
if last_row['adx'] < params['adx_threshold']:
signals.append('NO_TREND')
signal_desc.append(f"ADX < {params['adx_threshold']} (No strong trend)")
if mode != 'backtest':
log_status(f"No trade: Weak trend detected (ADX={last_row['adx']:.2f} < {params['adx_threshold']})", Fore.YELLOW)
return None, signal_desc
# SMA Signals
if 'sma' in indicators:
if last_row['close'] > last_row['sma_slow'] and last_row['sma_fast'] > last_row['sma_slow']:
signals.append('BUY_SMA_POS')
signal_desc.append("Price > SMA Slow & Fast > Slow")
elif last_row['close'] < last_row['sma_slow'] and last_row['sma_fast'] < last_row['sma_slow']:
signals.append('SELL_SMA_POS')
signal_desc.append("Price < SMA Slow & Fast < Slow")
if prev_row['sma_fast'] < prev_row['sma_slow'] and last_row['sma_fast'] > last_row['sma_slow']:
signals.append('BUY_SMA_CROSS')
signal_desc.append("SMA Crossover (Fast > Slow)")
elif prev_row['sma_fast'] > prev_row['sma_slow'] and last_row['sma_fast'] < last_row['sma_slow']:
signals.append('SELL_SMA_CROSS')
signal_desc.append("SMA Crossover (Fast < Slow)")
# MACD Signals
if 'macd' in indicators:
if last_row['macd'] > last_row['macd_signal'] and last_row['macd_hist'] > 0:
signals.append('BUY_MACD')
signal_desc.append("MACD > Signal Line & Histogram > 0")
elif last_row['macd'] < last_row['macd_signal'] and last_row['macd_hist'] < 0:
signals.append('SELL_MACD')
signal_desc.append("MACD < Signal Line & Histogram < 0")
# VWAP Signals
if 'vwap' in indicators:
if last_row['close'] > last_row['vwap']:
signals.append('BUY_VWAP')
signal_desc.append("Price > VWAP")
elif last_row['close'] < last_row['vwap']:
signals.append('SELL_VWAP')
signal_desc.append("Price < VWAP")
# ATR Signals
if 'atr' in indicators:
atr_threshold = 0.8 if timeframe in ['1m', '5m', '15m', '30m'] else 1.0
if last_row['atr'] > atr_threshold:
signals.append('BUY_SELL_ATR_THRESH')
signal_desc.append(f"ATR > {atr_threshold}")
if last_row['atr'] > prev_row['atr']:
signals.append('BUY_SELL_ATR_TREND')
signal_desc.append("ATR Increasing")
# Stochastic Signals (only for 1m, 5m)
if 'stoch' in indicators and timeframe in ['1m', '5m']:
if last_row['stoch_k'] < 25 and last_row['stoch_k'] > last_row['stoch_d']:
signals.append('BUY_STOCH')
signal_desc.append("Stochastic %K < 25 & %K > %D")
elif last_row['stoch_k'] > 75 and last_row['stoch_k'] < last_row['stoch_d']:
signals.append('SELL_STOCH')
signal_desc.append("Stochastic %K > 75 & %K < %D")
buy_signals = [s for s in signals if s.startswith('BUY')]
sell_signals = [s for s in signals if s.startswith('SELL')]
buy_sell_signals = [s for s in signals if s.startswith('BUY_SELL')]
required_signals = timeframe_required_signals.get(timeframe, 5)
signal = None
if len(buy_signals) + len(buy_sell_signals) >= required_signals:
signal = 'BUY'
log_status(f"🚀 BUY signal detected on {exchange_name} at price {last_row['close']:.2f}. Signals: {', '.join(signal_desc)}", Fore.GREEN)
elif len(sell_signals) + len(buy_sell_signals) >= required_signals:
signal = 'SELL'
log_status(f"📉 SELL signal detected on {exchange_name} at price {last_row['close']:.2f}. Signals: {', '.join(signal_desc)}", Fore.RED)
else:
if mode != 'backtest' and last_signal is not None:
log_status("No signal detected", Fore.CYAN)
return signal, signal_desc
6. การวิเคราะห์ผลลัพธ์บันทึกผลลัพธ์:ใช้ตาราง trade_analysis เพื่อวิเคราะห์สัญญาณที่ทำให้ขาดทุน:sql
SELECT signals, reason, COUNT(*) as count, AVG(profit) as avg_profit
FROM trade_analysis
GROUP BY signals, reason
ORDER BY count DESC, avg_profit ASC;
ตัวอย่าง: หากพบว่า Price < SMA Slow และ MACD Histogram < 0 ทำให้ขาดทุนบ่อยใน 4h, อาจเพิ่มเงื่อนไข VWAP หรือ ADX เข้มงวดขึ้น
Backtest Metrics:Win Rate: เป้าหมาย > 50%
Total Profit: เป้าหมาย > 0
Max Drawdown: เป้าหมาย < 20%
RRR: เป้าหมาย > 1:0.6
7. การทดสอบและปรับแต่งBacktest:bash
/backtest BNB/USDT 4h 10000
ใช้ timeframe 4h ซึ่งมี SL=4.2, TP=3.0 (ปรับแล้ว) เพื่อทดสอบกลยุทธ์
ตรวจสอบว่าสัญญาณจาก ADX และ VWAP ช่วยลดการขาดทุนหรือไม่
Simulated Trading:bash
/simulate BNB/USDT 4h 10000
ดูว่า Trailing Stop ทำงานได้ดีในแนวโน้มยาวหรือไม่
ตรวจสอบการแจ้งเตือนใน Telegram เพื่อดูว่าสัญญาณ BUY/SELL มีความแม่นยำ
Optimization:อัปเดต param_ranges เพื่อทดสอบ SL/TP ใหม่:python
param_ranges = {
'adx_threshold': [20, 25, 30],
'sma_fast_window': [8, 10, 12],
'sma_slow_window': [20, 25, 30],
'macd_fast': [12, 14],
'macd_slow': [26, 28],
'macd_signal': [9],
'atr_threshold': [0.8, 1.0, 1.2],
'sl_multiplier': [3.2, 4.2, 5.8, 6.8],
'tp_multiplier': [1.8, 2.4, 3.0, 4.0, 4.5],
'investment_percentage': [0.25]
}
ข้อดีของกลยุทธ์จับแนวโน้มที่แข็งแกร่ง:ADX และ VWAP ช่วยให้เข้าเทรดในช่วงที่มีแนวโน้มชัดเจน
ลดการเทรดในช่วง Sideways ซึ่งเป็นสาเหตุของการขาดทุนใน log เดิม
ลดสัญญาณหลอก:การใช้เงื่อนไขเข้มงวด (เช่น ADX > 25, ราคา > VWAP) ทำให้สัญญาณ BUY/SELL มีความน่าเชื่อถือมากขึ้น
การจัดการความเสี่ยงที่ดีขึ้น:Trailing Stop แบบ Dynamic ช่วยล็อกกำไรในแนวโน้มยาว
SL/TP ที่ปรับตาม ATR และ timeframe ทำให้เหมาะกับความผันผวน
เหมาะกับ timeframe ยาว:การตั้งค่า SL ที่กว้างใน 4h, 8h, 1d เหมาะกับการจับแนวโน้มใหญ่ เช่น การเคลื่อนไหวของ BTC/USDT ในรอบ 1-2 วัน
ข้อจำกัดและแนวทางแก้ไขSL ที่กว้างเกินไปใน 8h, 1d:SL=5.8 (8h) และ 6.8 (1d) อาจทำให้ขาดทุนหนักในช่วงที่แนวโน้มพลิกกลับเร็ว
แก้ไข: เพิ่มเงื่อนไข Exit เมื่อ MACD Histogram เปลี่ยนทิศทาง:python
if position['type'] == 'Long' and last_row['macd_hist'] < 0:
close_reason = "MACD Reversal"
close_position = True
สัญญาณใน timeframe สั้นอาจยังถี่เกินไป:ใน 1m, 5m สัญญาณอาจเกิดบ่อยในช่วงผันผวนต่ำ
แก้ไข: เพิ่มเงื่อนไข ATR < 0.8 เพื่อข้ามการเทรด:python
if last_row['atr'] < 0.8 and timeframe in ['1m', '5m']:
signals.append('LOW_VOLATILITY')
signal_desc.append("ATR < 0.8 (Low Volatility)")
RRR ต่ำใน timeframe ยาว:RRR ใน 1d (SL=6.8, TP=4.5, RRR=1:0.66) อาจไม่คุ้มในบางสภาวะ
แก้ไข: ใช้ Trailing Stop เป็นหลักและขยาย TP ในบางกรณีตาม ADX:python
if last_row['adx'] > 30:
tp_price = entry_price + (atr * multipliers['tp'] * 1.5) # ขยาย TP ในแนวโน้มแข็งแกร่ง