Pivot Average [Misu]
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// Author : @Fontiramisu
// Indicator based pivot points to determine "Pivot Average".
// @version=5
indicator("Pivot Average [Misu]", shorttitle="AP [Misu]", overlay=true)

// import Fontiramisu/fontLib/80 as fontilab
import Fontiramisu/fontilab/8 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=3, step=0.1, minval=0, tooltip=devTooltip, group="Pivot")
depth                       = input.int(title="Depth", defval=8, 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

// Plot.
// Get last Pivots.
var highP   = 0.0
var lowP    = 0.0
var midP    = 0.0
highP       := isHighLast ? pLast : highP
lowP        := not isHighLast ? pLast : lowP
midP        := (highP + lowP)/2

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

// Inputs
int maxResis                = input.int(title="Length", defval=6, minval=1, tooltip="History Lenght used to determine Pivot Average", group="Pivot Average")
int smoothLength            = input.int(title="Smoothing MA Lenght", defval=10, minval=1, group="Pivot Average")
float rangePercentResis     = input.float(title="Close Range %", defval=1.5, step=0.1, tooltip="Define price percentage change required to determine close pivots", group="Pivot Average")
color apColor               = input.color(color.yellow, title="Color", group="Pivot Average")

// 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/100, 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(1, 0, resWeights, resPrices, resBarIndexes)
    topResPrices    := dupTopResPrices
    topBarIndexes   := dupTopBarIndexes
    topResWeights   := dupTopResWeights


// Plot.
rawPA           = fontilab.getElFloatArrayFromEnd(topResPrices, 0)
smoothingPA     = ta.sma(rawPA,smoothLength)
plot(smoothingPA, title="res", color=apColor)

//]
