Divergence Detector [TradingFinder] RSI + MACD + AO Oscillator Reversal

TradingFinder · study · 281 行 · 点赞 2,590 · TradingView 原页

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

Pine Script

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TFlab
 
//@version=5
indicator("Divergence Detector [TradingFinder] RSI + MACD + AO Oscillator Reversal", shorttitle="Divergence Oscillator: MACD , RSI ,AO", overlay=false ,max_bars_back = 5000 , max_labels_count = 500, max_lines_count = 500)
ATR = ta.atr(55)
 
//MADC /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
[macdLine, signalLine, Hist_MACD] = ta.macd(close, 12, 26, 9)
//AO /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
Hist_AO = ta.sma(hl2,5) - ta.sma(hl2,34)
 
//AO /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
RSI_Adj = ta.rsi(close, 14) - 50
 
// Define "n" as the number of periods and keep a minimum value of 2 for error handling.
n = input.int(title="Fractal Periods", defval=4, minval=2 , group = 'Logic Setting')
// switch Method
 
string Hist_Type = input.string("MACD", "Divergence Detecte Method", options = ["MACD", "RSI" ,"AO"], group = 'Logic Setting')
float Hist = switch Hist_Type
    "MACD" => Hist_MACD
    "AO" => Hist_AO
    "RSI" => RSI_Adj
 
Show_Table = input.string('Yes' , 'Show Table' , options = ['Yes' , 'No'], group = 'display Setting')
Show_Label = input.string('Yes' , 'Show Label' , options = ['Yes' , 'No'], group = 'display Setting')
 
 
//Trend/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
EMA = ta.ema(close, 50)
up_trend =close[n] > EMA
down_trend = close[n] < EMA
 
//Fractal////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
// UpFractal
UpPivot = ta.pivothigh(n,n)
 
// downFractal
downPivot = ta.pivotlow(n,n)
 
 
 
upFractal = UpPivot and up_trend 
downFractal = downPivot and down_trend 
 
// plotshape(downFractal and Show_Fractal =='Yes', style=shape.triangleup, location=location.belowbar, offset=-n, color= color.rgb(57, 160, 60), size = size.tiny)
// plotshape(upFractal and Show_Fractal =='Yes', style=shape.triangledown,   location=location.abovebar, offset=-n, color= color.rgb(236, 69, 69), size = size.tiny)
 
//Data//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
// Bearish Divergennce Data
 
//price Data
High_Last_Price = ta.valuewhen(upFractal,high[n],0)
High_Per_Price = ta.valuewhen(upFractal,high[n],1)
 
//MACD hist Data
High_Last_Hist = ta.valuewhen(upFractal, Hist[n],0)
High_Per_Hist = ta.valuewhen(upFractal, Hist[n],1)
 
//Bar index Data
High_Last_Bar = ta.valuewhen(upFractal, bar_index[n],0)
High_Per_Bar = ta.valuewhen(upFractal, bar_index[n],1)
 
// Time Condition Bullish
Time_Condition_Bear = (High_Last_Bar + 30) > bar_index
 
 
//Bearish Divergenc Detector////////////////////////
Last_Bearish_Divergece = if High_Last_Hist > 0 and High_Per_Hist > 0 and Time_Condition_Bear and (High_Last_Bar - High_Per_Bar) < 30
    (High_Last_Price > High_Per_Price) and (High_Last_Hist < High_Per_Hist) 
else 
    false
//count_Bear_Divergence
var int CBearDive = 0 
 
if ta.change(High_Last_Price) and Last_Bearish_Divergece == true
    CBearDive := CBearDive + 1
else if Last_Bearish_Divergece == false
    CBearDive := 0
 
//Detecte change phase in Bearish Divergence
 
var bool Bear_Phase = na 
 
len_back_for_Bear_Phase_check = if not na(High_Last_Bar - High_Per_Bar)
    High_Last_Bar - High_Per_Bar
else 
    1
 
if ta.change(High_Last_Price) and Last_Bearish_Divergece == true
    for i = (n+1) to (n+1) + len_back_for_Bear_Phase_check
        if Hist[i] < 0 
            Bear_Phase := true
else if Last_Bearish_Divergece == false
    Bear_Phase := false
 
 
 
// Bullish Divergennce Data
 
//price Data
Low_Last_Price = ta.valuewhen(downFractal,low[n],0)
Low_Per_Price = ta.valuewhen(downFractal,low[n],1)
 
//MACD hist Data
Low_Last_Hist = ta.valuewhen(downFractal, Hist[n],0)
Low_Per_Hist = ta.valuewhen(downFractal, Hist[n],1)
 
//Bar index Data
Low_Last_Bar = ta.valuewhen(downFractal, bar_index[n],0)
Low_Per_Bar = ta.valuewhen(downFractal, bar_index[n],1)
 
 
 
// Time Condition Bullish
Time_Condition_Bull = (Low_Last_Bar + 30) > bar_index 
 
//Bullish Divergenc Detector////////////////////////
Last_Bullish_Divergece = if Low_Last_Hist < 0 and Low_Per_Hist < 0 and Time_Condition_Bull and (Low_Last_Bar - Low_Per_Bar) < 30
    (Low_Last_Price < Low_Per_Price) and (Low_Last_Hist > Low_Per_Hist)
else 
    false
//count_Bear_Divergence
var int CBullDive = 0 
 
   
if ta.change(Low_Last_Price) and Last_Bullish_Divergece == true
    CBullDive := CBullDive + 1
else if Last_Bullish_Divergece == false
    CBullDive := 0
 
 
//Detecte and count change phase in Bullish Divergence
//var int CBullPhase = 0
var bool Bull_Phase = na 
len_back_for_Bull_Phase_check = if not na(Low_Last_Bar - Low_Per_Bar)
    Low_Last_Bar - Low_Per_Bar
else 
    1
 
if ta.change(Low_Last_Price) and Last_Bullish_Divergece == true
    for i = (n+1) to (n+1) + len_back_for_Bull_Phase_check
        if Hist[i] > 0 
            //CBullPhase := CBullPhase + 1
            Bull_Phase := true
else if Last_Bullish_Divergece == false
    Bull_Phase := false
 
//Score Divergence
text_power_bull = if CBullDive == 1
    "Normal Bull Dive"
else if CBullDive == 2
    "Good Bull Dive"
else if CBullDive >= 3
    "Strong Bull Dive"
else 
    "-"
 
text_power_bear = if CBearDive == 1
    "Normal Bear Dive"
else if CBearDive == 2
    "Good Bear Dive"
else if CBearDive >= 3
    "Strong Bear Dive"
else 
    "-"
 
 
 
 
 
//Table
table bear_div_table = na
 
bear_div_table := table.new(position = position.top_right,
         columns = 3,
         rows = 5, 
         bgcolor = color.rgb(235, 234, 234, 75), 
         frame_width = 1,
         frame_color = color.black,
         border_color = color.black,
         border_width = 1)
a = false
if Show_Table == 'Yes'
    table.cell(table_id = bear_div_table, column = 0, row = 0, text = "Type Divergence : " , text_size = size.small)
    table.cell(table_id = bear_div_table, column = 0, row = 1, text = "Exist : ", text_size = size.small)
    table.cell(table_id = bear_div_table, column = 0, row = 2, text = "Consecutive : " , text_size = size.small)
 
    table.cell(table_id = bear_div_table, column = 1, row = 0, text = "Bearish Divergence", text_color = color.rgb(216, 66, 66), text_size = size.small)
    table.cell(table_id = bear_div_table, column = 1, row = 1, text = Last_Bearish_Divergece ? "+" : "-", text_color = color.rgb(216, 66, 66), text_size = size.small)
    table.cell(table_id = bear_div_table, column = 1, row = 2, text = str.tostring(CBearDive), text_size = size.small)
 
    table.cell(table_id = bear_div_table, column = 2, row = 0, text = "Bullish Divergence", text_color = color.rgb(40, 177, 52), text_size = size.small)
    table.cell(table_id = bear_div_table, column = 2, row = 1, text = Last_Bullish_Divergece ? "+" : "-", text_color = color.rgb(40, 177, 52), text_size = size.small)
    table.cell(table_id = bear_div_table, column = 2, row = 2, text = str.tostring(CBullDive), text_size = size.small)
 
    table.cell(table_id = bear_div_table, column = 0, row = 3, text = "Divergence Quality : " , text_size = size.small)
    table.cell(table_id = bear_div_table, column = 1, row = 3, text = text_power_bear, text_color = color.rgb(216, 66, 66), text_size = size.small)
    table.cell(table_id = bear_div_table, column = 2, row = 3, text = text_power_bull, text_color = color.rgb(40, 177, 52), text_size = size.small)
 
    //Check Change Phase
    table.cell(table_id = bear_div_table, column = 0, row = 4, text = "Change Phase Indicator : ", text_size = size.small)
    table.cell(table_id = bear_div_table, column = 1, row = 4, text = Bear_Phase ? "+" : "-", text_color = color.rgb(216, 66, 66), text_size = size.small)
    table.cell(table_id = bear_div_table, column = 2, row = 4, text = Bull_Phase ? "+" : "-", text_color = color.rgb(40, 177, 52), text_size = size.small)
 
 
// plot
 
//Divergence Line & Label
 
 
Drawing()=>
    var line BeLine = na
    var line BuLine = na 
    var label BeLabel = na
    var label BuLabel = na
 
// "MACD", "RSI" ,"AO"
    if Last_Bullish_Divergece
        if Hist_Type == "MACD" or Hist_Type == "AO"
            BuLine := line.new(Low_Per_Bar, Low_Per_Hist, Low_Last_Bar,Low_Last_Hist , color = color.rgb(21, 151, 25) , width = 2)
            if Show_Label == 'Yes'
                BuLabel := label.new(Low_Last_Bar,Low_Last_Hist , '+RD', color = color.green, textcolor = color.white, size = size.tiny , style = label.style_label_up)
        else if Hist_Type == "RSI"
            BuLine := line.new(Low_Per_Bar, Low_Per_Hist + 50, Low_Last_Bar,Low_Last_Hist + 50 , color = color.rgb(21, 151, 25), width = 2)
            if Show_Label == 'Yes'
                BuLabel := label.new(Low_Last_Bar,Low_Last_Hist + 50 , '+RD', color = color.green, textcolor = color.white, size = size.tiny, style = label.style_label_up)
    if Last_Bearish_Divergece
        if Hist_Type == "MACD" or Hist_Type == "AO"
            BeLine := line.new(High_Per_Bar, High_Per_Hist, High_Last_Bar,High_Last_Hist , color = color.rgb(212, 44, 44), width = 2)
            if Show_Label == 'Yes'
                BeLabel := label.new(High_Last_Bar,High_Last_Hist ,'-RD', color = color.red, textcolor = color.white, size = size.tiny, style = label.style_label_down)
        else if Hist_Type == "RSI"
            BeLine := line.new(High_Per_Bar, High_Per_Hist + 50, High_Last_Bar,High_Last_Hist + 50 , color = color.rgb(212, 44, 44), width = 2)
            if Show_Label == 'Yes'
                BeLabel := label.new(High_Last_Bar,High_Last_Hist + 50 ,'-RD', color = color.red, textcolor = color.white, size = size.tiny, style = label.style_label_down)
 
 
    var line UpLevl  = line.new(1 , 70 , 2, 70, style = line.style_dashed ,  color = color.rgb(132, 132, 133) ,extend = extend.both )
    var line MidLevl = line.new(1 , 50 , 2, 50, style = line.style_dashed ,  color = color.rgb(132, 132, 133) ,extend = extend.both )
    var line LowLevl = line.new(1 , 30 , 2, 30, style = line.style_dashed ,  color = color.rgb(132, 132, 133) ,extend = extend.both )
    var linefill  Fill= linefill.new(LowLevl , UpLevl , color = #66bdf321 )
 
    if barstate.isnew
        line.set_x1(UpLevl  ,bar_index - 1)
        line.set_x1(MidLevl ,bar_index - 1)
        line.set_x1(LowLevl ,bar_index - 1)
        line.set_x2(UpLevl  ,bar_index)
        line.set_x2(MidLevl ,bar_index)
        line.set_x2(LowLevl ,bar_index)
 
    if  Hist_Type == "MACD" or Hist_Type == "AO"
        UpLevl.delete()
        MidLevl.delete()
        LowLevl.delete()
        Fill.delete()
 
Hist_Color = if Hist_Type == "RSI"
    if Hist + 50 <= 70 and Hist + 50 >= 30
        color.black
    else if Hist + 50 > 70
        color.rgb(255, 82, 82, 50)
    else if Hist + 50 < 30
        color.rgb(76, 175, 79, 50)
else
    if Hist > 0 
        color.rgb(76, 175, 79, 50)
    else if Hist < 0
        color.rgb(255, 82, 82, 50)
 
 
 
plot(Hist_Type == "RSI" ? Hist + 50 : Hist , 'Show Oscillator' ,color = Hist_Color , style = Hist_Type == "RSI" ? plot.style_line : plot.style_columns)
Drawing()

← 返回列表