Chiến lược đảo ngược xu hướng giao cắt của chỉ báo động lượng


Ngày tạo: 2023-12-29 16:21:12 sửa đổi lần cuối: 2023-12-29 16:21:12
sao chép: 0 Số nhấp chuột: 559
1
tập trung vào
1621
Người theo dõi

Chiến lược đảo ngược xu hướng giao cắt của chỉ báo động lượng

Tổng quan

Chiến lược này sử dụng nhiều chỉ số kỹ thuật động lực như MACD, RSI, ADX, để nhận ra tín hiệu đảo ngược giá, sử dụng chiến lược đảo ngược, vào vào khi xu hướng mạnh. Chiến lược đồng thời thiết lập dừng lỗ và dừng để khóa lợi nhuận và kiểm soát rủi ro.

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

Chiến lược này đầu tiên kết hợp với đường trung bình nhanh chậm so với MACD để xác định xu hướng giá; sau đó kết hợp với chỉ số RSI để lọc các đợt phá vỡ giả mạo, đảm bảo rằng tín hiệu giao dịch sẽ được tạo ra sau khi sự đảo ngược giá thực sự xảy ra; và cuối cùng, sử dụng chỉ số ADX để xác minh lại xem giá có đi vào trạng thái xu hướng hay không. Chỉ khi nhiều điều kiện trên cùng lúc được đáp ứng, tín hiệu mua hoặc bán sẽ được tạo ra.

Cụ thể, khi MACD đi qua đường chậm, RSI cao hơn 50 và tăng lên, ADX cao hơn 20 là tín hiệu mua; khi MACD đi qua đường chậm, RSI thấp hơn 50 và giảm xuống, ADX cao hơn 20 là tín hiệu bán.

Phân tích lợi thế

Ưu điểm lớn nhất của chiến lược này là sử dụng nhiều chỉ số để kết hợp, có thể lọc hiệu quả các tín hiệu thị trường và tín hiệu sai sót, thực sự khóa các điểm đảo ngược xu hướng, do đó có tỷ lệ thắng cao hơn. Ngoài ra, thiết lập chặn lỗ để khóa lợi nhuận và kiểm soát rủi ro, có thể bảo vệ hiệu quả các tác động của sự kiện bất ngờ.

Phân tích rủi ro

Rủi ro lớn nhất của chiến lược này là sai lầm trong phán đoán xu hướng đảo ngược, chẳng hạn như giá có sự điều chỉnh sâu dẫn đến sai lầm. Ngoài ra, xu hướng mới sau khi đảo ngược có thể không bền vững để có được lợi nhuận đủ.

Giải pháp là tối ưu hóa các tham số hơn nữa, điều chỉnh độ dừng, hoặc lọc tín hiệu kết hợp với nhiều chỉ số phụ trợ.

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

Chiến lược này có thể được tối ưu hóa hơn nữa theo các hướng sau:

  1. Tối ưu hóa sự kết hợp của MACD và RSI để cải thiện tính chính xác của giá

  2. Thêm nhiều bộ lọc chỉ số, chẳng hạn như KD, BOLL, để tạo hiệu ứng bao quanh chỉ số

  3. Động thái điều chỉnh mức dừng lỗ, điều chỉnh theo các điều kiện thị trường khác nhau

  4. Điều chỉnh vị trí dừng theo thời gian thực sau khi đảo ngược

Tóm tắt

Chiến lược này sử dụng nhiều chỉ số động lực để xác định cơ hội đảo ngược giá tiềm năng. Bằng cách tối ưu hóa tham số, kết hợp nhiều chỉ số phụ trợ, điều chỉnh động theo chiến lược dừng lỗ, bạn có thể nâng cao hơn nữa sự ổn định và độ tin cậy của chiến lược, khóa các cơ hội giao dịch khác nhau mà thị trường cung cấp.

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

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AHMEDABDELAZIZZIZO

//@version=5
strategy("Ta Strategy", overlay=true )

// inputs
inversestrategy = input.bool(false, title = "Inverse Strategy",tooltip = "This option makes you reverse the strategy so that long signals become where to short  ")
direction = input.string(defval = "Both" , options = ["Both" , "Short" , "Long"] )

leftbars= input(6,title = " Left Bars" , group = "Support and resistance")
rightbars = input(6, title = " Right Bars", group = "Support and resistance")

macdfast = input(12, title = "MACD Fast", group = "MACD")
macdslow = input(26, title = "MACD Slow",group = "MACD")
macdsignal = input(7, "MACD Signal",group = "MACD")

sellqty = input(50, title = "QTY to sell at TP 1")

len = input(14, title="ADX Length" , group = "ADX")


// sup and res
res = fixnan(ta.pivothigh(high,leftbars,rightbars))
sup = fixnan(ta.pivotlow(low , leftbars,rightbars))

// macd
macd =ta.ema(close,macdfast) - ta.ema(close,macdslow)
signal=ta.ema(macd,macdsignal)


//adx
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)
truerange = ta.rma(ta.tr,len)
plusDI = 100 * ta.rma(plusDM, len) / truerange
minusDI = 100 * ta.rma(minusDM, len) / truerange
dx = 100 * ta.rma(math.abs(plusDI - minusDI) / (plusDI + minusDI), len)
adx = ta.sma(dx, len)

// start deal condition
longcondition =  ta.crossover(macd,signal) and close > res and ta.rsi(close,14) > 50 and plusDI > minusDI and adx > 20 
shortcondition = ta.crossunder(macd,signal) and close < sup and ta.rsi(close,14) < 50 and plusDI < minusDI and adx > 20 

//tp
longtp1   = input.float(6, "Long TP 1", minval = 0.0, step = 0.25, group = "Exit LONG Orders") /100
longtp2   = input.float(12, "Long TP 2", minval = 0.0, step = 0.25, group = "Exit LONG Orders") /100
longsl1 = input.float(3.0, "Long SL",  minval = 0.0, step = 0.25, group = "Exit LONG Orders") /100
longtakeprofit1 = (strategy.position_avg_price * (1 + longtp1)) 
longstoploss1 = (strategy.position_avg_price * (1 - longsl1)) 
longtakeprofit2 = (strategy.position_avg_price * (1 + longtp2)) 

//sl
shorttp1   = input.float(6.0, "Short TP 1 ", minval = 0.0, step = 0.25, group = "Exit SHORT Orders")/100
shorttp2   = input.float(12.0, "Short TP 2", minval = 0.0, step = 0.25, group = "Exit SHORT Orders")/100
shortsl1 = input.float(3.0, "Short SL",  minval = 0.0, step = 0.25, group = "Exit SHORT Orders")/100
shorttakeprofit1 = (strategy.position_avg_price * (1- shorttp1))
shortstoploss1 = (strategy.position_avg_price * (1 + shortsl1))
shorttakeprofit2 = (strategy.position_avg_price * (1- shorttp2))

//placeorders
if inversestrategy == false
    if direction == "Both"
        if longcondition and strategy.opentrades == 0
            strategy.entry("long" , strategy.long )
        strategy.exit("exit long 1","long",qty_percent = sellqty ,limit = longtakeprofit1,stop = longstoploss1)
        strategy.exit("exit long 2","long",qty_percent = 100 ,limit = longtakeprofit2,stop = longstoploss1)
        if high >= longtakeprofit1
            strategy.cancel("exit long 2")
            strategy.exit("exit long 3","long",qty_percent = 100 ,limit = longtakeprofit2,stop = strategy.position_avg_price)
        if shortcondition and strategy.opentrades == 0
            strategy.entry("short",strategy.short)
        strategy.exit("exit short 1","short",qty_percent = sellqty ,limit = shorttakeprofit1,stop = shortstoploss1)
        strategy.exit("exit short 2","short",qty_percent = 100 ,limit = shorttakeprofit2,stop = shortstoploss1)
        if low <= shorttakeprofit1
            strategy.cancel("exit short 2")
        strategy.exit("exit short 3","short",qty_percent = 100 ,limit = shorttakeprofit2,stop = strategy.position_avg_price)
    else if direction == "Long"
        if longcondition and strategy.opentrades == 0
            strategy.entry("long" , strategy.long )
        strategy.exit("exit long 1","long",qty_percent = sellqty ,limit = longtakeprofit1,stop = longstoploss1)
        strategy.exit("exit long 2","long",qty_percent = 100 ,limit = longtakeprofit2,stop = longstoploss1)
        if high >= longtakeprofit1
            strategy.cancel("exit long 2")
            strategy.exit("exit long 3","long",qty_percent = 100 ,limit = longtakeprofit2,stop = strategy.position_avg_price)
    else if direction == "Short"
        if shortcondition and strategy.opentrades == 0
            strategy.entry("short",strategy.short)
        strategy.exit("exit short 1","short",qty_percent = sellqty ,limit = shorttakeprofit1,stop = shortstoploss1)
        strategy.exit("exit short 2","short",qty_percent = 100 ,limit = shorttakeprofit2,stop = shortstoploss1)
        if low <= shorttakeprofit1
            strategy.cancel("exit short 2")
        strategy.exit("exit short 3","short",qty_percent = 100 ,limit = shorttakeprofit2,stop = strategy.position_avg_price)
else
    if direction == "Both"
        if shortcondition and strategy.opentrades == 0
            strategy.entry("long" , strategy.long )
        strategy.exit("exit long 1","long",qty_percent = sellqty ,limit = longtakeprofit1,stop = longstoploss1)
        strategy.exit("exit long 2","long",qty_percent = 100 ,limit = longtakeprofit2,stop = longstoploss1)
        if high >= longtakeprofit1
            strategy.cancel("exit long 2")
            strategy.exit("exit long 3","long",qty_percent = 100 ,limit = longtakeprofit2,stop = strategy.position_avg_price)
        if longcondition and strategy.opentrades == 0
            strategy.entry("short",strategy.short)
        strategy.exit("exit short 1","short",qty_percent = sellqty ,limit = shorttakeprofit1,stop = shortstoploss1)
        strategy.exit("exit short 2","short",qty_percent = 100 ,limit = shorttakeprofit2,stop = shortstoploss1)
        if low <= shorttakeprofit1
            strategy.cancel("exit short 2")
        strategy.exit("exit short 3","short",qty_percent = 100 ,limit = shorttakeprofit2,stop = strategy.position_avg_price)
    else if direction == "Long"
        if shortcondition and strategy.opentrades == 0
            strategy.entry("long" , strategy.long )
        strategy.exit("exit long 1","long",qty_percent = sellqty ,limit = longtakeprofit1,stop = longstoploss1)
        strategy.exit("exit long 2","long",qty_percent = 100 ,limit = longtakeprofit2,stop = longstoploss1)
        if high >= longtakeprofit1
            strategy.cancel("exit long 2")
            strategy.exit("exit long 3","long",qty_percent = 100 ,limit = longtakeprofit2,stop = strategy.position_avg_price)
    else if direction == "Short"
        if longcondition and strategy.opentrades == 0
            strategy.entry("short",strategy.short)
        strategy.exit("exit short 1","short",qty_percent = sellqty ,limit = shorttakeprofit1,stop = shortstoploss1)
        strategy.exit("exit short 2","short",qty_percent = 100 ,limit = shorttakeprofit2,stop = shortstoploss1)
        if low <= shorttakeprofit1
            strategy.cancel("exit short 2")
        strategy.exit("exit short 3","short",qty_percent = 100 ,limit = shorttakeprofit2,stop = strategy.position_avg_price)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
lsl1 = plot(strategy.position_size <= 0 ? na : longstoploss1, color=color.rgb(124, 11, 11), style=plot.style_linebr, linewidth=1)
ltp1 = plot(strategy.position_size <= 0 ? na : longtakeprofit1, color=color.rgb(15, 116, 18), style=plot.style_linebr, linewidth=1)
ltp2 = plot(strategy.position_size <= 0 ? na : longtakeprofit2, color=color.rgb(15, 116, 18), style=plot.style_linebr, linewidth=1)
avg = plot(strategy.position_avg_price, color=color.rgb(255, 153, 0, 47), style=plot.style_linebr, linewidth=1)
fill(ltp1,avg , color =strategy.position_size <= 0 ? na : color.rgb(82, 255, 97, 90))
fill(ltp2,ltp1 , color =strategy.position_size <= 0 ? na : color.rgb(82, 255, 97, 90))
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ssl1 = plot(strategy.position_size >= 0 ? na : shortstoploss1, color=color.red, style=plot.style_linebr, linewidth=1)
stp1 = plot(strategy.position_size >= 0 ? na : shorttakeprofit2, color=color.green, style=plot.style_linebr, linewidth=1)
stp2 = plot(strategy.position_size >= 0 ? na : shorttakeprofit1, color=color.green, style=plot.style_linebr, linewidth=1)
fill(stp1,avg , color =strategy.position_size >= 0 ? na : color.rgb(30, 92, 35, 90))
fill(stp2,stp1 , color =strategy.position_size >= 0 ? na : color.rgb(30, 92, 35, 90))
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
resplot = plot(res, color=ta.change(res) ? na : #bf141446,  linewidth=3, offset=-(rightbars+1), title="res")
supplot = plot(sup, color=ta.change(sup) ? na : #118f113a,  linewidth=3, offset=-(rightbars+1), title="sup")