
Chiến lược này là một hệ thống giao dịch hoàn chỉnh kết hợp phân tích nhiều khung thời gian, theo dõi xu hướng và quản lý vị trí động. Chiến lược sử dụng EMA làm chỉ số xu hướng chính, MACD làm chỉ số xác nhận cấp hai, đồng thời kết hợp với ATR để kiểm soát rủi ro và thiết lập dừng lỗ.
Chiến lược này được thiết kế theo cách thức phân tầng, bao gồm các thành phần cốt lõi sau:
Chiến lược này xây dựng một hệ thống giao dịch theo dõi xu hướng hoàn chỉnh thông qua phân tích nhiều khung thời gian và quản lý vị trí động. Ưu điểm của chiến lược là tư duy thiết kế có hệ thống và cơ chế kiểm soát rủi ro tốt, nhưng cũng cần chú ý đến vấn đề thích ứng với môi trường thị trường và tối ưu hóa tham số.
/*backtest
start: 2024-02-22 00:00:00
end: 2025-02-19 08:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"ETH_USDT"}]
*/
//@version=6
strategy('Optimized Trend Strategy', overlay = true, initial_capital = 10000, default_qty_type = strategy.cash, default_qty_value = 50, commission_value = 0.1)
// 🟢 核心指標
ema7 = ta.ema(close, 7)
ema90 = ta.ema(close, 90)
atr = ta.atr(14)
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)
// 🟢 8 小時多時間框架確認
h8Close = request.security(syminfo.tickerid, '480', close)
h8Volume = request.security(syminfo.tickerid, '480', volume)
h8Ema7 = ta.ema(h8Close, 7)
h8Signal = h8Close > h8Ema7 and h8Volume > ta.sma(h8Volume, 50)
// 🟢 動態風控
stopLoss = close - 1.5 * atr
takeProfit = close + 3 * atr
// 🟢 交易信號
longCondition = close > ema7 and ema7 > ema90 and ta.crossover(macdLine, signalLine) and h8Signal
shortCondition = close < ema7 and ema7 < ema90 and ta.crossunder(macdLine, signalLine) and h8Signal
// 🟢 倉位管理(根據趨勢強度)
trendStrength = (ema7 - ema90) / (atr / close)
var float positionSize = na
if trendStrength > 2
positionSize := strategy.equity * 0.7 / close
positionSize
else if trendStrength < 0.5
positionSize := strategy.equity * 0.3 / close
positionSize
else
positionSize := strategy.equity * 0.5 / close
positionSize
// 🟢 訂單執行
if longCondition
strategy.entry('Long', strategy.long, qty = positionSize)
strategy.exit('Long Exit', from_entry = 'Long', stop = stopLoss, limit = takeProfit)
if shortCondition
strategy.entry('Short', strategy.short, qty = positionSize)
strategy.exit('Short Exit', from_entry = 'Short', stop = stopLoss, limit = takeProfit)