Inverse Divergence [HeWhoMustNotBeNamed]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HeWhoMustNotBeNamed

//   __    __            __       __  __                  __       __                        __      __    __              __      _______             __    __                                          __ 
//  /  |  /  |          /  |  _  /  |/  |                /  \     /  |                      /  |    /  \  /  |            /  |    /       \           /  \  /  |                                        /  |
//  $$ |  $$ |  ______  $$ | / \ $$ |$$ |____    ______  $$  \   /$$ | __    __   _______  _$$ |_   $$  \ $$ |  ______   _$$ |_   $$$$$$$  |  ______  $$  \ $$ |  ______   _____  ____    ______    ____$$ |
//  $$ |__$$ | /      \ $$ |/$  \$$ |$$      \  /      \ $$$  \ /$$$ |/  |  /  | /       |/ $$   |  $$$  \$$ | /      \ / $$   |  $$ |__$$ | /      \ $$$  \$$ | /      \ /     \/    \  /      \  /    $$ |
//  $$    $$ |/$$$$$$  |$$ /$$$  $$ |$$$$$$$  |/$$$$$$  |$$$$  /$$$$ |$$ |  $$ |/$$$$$$$/ $$$$$$/   $$$$  $$ |/$$$$$$  |$$$$$$/   $$    $$< /$$$$$$  |$$$$  $$ | $$$$$$  |$$$$$$ $$$$  |/$$$$$$  |/$$$$$$$ |
//  $$$$$$$$ |$$    $$ |$$ $$/$$ $$ |$$ |  $$ |$$ |  $$ |$$ $$ $$/$$ |$$ |  $$ |$$      \   $$ | __ $$ $$ $$ |$$ |  $$ |  $$ | __ $$$$$$$  |$$    $$ |$$ $$ $$ | /    $$ |$$ | $$ | $$ |$$    $$ |$$ |  $$ |
//  $$ |  $$ |$$$$$$$$/ $$$$/  $$$$ |$$ |  $$ |$$ \__$$ |$$ |$$$/ $$ |$$ \__$$ | $$$$$$  |  $$ |/  |$$ |$$$$ |$$ \__$$ |  $$ |/  |$$ |__$$ |$$$$$$$$/ $$ |$$$$ |/$$$$$$$ |$$ | $$ | $$ |$$$$$$$$/ $$ \__$$ |
//  $$ |  $$ |$$       |$$$/    $$$ |$$ |  $$ |$$    $$/ $$ | $/  $$ |$$    $$/ /     $$/   $$  $$/ $$ | $$$ |$$    $$/   $$  $$/ $$    $$/ $$       |$$ | $$$ |$$    $$ |$$ | $$ | $$ |$$       |$$    $$ |
//  $$/   $$/  $$$$$$$/ $$/      $$/ $$/   $$/  $$$$$$/  $$/      $$/  $$$$$$/  $$$$$$$/     $$$$/  $$/   $$/  $$$$$$/     $$$$/  $$$$$$$/   $$$$$$$/ $$/   $$/  $$$$$$$/ $$/  $$/  $$/  $$$$$$$/  $$$$$$$/ 
//                                                                                                                                                                                                          
//                                                                                                                                                                                                          
//
//@version=5
indicator("Inverse Divergence [HeWhoMustNotBeNamed]", max_lines_count=500, max_labels_count=500, max_boxes_count=500)
import HeWhoMustNotBeNamed/customcandles/2 as cc
import HeWhoMustNotBeNamed/zigzag/7 as zg
zigzagLength = input.int(5, step=5, minval=3, title='', group='Zigzag', inline='z1')
zigzagColor = input.color(color.rgb(251, 192, 45, 0), title='', group='Zigzag', inline='z1')
usePercentile = input.bool(false, "Percent Candles")
pLength = input.int(200, "Percent Rank Length")

numberOfPivots = 10

oscillatorType = input.string('rsi', title='Oscillator Source', options=["cci", "cmo", "cog", "mfi", "roc", "rsi",  "tsi"], group='Support/Resistance')

length = zigzagLength*3
longLength = zigzagLength*5
[oOpen, oHigh, oLow, oClose] = cc.ocandles(oscillatorType, length, length,longLength, percentCandles = usePercentile)

oRankHigh = ta.percentrank(oHigh, pLength)
oRankLow = ta.percentrank(oLow, pLength)

dir = oRankHigh > 60 and oRankLow > 60 ? 1 : oRankHigh < 40 and oRankLow < 40 ? -1 : 0
[zigzagpivots, zigzagpivotbars, zigzagpivotdirs, zigzagpivotratios, zigzagprice,
                                     zigzagpricedirs, zigzagtrendbias, zigzagdivergence, 
                                     lines, labels] = 
                                     zg.drawczigzag(zigzagLength, numberOfPivots, math.max(oHigh, oOpen, oClose), math.min(oLow, oOpen, oClose), high, low, dir, true, true, true, true, linecolor=zigzagColor)

plotcandle(oOpen, oHigh, oLow, oClose, title="Oscillator Candles")

watermark = table.new(position=position.middle_center, columns=1, rows=1, border_width=0)
table.cell(table_id=watermark, column=0, row=0, text="Inverse Divergence\nHeWhoMustNotBeNamed", text_color=color.new(color.aqua, 80), text_size=size.huge)