Chiến lược phân kỳ RSI dựa trên pivot

Tác giả:ChaoZhang, Ngày: 2023-11-28 13:43:05
Tags:

img

Tổng quan

Chiến lược này được gọi là Chiến lược Phân biệt RSI dựa trên trục trục. Nó sử dụng sự khác biệt giữa các chỉ số RSI ở các chu kỳ khác nhau để xác định các điểm nhập và thoát, và thêm RSI dài hạn như một điều kiện lọc để cải thiện sự ổn định của chiến lược.

Chiến lược logic

Chiến lược chủ yếu đánh giá cơ hội mua khi có sự khác biệt tăng ẩn hoặc sự khác biệt tăng thường xuyên giữa chỉ số RSI ngắn hạn (như chỉ số RSI 5 ngày) và giá; và bán khi có sự khác biệt giảm ẩn hoặc sự khác biệt giảm thường xuyên.

Một sự chênh lệch tăng thường xuyên có nghĩa là giá tạo ra mức thấp mới trong khi chỉ số RSI không tạo ra mức thấp mới; một sự chênh lệch tăng ẩn là ngược lại, giá không tạo ra mức thấp mới trong khi chỉ số RSI tạo ra mức thấp mới.

Ngoài ra, chiến lược cũng giới thiệu RSI dài hạn (như RSI 50 ngày) như một điều kiện lọc. Nó chỉ xem xét tín hiệu mua khi RSI dài lớn hơn 50; và xem xét dừng lỗ hoặc lấy lợi nhuận khi RSI dài nhỏ hơn 30.

Ưu điểm

Ưu điểm lớn nhất của chiến lược này là nó sử dụng cả các tín hiệu chênh lệch RSI về ngắn hạn và bộ lọc của RSI dài hạn, có thể tránh bị mắc kẹt và bỏ lỡ xu hướng ở một mức độ nào đó.

  1. Tín hiệu chênh lệch RSI ngắn hạn có thể đưa ra đánh giá sớm về cơ hội đảo ngược giá và kịp thời nắm bắt các điểm chuyển đổi;
  2. Bộ lọc RSI dài hạn tránh đi dài mù khi xu hướng không chắc chắn;
  3. Nhiều loại phương pháp lấy lợi nhuận và lấy lợi nhuận một phần giúp giảm rủi ro;
  4. Cơ chế kim tự tháp cho phép thêm các vị trí và mở rộng tiềm năng lợi nhuận hơn nữa.

Rủi ro

Chiến lược cũng có một số rủi ro cần lưu ý:

  1. Sự chênh lệch RSI không phải lúc nào cũng hợp lệ và có thể có tín hiệu sai;
  2. Rủi ro sẽ tăng lên sau khi làm kim tự tháp.
  3. Các thiết lập lợi nhuận không chính xác cũng có thể dẫn đến lợi nhuận sớm hoặc lợi nhuận không đủ.

Các biện pháp quản lý rủi ro tương ứng bao gồm: thiết lập hợp lý các điều kiện dừng lỗ / lấy lợi nhuận, kiểm soát kích thước vị trí, lấy lợi nhuận một phần để làm mịn đường cong vốn chủ sở hữu, v.v.

Hướng dẫn tối ưu hóa

Có chỗ cho việc tối ưu hóa thêm chiến lược:

  1. Các thông số RSI có thể được tối ưu hóa thêm để tìm ra sự kết hợp tốt nhất;
  2. Các tín hiệu phân kỳ của các chỉ số khác như MACD và KD có thể được kiểm tra;
  3. Các thông số có thể được tối ưu hóa cụ thể cho một số sản phẩm (như dầu thô, kim loại quý, v.v.) để cải thiện khả năng thích nghi.

Tóm lại

Chiến lược này kết hợp các tín hiệu chênh lệch RSI dài / ngắn của ngắn hạn và dài hạn để cải thiện lợi nhuận trong khi kiểm soát rủi ro. Nó phản ánh nhiều nguyên tắc trong thiết kế chiến lược định lượng, bao gồm thời điểm tham gia, thời điểm thoát, lấy lợi nhuận một phần, thiết lập dừng lỗ / lấy lợi nhuận, v.v. Đây là một chiến lược chênh lệch RSI điển hình để tham khảo.


/*backtest
start: 2023-11-20 00:00:00
end: 2023-11-27 00:00:00
period: 5m
basePeriod: 1m
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/
// © mohanee

//@version=4

//GOOGL setting  5 ,50 close, 3 , 1  profitLevel at 75 and No stop Loss shows win rate 99.03 % profit factor 5830.152

strategy(title="RSI5_50 with Divergence", overlay=false,pyramiding=2, default_qty_type=strategy.fixed, default_qty_value=3,    initial_capital=10000, currency=currency.USD)

len = input(title="RSI Period", minval=1, defval=5)
longRSILen = input(title="Long RSI Period", minval=10, defval=50)
src = input(title="RSI Source", defval=close)
lbR = input(title="Pivot Lookback Right", defval=3)
lbL = input(title="Pivot Lookback Left", defval=1)
takeProfitRSILevel = input(title="Take Profit at RSI Level", minval=50, defval=75)
stopLoss = input(title="Stop Loss%(if checked 8% rule applied)", defval=false)

shortTermRSI = rsi(close,len)
longTermRSI = rsi(close,longRSILen)

rangeUpper = input(title="Max of Lookback Range", defval=60)
rangeLower = input(title="Min of Lookback Range", defval=5)
plotBull = input(title="Plot Bullish", defval=true)
plotHiddenBull = input(title="Plot Hidden Bullish", defval=true)
plotBear = input(title="Plot Bearish", defval=true)
plotHiddenBear = input(title="Plot Hidden Bearish", defval=false)


bearColor = color.purple
bullColor = color.green
hiddenBullColor = color.new(color.green, 80)
hiddenBearColor = color.new(color.red, 80)
textColor = color.white
noneColor = color.new(color.white, 100)



plot(shortTermRSI, title="RSI", linewidth=2, color=#8D1699)
plot(longTermRSI, title="longTermRSI", linewidth=2, color=color.orange)

hline(50, title="Middle Line", linestyle=hline.style_dotted)
obLevel = hline(70, title="Overbought", linestyle=hline.style_dotted)
osLevel = hline(30, title="Oversold", linestyle=hline.style_dotted)
fill(obLevel, osLevel, title="Background", color=longTermRSI >=50 ? color.green:color.purple, transp=65)  // longTermRSI >=50

plFound = na(pivotlow(shortTermRSI, lbL, lbR)) ? false : true
phFound = na(pivothigh(shortTermRSI, lbL, lbR)) ? false : true

_inRange(cond) =>
    bars = barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper

//------------------------------------------------------------------------------
// Regular Bullish

// shortTermRSI: Higher Low
oscHL = shortTermRSI[lbR] > valuewhen(plFound, shortTermRSI[lbR], 1) and _inRange(plFound[1])

// Price: Lower Low
priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)

bullCond = plotBull and priceLL and oscHL and plFound

plot(
	 plFound ? shortTermRSI[lbR] : na,
	 offset=-lbR,
	 title="Regular Bullish",
	 linewidth=2,
	 color=(bullCond ? bullColor : noneColor),
	 transp=0
	 )


plotshape(
	 bullCond ? shortTermRSI[lbR] : na,
	 offset=-lbR,
	 title="Regular Bullish Label",
	 text=" Bull ",
	 style=shape.labelup,
	 location=location.absolute,
	 color=bullColor,
	 textcolor=textColor,
	 transp=0
	 )

//------------------------------------------------------------------------------
// Hidden Bullish

// shortTermRSI: Lower Low
oscLL = shortTermRSI[lbR] < valuewhen(plFound, shortTermRSI[lbR], 1) and _inRange(plFound[1])

// Price: Higher Low
priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)

hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound

plot(
	 plFound ? shortTermRSI[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bullish",
	 linewidth=2,
	 color=(hiddenBullCond ? hiddenBullColor : noneColor),
	 transp=0
	 )

plotshape(
	 hiddenBullCond ? shortTermRSI[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bullish Label",
	 text=" H Bull ",
	 style=shape.labelup,
	 location=location.absolute,
	 color=bullColor,
	 textcolor=textColor,
	 transp=0
	 )

longCondition= longTermRSI >=50  and ( (bullCond or hiddenBullCond ) )  or  (strategy.position_size>0 and crossover(shortTermRSI,20) )
//last condition above is to leg in if you are already in the Long trade, 


strategy.entry(id="RSIDivLE", long=true,  when=longCondition)


//------------------------------------------------------------------------------
// Regular Bearish

// shortTermRSI: Lower High
oscLH = shortTermRSI[lbR] < valuewhen(phFound, shortTermRSI[lbR], 1) and _inRange(phFound[1])

// Price: Higher High
priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)

bearCond = plotBear and priceHH and oscLH and phFound

plot(
	 phFound ? shortTermRSI[lbR] : na,
	 offset=-lbR,
	 title="Regular Bearish",
	 linewidth=2,
	 color=(bearCond ? bearColor : noneColor),
	 transp=0
	 )

plotshape(
	 bearCond ? shortTermRSI[lbR] : na,
	 offset=-lbR,
	 title="Regular Bearish Label",
	 text=" Bear ",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=bearColor,
	 textcolor=textColor,
	 transp=0
	 )

//------------------------------------------------------------------------------
// Hidden Bearish

// shortTermRSI: Higher High
oscHH = shortTermRSI[lbR] > valuewhen(phFound, shortTermRSI[lbR], 1) and _inRange(phFound[1])

// Price: Lower High
priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)

hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound

plot(
	 phFound ? shortTermRSI[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bearish",
	 linewidth=2,
	 color=(hiddenBearCond ? hiddenBearColor : noneColor),
	 transp=0
	 )

plotshape(
	 hiddenBearCond ? shortTermRSI[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bearish Label",
	 text=" H Bear ",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=bearColor,
	 textcolor=textColor,
	 transp=0
	 )
	 
	 
//calculate stop Loss
stopLossVal = stopLoss==true ? ( strategy.position_avg_price -  (strategy.position_avg_price*0.08) ) : 0

//partial profit
strategy.close(id="RSIDivLE", comment="TP1", qty=strategy.position_size*3/4, when=strategy.position_size>0 and (longTermRSI>=takeProfitRSILevel or crossover(longTermRSI,90)))
strategy.close(id="RSIDivLE",comment="TP2",   qty=strategy.position_size*3/4 , when=crossover(longTermRSI,70))
strategy.close(id="RSIDivLE",comment="TP3",   qty=strategy.position_size/2, when=crossover(longTermRSI,65))
strategy.close(id="RSIDivLE",comment="TP4",   qty=strategy.position_size/2 , when=crossover(longTermRSI,60))

//close the whole position when stoploss hits or longTermRSI goes below 30
strategy.close(id="RSIDivLE",comment="Exit",    when=crossunder(longTermRSI,30) or close<stopLossVal)



Thêm nữa