Trend Following Strategy
Trend Following Strategy เทรดตามเทรนด์ Moving Average Donchian ATR ADX Entry Exit Stop Loss Position Sizing Risk Management
| Indicator | Purpose | Setting | Signal |
|---|---|---|---|
| EMA Crossover | Trend Direction | EMA 20/50 | 20 ตัดขึ้น 50 = Buy |
| Donchian Channel | Breakout | 20 วัน | ทะลุ High 20 = Buy |
| ATR | Volatility/Stop | 14 วัน | Stop = 2 × ATR |
| ADX | Trend Strength | 14 วัน | > 25 = Trend แข็ง |
| Supertrend | Trend + Stop | 10, 3 | เขียว = Buy แดง = Sell |
| MACD | Momentum | 12,26,9 | MACD > Signal = Buy |
Trading System
# === Trend Following Trading System ===
from dataclasses import dataclass
@dataclass
class TradeRule:
rule: str
indicator: str
condition: str
action: str
entry_rules = [
TradeRule("Entry Long",
"EMA 20 + EMA 50 + ADX",
"EMA 20 > EMA 50 AND ADX > 25",
"Buy at Market | Stop = Entry - 2×ATR"),
TradeRule("Entry Long (Breakout)",
"Donchian Channel 20",
"Price > Highest High 20 Days AND Volume > Avg",
"Buy at Breakout | Stop = Lowest Low 10 Days"),
TradeRule("Entry Long (Pullback)",
"EMA 20 + Price Action",
"Uptrend (EMA 20 > 50) AND Price touches EMA 20",
"Buy at EMA 20 Bounce | Stop = Below EMA 50"),
TradeRule("Exit Long (Trailing)",
"ATR Trailing Stop",
"Price < Entry + Highest Since Entry - 2×ATR",
"Sell at Market | Lock Profit"),
TradeRule("Exit Long (MA Cross)",
"EMA 20 + EMA 50",
"EMA 20 < EMA 50",
"Sell at Market | Trend Reversed"),
TradeRule("Stop Loss",
"ATR 14",
"Price < Entry - 2×ATR",
"Sell at Stop | Max Loss 1-2% Account"),
]
print("=== Trading Rules ===")
for r in entry_rules:
print(f" [{r.rule}] {r.indicator}")
print(f" Condition: {r.condition}")
print(f" Action: {r.action}")
# Position Sizing Formula:
# Risk Per Trade = Account * Risk% (1-2%)
# Position Size = Risk Per Trade / (Entry Price - Stop Loss)
#
# Example:
# Account = 1,000,000 THB
# Risk = 1% = 10,000 THB
# Entry = 100 THB
# Stop = 95 THB (ATR-based)
# Position = 10,000 / (100-95) = 2,000 shares
# Total Position Value = 2,000 * 100 = 200,000 THB (20% of account)
Backtest Results
# === Backtest Framework ===
# import pandas as pd
# import numpy as np
#
# def ema_crossover_strategy(df, fast=20, slow=50, atr_period=14, atr_mult=2):
# df['ema_fast'] = df['close'].ewm(span=fast).mean()
# df['ema_slow'] = df['close'].ewm(span=slow).mean()
# df['atr'] = calculate_atr(df, atr_period)
#
# # Signals
# df['signal'] = 0
# df.loc[(df['ema_fast'] > df['ema_slow']) &
# (df['ema_fast'].shift(1) <= df['ema_slow'].shift(1)), 'signal'] = 1 # Buy
# df.loc[(df['ema_fast'] < df['ema_slow']) &
# (df['ema_fast'].shift(1) >= df['ema_slow'].shift(1)), 'signal'] = -1 # Sell
#
# return df
@dataclass
class BacktestResult:
strategy: str
market: str
period: str
win_rate: str
avg_win: str
avg_loss: str
profit_factor: str
max_drawdown: str
cagr: str
results = [
BacktestResult("EMA 20/50 Crossover",
"SET Index (Thailand)",
"2010-2024 (14 ปี)",
"35%",
"+8.5% ต่อ Trade",
"-2.1% ต่อ Trade",
"1.8",
"-22%",
"+12% ต่อปี"),
BacktestResult("Donchian 20 Breakout",
"S&P 500 (USA)",
"2000-2024 (24 ปี)",
"38%",
"+12% ต่อ Trade",
"-3% ต่อ Trade",
"2.1",
"-25%",
"+15% ต่อปี"),
BacktestResult("Supertrend (10,3)",
"BTC/USDT (Crypto)",
"2017-2024 (7 ปี)",
"32%",
"+25% ต่อ Trade",
"-5% ต่อ Trade",
"2.5",
"-35%",
"+45% ต่อปี (volatile)"),
BacktestResult("Turtle Trading (Donchian 20/10)",
"Multi-market (Commodity Forex Stock)",
"1983-2024 (40+ ปี)",
"40%",
"+10% ต่อ Trade",
"-2.5% ต่อ Trade",
"2.0",
"-30%",
"+20% ต่อปี (diversified)"),
]
print("=== Backtest Results ===")
for r in results:
print(f"\n [{r.strategy}] {r.market}")
print(f" Period: {r.period}")
print(f" Win Rate: {r.win_rate} | Avg Win: {r.avg_win} | Avg Loss: {r.avg_loss}")
print(f" PF: {r.profit_factor} | Max DD: {r.max_drawdown} | CAGR: {r.cagr}")
Risk Management
# === Risk Management Framework ===
@dataclass
class RiskRule:
rule: str
parameter: str
value: str
rationale: str
risk_rules = [
RiskRule("Risk Per Trade",
"Max Loss per Trade",
"1-2% ของ Account",
"ขาดทุน 10 ครั้งติด เสีย 10-20% ยังเทรดต่อได้"),
RiskRule("Total Open Risk",
"Max Total Risk ทุก Position รวม",
"6-10% ของ Account",
"ป้องกัน Correlation Risk ทุก Position ขาดทุนพร้อมกัน"),
RiskRule("Position Size",
"จำนวน Lot/หุ้น ต่อ Trade",
"Account × Risk% / (Entry - Stop)",
"ขนาด Position ตาม Volatility ATR-based"),
RiskRule("Stop Loss",
"จุด Cut Loss",
"Entry - 2×ATR (ไม่เลื่อนลง)",
"ตัดขาดทุนเร็ว ป้องกัน Loss ใหญ่"),
RiskRule("Trailing Stop",
"จุด Lock Profit",
"Highest Since Entry - 2×ATR (เลื่อนขึ้นอย่างเดียว)",
"ให้กำไรวิ่ง ล็อกกำไรเมื่อ Trend กลับ"),
RiskRule("Max Drawdown",
"Drawdown ที่ยอมรับ",
"20-30% (ลด Size ที่ 20% หยุดที่ 30%)",
"ป้องกัน Ruin ถ้า Drawdown > ที่ตั้ง Review Strategy"),
RiskRule("Diversification",
"จำนวนตลาด/สินทรัพย์",
"3-5 ตลาด Low Correlation",
"ลด Risk จาก 1 ตลาด ใช้ Multi-market Portfolio"),
]
print("=== Risk Management Rules ===")
for r in risk_rules:
print(f" [{r.rule}] {r.parameter}")
print(f" Value: {r.value}")
print(f" Why: {r.rationale}")
เคล็ดลับ
- ADX: เช็ค ADX > 25 ก่อนเข้า ไม่เทรดใน Sideways
- ATR Stop: ใช้ 2×ATR Stop Loss ทุก Trade ไม่เลื่อนลง
- Win Rate: ยอมรับ Win Rate 30-40% Average Win ต้องมากกว่า Loss
- Journal: บันทึกทุก Trade ทำไมเข้า ทำไมออก เรียนรู้ย้อนหลัง
- Backtest: Backtest ก่อนใช้เงินจริง อย่างน้อย 5 ปีข้อมูล
Trend Following คืออะไร
เทรดตาม Trend Buy Uptrend Sell Downtrend Cut Loss Let Profit Run Win Rate 30-40% Average Win > Loss Turtle Traders
Indicators ที่ใช้มีอะไร
EMA SMA Crossover Donchian Channel ATR ADX Supertrend MACD Golden Cross Death Cross Breakout Volatility Trend Strength
Entry/Exit Rules เป็นอย่างไร
EMA Cross ADX > 25 Donchian Breakout Pullback ATR Trailing Stop MA Cross Exit Stop Loss 2×ATR Position Sizing 1-2% Risk
Risk Management ทำอย่างไร
Risk 1-2% Total 6-10% ATR Stop Trailing Stop Drawdown 20-30% Diversification Multi-market Journal Backtest Psychology
สรุป
Trend Following Strategy EMA Donchian ATR ADX Supertrend Entry Exit Stop Loss Trailing Position Sizing Risk Management Backtest
