Chiến lược theo dõi xu hướng giao cắt nhiều lần của EMA và CCI

EMA CCI
Ngày tạo: 2024-09-26 15:43:50 sửa đổi lần cuối: 2024-09-26 15:43:50
sao chép: 1 Số nhấp chuột: 651
1
tập trung vào
1617
Người theo dõi

Chiến lược theo dõi xu hướng giao cắt nhiều lần của EMA và CCI

Tổng quan

Đây là một chiến lược theo dõi xu hướng dựa trên moving average (EMA) và chỉ số kênh hàng hóa (CCI) của nhiều chỉ số. Chiến lược này sử dụng sự giao thoa của EMA trong nhiều chu kỳ thời gian để xác định sự thay đổi xu hướng tiềm năng và kết hợp với chỉ số CCI để xác nhận tình trạng thị trường quá mua hoặc quá bán, do đó cải thiện độ chính xác của thời điểm vào. Chiến lược cũng bao gồm các cơ chế dừng lỗ động dựa trên thời gian và giá để quản lý rủi ro và khóa lợi nhuận.

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

Chiến lược này dựa trên một số yếu tố quan trọng:

  1. Multiple EMA crossover: sử dụng EMA 8, 12, 24 và 72 chu kỳ. Khi EMA chu kỳ ngắn ((8, 12, 24) cùng lúc vượt qua 72 chu kỳ EMA, được coi là tín hiệu đa tiềm năng; ngược lại là tín hiệu trống.

  2. Chứng nhận chỉ số CCI: Sử dụng chỉ số CCI 20 chu kỳ, xác nhận tình trạng quá mua khi CCI lớn hơn 150 và xác nhận tình trạng quá bán khi nhỏ hơn 150

  3. Điều kiện tham gia:

    • Làm nhiều hơn: Giai đoạn ngắn EMA cùng lúc trên 72 chu kỳ EMA, và CCI lớn hơn 150, giá trên 72 chu kỳ EMA.
    • Làm trống: Giai đoạn ngắn EMA đồng thời đi qua 72 chu kỳ EMA, và CCI nhỏ hơn 150, giá dưới 72 chu kỳ EMA.
  4. Động lực dừng lỗ:

    • Có hai chế độ nhập cảnh: giao thoa một lần và giao thoa trong cửa sổ thời gian
    • Cài đặt tỷ lệ Stop Loss khác nhau cho các mô hình nhập cảnh khác nhau.
  5. Quản lý vị trí: Chiến lược sử dụng giao dịch toàn vị trí, tức là giao dịch với 100% số tiền trong tài khoản.

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

  1. Cơ chế xác nhận đa dạng: Bằng cách kết hợp nhiều chỉ số EMA và CCI, hiệu quả làm giảm tác động của tín hiệu giả và tăng độ chính xác nhập cảnh.

  2. Cơ chế nhập cảnh linh hoạt: Chiến lược xem xét hai trường hợp giao thoa một lần và giao thoa trong cửa sổ thời gian, thích ứng với các môi trường thị trường khác nhau.

  3. Quản lý rủi ro năng động: Thiết lập tỷ lệ dừng lỗ khác nhau cho các mô hình đầu vào khác nhau để cân bằng lợi nhuận và rủi ro tốt hơn.

  4. Khả năng theo dõi xu hướng: Sử dụng nhiều giao chéo EMA để nắm bắt hiệu quả các thay đổi trong xu hướng trung hạn và dài hạn.

  5. Bộ lọc biến động: Thông qua các chỉ số CCI để đánh giá mua và bán quá mức, giúp tránh giao dịch thường xuyên trong thị trường biến động ngang.

Rủi ro chiến lược

  1. Sự chậm trễ: EMA và CCI là các chỉ số chậm trễ, có thể không phản ứng kịp thời trong thị trường biến động mạnh.

  2. Giao dịch thường xuyên: Trong thị trường bất ổn, có thể có nhiều tín hiệu phá vỡ giả tạo, dẫn đến giao dịch thường xuyên và phí xử lý tăng lên.

  3. Rủi ro toàn vị trí: Sử dụng giao dịch 100% vị trí có thể mang lại rủi ro rút lui lớn hơn.

  4. Lệnh dừng phần trăm cố định: Trong thị trường có nhiều biến động, lệnh dừng phần trăm cố định có thể thoát khỏi lợi nhuận sớm.

  5. Tùy thuộc vào dữ liệu lịch sử: Hiệu suất chiến lược có thể bị ảnh hưởng bởi dữ liệu lịch sử và cần tối ưu lại các tham số khi môi trường thị trường thay đổi trong tương lai.

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

  1. Tham gia chỉ số tỷ lệ biến động: Xem xét thêm chỉ số ATR (Average True Range) để điều chỉnh mức dừng lỗ theo biến động thị trường để phù hợp với môi trường thị trường khác nhau.

  2. Tối ưu hóa quản lý vị trí: giới thiệu cơ chế quản lý vị trí động, điều chỉnh kích thước vị trí tùy theo cường độ của xu hướng thị trường và khả năng chịu rủi ro của tài khoản.

  3. Thêm các điều kiện lọc: Bạn có thể xem xét thêm các chỉ số như khối lượng giao dịch, cường độ xu hướng, để lọc thêm tín hiệu giao dịch và tăng tỷ lệ thắng.

  4. Tối ưu hóa tham số: Sử dụng các phương pháp như thuật toán di truyền hoặc tìm kiếm lưới, tối ưu hóa các tham số như chu kỳ EMA, ngưỡng CCI để cải thiện khả năng thích ứng của chiến lược trong các môi trường thị trường khác nhau.

  5. Tham gia nhận dạng chế độ thị trường: phát triển mô-đun nhận dạng trạng thái thị trường (( xu hướng, chấn động, biến động cao), điều chỉnh các tham số chiến lược hoặc tạm dừng giao dịch theo các trạng thái thị trường khác nhau.

Tóm tắt

Chiến lược theo dõi xu hướng EMA và CCI đa chéo là một hệ thống giao dịch định lượng kết hợp phân tích kỹ thuật và quản lý rủi ro động. Bằng cách kết hợp các chỉ số đa chéo EMA và CCI, chiến lược này có thể nắm bắt được xu hướng thị trường một cách hiệu quả, đồng thời quản lý rủi ro thông qua cơ chế nhập cảnh linh hoạt và dừng dừng động động. Mặc dù có một số rủi ro vốn có của chiến lược, chẳng hạn như chậm trễ và rút lui tiềm năng cao của giao dịch toàn vị trí, nhưng bằng cách tối ưu hóa và cải tiến thêm, chẳng hạn như đưa ra các phương pháp điều chỉnh tỷ lệ biến động, nhận diện vị trí động và chế độ quản lý thị trường, có thể cải thiện đáng kể sự ổn định và thích ứng của chiến lược.

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

//@version=5
strategy("EMA & CCI Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

// Параметры EMA
ema8_length = 8
ema12_length = 12
ema24_length = 24
ema72_length = 72

// Расчет EMA
ema8 = ta.ema(close, ema8_length)
ema12 = ta.ema(close, ema12_length)
ema24 = ta.ema(close, ema24_length)
ema72 = ta.ema(close, ema72_length)

// Параметры CCI
cci_length = 20
cci_overbought = 150
cci_oversold = -150

// Параметры тейк-профита и стоп-лосса
takeProfitPercent = input.float(1.5, title="Take Profit (%)", step=0.1)
stopLossPercent = input.float(0.5, title="Stop Loss (%)", step=0.1)
takeProfitPercentTime = input.float(0.5, title="Take Profit (%) for Time-based", step=0.1)
stopLossPercentTime = input.float(0.2, title="Stop Loss (%) for Time-based", step=0.1)
max_wait_bars = input.float(2, title="Max wait candles", step=1)
// Расчет CCI
cci = ta.cci(close, cci_length)

// Состояние открытой позиции
sz = strategy.position_size

// Флаги для отслеживания пересечений EMA вверх
var int ema8_cross_index_up = na
var int ema12_cross_index_up = na
var int ema24_cross_index_up = na

// Флаги для отслеживания пересечений EMA вниз
var int ema8_cross_index_down = na
var int ema12_cross_index_down = na
var int ema24_cross_index_down = na

// Проверка пересечения EMA с 72 вверх и обновление индекса пересечения
if (ta.crossover(ema8, ema72))
    ema8_cross_index_up := bar_index
if (ta.crossover(ema12, ema72))
    ema12_cross_index_up := bar_index
if (ta.crossover(ema24, ema72))
    ema24_cross_index_up := bar_index

// Проверка пересечений EMA вниз и обновление индекса пересечения
if (ta.crossunder(ema8, ema72))
    ema8_cross_index_down := bar_index
if (ta.crossunder(ema12, ema72))
    ema12_cross_index_down := bar_index
if (ta.crossunder(ema24, ema72))
    ema24_cross_index_down := bar_index

// Условия пересечения за одну свечу (лонг и шорт)
cross_condition_one_candle_long = (na(ema8_cross_index_up) == false and (bar_index - ema8_cross_index_up) == 0) and
                                  (na(ema12_cross_index_up) == false and (bar_index - ema12_cross_index_up) == 0) and
                                  (na(ema24_cross_index_up) == false and (bar_index - ema24_cross_index_up) == 0)

cross_condition_one_candle_short = (na(ema8_cross_index_down) == false and (bar_index - ema8_cross_index_down) == 0) and
                                   (na(ema12_cross_index_down) == false and (bar_index - ema12_cross_index_down) == 0) and
                                   (na(ema24_cross_index_down) == false and (bar_index - ema24_cross_index_down) == 0)

// Условия пересечения в течение указанного времени (лонг и шорт)
cross_condition_within_time_long = (not na(ema8_cross_index_up) and (bar_index - ema8_cross_index_up) <= max_wait_bars) and
                                   (not na(ema12_cross_index_up) and (bar_index - ema12_cross_index_up) <= max_wait_bars) and
                                   (not na(ema24_cross_index_up) and (bar_index - ema24_cross_index_up) <= max_wait_bars)

cross_condition_within_time_short = (not na(ema8_cross_index_down) and (bar_index - ema8_cross_index_down) <= max_wait_bars) and (not na(ema12_cross_index_down) and (bar_index - ema12_cross_index_down) <= max_wait_bars) and (not na(ema24_cross_index_down) and (bar_index - ema24_cross_index_down) <= max_wait_bars)

// Условие для открытия лонга
long_condition_one = cross_condition_one_candle_long and cci > cci_overbought and close > ema72
long_condition_time = cross_condition_within_time_long and cci > cci_overbought and close > ema72

// Условие для открытия шорта
short_condition_one = cross_condition_one_candle_short and cci < cci_oversold and close < ema72
short_condition_time = cross_condition_within_time_short and cci < cci_oversold and close < ema72

// Вход в лонг
if (long_condition_one and sz == 0)
    strategy.entry(id='Long_one', direction=strategy.long)

if (long_condition_time and sz == 0)
    strategy.entry(id='Long_time', direction=strategy.long)

// Вход в шорт
if (short_condition_one and sz == 0)
    strategy.entry(id='Short_one', direction=strategy.short)

if (short_condition_time and sz == 0)
    strategy.entry(id='Short_time', direction=strategy.short)

// Вычисление цен тейк-профита и стоп-лосса для лонга
if (sz > 0 and strategy.opentrades.entry_id(0) == 'Long_one')
    entryPriceLong = strategy.opentrades.entry_price(0)
    takeProfitPriceLong = entryPriceLong * (1 + takeProfitPercent / 100)
    stopLossPriceLong = entryPriceLong * (1 - stopLossPercent / 100)
    strategy.exit("Close long one", "Long_one", limit=takeProfitPriceLong, stop=stopLossPriceLong)
    ema8_cross_index_up := na
    ema12_cross_index_up := na
    ema24_cross_index_up := na

if (sz > 0 and strategy.opentrades.entry_id(0) == 'Long_time')
    entryPriceLongTime = strategy.opentrades.entry_price(0)
    takeProfitPriceLongTime = entryPriceLongTime * (1 + takeProfitPercentTime / 100)
    stopLossPriceLongTime = entryPriceLongTime * (1 - stopLossPercentTime / 100)
    strategy.exit("Close long time", "Long_time", limit=takeProfitPriceLongTime, stop=stopLossPriceLongTime)
    ema8_cross_index_up := na
    ema12_cross_index_up := na
    ema24_cross_index_up := na

// Вычисление цен тейк-профита и стоп-лосса для шорта
if (sz < 0 and strategy.opentrades.entry_id(0) == 'Short_one')
    entryPriceShort = strategy.opentrades.entry_price(0)
    takeProfitPriceShort = entryPriceShort * (1 - takeProfitPercent / 100)
    stopLossPriceShort = entryPriceShort * (1 + stopLossPercent / 100)
    strategy.exit("Close short one", "Short_one", limit=takeProfitPriceShort, stop=stopLossPriceShort)
    ema8_cross_index_down := na
    ema12_cross_index_down := na
    ema24_cross_index_down := na

if (sz < 0 and strategy.opentrades.entry_id(0) == 'Short_time')
    entryPriceShortTime = strategy.opentrades.entry_price(0)
    takeProfitPriceShortTime = entryPriceShortTime * (1 - takeProfitPercentTime / 100)
    stopLossPriceShortTime = entryPriceShortTime * (1 + stopLossPercentTime / 100)
    strategy.exit("Close short time", "Short_time", limit=takeProfitPriceShortTime, stop=stopLossPriceShortTime)
    ema8_cross_index_down := na
    ema12_cross_index_down := na
    ema24_cross_index_down := na

// Отображение EMA на графике
plot(ema8, title="EMA 8", color=color.blue, linewidth=2)
plot(ema12, title="EMA 12", color=color.orange, linewidth=2)
plot(ema24, title="EMA 24", color=color.green, linewidth=2)
plot(ema72, title="EMA 72", color=color.red, linewidth=2)

// Вывод CCI в подвале
//plot(cci, title="CCI", color=color.purple)
//hline(100, "CCI 150", color=color.green)
//hline(-100, "CCI -150", color=color.red)
//hline(0, "CCI 0", color=color.gray)


// Отладочная информация
//plotshape(series=long_condition_one, location=location.belowbar, color=color.lime, style=shape.labelup, title="Long Condition")
//plotshape(series=cross_condition_one_candle_long, location=location.belowbar, color=color.blue, style=shape.triangleup, title="Cross Condition Long")
//plotshape(series=long_condition_time, location=location.belowbar, color=#e6d700, style=shape.labelup, title="Long Condition Time")
//plotshape(series=cross_condition_within_time_long, location=location.belowbar, color=#a21dbd, style=shape.triangleup, title="Cross Condition Time Long")
//plotshape(series=short_condition_one, location=location.abovebar, color=color.red, style=shape.labeldown, title="Short Condition")
//plotshape(series=cross_condition_one_candle_short, location=location.abovebar, color=color.blue, style=shape.triangledown, title="Cross Condition Short")
//plotshape(series=short_condition_time, location=location.abovebar, color=#e6d700, style=shape.labeldown, title="Short Condition Time")
//plotshape(series=cross_condition_within_time_short, location=location.abovebar, color=#a21dbd, style=shape.triangledown, title="Cross Condition Time Short")