Chỉ báo chiến lược định lượng phân kỳ giá RSI giám sát động và hệ thống tối ưu hóa thích ứng

RSI TP SL
Ngày tạo: 2025-01-10 16:20:25 sửa đổi lần cuối: 2025-01-10 16:20:25
sao chép: 5 Số nhấp chuột: 421
1
tập trung vào
1617
Người theo dõi

Chỉ báo chiến lược định lượng phân kỳ giá RSI giám sát động và hệ thống tối ưu hóa thích ứng

Tổng quan

Chiến lược này là một hệ thống giao dịch thông minh dựa trên RSI và sự phân kỳ giá. Nó nắm bắt các tín hiệu đảo chiều thị trường bằng cách theo dõi động mối quan hệ phân kỳ giữa chỉ báo RSI và xu hướng giá. Chiến lược này tích hợp các phân số như một xác nhận phụ trợ và được trang bị cơ chế dừng lỗ và dừng lãi thích ứng để thực hiện giao dịch hoàn toàn tự động. Hệ thống hỗ trợ nhiều ứng dụng đa dạng và đa chu kỳ, có tính linh hoạt và tính thực tiễn cao.

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

Logic cốt lõi của chiến lược này dựa trên các yếu tố chính sau:

  1. Xác định phân kỳ RSI: Xác định phân kỳ tiềm ẩn bằng cách so sánh mức cao và mức thấp của chỉ báo RSI và hành động giá. Khi giá đạt mức cao mới nhưng RSI không đạt mức cao mới, tín hiệu bán phân kỳ đỉnh được hình thành; khi giá đạt mức thấp mới nhưng RSI không đạt mức thấp mới, tín hiệu mua phân kỳ đáy được hình thành.
  2. Xác nhận fractal: Sử dụng fractal để phân tích cấu trúc giá, xác nhận tính hợp lệ của sự phân kỳ bằng cách phát hiện mức cao và mức thấp cục bộ và cải thiện độ tin cậy của tín hiệu.
  3. Điều chỉnh tham số: Hệ thống đưa ra tham số độ nhạy để điều chỉnh động khoảng đánh giá fractal nhằm đạt được khả năng thích ứng với các môi trường thị trường khác nhau.
  4. Kiểm soát rủi ro: Tích hợp cơ chế Dừng lỗ và Chốt lời theo phần trăm để đảm bảo rủi ro của mỗi giao dịch có thể kiểm soát được.

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

  1. Độ tin cậy tín hiệu cao: Cơ chế xác nhận kép của phân kỳ RSI và lý thuyết fractal cải thiện đáng kể độ chính xác của tín hiệu giao dịch.
  2. Khả năng thích ứng mạnh mẽ: Chiến lược có thể điều chỉnh linh hoạt các thông số theo các điều kiện thị trường khác nhau và có khả năng thích ứng tốt với môi trường.
  3. Quản lý rủi ro hoàn hảo: Tích hợp cơ chế dừng lỗ và dừng lãi động để kiểm soát hiệu quả mức độ rủi ro của mỗi giao dịch.
  4. Mức độ tự động hóa cao: Toàn bộ quá trình từ nhận dạng tín hiệu đến thực hiện giao dịch đều được tự động hóa, giảm thiểu tác động về mặt cảm xúc của sự can thiệp của con người.
  5. Khả năng mở rộng tốt: Khung chiến lược hỗ trợ nhiều ứng dụng đa dạng và đa chu kỳ, tạo điều kiện thuận lợi cho đầu tư danh mục đầu tư.

Rủi ro chiến lược

  1. Sự phụ thuộc vào môi trường thị trường: Trong một thị trường có xu hướng rõ ràng, độ tin cậy của các tín hiệu phân kỳ có thể giảm và cần phải bổ sung cơ chế lọc xu hướng.
  2. Độ nhạy tham số: Các tham số chính của chiến lược, chẳng hạn như ngưỡng RSI và khoảng đánh giá fractal, cần được gỡ lỗi cẩn thận. Cài đặt tham số không phù hợp có thể ảnh hưởng đến hiệu suất của chiến lược.
  3. Độ trễ tín hiệu: Vì cần phải đợi mô hình phân kỳ hình thành hoàn chỉnh trước khi có thể xác nhận tín hiệu nên có thể có độ trễ thời gian vào lệnh nhất định.
  4. Nhiễu thị trường: Trong một thị trường biến động, các tín hiệu phân kỳ sai có thể được tạo ra và cần phải bổ sung thêm các điều kiện lọc.

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

  1. Tăng cường lọc xu hướng: Giới thiệu các chỉ báo đánh giá xu hướng để lọc các tín hiệu đảo ngược trong thị trường có xu hướng mạnh và cải thiện khả năng thích ứng của các chiến lược trong các môi trường thị trường khác nhau.
  2. Tối ưu hóa việc điều chỉnh tham số: Phát triển cơ chế điều chỉnh tham số động dựa trên sự biến động của thị trường để cải thiện khả năng phản ứng của chiến lược với những thay đổi của thị trường.
  3. Cải thiện kiểm soát rủi ro: đưa ra cơ chế dừng lỗ động để tự động điều chỉnh vị thế dừng lỗ theo biến động của thị trường và tối ưu hóa hiệu quả quản lý quỹ.
  4. Xác nhận tín hiệu nâng cao: Kết hợp các chỉ số vi mô thị trường như khối lượng giao dịch và tính biến động để thiết lập hệ thống xác nhận tín hiệu hoàn thiện hơn.

Tóm tắt

Chiến lược này xây dựng một hệ thống giao dịch mạnh mẽ thông qua sự kết hợp sáng tạo giữa phân kỳ RSI và lý thuyết fractal. Ưu điểm của chiến lược này nằm ở độ tin cậy tín hiệu cao, khả năng thích ứng mạnh mẽ và cơ chế kiểm soát rủi ro hoàn chỉnh. Thông qua quá trình tối ưu hóa và cải tiến liên tục, chiến lược này được kỳ vọng sẽ duy trì hiệu suất ổn định trong nhiều môi trường thị trường khác nhau. Khi áp dụng theo thời gian thực, nên kiểm tra và tối ưu hóa đầy đủ các thông số theo đặc điểm thị trường và thực hiện nghiêm ngặt các biện pháp kiểm soát rủi ro.

Mã nguồn chiến lược
/*backtest
start: 2025-01-02 00:00:00
end: 2025-01-09 00:00:00
period: 5m
basePeriod: 5m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT","balance":49999}]
*/

//FRACTALS
//@version=5

//last : 30m 70 68 22 25 0 0 4.7 11.5

//init
capital=1000
percent=100
fees=0//in percent for each entry and exit

//Inputs
start = input(timestamp("1 Feb 2002"), "Start Time", group = "Date")
end = input(timestamp("1 Feb 2052"), "End Time", group = "Date")

//Strategy
strategy("Divergence Finder (RSI/Price) Strategy with Options", overlay = true, initial_capital=capital, default_qty_value=percent, default_qty_type=strategy.percent_of_equity, commission_type=strategy.commission.percent, calc_on_order_fills=false,process_orders_on_close=true , commission_value=fees, currency=currency.EUR, calc_on_every_tick=true, use_bar_magnifier=false)
//indicator("Divergence Finder (RSI/Price) with Options", overlay=true, max_boxes_count=200, max_bars_back=500,max_labels_count=500)


srcUp=input.source(close, "Source for Price Buy Div", group="sources")
srcDn=input.source(close, "Source for Price Sell Div", group="sources")
srcRsi=input.source(close, "Source for RSI Div", group="sources")


HighRSILimit=input.int(70, "Min RSI for Sell divergence (p1:pre last)", group="signals", inline="1", step=1)
HighRSILimit2=input.int(68, "Min RSI for Sell divergence (p2):last", group="signals", inline="1", step=1)
LowRSILimit=input.int(22, "Min RSI for Buy divergence (p1:pre last)", group="signals", inline="2", step=1)
LowRSILimit2=input.int(25, "Min RSI for Buy divergence (p2:last)", group="signals", inline="2", step=1)


minMarginP=input.float(0, "Min margin between price for displaying divergence (%)", group="signals", step=0.01)
minMarginR=input.float(0, "Min margin between RSI for displaying divergence (%)", group="signals", step=1)

nb=input.int(2, "Sensivity: Determine how many candle will be used to determine last top or bot (too high cause lag, too low cause repaint)", group="Sensivity", inline="3", step=1)


stopPer= input.float(4.7, title='Stop %', group = "Per", inline="3", step=0.01)
tpPer = input.float(11.5, title='TP %', group = "Per", inline="4", step=0.01)

//nb=2
leftBars = nb
rightBars=nb


labels=input.bool(true, "Display Divergence labels", group="Display")
draw=input.bool(true, "Display tops/bottoms")



dnFractal = (close[nb-2] < close[nb]) and (close[nb-1] < close[nb]) and (close[nb+1] < close[nb]) and (close[nb+2] < close[nb])
upFractal = (close[nb-2] > close[nb]) and (close[nb-1] > close[nb]) and (close[nb+1] > close[nb]) and (close[nb+2] > close[nb])
ph=dnFractal
pl=upFractal

plot(dnFractal and draw ? close[nb] : na, style=plot.style_line,offset=-2, color=color.lime, title="tops")
plot(upFractal and draw ? close[nb] : na,  style=plot.style_line, offset=-2, color=color.red, title="botts")

plotchar(dnFractal ? high[nb] : na, char='⮝',location=location.absolute,offset=-2, color=color.rgb(236, 255, 63), title="Down Fractal")
plotchar(upFractal ? low[nb] : na, char='⮟', location=location.absolute, offset=-2, color=color.rgb(67, 227, 255), title="Up Fractal")


float myRSI=ta.rsi(srcRsi, 14)

bool divUp=false
bool divDn=false

//compare lasts bots
p2=ta.valuewhen( ph,srcDn[nb], 0 ) //last price
p1=ta.valuewhen( ph,srcDn[nb], 1 ) //pre last price

r2=ta.valuewhen( ph,myRSI[nb], 0 )  //last rsi
r1=ta.valuewhen( ph,myRSI[nb], 1 )  //pre last rsi


if ph
    if p1 < p2// - (p2 * minMarginP)/100
        if r1 > HighRSILimit and r2 > HighRSILimit2
            if r1 > r2 + (r2 * minMarginR)/100
                divDn:=true

plot(divDn ? close:na, style=plot.style_cross, linewidth=3, color= color.red, offset=-rightBars, title="Sell Div")
if labels and divDn and strategy.position_size >= 0
    label.new(bar_index-nb,high, "Sell Divergence "+str.tostring(p1)+"   "+str.tostring(math.round(r1, 2))+"  "+str.tostring(p2)+"   "+str.tostring(math.round(r2, 2)),xloc=xloc.bar_index,yloc=yloc.abovebar, color = color.red, style = label.style_label_down)
else if divDn and strategy.position_size >= 0
    label.new(bar_index-nb,high, "Sell Divergence",xloc=xloc.bar_index,yloc=yloc.abovebar, color = color.red, style = label.style_label_down)



p2:=ta.valuewhen( pl,srcUp[nb], 0 )
p1:=ta.valuewhen( pl,srcUp[nb], 1 )

r2:=ta.valuewhen( pl,myRSI[nb], 0 )
r1:=ta.valuewhen( pl,myRSI[nb], 1 )


if pl
    if p1 > p2 + (p2 * minMarginP)/100
        if r1 < LowRSILimit and r2 < LowRSILimit2
            if r1 < r2 - (r2 * minMarginR)/100
                divUp:=true
               
plot(divUp ? close:na, style=plot.style_cross, linewidth=3, color= color.green, offset=-rightBars, title="Buy Div")
if labels and divUp and strategy.position_size <= 0
    label.new(bar_index-nb,high, "Buy Divergence "+str.tostring(p1)+"   "+str.tostring(math.round(r1, 2))+"  "+str.tostring(p2)+"   "+str.tostring(math.round(r2, 2)),xloc=xloc.bar_index,yloc=yloc.belowbar, color = color.green, style = label.style_label_up)
else if divUp and strategy.position_size <= 0
    label.new(bar_index-nb,high, "Buy Divergence",xloc=xloc.bar_index,yloc=yloc.belowbar, color = color.green, style = label.style_label_up)


//strat LONG
longEntry = divUp//  and strategy.position_size == 0
longExit = divDn//  and strategy.position_size == 0

//strat SHORT
shortEntry = divDn
shortExit = divUp

LongActive=input(true, title='Activate Long', group = "Directions", inline="2")
ShortActive=input(true, title='Activate Short', group = "Directions", inline="2")
//StopActive=input(false, title='Activate Stop', group = "Directions", inline="2")


//tpActive =  input(false, title='Activate Take Profit', group = "TP", inline="4")
//RR=input(0.5, title='Risk Reward Multiplier', group = "TP")
//QuantityTP = input(100.0, title='Trade Ammount %', group = "TP")


//calc stop
//longStop = strategy.position_avg_price * (1 - stopPer)
//shortStop = strategy.position_avg_price * (1 + stopPer)

longStop = strategy.position_avg_price - (strategy.position_avg_price * stopPer/100)
shortStop = strategy.position_avg_price + (strategy.position_avg_price * stopPer/100)

longTP = strategy.position_avg_price + (strategy.position_avg_price * tpPer/100)
shortTP = strategy.position_avg_price - (strategy.position_avg_price * tpPer/100)

//Calc TP
//longTP = ((strategy.position_avg_price-longStop)*RR+strategy.position_avg_price)
//shortTP = (strategy.position_avg_price-((shortStop-strategy.position_avg_price)*RR))


//display stops
plot(strategy.position_size > 0 ? longStop : na, style=plot.style_linebr, color=color.red, linewidth=1, title="Long Fixed SL")
plot(strategy.position_size < 0 ? shortStop : na, style=plot.style_linebr, color=color.purple, linewidth=1, title="Short Fixed SL")


//display TP
plot(strategy.position_size > 0 ? longTP : na, style=plot.style_linebr, color=color.green, linewidth=1, title="Long Fixed TP")
plot(strategy.position_size < 0 ? shortTP : na, style=plot.style_linebr, color=color.green, linewidth=1, title="Short Fixed TP")

//do
if true
    //check money available
    if strategy.equity > 0
        //if tpActive //Need to put TP before Other exit
        strategy.exit("Close Long", from_entry="Long", limit=longTP,stop=longStop, comment="Close Long with : "+ str.tostring(math.round(strategy.equity)) +" $ ", qty_percent=100)
        strategy.exit("Close Short", from_entry="Short", limit=shortTP,stop=shortStop, comment="Close Short with : "+ str.tostring(math.round(strategy.equity)) +" $ ", qty_percent=100)
        //Set Stops
        //if StopActive
        //    strategy.exit("Stop Long", from_entry="Long", stop=longStop, comment="Stop Long with : "+ str.tostring(math.round(strategy.equity)) +" $ ")
        //    strategy.exit("Stop Short", from_entry="Short", stop=shortStop, comment="Stop Short with : "+ str.tostring(math.round(strategy.equity)) +" $ ")
        if longEntry
            if ShortActive
                strategy.close("Short",comment="Close Short with : "+ str.tostring(math.round(strategy.equity)) +" $ ")
                alert("Close Short")
            if LongActive
                strategy.entry("Long", strategy.long, comment="Open Long with : "+ str.tostring(math.round(strategy.equity)) +" $ ")
                alert("Open Long")
        if longExit
            if LongActive
                strategy.close("Long",comment="Close Long with : "+ str.tostring(math.round(strategy.equity)) +" $ ")
                alert("Close Long")
            if ShortActive
                strategy.entry("Short", strategy.short, comment="Open Short with : "+ str.tostring(math.round(strategy.equity)) +" $ ")
                alert("Open Short")


//alertcondition(longEntry and LongActive, title="Buy Divergence Open", message="Buy Divergence Long Opened!")
//alertcondition(longExit and ShortActive, title="Sell Divergence Open", message="Buy Divergence Short Opened!")

//alertcondition(longExit and LongActive, title="Buy Divergence Closed", message="Buy Divergence Long Closed!")
//alertcondition(longEntry and ShortActive, title="Sell Divergence Closed", message="Buy Divergence Short Closed!")