RSI Div Finder 2.0

scarf · study · 113 行 · 点赞 1,711 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=3
study(title='RSI Div Finder 2.0', overlay=true)
 
//  ||  General Input:
src = input(title="Source", type=source, defval=close)
lengthRSI   = input(defval=14, minval=1, title="RSI")
uReg = input(true,title="Show Regular Divergences")
uHid = input(false,title="Show Hidden Divergences")
SHOW_LABEL = input(title='Show Labels', type=bool, defval=true)
SHOW_CHANNEL = input(title='Show Channel', type=bool, defval=false)
SHOW_DIVLINES = input(title='Show Divergence Lines', type=bool, defval=true) 
 
//  ||  Filter Divergences Input:
filter_divergences = true //input(title='Filter Divergences?', defval=true, type=bool)
show_filtered = input(title='Show Filtered Ghost Divergences?', defval=true, type=bool)
n_bar_min = input(title='Minimum bar length:', defval=6)
n_bar_max = input(title='Maximum bar length:', defval=1000)
price_change_min = input(title='Minimum price change %:', defval=0.1, type=float, step=0.02)
price_change_max = input(title='Maximum price change %:', defval=100.0, type=float, step=0.02)
 
//  ||  Functions:
f_top_fractal(_src)=>_src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and _src[2] > _src[0]
f_bot_fractal(_src)=>_src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and _src[2] < _src[0]
f_fractalize(_src)=>f_top_fractal(_src) ? 1 : f_bot_fractal(_src) ? -1 : 0
 
OverSold = 25
OverBought = 75
 
//len = input(14, minval=1, title="Length")
up = rma(max(change(src), 0), lengthRSI)
down = rma(-min(change(src), 0), lengthRSI)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
 
//  ||  Method selection
// These rsi() functions generate different RSI lines to the built in code. WTF? But the divs are more accurate
oscilator_high = rsi(high, lengthRSI)
oscilator_low = rsi(low, lengthRSI)
 
fractal_top = f_fractalize(oscilator_high) > 0 ? oscilator_high[2] : na
fractal_bot = f_fractalize(oscilator_low) < 0 ? oscilator_low[2] : na
 
high_prev = valuewhen(fractal_top, oscilator_high[2], 0)[2]
high_price = valuewhen(fractal_top, high[2], 0)[2]
low_prev = valuewhen(fractal_bot, oscilator_low[2], 0)[2] 
low_price = valuewhen(fractal_bot, low[2], 0)[2]
 
regular_bearish_div = fractal_top and high[2] > high_price and oscilator_high[2] < high_prev and uReg
hidden_bearish_div = fractal_top and high[2] < high_price and oscilator_high[2] > high_prev and uHid
regular_bullish_div = fractal_bot and low[2] < low_price and oscilator_low[2] > low_prev and uReg
hidden_bullish_div = fractal_bot and low[2] > low_price and oscilator_low[2] < low_prev and uHid
 
//  |   Filter divergences:
//debug_var_1 = na, debug_var_2 = na, debug_var_3 = na, debug_var_4 = na, debug_var_5 = na, debug_var_6 = na
filter_bull = false
filter_bear = false
if filter_divergences
    if regular_bearish_div or hidden_bearish_div
        bars_from_previous = n[2] - valuewhen(fractal_top, n[2], 0)[2]
        previous_price = valuewhen(fractal_top, high[2], 0)[2]
        price_perc_change = ((abs(high[2] - previous_price) / previous_price) * 100)
        bars_check = bars_from_previous >= n_bar_min and bars_from_previous <= n_bar_max ? true : false
        price_check = price_perc_change >= price_change_min and price_perc_change <= price_change_max ? true : false
        if bars_check and price_check
            filter_bear := true
            // debug_var_1 := price_perc_change
            // debug_var_2 := bars_from_previous
            // debug_var_3 := previous_price
    if regular_bullish_div or hidden_bullish_div
        bars_from_previous = n[2] - valuewhen(fractal_bot, n[2], 0)[2]
        previous_price = valuewhen(fractal_bot, low[2], 0)[2]
        price_perc_change = ((abs(low[2] - previous_price) / previous_price) * 100)
        bars_check = bars_from_previous >= n_bar_min and bars_from_previous <= n_bar_max ? true : false
        price_check = price_perc_change >= price_change_min and price_perc_change <= price_change_max ? true : false
        if bars_check and price_check
            filter_bull := true
            // debug_var_4 := price_perc_change
            // debug_var_5 := bars_from_previous
            // debug_var_6 := previous_price
 
// plot(debug_var_1, color=blue, offset=-2)
// plot(debug_var_2, color=blue, offset=-2)
// plot(debug_var_3, color=blue, offset=-2)
// plot(debug_var_4, color=blue, offset=-2)
// plot(debug_var_5, color=blue, offset=-2)
// plot(debug_var_6, color=blue, offset=-2)
 
invert_sigs = input(title="Invert Signal Colours", type=bool, defval=false)
 
// Code inverts logic into color section
plotshape(filter_divergences and filter_bear and regular_bearish_div ? high[2] : na, text='R', style=shape.labeldown, location=location.absolute, color=not invert_sigs ? color(maroon,0) : na, textcolor=not invert_sigs ? color(white,0) : color(maroon,0), offset=-2)
plotshape(filter_divergences and filter_bull and regular_bullish_div ? low[2]  : na, text='R', style=shape.labelup,   location=location.absolute, color=not invert_sigs ? color(green,0) :  na, textcolor=not invert_sigs ? color(white,0) : color(green,0), offset=-2) 
plotshape(filter_divergences and filter_bear and hidden_bearish_div ? high[2] : na, text='H', style=shape.labeldown, location=location.absolute, color=not invert_sigs ? color(maroon,0) : na, textcolor=not invert_sigs ? color(white,0) : color(maroon,0), offset=-2)
plotshape(filter_divergences and filter_bull and hidden_bullish_div ? low[2]  : na, text='H', style=shape.labelup,   location=location.absolute, color=not invert_sigs ? color(green,0) :  na, textcolor=not invert_sigs ? color(white,0) : color(green,0), offset=-2) 
 
//plot(filter_divergences and filter_bear and fractal_top ? high[2] : na, color=(regular_bearish_div and uReg and SHOW_DIVLINES) ? maroon : na, offset=-2, linewidth=1, title='High Fractal')
//plot(filter_divergences and filter_bull and fractal_bot ? low[2] : na, color=(regular_bullish_div and uReg and SHOW_DIVLINES) ? green : na, offset=-2, linewidth=1, title='Low Fractal')
 
plot(fractal_top ? high[2] : na, color=(filter_divergences and filter_bear and regular_bearish_div and uReg and SHOW_DIVLINES) or (filter_divergences and filter_bear and hidden_bearish_div and uHid and SHOW_DIVLINES) ? maroon : na, offset=-2, linewidth=1, title='High Fractal')
plot(fractal_bot ? low[2]  : na, color=(filter_divergences and filter_bull and regular_bullish_div and uReg and SHOW_DIVLINES) or (filter_divergences and filter_bull and hidden_bullish_div and uHid and SHOW_DIVLINES)  ? green : na, offset=-2, linewidth=1, title='Low Fractal')
 
//plot(fractal_top ? high[2] : na, style=circles, color=(regular_bearish_div and uReg and SHOW_DIVLINES) or (hidden_bearish_div and uHid and SHOW_DIVLINES) ? maroon : not SHOW_CHANNEL ? na : silver, linewidth=1, offset=-2, title='High Dot')
//plot(fractal_bot ? low[2] : na, style=circles, color=(regular_bullish_div and uReg and SHOW_DIVLINES) or (hidden_bullish_div and uHid and SHOW_DIVLINES) ? green : not SHOW_CHANNEL ? na : silver, linewidth=1, offset=-2, title='Low Dot')
 
plotshape(title='regular_bearish_div', series=not SHOW_LABEL or not uReg? na : show_filtered and regular_bearish_div ? high[2] : na, text='R', style=shape.labeldown, location=location.absolute, color=color(maroon,80), textcolor=white, offset=-2)
plotshape(title='hidden_bearish_div', series=not SHOW_LABEL or not uHid? na : show_filtered and hidden_bearish_div ? high[2] : na, text='H', style=shape.labeldown, location=location.absolute, color=color(maroon,80), textcolor=white, offset=-2)
plotshape(title='regular_bullish_div', series=not SHOW_LABEL or not uReg? na : show_filtered and regular_bullish_div ? low[2] : na, text='R', style=shape.labelup, location=location.absolute, color=color(green,80), textcolor=white, offset=-2)
plotshape(title='hidden_bullish_div', series=not SHOW_LABEL or not uHid? na : show_filtered and hidden_bullish_div ? low[2] : na, text='H', style=shape.labelup, location=location.absolute, color=color(green,80), textcolor=white, offset=-2)
plot(show_filtered and fractal_top ? high[2] : na, color=(regular_bearish_div and uReg and SHOW_DIVLINES) or (hidden_bearish_div and uHid and SHOW_DIVLINES) ? color(maroon,80) : na, offset=-2, linewidth=1, title='High Fractal Fade')
plot(show_filtered and fractal_bot ? low[2]  : na, color=(regular_bullish_div and uReg and SHOW_DIVLINES) or (hidden_bullish_div and uHid and SHOW_DIVLINES) ? color(green,80) : na, offset=-2, linewidth=1, title='Low Fractal Fade')
 
alertcondition(filter_divergences and filter_bear and regular_bearish_div, title='RSI Div: Short', message='RSI Div: Short ')
alertcondition(filter_divergences and filter_bull and regular_bullish_div, title='RSI Div: Long', message='RSI Div: Long ')
alertcondition(filter_divergences and filter_bear and (regular_bearish_div or regular_bullish_div), title='RSI Div: Signal', message='RSI Div: Signal ')

← 返回列表