资源加载中... loading...

RSI枢轴点背离策略

Author: 雨幕(youquant), Date: 2022-05-26 11:16:07
Tags: EMABollWMARSI

带支点的RSI发散,BB,MA是一种基本的RSI振荡器,具有一些有用的功能,它允许用户直接在RSI指示器上显示发散,支点和移动平均值。

可以在RSI振荡器上显示的移动平均值为:

  • SMA
  • EMA
  • SMMA
  • WMA
  • VWMA
  • 布林线

回测测试

img

img

img


/*backtest
start: 2021-11-01 00:00:00
end: 2022-05-25 00:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
args: [["ContractType","rb2210",360008]]
*/

////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                      ====== My Crypto Succubus ======                      //
//   ====== RSI Divergence with Pivot, BB, SMA, EMA, SMMA, WMA, VWMA ======   //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// My Crypto Succubus is a project based on the community and  participatory  //
// aspect, knowledge sharing is the core of the project, the act of sharing   //
// is destined to get richer, either on the intellectual or the wealth        //
// side, the ultimate goal of MCS is that every single one of our members     //
// can reach the financial freedom we all deserve.                            //
//                                                                            //
//   ====== Join us on our Discord : https://discord.gg/TmW6RQeyXp  ======    //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
//@version=5
indicator(title="RSI Divergences with Pivots, BB, MA [My Crypto Succubus]", shorttitle="RSI Div, Pivot, BB, MA [My Crypto Succubus]", format=format.price, precision=2)

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

rsiLengthInput = input.int(8, minval=1, title="RSI周期", group="RSI Settings")
rsiSourceInput = input.source(close, title="数据源", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(8, title="MA周期", group="MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB标准差", group="MA Settings")

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"

plot(rsi, "RSI", color=#ffffff, linewidth=1)
plot(rsiMA, "RSI-based MA", color=color.yellow)
rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(30, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(178, 8, 120, 90), title="RSI Background Fill")
bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper Bollinger Band", color=color.green)
bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower Bollinger Band", color=color.green)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill")

///////////////////////////////////////////////////////////////////////////////////////////////////////

len = input.int(6, minval=1, title='RSI周期', group="RSI Settings")
ob = input.int(defval=70, title='超买', minval=0, maxval=100, group="RSI Settings")
os = input.int(defval=30, title='超卖', minval=0, maxval=100, group="RSI Settings")

// RSI code
band1 = hline(ob)
band0 = hline(os)
plot(rsi, color=rsi > ob or rsi < os ? color.rgb(255, 0, 0, 0) : color.new(color.black, 100), linewidth=1)
fill(band1, band0, color=color.new(color.purple, 97))

// DIVS code
piv = input(false, '隐藏枢轴?')
// shrt = input(false, 'Shorter labels?')
// hidel = input(true, 'Hide labels and color background')
xbars = input.int(defval=90, title='Div回溯周期(K线柱)?', minval=1)
hb = math.abs(ta.highestbars(rsi, xbars))  // Finds bar with highest value in last X bars
lb = math.abs(ta.lowestbars(rsi, xbars))  // Finds bar with lowest value in last X bars

// Defining variable values, mandatory in Pine 3
max = float(na)
max_rsi = float(na)
min = float(na)
min_rsi = float(na)
pivoth = bool(na)
pivotl = bool(na)
divbear = bool(na)
divbull = bool(na)

// If bar with lowest / highest is current bar, use it's value
max := hb == 0 ? close : na(max[1]) ? close : max[1]
max_rsi := hb == 0 ? rsi : na(max_rsi[1]) ? rsi : max_rsi[1]
min := lb == 0 ? close : na(min[1]) ? close : min[1]
min_rsi := lb == 0 ? rsi : na(min_rsi[1]) ? rsi : min_rsi[1]

// Compare high of current bar being examined with previous bar's high
// If curr bar high is higher than the max bar high in the lookback window range
if close > max  // we have a new high
    max := close  // change variable "max" to use current bar's high value
    max
if rsi > max_rsi  // we have a new high
    max_rsi := rsi  // change variable "max_rsi" to use current bar's RSI value
    max_rsi
if close < min  // we have a new low
    min := close  // change variable "min" to use current bar's low value
    min
if rsi < min_rsi  // we have a new low
    min_rsi := rsi  // change variable "min_rsi" to use current bar's RSI value
    min_rsi

// Finds pivot point with at least 2 right candles with lower value
pivoth := max_rsi == max_rsi[2] and max_rsi[2] != max_rsi[3] ? true : na
pivotl := min_rsi == min_rsi[2] and min_rsi[2] != min_rsi[3] ? true : na

// Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences
if max[1] > max[2] and rsi[1] < max_rsi and rsi <= rsi[1]
    divbear := true
    divbear
if min[1] < min[2] and rsi[1] > min_rsi and rsi >= rsi[1]
    divbull := true
    divbull

// Alerts
alertcondition(divbear, title='Bear div', message='Bear div')
alertcondition(divbull, title='Bull div', message='Bull div')
alertcondition(pivoth, title='Pivot high', message='Pivot high')
alertcondition(pivotl, title='Pivot low', message='Pivot low')

// Plots divergences and pivots with offest
//l = divbear ? label.new(bar_index - 1, rsi[1] + 1, 'BEAR', color=color.red, textcolor=color.white, style=label.style_label_down, yloc=yloc.price, size=size.small) : divbull ? label.new(bar_index - 1, rsi[1] - 1, 'BULL', color=color.green, textcolor=color.white, style=label.style_label_up, yloc=yloc.price, size=size.small) : pivoth ? label.new(bar_index - 2, max_rsi + 1, 'PIVOT', color=color.blue, textcolor=color.white, style=label.style_label_down, yloc=yloc.price, size=size.small) : pivotl ? label.new(bar_index - 2, min_rsi - 1, 'PIVOT', color=color.blue, textcolor=color.white, style=label.style_label_up, yloc=yloc.price, size=size.small) : na

// Shorter labels
//if shrt
//    label.set_text(l, na)
// Hides pivots or labels
//if piv and (pivoth or pivotl) or hidel
//    label.delete(l)
// Colors indicator background
//bgcolor(hidel ? divbear ? color.new(color.red, 50) : divbull ? color.new(color.green, 50) : na : na, offset=-1, transp=90)
//bgcolor(hidel ? piv ? na : pivoth or pivotl ? color.new(color.blue, 50) : na : na, offset=-2, transp=90)

// Debug tools
// plot(max, color=blue, linewidth=2)
// plot(max_rsi, color=red, linewidth=2)
// plot(hb, color=orange, linewidth=2)
// plot(lb, color=purple, linewidth=1)
// plot(min_rsi, color=lime, linewidth=1)
// plot(min, color=black, linewidth=1)

////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                      ====== My Crypto Succubus ======                      //
//   ====== RSI Divergence with Pivot, BB, SMA, EMA, SMMA, WMA, VWMA ======   //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// My Crypto Succubus is a project based on the community and  participatory  //
// aspect, knowledge sharing is the core of the project, the act of sharing   //
// is destined to get richer, either on the intellectual or the wealth        //
// side, the ultimate goal of MCS is that every single one of our members     //
// can reach the financial freedom we all deserve.                            //
//                                                                            //
//   ====== Join us on our Discord : https://discord.gg/TmW6RQeyXp  ======    //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

if divbear
    strategy.entry("Enter Long", strategy.long)
else if divbull
    strategy.entry("Enter Short", strategy.short)
template: strategy.tpl:40:21: executing "strategy.tpl" at <.api.GetStrategyListByName>: wrong number of args for GetStrategyListByName: want 7 got 6