Pivot Trend Levels
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © alipeikani
// I have to thank stackoverflow for the help
//@version=5

indicator('pivot trend levels', overlay=true)

bars = input(3)
checkbox = input.bool(title= "color chenging by trend",  defval=true)


ph = ta.pivothigh(high, bars, bars)
pl = ta.pivotlow(low, bars, bars)

//INIT VARIABLES
var int ph_uptrend_flag = 0
var float ph_valid1 = 0
var float ph_valid2 = 0
var float ph_valid3 = 0
var float ph_valid4 = 0
var float ph_valid5 = 0
var float pl_valid1 = 0
var float pl_valid2 = 0
var float pl_valid3 = 0
var float pl_valid4 = 0
var float pl_valid5 = 0


var float ph_valid_old = 2
var float pl_valid_old = 0  //  non meaningful number here for initialization only

ph_non_na = nz(ph, 0)
pl_non_na = nz(pl, 0)  // stores 0's instead of na's for non-pivot-pointed bars

// re-calculate uptrend every time a new pivot comes in, otherwise keep last known value for uptrend flag
if ph_non_na != 0
    ph_valid5 := ph_valid4
    ph_valid4 := ph_valid3
    ph_valid3 := ph_valid2
    ph_valid2 := ph_valid1
    ph_valid1 := ph_non_na
    ph_valid_old := ph_valid1
    ph_valid_old
else
    ph_valid1 := ph_valid1
    ph_valid_old := ph_valid_old
    ph_valid_old


if pl_non_na != 0
    pl_valid5 := pl_valid4
    pl_valid4 := pl_valid3
    pl_valid3 := pl_valid2
    pl_valid2 := pl_valid1
    pl_valid1 := pl_non_na
    pl_valid_old := pl_valid1
    pl_valid_old
else
    pl_valid1 := pl_valid1
    pl_valid_old := pl_valid_old
    pl_valid_old


max = math.max(ph_valid2, ph_valid1)
//lph = math.min(ph_valid2, ph_valid1)   // lower pivots of highs

//hpl = math.max(pl_valid2, pl_valid1)   // higher pivots of lows
min = math.min(pl_valid2, pl_valid1)   

avg = math.avg(ph_valid2, ph_valid1,pl_valid2, pl_valid1)

/// I need help here!!!  I tried to build a trend detection system based on which if the last move of the "max" and the "min" upwards, Trend will change to Bullish and if their last move is downward trend will change bearish.
// If the direction of their movement is not consistent, trend will not change
//float maxup = 0
//float minup = 0




//if max > max[1] 

//    maxup := 1
//
//else

//    if max < max[1] 

//        maxup := -1

//    else
//
//        maxup := maxup[1] 


//if min > min[1] 

//    minup := 1

//else

//    if min < min[1] 

//        minup := -1

//    else

 //       minup := minup[1] 
  
    
//xup = if max == max[1]

//    na

//else

//    maxup


//nup = if min == min[1]

 //   na

//else

//    minup



//v = if close > open

//    1

//else

 //   if open > close

//        open

//    else

//        na



//clrchecked = if Trendup == 1

//    color.lime

//else

//    if Trendup == -1

//        color.red

//    else

 //       color.white



plot(max, title = "High level", color=color.lime)

plot(min, title = "Low level", color=color.lime)

plot(avg, title = "Mid level", color=color.yellow )















