Chiến lược theo dõi xu hướng trung bình động và lọc biến động động

EMA TR ATR
Ngày tạo: 2025-01-17 15:00:37 sửa đổi lần cuối: 2025-01-17 15:00:37
sao chép: 3 Số nhấp chuột: 303
1
tập trung vào
1617
Người theo dõi

Chiến lược theo dõi xu hướng trung bình động và lọc biến động động

Tổng quan

Chiến lược này là một hệ thống giao dịch thông minh kết hợp giữa việc theo dõi xu hướng và lọc biến động. Công cụ này xác định xu hướng thị trường thông qua Đường trung bình động theo hàm mũ (EMA), sử dụng Phạm vi thực (TR) và bộ lọc biến động động để xác định thời điểm vào lệnh và quản lý rủi ro bằng cơ chế dừng lỗ và dừng lãi động dựa trên biến động. Chiến lược này hỗ trợ hai chế độ giao dịch: Scalp và Swing, có thể linh hoạt chuyển đổi tùy theo môi trường thị trường và phong cách giao dịch khác nhau.

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

Logic cốt lõi của chiến lược bao gồm các thành phần chính sau:

  1. Xác định xu hướng: Sử dụng EMA 50 kỳ làm bộ lọc xu hướng và chỉ mua vào khi giá cao hơn EMA và bán ra khi giá thấp hơn EMA.
  2. Lọc độ biến động: Tính toán EMA của Phạm vi thực (TR) và sử dụng hệ số lọc có thể điều chỉnh (mặc định là 1,5) để lọc bỏ nhiễu thị trường.
  3. Điều kiện vào lệnh: Kết hợp với phân tích hình thái của ba đường K liên tiếp, chuyển động giá phải liên tục và tăng tốc.
  4. Chốt lời và dừng lỗ: Ở chế độ ngắn hạn, lệnh được thiết lập dựa trên TR hiện tại; ở chế độ dải, lệnh được thiết lập dựa trên điểm cao và thấp trước đó để đạt được mục tiêu quản lý rủi ro năng động.

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

  1. Khả năng thích ứng mạnh mẽ: Thông qua sự kết hợp giữa tính năng lọc biến động động và theo dõi xu hướng, nó có thể thích ứng với nhiều môi trường thị trường khác nhau.
  2. Quản lý rủi ro hoàn hảo: Cung cấp cơ chế dừng lỗ và dừng lãi năng động cho hai chế độ giao dịch, có thể linh hoạt lựa chọn theo đặc điểm thị trường.
  3. Khả năng điều chỉnh tham số tốt: các tham số chính như hệ số lọc, chu kỳ xu hướng, v.v. có thể được tối ưu hóa theo đặc điểm của sản phẩm giao dịch.
  4. Hiệu ứng trực quan tốt: Cung cấp tín hiệu mua và bán rõ ràng cùng màn hình hiển thị vị trí dừng lãi và dừng lỗ để dễ dàng theo dõi giao dịch.

Rủi ro chiến lược

  1. Rủi ro đảo ngược xu hướng: có thể xảy ra tình trạng dừng liên tiếp tại các điểm chuyển hướng xu hướng.
  2. Rủi ro đột phá sai: Tín hiệu sai có thể xuất hiện khi độ biến động đột ngột tăng.
  3. Độ nhạy tham số: Việc thiết lập hệ số bộ lọc không đúng cách có thể dẫn đến tín hiệu quá nhiều hoặc quá ít.
  4. Tác động của trượt giá: Trong một thị trường biến động nhanh, bạn có thể phải đối mặt với sự trượt giá lớn, điều này có thể ảnh hưởng đến hiệu quả chiến lược của bạn.

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

  1. Thêm tính năng lọc cường độ xu hướng: Có thể đưa các chỉ báo như ADX vào để đánh giá cường độ xu hướng và cải thiện hiệu ứng theo dõi xu hướng.
  2. Tối ưu hóa mức chốt lời và dừng lỗ: Cân nhắc áp dụng mức dừng lỗ động để bảo vệ nhiều lợi nhuận hơn.
  3. Cải thiện mô hình giao dịch theo xu hướng: có thể thêm nhiều điều kiện đánh giá cụ thể cho giao dịch theo xu hướng để cải thiện khả năng nắm giữ trung và dài hạn.
  4. Thêm phân tích khối lượng: Kết hợp các thay đổi về khối lượng để xác nhận tính hợp lệ của đột phá.

Tóm tắt

Chiến lược này xây dựng một hệ thống giao dịch hoàn chỉnh bằng cách kết hợp hữu cơ việc theo dõi xu hướng, lọc biến động và quản lý rủi ro năng động. Ưu điểm của chiến lược này là khả năng thích ứng cao và kiểm soát rủi ro, đồng thời cung cấp nhiều không gian để tối ưu hóa. Bằng cách thiết lập các thông số hợp lý và lựa chọn chế độ giao dịch phù hợp, chiến lược có thể duy trì hiệu suất ổn định trong các môi trường thị trường khác nhau. Nhà giao dịch nên tiến hành kiểm tra ngược và tối ưu hóa thông số đầy đủ trước khi sử dụng thực tế và thực hiện các điều chỉnh tương ứng dựa trên đặc điểm của từng sản phẩm giao dịch cụ thể.

Mã nguồn chiến lược
/*backtest
start: 2024-12-17 00:00:00
end: 2025-01-15 08:00:00
period: 2h
basePeriod: 2h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT","balance":49999}]
*/

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

//@version=5
strategy("Scalp Slayer (I)", overlay=true)

// Input Parameters
filterNumber = input.float(1.5, "Filter Number", minval=1.0, maxval=10.0, tooltip="Higher = More aggressive Filter, Lower = Less aggressive")
emaTrendPeriod = input.int(50, "EMA Trend Period", minval=1, tooltip="Period for the EMA used for trend filtering")
lookbackPeriod = input.int(20, "Lookback Period for Highs/Lows", minval=1, tooltip="Period for determining recent highs/lows")
colorTP = input.color(title='Take Profit Color', defval=color.orange)
colorSL = input.color(title='Stop Loss Color', defval=color.red)

// Inputs for visibility
showBuyLabels = input.bool(true, title="Show Buy Labels")
showSellLabels = input.bool(true, title="Show Sell Labels")

// Alert Options
alertOnCondition = input.bool(true, title="Alert on Condition Met", tooltip="Enable to alert when condition is met")

// Trade Mode Toggle
tradeMode = input.bool(false, title="Trade Mode (ON = Swing, OFF = Scalp)", tooltip="Swing-mode you can use your own TP/SL.")

// Calculations
tr = high - low
ema = filterNumber * ta.ema(tr, 50)
trendEma = ta.ema(close, emaTrendPeriod)  // Calculate the EMA for the trend filter

// Highest and lowest high/low within lookback period for swing logic
swingHigh = ta.highest(high, lookbackPeriod)
swingLow = ta.lowest(low, lookbackPeriod)

// Variables to track the entry prices and SL/TP levels
var float entryPriceLong = na
var float entryPriceShort = na
var float targetPriceLong = na
var float targetPriceShort = na
var float stopLossLong = na
var float stopLossShort = na
var bool tradeActive = false

// Buy and Sell Conditions with Trend Filter
buyCondition = close > trendEma and  // Buy only if above the trend EMA
      close[2] > open[2] and close[1] > open[1] and close > open and 
      (math.abs(close[2] - open[2]) > math.abs(close[1] - open[1])) and 
      (math.abs(close - open) > math.abs(close[1] - open[1])) and 
      close > close[1] and close[1] > close[2] and tr > ema

sellCondition = close < trendEma and  // Sell only if below the trend EMA
       close[2] < open[2] and close[1] < open[1] and close < open and 
       (math.abs(close[2] - open[2]) > math.abs(close[1] - open[1])) and 
       (math.abs(close - open) > math.abs(close[1] - open[1])) and 
       close < close[1] and close[1] < close[2] and tr > ema

// Entry Rules
if (buyCondition and not tradeActive)
    entryPriceLong := close  // Track entry price for long position
    stopLossLong := tradeMode ? ta.lowest(low, lookbackPeriod) : swingLow  // Scalping: recent low, Swing: lowest low of lookback period
    targetPriceLong := tradeMode ? close + tr : swingHigh  // Scalping: close + ATR, Swing: highest high of lookback period
    tradeActive := true

if (sellCondition and not tradeActive)
    entryPriceShort := close  // Track entry price for short position
    stopLossShort := tradeMode ? ta.highest(high, lookbackPeriod) : swingHigh  // Scalping: recent high, Swing: highest high of lookback period
    targetPriceShort := tradeMode ? close - tr : swingLow  // Scalping: close - ATR, Swing: lowest low of lookback period
    tradeActive := true

// Take Profit and Stop Loss Logic
signalBuyTPPrint = (not na(entryPriceLong) and close >= targetPriceLong)
signalSellTPPrint = (not na(entryPriceShort) and close <= targetPriceShort)

signalBuySLPrint = (not na(entryPriceLong) and close <= stopLossLong)
signalSellSLPrint = (not na(entryPriceShort) and close >= stopLossShort)

if (signalBuyTPPrint or signalBuySLPrint)
    entryPriceLong := na  // Reset entry price for long position
    targetPriceLong := na  // Reset target price for long position
    stopLossLong := na  // Reset stop-loss for long position
    tradeActive := false

if (signalSellTPPrint or signalSellSLPrint)
    entryPriceShort := na  // Reset entry price for short position
    targetPriceShort := na  // Reset target price for short position
    stopLossShort := na  // Reset stop-loss for short position
    tradeActive := false

// Plot Buy and Sell Labels with Visibility Conditions
plotshape(showBuyLabels and buyCondition, "Buy", shape.labelup, location=location.belowbar, color=color.green, text="BUY", textcolor=color.white, size=size.tiny)
plotshape(showSellLabels and sellCondition, "Sell", shape.labeldown, location=location.abovebar, color=color.red, text="SELL", textcolor=color.white, size=size.tiny)

// Plot Take Profit Flags
plotshape(showBuyLabels and signalBuyTPPrint, title="Take Profit (buys)", text="TP", style=shape.flag, location=location.abovebar, color=colorTP, textcolor=color.white, size=size.tiny)
plotshape(showSellLabels and signalSellTPPrint, title="Take Profit (sells)", text="TP", style=shape.flag, location=location.belowbar, color=colorTP, textcolor=color.white, size=size.tiny)

// Plot Stop Loss "X" Marker
plotshape(showBuyLabels and signalBuySLPrint, title="Stop Loss (buys)", text="X", style=shape.xcross, location=location.belowbar, color=colorSL, textcolor=color.white, size=size.tiny)
plotshape(showSellLabels and signalSellSLPrint, title="Stop Loss (sells)", text="X", style=shape.xcross, location=location.abovebar, color=colorSL, textcolor=color.white, size=size.tiny)

// Alerts

alertcondition(buyCondition and alertOnCondition, title="Buy Alert", message='{"content": "Buy {{ticker}} at {{close}}"}')
alertcondition(sellCondition and alertOnCondition, title="Sell Alert", message='{"content": "Sell {{ticker}} at {{close}}"}')
alertcondition(signalBuyTPPrint and alertOnCondition, title="Buy TP Alert", message='{"content": "Buy TP {{ticker}} at {{close}}"}')
alertcondition(signalSellTPPrint and alertOnCondition, title="Sell TP Alert", message='{"content": "Sell TP {{ticker}} at {{close}}"}')
alertcondition(signalBuySLPrint and alertOnCondition, title="Buy SL Alert", message='{"content": "Buy SL {{ticker}} at {{close}}"}')
alertcondition(signalSellSLPrint and alertOnCondition, title="Sell SL Alert", message='{"content": "Sell SL {{ticker}} at {{close}}"}')

if buyCondition
    strategy.entry("Enter Long", strategy.long)
else if sellCondition
    strategy.entry("Enter Short", strategy.short)