Chiến lược đột phá giá với vị thế mua dừng lỗ động và bộ lọc theo mùa


Ngày tạo: 2024-02-27 14:01:56 sửa đổi lần cuối: 2024-02-27 14:01:56
sao chép: 0 Số nhấp chuột: 631
1
tập trung vào
1621
Người theo dõi

Chiến lược đột phá giá với vị thế mua dừng lỗ động và bộ lọc theo mùa

Tổng quan

Chiến lược này được thiết kế dựa trên chỉ số di động di động ((DMI) để thiết kế một chiến lược dài chỉ làm nhiều đầu, đồng thời kết hợp với mức sóng thực trung bình ((ATR) để dừng theo dõi để kiểm soát rủi ro mất mát. Để tối ưu hóa hơn nữa, chiến lược này cũng tích hợp các điều kiện lọc theo mùa của thời gian giao dịch và chỉ số S&P 500, có một số lợi thế.

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

  1. Chiến lược này chỉ mở các vị trí trong ngày giao dịch được chỉ định (từ thứ Hai đến thứ Sáu) và giờ giao dịch (từ 9h30 đến 20h30 giờ địa phương mặc định).

  2. Khi ADX lớn hơn 27, nó cho thấy hiện tại đang ở trạng thái xu hướng giá. Tại thời điểm này, nếu + DI trên đường DI, tạo ra tín hiệu đa.

  3. Sau khi mở vị trí, thiết lập mức dừng lỗ gấp 5,5 lần ATR và đường dừng lỗ sẽ di chuyển lên khi giá tăng, đảm bảo lợi nhuận.

  4. Lựa chọn áp dụng quy tắc theo mùa của chỉ số S&P 500, chỉ mở đầu tư vào những thời điểm tốt nhất trong lịch sử.

Phân tích lợi thế

  1. Kết hợp với chỉ số xu hướng và cơ chế dừng lỗ, nó có thể theo dõi xu hướng một cách hiệu quả và kiểm soát tổn thất của từng vị trí.

  2. Sử dụng thời gian giao dịch và các điều kiện lọc theo mùa, bạn có thể tránh các giai đoạn biến động bất thường của thị trường và giảm tỷ lệ báo cáo sai.

  3. DMI và ATR đều là các chỉ số kỹ thuật trưởng thành, các tham số được điều chỉnh linh hoạt, phù hợp với tối ưu hóa định lượng.

Phân tích rủi ro

  1. DMI và ATR tham số thiết lập không đúng có thể dẫn đến tín hiệu quá nhiều hoặc quá ít. Cần điều chỉnh tham số để thử nghiệm.

  2. Lượng dừng lỗ lớn có thể gây ra tổn thất không cần thiết. Lượng dừng lỗ nhỏ có thể không kiểm soát được tổn thất một cách hiệu quả.

  3. Thời gian giao dịch và các quy tắc theo mùa có thể lọc ra một số cơ hội lợi nhuận.

Hướng tối ưu hóa

  1. Có thể cân nhắc kết hợp các chỉ số khác như MACD, Brin và các chỉ số khác để thiết kế quy tắc vào và ra sân.

  2. Có thể thử nghiệm các phương thức dừng lỗ khác nhau của ATR, cũng có thể xem xét việc điều chỉnh động mức dừng lỗ.

  3. Bạn có thể thử nghiệm điều chỉnh thời gian giao dịch hoặc tối ưu hóa thời gian bắt đầu và kết thúc giao dịch theo mùa.

  4. Bạn có thể thử áp dụng phương pháp học máy để tự động tối ưu hóa tham số.

Tóm tắt

Chiến lược này tích hợp phân tích xu hướng và kỹ thuật kiểm soát rủi ro, vượt qua một phần các vấn đề biến động mạnh mẽ của chiến lược theo dõi xu hướng. Đồng thời thêm thời gian giao dịch và lọc theo mùa, có thể làm giảm tín hiệu sai.

Mã nguồn chiến lược
/*backtest
start: 2024-01-27 00:00:00
end: 2024-02-26 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy(title="DMI Strategy with ADX and ATR-based Trailing SL (Long Only) and Seasonality", shorttitle="MBV-SP500-CLIMBER", overlay=true)

// Eingabeparameter für Long-Positionen
len = input.int(14, minval=1, title="DI Length")
lensig = input.int(14, title="ADX Smoothing", minval=1, maxval=50)
adxLongThreshold = input.float(27.0, title="ADX Threshold for Long", minval=0)
atrLength = input.int(14, title="ATR Length")
atrLongMultiplier = input.float(5.5, title="ATR Multiplier for Trailing SL (Long)")

startTimeHH = input.int(09, title="startTime hh")
startTimeMM = input.int(30, title="startTime mm")

endTimeHH = input.int(20, title="endTime hh")
endTimeMM = input.int(30, title="endTime mm")

// Zeitzone des Nutzers als Eingabeparameter
timezoneOffset = input.int(1, title="Timezone Offset (Hours from UTC)", minval=-12, maxval=14)


// Zusätzliche Einstellung für SP500-Saisonalität
enableSeasonality = input.bool(false, title="Enable SP500 Seasonality")
seasonColor = color.new(color.blue, 90)
activeTimeColor = color.new(color.yellow, 90) // Farbe für aktive Handelszeiten

// Handelstage und -zeiten
tradeMonday = input.bool(true, title="Trade on Monday")
tradeTuesday = input.bool(true, title="Trade on Tuesday")
tradeWednesday = input.bool(true, title="Trade on Wednesday")
tradeThursday = input.bool(true, title="Trade on Thursday")
tradeFriday = input.bool(true, title="Trade on Friday")

// Konvertierung der Uhrzeit in Unix-Zeitstempel
getUnixTime(hour, minute) =>
    adjustedHour = hour - timezoneOffset
    sessionDate = timestamp(year, month, dayofmonth, 0, 0)
    sessionDate + adjustedHour * 60 * 60000 + minute * 60000

// Start- und Endzeit als Unix-Zeitstempel
// + 1 Stunde wegen UTC
startTime = getUnixTime(startTimeHH, startTimeMM)
endTime = getUnixTime(endTimeHH, endTimeMM)


// Überprüfen, ob der aktuelle Zeitpunkt innerhalb der Handelszeit liegt
isTradingTime() => true

// Saisonale Zeiträume definieren
isSeason(time) =>
    m = month(time)
    d = dayofmonth(time)
    (m == 1 and d >= 1) or (m == 2 and d <= 15) or (m == 3 and d >= 23) or (m == 4 and d <= 17) or (m == 5 and d >= 12) or (m == 6 and d >= 27 and d <= 8) or (m == 7 and d <= 29) or (m == 10 and d >= 15) or (m == 11 and d >= 1) or (m == 12 and d <= 2) or (m == 12 and d >= 20 and d <= 27)

// Hintergrundfarbe für saisonale Bereiche und aktive Handelszeiten
bgcolor(enableSeasonality and isSeason(time) ? seasonColor : na)
bgcolor(isTradingTime() ? color.new(activeTimeColor, 90) : na)

// Berechnung von +DM, -DM, ATR
up = ta.change(high)
down = -ta.change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
trur = ta.rma(ta.tr, len)
atr = ta.atr(atrLength)

// Berechnung von +DI, -DI und ADX
plus = fixnan(100 * ta.rma(plusDM, len) / trur)
minus = fixnan(100 * ta.rma(minusDM, len) / trur)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)

// Logik für LONG Signale unter Berücksichtigung der Saisonalität und Zeitfilter
longSignal = ta.crossover(adx, adxLongThreshold) and plus > minus and isTradingTime()
longSignal := longSignal and (not enableSeasonality or (enableSeasonality and isSeason(time)))


// Variable für Trailing Stop-Loss
var float longTrailingSL = na

// Variablen für die Eröffnungszeit und den Eröffnungspreis der Position
var int openBarIndex = na
var float openPrice = na

// Handelslogik für Long-Positionen
// ohne strategy.position_size == 0 gilt die Kondition für ALLE Signale und nicht nur für das erste
if (longSignal and strategy.position_size == 0)
    strategy.entry("Long", strategy.long)
    openBarIndex := bar_index
    openPrice := close
    longTrailingSL := close - atr * atrLongMultiplier

//if (longSignal)
   //longTrailingSL := close - atr * atrLongMultiplier

// Aktualisierung des Trailing Stop-Loss
if strategy.position_size > 0
    longTrailingSL := math.max(longTrailingSL, close - atr * atrLongMultiplier)

// Ausstieg aus Long-Positionen
strategy.exit("Close Long", "Long", stop=longTrailingSL)

// Anzeige des ATR-basierten Trailing Stops für Long-Positionen
//plot(strategy.position_size > 0 ? longTrailingSL : na, color=color.red, title="ATR Trailing Stop Long")

// Anzeige des ATR-basierten Trailing Stops für Long-Positionen
plot(strategy.position_size > 0 ? longTrailingSL : na, color=color.new(color.red, 75), style=plot.style_circles, linewidth=1, title="Trailing Stop-Loss")


// Wenn eine Position geschlossen wird, zeichnen Sie die Linie
// if strategy.position_size[1] > 0 and strategy.position_size == 0
//     lineColor = longTrailingSL > openPrice ? color.new(color.green, 50) : color.new(color.red, 50) // Hellgrün für Gewinne, Hellrot für Verluste
//     line.new(openBarIndex, openPrice, bar_index, longTrailingSL, width=3, color=lineColor)