Chiến lược giao dịch thích ứng theo dõi xu hướng trung bình động kép và kiểm soát rủi ro ATR

SMA ATR TP SL HTF
Ngày tạo: 2024-11-29 14:56:43 sửa đổi lần cuối: 2024-11-29 14:56:43
sao chép: 0 Số nhấp chuột: 522
1
tập trung vào
1617
Người theo dõi

Chiến lược giao dịch thích ứng theo dõi xu hướng trung bình động kép và kiểm soát rủi ro ATR

Tổng quan

Chiến lược này là một hệ thống giao dịch tự điều chỉnh kết hợp theo dõi xu hướng hai đường cong cổ điển và kiểm soát rủi ro động ATR. Chiến lược cung cấp hai mô hình giao dịch: mô hình cơ bản sử dụng giao dịch chéo hai đường cong đơn giản để theo dõi xu hướng, mô hình cao cấp thêm bộ lọc xu hướng cho khung thời gian cao hơn và cơ chế dừng lỗ động dựa trên ATR. Chiến lược có thể chuyển đổi giữa hai mô hình thông qua menu kéo xuống đơn giản, vừa chăm sóc sự dễ sử dụng của người mới học, vừa đáp ứng nhu cầu kiểm soát rủi ro của người kinh nghiệm.

Nguyên tắc chiến lược

Chiến lược 1 ((mô hình cơ bản) sử dụng hệ thống hai đường trung bình 21 và 49 ngày, tạo ra nhiều tín hiệu khi đường trung bình nhanh vượt qua đường trung bình chậm. Mục tiêu lợi nhuận có thể chọn phương thức phần trăm hoặc số điểm, đồng thời cung cấp chức năng dừng lỗ di động có thể chọn để khóa lợi nhuận. Chiến lược 2 ((mô hình nâng cao) dựa trên hệ thống hai đường trung bình, thêm bộ lọc xu hướng ở cấp độ ngày, chỉ cho phép tham gia khi giá nằm trên đường trung bình khung thời gian cao hơn.

Lợi thế chiến lược

  1. Chiến lược có khả năng thích ứng mạnh mẽ, có thể chuyển đổi một cách linh hoạt tùy thuộc vào mức độ kinh nghiệm của nhà giao dịch và môi trường thị trường
  2. Phân tích nhiều khung thời gian trong chế độ cao cấp cải thiện chất lượng tín hiệu
  3. ATR có thể thích ứng với các điều kiện thị trường khác nhau
  4. Một số cơ chế lợi nhuận cân bằng giữa bảo vệ lợi nhuận và duy trì xu hướng
  5. Thiết lập tham số linh hoạt để tối ưu hóa phù hợp với các đặc điểm thị trường khác nhau

Rủi ro chiến lược

  1. Hệ thống hai đường đều có thể tạo ra các tín hiệu sai thường xuyên trong thị trường chấn động
  2. Trình lọc xu hướng có thể làm cho tín hiệu bị chậm và bỏ lỡ cơ hội giao dịch.
  3. ATR dừng có thể không kịp thời khi biến động tỷ lệ
  4. Một số lợi nhuận có thể giảm vị thế sớm, ảnh hưởng đến lợi nhuận xu hướng

Hướng tối ưu hóa chiến lược

  1. Có thể làm tăng số lượng giao dịch và dao động chỉ số lọc tín hiệu giả
  2. Xem xét việc giới thiệu cơ chế tự điều chỉnh các tham số động, tự động điều chỉnh chu kỳ trung bình theo tình trạng thị trường
  3. Tối ưu hóa chu kỳ tính toán ATR, cân bằng độ nhạy và độ ổn định
  4. Thêm mô-đun nhận dạng trạng thái thị trường, tự động chọn mô hình chiến lược tối ưu
  5. Thêm nhiều lựa chọn để dừng lỗ, chẳng hạn như dừng theo dõi, dừng thời gian.

Tóm tắt

Đây là một hệ thống chiến lược giao dịch được thiết kế hợp lý và đầy đủ chức năng. Bằng cách kết hợp theo dõi xu hướng hai đường thẳng và kiểm soát ATR, nó đảm bảo độ tin cậy của chiến lược và cung cấp quản lý rủi ro tốt. Thiết kế hai mô hình đáp ứng nhu cầu của các nhà giao dịch ở nhiều cấp độ khác nhau, thiết lập tham số phong phú cung cấp không gian tối ưu hóa đầy đủ.

Mã nguồn chiến lược
/*backtest
start: 2019-12-23 08:00:00
end: 2024-11-27 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © shaashish1

//@version=5
strategy("Dual Strategy Selector V2 - Cryptogyani", overlay=true, pyramiding=0, 
         default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=100000)

//#region STRATEGY SELECTION
strategyOptions = input.string(title="Select Strategy", defval="Strategy 1", options=["Strategy 1", "Strategy 2"], group="Strategy Selection")
//#endregion STRATEGY SELECTION

// ####################### STRATEGY 1: Original Logic ########################
//#region STRATEGY 1 INPUTS
s1_fastMALen = input.int(defval=21, title="Fast SMA Length (S1)", minval=1, group="Strategy 1 Settings", inline="S1 MA")
s1_slowMALen = input.int(defval=49, title="Slow SMA Length (S1)", minval=1, group="Strategy 1 Settings", inline="S1 MA")
s1_takeProfitMode = input.string(defval="Percentage", title="Take Profit Mode (S1)", options=["Percentage", "Pips"], group="Strategy 1 Settings")
s1_takeProfitPerc = input.float(defval=7.0, title="Take Profit % (S1)", minval=0.05, step=0.05, group="Strategy 1 Settings") / 100
s1_takeProfitPips = input.float(defval=50, title="Take Profit Pips (S1)", minval=1, step=1, group="Strategy 1 Settings")
s1_trailingTakeProfitEnabled = input.bool(defval=false, title="Enable Trailing (S1)", group="Strategy 1 Settings")
//#endregion STRATEGY 1 INPUTS

// ####################### STRATEGY 2: Enhanced with Recommendations ########################
//#region STRATEGY 2 INPUTS
s2_fastMALen = input.int(defval=20, title="Fast SMA Length (S2)", minval=1, group="Strategy 2 Settings", inline="S2 MA")
s2_slowMALen = input.int(defval=50, title="Slow SMA Length (S2)", minval=1, group="Strategy 2 Settings", inline="S2 MA")
s2_atrLength = input.int(defval=14, title="ATR Length (S2)", group="Strategy 2 Settings", inline="ATR")
s2_atrMultiplier = input.float(defval=1.5, title="ATR Multiplier for Stop-Loss (S2)", group="Strategy 2 Settings", inline="ATR")
s2_partialTakeProfitPerc = input.float(defval=50.0, title="Partial Take Profit % (S2)", minval=10, maxval=100, step=10, group="Strategy 2 Settings")
s2_timeframeTrend = input.timeframe(defval="1D", title="Higher Timeframe for Trend Filter (S2)", group="Strategy 2 Settings")
//#endregion STRATEGY 2 INPUTS

// ####################### GLOBAL VARIABLES ########################
var float takeProfitPrice = na
var float stopLossPrice = na
var float trailingStopPrice = na
var float fastMA = na
var float slowMA = na
var float higherTimeframeTrendMA = na
var bool validOpenLongPosition = false

// Precalculate higher timeframe values (global scope for Strategy 2)
higherTimeframeTrendMA := request.security(syminfo.tickerid, s2_timeframeTrend, ta.sma(close, s2_slowMALen))

// ####################### LOGIC ########################
if (strategyOptions == "Strategy 1")
    // Strategy 1 Logic (Original Logic Preserved)
    fastMA := ta.sma(close, s1_fastMALen)
    slowMA := ta.sma(close, s1_slowMALen)
    openLongPosition = ta.crossover(fastMA, slowMA)
    validOpenLongPosition := openLongPosition and strategy.opentrades.size(strategy.opentrades - 1) == 0
    
    // Take Profit Price
    takeProfitPrice := if (s1_takeProfitMode == "Percentage")
        close * (1 + s1_takeProfitPerc)
    else
        close + (s1_takeProfitPips * syminfo.mintick)

    // Trailing Stop Price (if enabled)
    if (strategy.position_size > 0 and s1_trailingTakeProfitEnabled)
        trailingStopPrice := high - (s1_takeProfitPips * syminfo.mintick)
    else
        trailingStopPrice := na

else if (strategyOptions == "Strategy 2")
    // Strategy 2 Logic with Recommendations
    fastMA := ta.sma(close, s2_fastMALen)
    slowMA := ta.sma(close, s2_slowMALen)
    openLongPosition = ta.crossover(fastMA, slowMA) and close > higherTimeframeTrendMA
    validOpenLongPosition := openLongPosition and strategy.opentrades.size(strategy.opentrades - 1) == 0

    // ATR-Based Stop-Loss
    atr = ta.atr(s2_atrLength)
    stopLossPrice := close - (atr * s2_atrMultiplier)

    // Partial Take Profit Logic
    takeProfitPrice := close * (1 + (s2_partialTakeProfitPerc / 100))
//#endregion STRATEGY LOGIC

// ####################### PLOTTING ########################
plot(series=fastMA, title="Fast SMA", color=color.yellow, linewidth=1)
plot(series=slowMA, title="Slow SMA", color=color.orange, linewidth=1)
plot(series=takeProfitPrice, title="Take Profit Price", color=color.teal, linewidth=1, style=plot.style_linebr)

// Trailing Stop and ATR Stop-Loss Plots (Global Scope)
plot(series=(strategyOptions == "Strategy 1" and s1_trailingTakeProfitEnabled) ? trailingStopPrice : na, title="Trailing Stop", color=color.red, linewidth=1, style=plot.style_linebr)
plot(series=(strategyOptions == "Strategy 2") ? stopLossPrice : na, title="ATR Stop-Loss", color=color.red, linewidth=1, style=plot.style_linebr)
//#endregion PLOTTING

// ####################### POSITION ORDERS ########################
//#region POSITION ORDERS
if (validOpenLongPosition)
    strategy.entry(id="Long Entry", direction=strategy.long)

if (strategyOptions == "Strategy 1")
    if (strategy.position_size > 0)
        if (s1_trailingTakeProfitEnabled)
            strategy.exit(id="Trailing Take Profit", from_entry="Long Entry", stop=trailingStopPrice)
        else
            strategy.exit(id="Take Profit", from_entry="Long Entry", limit=takeProfitPrice)

else if (strategyOptions == "Strategy 2")
    if (strategy.position_size > 0)
        strategy.exit(id="Partial Take Profit", from_entry="Long Entry", qty_percent=s2_partialTakeProfitPerc, limit=takeProfitPrice)
        strategy.exit(id="Stop Loss", from_entry="Long Entry", stop=stopLossPrice)
//#endregion POSITION ORDERS