Backtesting Trên TradingView — Cách Test Strategy Đúng
Backtesting là quá trình test strategy trên historical data để đánh giá performance. Đây là step BẮT BUỘC trước khi deploy live. Tutorial này hướng dẫn pakai Strategy Tester của TradingView đúng cách, tránh overfitting, và interpret results.
⚠️ Câu Hỏi Quan Trọng
“Strategy này có profitable trong past 3-5 năm không?” — Không backtest = không có câu trả lời. Trader skip backtest đa phần lose money. Backtest tốt = filter strategies bad trước khi mất real money.
Convert Indicator → Strategy Pine Script
Indicators chỉ plot signals. Strategies có thể được tested với Strategy Tester. Conversion đơn giản:
//@version=5
strategy("RSI Strategy Test",
overlay=true,
initial_capital=10000,
default_qty_type=strategy.percent_of_equity,
default_qty_value=2, // 2% per trade
commission_type=strategy.commission.percent,
commission_value=0.05, // 0.05% commission
slippage=2) // 2 ticks slippage
// === INPUTS ===
rsiLen = input.int(14, "RSI Length")
osLevel = input.int(30, "Oversold")
obLevel = input.int(70, "Overbought")
// === CALCULATIONS ===
rsi = ta.rsi(close, rsiLen)
// === SIGNALS ===
buySignal = ta.crossover(rsi, osLevel)
sellSignal = ta.crossunder(rsi, obLevel)
// === STRATEGY ORDERS ===
if (buySignal)
strategy.entry("Long", strategy.long)
if (sellSignal)
strategy.close("Long")
// === STOP LOSS / TAKE PROFIT ===
if strategy.position_size > 0
strategy.exit("Exit", "Long",
stop=strategy.position_avg_price * 0.98, // 2% SL
limit=strategy.position_avg_price * 1.04) // 4% TP
Run Backtest Trên TradingView
- Mở chart asset bạn want test (V75 hoặc EUR/USD)
- Click tab “Pine Editor” ở bottom panel
- Paste strategy code trên
- Save (Ctrl+S), đặt tên “RSI Strategy Test”
- Click “Add to chart”
- Tab “Strategy Tester” ở bottom panel — results hiển thị tự động
Strategy Tester Metrics — Cách Đọc
| Metric | Ý Nghĩa | Good Threshold |
|---|---|---|
| Net Profit | Tổng lợi nhuận sau commission | Positive |
| Total Trades | Số trade đã thực hiện | 30+ (significance) |
| Win Rate | % trades winning | > 55% |
| Profit Factor | Total wins ÷ Total losses | > 1.5 |
| Sharpe Ratio | Risk-adjusted return | > 1.0 |
| Max Drawdown | % biggest peak-to-trough loss | < 20% |
| Avg Win/Loss | Avg win ÷ Avg loss ratio | > 1.2 |
| Largest Loss | Biggest single trade loss | < 2x avg loss |
✓ Strategy “Pass” Criteria
Strategy passable nếu ALL:
1. Profit Factor > 1.5
2. Max Drawdown < 20%
3. Win Rate > 55% (hoặc R:R ratio > 1:2 với WR thấp hơn)
4. Total trades > 30 (preferably 100+)
5. Sharpe Ratio > 1.0
Nếu pass tất cả → consider for demo testing. Nếu fail 1+ → iterate strategy.
Overfitting — Kẻ Thù Số 1
Overfitting là strategy quá fit historical data nhưng fail trên live. Signs:
🚩 Overfitting Red Flags
1. Win rate > 80% (impossible long-term)
2. Max DD < 5% (unrealistic)
3. Profit factor > 5 (suspicious)
4. Curve perfect smooth equity (không có drawdowns)
5. 10+ parameters optimized (over-engineered)
6. Strategy fails khi đổi 1 parameter chút (fragile)
7. Backtest period quá ngắn (only 1-3 tháng)
Nếu có 2+ flags → strategy overfit, sẽ fail forward.
Cách Tránh Overfitting
1. Walk-Forward Analysis
Test strategy trên multiple time periods:
- In-sample (training): Optimize parameters trên 70% data (ví dụ 2023-2024)
- Out-of-sample (testing): Test optimized strategy trên 30% data (2025-2026)
- Performance similar? Strategy robust. Big drop? Overfit.
2. Multiple Assets Test
Strategy work tốt V75? Test cả EUR/USD, BTC, V100. Nếu work trên ≥3 assets → robust. Chỉ work 1 asset → có khả năng coincidence.
3. Multiple Timeframes
Strategy profitable M5? Test M15, H1, H4. Robust strategy thường work cross-timeframe (với adjusted parameters).
4. Parameter Sensitivity
Optimal RSI period = 14? Test RSI 12, 16, 18, 20. Strategy robust = results similar. Big variance = overfit.
5. Limit Parameters
- Max 3 parameters để optimize
- Mỗi parameter phải có logical justification
- Avoid “data mining” — chọn parameters dựa logic, không dựa best historical
Backtest Data Quality
TradingView free tier có data limitations:
- Lookback bars: 5000-20000 candles (depends timeframe)
- M5 candles: ~70 ngày history
- H1 candles: ~2 năm history
- D1 candles: 10+ năm history
Recommend: Test cả multiple market regimes — bull market, bear market, sideways. Backtest period càng đa dạng càng better.
Common Backtesting Mistakes
⚠️ Avoid These Mistakes
1. Look-ahead bias: Pakai data future trong calculations (Pine Script auto-prevents này)
2. Survivorship bias: Test chỉ assets vẫn exist (delisted stocks ignored)
3. Optimism bias: Backtest không include slippage, commission realistic
4. Cherry-picking: Test 10 periods, report best 1
5. Small sample: 5 trades = không significant
6. Ignoring drawdowns: Focus chỉ Net Profit, ignore drawdown psychology
7. No real costs: Forgot spread, swap fees, slippage
Realistic Backtest Settings
- Commission: 0.05% per trade (typical broker fee)
- Slippage: 1-3 ticks (executed worse than expected price)
- Initial capital: $1,000-10,000 (test realistic)
- Position size: 2% per trade (max safe)
- Currency: Match broker (USD usually)
TradingView Strategy parameters trong code (đã set ở example trên).
From Backtest → Live: Reality Adjustment
Demo và backtest thường tốt hơn live:
| Metric | Backtest | Live Expected |
|---|---|---|
| Win Rate | 65% | 58-62% |
| Profit Factor | 2.0 | 1.4-1.7 |
| Max Drawdown | 15% | 20-25% |
| Net Return | +25% | +15-20% |
Strategy backtest 25% return = expect 15-20% live realistic. Bỏ qua slippage, latency, psychology gap.
Workflow Backtesting Đề Xuất
- Idea generation: Pakai AI prompt #2
- Code strategy: Pine Script v5
- Initial backtest: 6 tháng data
- If pass criteria: Extend backtest 1-2 năm
- Walk-forward: 70/30 split test
- Cross-asset test: Try 3+ assets
- Sensitivity test: Vary parameters
- If still robust: Deploy demo 30 ngày
- If demo profitable: Live với stake nhỏ
- Monitor & iterate: Always
Sanity Check — Trade Manually 10 Signals
Sau khi automated backtest, làm manual sanity check:
- List 10 signals từ strategy
- Verify chart manually — signal makes sense?
- Check: entry timing, exit timing reasonable?
- Look at trades làm sai — pattern xác định?
- Logic flaw nào emerges?
Sanity check often reveals backtest bugs mà automated tester miss.
🚀 Sau backtest pass, test live trên Deriv demo (real conditions):
Mở Demo Deriv Miễn PhíBài Liên Quan
- TradingView Free Setup
- ChatGPT & Claude cho Indicators
- Risk Management Bots
- Real Test Backtest → Live
