
Chiến lược này là một hệ thống theo dõi xu hướng dựa trên đường chéo và dừng động. Lập luận cốt lõi là bắt điểm bắt đầu xu hướng tăng bằng cách sử dụng đường trung bình nhanh ((EMA5) và đường trung bình chậm ((EMA200) và kết hợp với dừng động ATR để bảo vệ lợi nhuận. Chiến lược cũng đặt mục tiêu dừng cố định phần trăm để cân bằng lợi nhuận rủi ro.
Chiến lược này hoạt động dựa trên các cơ chế cốt lõi sau:
Đây là một chiến lược theo dõi xu hướng kết hợp các chỉ số kỹ thuật cổ điển với quản lý rủi ro hiện đại. Nó hoạt động tốt trong thị trường xu hướng bằng cách nắm bắt xu hướng bằng đường chéo đồng nhất, sử dụng ATR động để bảo vệ lợi nhuận. Mặc dù có một số rủi ro tín hiệu giả, nhưng sự ổn định của chiến lược có thể được nâng cao đáng kể bằng cách tối ưu hóa tham số và thêm bộ lọc.
/*backtest
start: 2024-02-21 00:00:00
end: 2025-02-18 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Binance","currency":"ETH_USDT"}]
*/
// -----------------------------------------------------------
// Title: EMA5 Cross-Up EMA200 with ATR Trailing Stop & Take-Profit
// Author: ChatGPT
// Version: 1.1 (Pine Script v6)
// Notes: Enter Long when EMA(5) crosses above EMA(200).
// Exit on either ATR-based trailing stop or
// specified % Take-Profit.
// -----------------------------------------------------------
//@version=6
strategy(title="EMA5 Cross-Up EMA200 ATR Stop", shorttitle="EMA5x200_ATRStop_v6", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity,default_qty_value=100)
// -- 1) Inputs
emaFastLength = input.int(5, "Fast EMA Length")
emaSlowLength = input.int(200, "Slow EMA Length")
atrPeriod = input.int(14, "ATR Period")
atrMult = input.float(2.0,"ATR Multiplier", step=0.1)
takeProfitPerc = input.float(5.0,"Take-Profit %", step=0.1)
// -- 2) Indicator Calculations
emaFast = ta.ema(close, emaFastLength)
emaSlow = ta.ema(close, emaSlowLength)
atrValue = ta.atr(atrPeriod)
// -- 3) Entry Condition: EMA5 crosses above EMA200
emaCrossUp = ta.crossover(emaFast, emaSlow)
// -- 4) Determine a dynamic ATR-based stop loss (for trailing)
longStopPrice = close - (atrValue * atrMult)
// -- 5) Take-Profit Price
// We store it in a variable so we can update it when in position.
var float takeProfitPrice = na
var float avgEntryPrice = na
if strategy.position_size > 0
// If there is an open long, get the average fill price:
avgEntryPrice := strategy.position_avg_price
takeProfitPrice := avgEntryPrice * (1 + takeProfitPerc / 100)
else
// If no open position, reset
takeProfitPrice := na
avgEntryPrice := na
// -- 6) Submit Entry Order
if emaCrossUp
strategy.entry(id="Long", direction=strategy.long)
// -- 7) Submit Exit Orders (Stop or Take-Profit)
strategy.exit(id = "Exit Long",stop = longStopPrice,limit = takeProfitPrice)
// -- 8) (Optional) Plotting for Visuals
plot(emaFast, color=color.new(color.yellow, 0), linewidth=2, title="EMA Fast")
plot(emaSlow, color=color.new(color.blue, 0), linewidth=2, title="EMA Slow")
plot(longStopPrice, color=color.red, linewidth=2, title="ATR Trailing Stop")