Resistance/Support Indicator - Fontiramisu
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Author : @Fontiramisu
// Indicator based on pivots to find resistances and support.
// @version=5
indicator("Resistance/Support Indicator", overlay=true)

// import Fontiramisu/fontLib/80 as fontilab
import Fontiramisu/fontilab/6 as fontilab


// ------------------------------------- Find dev pivots------------------------------------------------------------------------------------------ [
// -- Var user input --
var devTooltip              = "Deviation is a multiplier that affects how much the price should deviate from the previous pivot in order for the bar to become a new pivot."
var depthTooltip            = "The minimum number of bars that will be taken into account when analyzing pivots."
thresholdMultiplier         = input.float(title="Deviation", defval=2.5, minval=0, tooltip=devTooltip, group="Pivot")
depth                       = input.int(title="Depth", defval=10, minval=1, tooltip=depthTooltip, group="Pivot") 

// Prepare pivot variables
var line lineLast = na
var int iLast = 0 // Index last
var int iPrev = 0 // Index previous
var float pLast = 0 // Price last
var float pLastHigh = 0 // Price last
var float pLastLow = 0 // Price last
var isHighLast = false // If false then the last pivot was a pivot low
isPivotUpdate = false


// Get pivot information from dev pivot finding function
[dupLineLast, dupIsHighLast, dupIPrev, dupILast, dupPLast, dupPLastHigh, dupPLastLow] = 
     fontilab.getDeviationPivots(thresholdMultiplier, depth, lineLast, isHighLast, iLast, pLast, true, close, high, low)

if not na(dupIsHighLast)
    lineLast := dupLineLast
    isHighLast := dupIsHighLast
    iPrev := dupIPrev
    iLast := dupILast
    pLast := dupPLast
    pLastHigh := dupPLastHigh
    pLastLow := dupPLastLow
    isPivotUpdate := true

// ] ------------------------------------- Resistance Script ------------------------------------------------------------------------------------------ [

// Inputs
int maxResis            = input.int(title="Nb Max res/sup", defval=10, minval=5, tooltip="Number max res/sup to keep in our res/sup history array. The greater it is the older bar index will be taken", group="Resistance/Support")
int nbShowRes           = input.int(title="Nb show res/sup", defval=10, minval=1, tooltip="Number of res/sup to be shown", group="Resistance/Support")
int minTopWeight        = input.int(title="Min weight shown", defval=15, tooltip="Define min res/sup weight to be shown. Weight is used to measure strengh of res/sup.", group="Resistance/Support")
float rangePercentResis = input.float(title="% range stack", defval=0.015, tooltip="Define price percentage change required to stack a pivot into an existing res/sup. Default is 0.03 = 3%", group="Resistance/Support")

// Vars
var float[] resPrices  = array.new_float(1, 0)
var int[] resWeights   = array.new_int(1, 0)
var int[] resBarIndexes = array.new_int(1, 0)
var int[] resNbLows   = array.new_int(1, 0)
var int[] resNbHighs  = array.new_int(1, 0)

if isPivotUpdate
    [dupResPrices, dupResWeights, dupResBarIndexes, dupResNbLows, dupResNbHighs] = fontilab.getResistances(resPrices, resBarIndexes, resWeights, resNbLows, resNbHighs, maxResis, rangePercentResis, pLast, iLast, isHighLast)

    if not na(dupResPrices)
        resPrices       := dupResPrices 
        resWeights      := dupResWeights
        resBarIndexes   := dupResBarIndexes
        resNbHighs      := dupResNbHighs
        resNbLows       := dupResNbLows

// -- Show Resis --
var float[] topResPrices    = array.new_float(1, 0)
var int[] topBarIndexes     = array.new_int(1, 0)
var int[] topResWeights     = array.new_int(1, 0)

// Update lines every pivot update
if isPivotUpdate
    [dupTopResPrices, dupTopResWeights, dupTopBarIndexes] = fontilab.getTopRes(nbShowRes, minTopWeight, resWeights, resPrices, resBarIndexes)
    topResPrices    := dupTopResPrices
    topBarIndexes   := dupTopBarIndexes
    topResWeights   := dupTopResWeights
                
// ] ------------------------------------- Do Some Drawings------------------------------------------------------------------------------------------ [

if isPivotUpdate
    // DrawLines
    fontilab.drawResLines(topResPrices, topBarIndexes)
    
// ] DEBUG -- Show Table -- [
show_table = input(true,  "Show table with stats", group = "Drawings")

// Helping vars
index = 0
// textresPrices      = array.size(resPrices) > index ? str.tostring(resPrices[index]) : "na" 
// textresWeights     = array.size(resWeights) > index ? str.tostring(resWeights[index]) : "na" 
// textresBarIndexes   = array.size(resBarIndexes) > index ? str.tostring(resBarIndexes[index]) : "na" 
// textresNbHighs    = array.size(resNbHighs) > index ? str.tostring(resNbHighs[index]) : "na" 
// textresNbLows     = array.size(resNbLows) > index ? str.tostring(resNbLows[index]) : "na" 
textTopResPrices    = array.size(topResPrices) > index ? str.tostring(topResPrices[index]) : "na" 
textTopResWeights    = array.size(topResWeights) > index ? str.tostring(topResWeights[index]) : "na" 

if show_table
    var table ATHtable = table.new(position.bottom_right, 10, 10, frame_color = color.gray, bgcolor = color.gray, border_width = 1, frame_width = 1, border_color = color.white)
    
    // table.cell(ATHtable, 0, 0, text="resPrices", bgcolor=color.silver, tooltip="")
    // // table.cell(ATHtable, 1, 0, text="na", bgcolor=color.green, tooltip="")
    // table.cell(ATHtable, 1, 0, text=textresPrices, bgcolor=color.green, tooltip="")
    
    // table.cell(ATHtable, 0, 1, text="resWeights", bgcolor=color.silver, tooltip="")
    // table.cell(ATHtable, 1, 1, text=textresWeights, bgcolor=color.green, tooltip="")
    
    // table.cell(ATHtable, 0, 2, text="resBarIndexes", bgcolor=color.silver, tooltip="")
    // table.cell(ATHtable, 1, 2, text=textresBarIndexes, bgcolor=color.green, tooltip="")
    
    // table.cell(ATHtable, 0, 3, text="resNbHighs", bgcolor=color.silver, tooltip="")
    // table.cell(ATHtable, 1, 3, text=textresNbHighs, bgcolor=color.green, tooltip="")
    
    // table.cell(ATHtable, 0, 4, text="resNbLows", bgcolor=color.silver, tooltip="")
    // table.cell(ATHtable, 1, 4, text=textresNbLows, bgcolor=color.green, tooltip="")

    table.cell(ATHtable, 0, 5, text="Prices", bgcolor=color.silver, tooltip="")
    table.cell(ATHtable, 1, 5, text=str.tostring(textTopResPrices), bgcolor=color.green, tooltip="")
    
    table.cell(ATHtable, 0, 6, text="Weights", bgcolor=color.silver, tooltip="")
    table.cell(ATHtable, 1, 6, text=str.tostring(textTopResWeights), bgcolor=color.green, tooltip="")

// ]