Chiến lược tỷ lệ trung bình động chuỗi lọc xu hướng kép


Ngày tạo: 2023-12-28 17:37:14 sửa đổi lần cuối: 2023-12-28 17:37:14
sao chép: 1 Số nhấp chuột: 623
1
tập trung vào
1623
Người theo dõi

Chiến lược tỷ lệ trung bình động chuỗi lọc xu hướng kép

Tổng quan

Chiến lược này dựa trên các chỉ số tỷ lệ trung bình di chuyển kép, kết hợp với bộ lọc Brin và các chỉ số lọc xu hướng kép, sử dụng cơ chế thoát ra theo xu hướng. Chiến lược này nhằm sử dụng các chỉ số tỷ lệ trung bình di chuyển để xác định xu hướng đường dài, chọn điểm vào tốt nhất khi xu hướng xu hướng rõ ràng, và thiết lập các hệ thống dừng lỗ, dừng lỗ và thoát ra để khóa lợi nhuận, giảm tổn thất.

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

  1. Tính trung bình di chuyển nhanh (đường 10 ngày) và trung bình di chuyển chậm (đường 50 ngày) và tính tỷ lệ của chúng, được gọi là tỷ lệ trung bình di chuyển giá. Tỷ lệ này có thể xác định hiệu quả sự thay đổi của xu hướng đường dài trong giá.
  2. Chuyển đổi tỷ lệ giá trung bình di chuyển thành tỷ lệ phần trăm, tức là tỷ lệ hiện tại tương đối mạnh trong một khoảng thời gian trước.
  3. Khi dao động trên vượt qua ngưỡng mua đã đặt ((10) tạo ra tín hiệu mua, khi phá vỡ ngưỡng bán ((90) tạo ra tín hiệu bán, theo xu hướng.
  4. Kết hợp với chỉ số băng thông Brin để lọc tín hiệu giao dịch, hoạt động khi băng thông Brin thu hẹp.
  5. Sử dụng chỉ số lọc xu hướng kép, chỉ tạo ra tín hiệu mua khi giá ở kênh xu hướng tăng và chỉ tạo ra tín hiệu bán khi giá ở kênh giảm, do đó tránh hoạt động ngược.
  6. Thiết lập cơ chế thoát chuỗi, bao gồm dừng, dừng lỗ và thoát kết hợp, có thể đặt trước nhiều điều kiện thoát, ưu tiên thoát điều kiện có lợi nhuận cao nhất.

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

  1. Cơ chế lọc xu hướng kép, xác định một cách đáng tin cậy hướng của xu hướng chính, tránh hoạt động ngược.
  2. Chỉ số tỷ lệ trung bình di chuyển có hiệu quả hơn so với chỉ số trung bình di chuyển đơn lẻ để xác định sự thay đổi xu hướng.
  3. Chỉ số băng thông của Brin có thể định vị hiệu quả thị trường trong thời gian biến động thấp, khi tín hiệu giao dịch được tin cậy hơn.
  4. Các cơ chế thoát hàng chuỗi giúp lợi nhuận ổn định hơn và tối đa hóa toàn bộ lợi nhuận.

Rủi ro và giải pháp

  1. Trong trường hợp không có xu hướng rõ ràng trong các biến động, sẽ có nhiều tín hiệu sai và đảo ngược. Giải pháp là kết hợp với Brin bandwidth filtering, hoạt động khi thu hẹp.
  2. Khi có một sự đảo ngược xu hướng rõ ràng, đường trung bình di chuyển sẽ bị tụt hậu và không thể nhận ra tín hiệu đảo ngược ngay lập tức. Giải pháp là rút ngắn các tham số chu kỳ đường trung bình di chuyển một cách thích hợp.
  3. Trong trường hợp có lỗ hổng nhảy vọt, điểm dừng có thể bị tấn công ngay lập tức, gây ra tổn thất lớn. Giải pháp là giải phóng các tham số của điểm dừng một cách thích hợp.

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

  1. Tối ưu hóa tham số. Có thể thử nghiệm ít nhất các tham số chuyển động trung bình, điểm mua bán dao động, tham số Brin và tham số lọc xu hướng để tìm các tham số kết hợp tốt nhất.
  2. Tham gia các chỉ số khác. Bạn có thể xem xét thêm các chỉ số khác để đánh giá xu hướng đảo ngược, chẳng hạn như chỉ số KD, chỉ số MACD, để cải thiện độ chính xác của chiến lược.
  3. Học máy có thể thu thập dữ liệu lịch sử, sử dụng mô hình đào tạo thuật toán học máy, tối ưu hóa các tham số động, thực hiện điều chỉnh thích ứng của tham số

Tóm tắt

Chiến lược này kết hợp sử dụng chỉ số tỷ lệ trung bình di chuyển kép và chỉ số Brin để đánh giá xu hướng xu hướng đường dài, tìm kiếm điểm nhập cảnh tốt nhất sau khi xác nhận xu hướng, và thiết lập cơ chế thoát hàng chuỗi để khóa lợi nhuận, độ tin cậy cao, hiệu quả rõ ràng. Chiến lược này có thể được cải thiện hơn nữa và tăng lợi nhuận bằng cách tối ưu hóa tham số, thêm các chỉ số phán đoán hỗ trợ khác và học máy.

Mã nguồn chiến lược
/*backtest
start: 2023-12-20 00:00:00
end: 2023-12-27 00:00:00
period: 3m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy("Premium MA Ratio Strategy", overlay = true)

// Input: Adjustable parameters for Premium MA Ratio
fast_length = input(10, title = "Fast MA Length")
slow_length = input(50, title = "Slow MA Length")
oscillator_threshold_buy = input(10, title = "Oscillator Buy Threshold")
oscillator_threshold_sell = input(90, title = "Oscillator Sell Threshold")

// Input: Adjustable parameters for Bollinger Bands
bb_length = input(20, title = "Bollinger Bands Length")
bb_source = input(close, title = "Bollinger Bands Source")
bb_deviation = input(2.0, title = "Bollinger Bands Deviation")
bb_width_threshold = input(30, title = "BB Width Threshold")
use_bb_filter = input(true, title = "Use BB Width Filter?")

// Input: Adjustable parameters for Trend Filter
use_trend_filter = input(true, title = "Use Trend Filter?")
trend_filter_period_1 = input(50, title = "Trend Filter Period 1")
trend_filter_period_2 = input(200, title = "Trend Filter Period 2")
use_second_trend_filter = input(true, title = "Use Second Trend Filter?")

// Input: Adjustable parameters for Exit Strategies
use_exit_strategies = input(true, title = "Use Exit Strategies?")
use_take_profit = input(true, title = "Use Take Profit?")
take_profit_ticks = input(150, title = "Take Profit in Ticks")
use_stop_loss = input(true, title = "Use Stop Loss?")
stop_loss_ticks = input(100, title = "Stop Loss in Ticks")
use_combined_exit = input(true, title = "Use Combined Exit Strategy?")
combined_exit_ticks = input(50, title = "Combined Exit Ticks")

// Input: Adjustable parameters for Time Filter
use_time_filter = input(false, title = "Use Time Filter?")
start_hour = input(8, title = "Start Hour")
end_hour = input(16, title = "End Hour")

// Calculate moving averages
fast_ma = sma(close, fast_length)
slow_ma = sma(close, slow_length)

// Calculate the premium price moving average ratio
premium_ratio = fast_ma / slow_ma * 100

// Calculate the percentile rank of the premium ratio
percentile_rank(src, length) =>
    rank = 0.0
    for i = 1 to length
        if src > src[i]
            rank := rank + 1.0
    percentile = rank / length * 100

// Calculate the percentile rank for the premium ratio using slow_length periods
premium_ratio_percentile = percentile_rank(premium_ratio, slow_length)

// Calculate the oscillator based on the percentile rank
oscillator = premium_ratio_percentile

// Dynamic coloring for the oscillator line
oscillator_color = oscillator > 50 ? color.green : color.red

// Plot the oscillator on a separate subplot as a line
hline(50, "Midline", color = color.gray)
plot(oscillator, title = "Oscillator", color = oscillator_color, linewidth = 2)

// Highlight the overbought and oversold areas
bgcolor(oscillator > oscillator_threshold_sell ? color.red : na, transp = 80)
bgcolor(oscillator < oscillator_threshold_buy ? color.green : na, transp = 80)

// Plot horizontal lines for threshold levels
hline(oscillator_threshold_buy, "Buy Threshold", color = color.green)
hline(oscillator_threshold_sell, "Sell Threshold", color = color.red)

// Calculate Bollinger Bands width
bb_upper = sma(bb_source, bb_length) + bb_deviation * stdev(bb_source, bb_length)
bb_lower = sma(bb_source, bb_length) - bb_deviation * stdev(bb_source, bb_length)
bb_width = bb_upper - bb_lower

// Calculate the percentile rank of Bollinger Bands width
bb_width_percentile = percentile_rank(bb_width, bb_length)

// Plot the Bollinger Bands width percentile line
plot(bb_width_percentile, title = "BB Width Percentile", color = color.blue, linewidth = 2)

// Calculate the trend filters
trend_filter_1 = sma(close, trend_filter_period_1)
trend_filter_2 = sma(close, trend_filter_period_2)

// Strategy logic
longCondition = crossover(premium_ratio_percentile, oscillator_threshold_buy)
shortCondition = crossunder(premium_ratio_percentile, oscillator_threshold_sell)

// Apply Bollinger Bands width filter if enabled
if (use_bb_filter)
    longCondition := longCondition and bb_width_percentile < bb_width_threshold
    shortCondition := shortCondition and bb_width_percentile < bb_width_threshold

// Apply trend filters if enabled
if (use_trend_filter)
    longCondition := longCondition and (close > trend_filter_1)
    shortCondition := shortCondition and (close < trend_filter_1)

// Apply second trend filter if enabled
if (use_trend_filter and use_second_trend_filter)
    longCondition := longCondition and (close > trend_filter_2)
    shortCondition := shortCondition and (close < trend_filter_2)

// Apply time filter if enabled
if (use_time_filter)
    longCondition := longCondition and (hour >= start_hour and hour <= end_hour)
    shortCondition := shortCondition and (hour >= start_hour and hour <= end_hour)

// Generate trading signals with exit strategies
if (use_exit_strategies)
    strategy.entry("Buy", strategy.long, when = longCondition)
    strategy.entry("Sell", strategy.short, when = shortCondition)
    
    // Define unique exit names for each order
    buy_take_profit_exit = "Buy Take Profit"
    buy_stop_loss_exit = "Buy Stop Loss"
    sell_take_profit_exit = "Sell Take Profit"
    sell_stop_loss_exit = "Sell Stop Loss"
    combined_exit = "Combined Exit"
    
    // Exit conditions for take profit
    if (use_take_profit)
        strategy.exit(buy_take_profit_exit, from_entry = "Buy", profit = take_profit_ticks)
        strategy.exit(sell_take_profit_exit, from_entry = "Sell", profit = take_profit_ticks)
    
    // Exit conditions for stop loss
    if (use_stop_loss)
        strategy.exit(buy_stop_loss_exit, from_entry = "Buy", loss = stop_loss_ticks)
        strategy.exit(sell_stop_loss_exit, from_entry = "Sell", loss = stop_loss_ticks)
    
    // Combined exit strategy
    if (use_combined_exit)
        strategy.exit(combined_exit, from_entry = "Buy", loss = combined_exit_ticks, profit = combined_exit_ticks)