ZWAP (ZigZag Anchored VWAP) [Kioseff Trading]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © KioseffTrading
// 
//@version=5
indicator("ZWAP [Kioseff Trading]", overlay = true, max_polylines_count = 100, max_lines_count = 500, max_labels_count = 500)

import TradingView/ZigZag/6 as ZigZagLib 
import kaigouthro/hsvColor/15 as kai
import HeWhoMustNotBeNamed/arraymethods/1

use     = input.bool  (defval = false, title = "Use Visible Range")
curvy   = input.bool  (defval = true, title = "Curvy Zig Zag")
show    = not use ? input.int   (defval = 25, minval = 1, title = "Pivots To Show", maxval = 100) + 1 : 100
start   = input.time  (timestamp("20 Jul 2000 00:00 +0300"), "Start Time")
colType = input.bool  (defval = true, title = "Color As Support / Resistance")
sCol    = input.color (defval = #14D990, title = "VWAP Support Color", inline = "Col"), rCol = input.color (defval = #F24968, title = "VWAP Resistance Color", inline = "Col")
transp  = input.int   (defval = 0, maxval = 100, title = "Color Transparency")
curc    = input.color (defval = #6929F2, title = "Curve ZZ Color")
lineS   = input.string(defval = "Solid", options = ["Solid", "Dotted", "Dashed"], title = "Line Style")


styl = switch lineS 

    "Solid"  => line.style_solid
    "Dotted" => line.style_dotted 
    "Dashed" => line.style_dashed



var zigZag = ZigZagLib.newInstance(
  ZigZagLib.Settings.new(
      input.float(0.00001, "Price deviation for reversals (%)", 0.00001, 100.0, 0.5, "0.00001 - 100", group = "Zig Zag Settings"),
      input.int(65, "Pivot legs", 2, group = "Zig Zag Settings"),
      input(#00000000, "Line color", group = "Zig Zag Settings"),
      input(true, "Extend to last bar", group = "Zig Zag Settings"),
      input(false, "Display reversal price", group = "Zig Zag Settings"),
      input(false, "Display cumulative volume", group = "Zig Zag Settings"),
      input(false, "Display reversal price change", inline = "priceRev", group = "Zig Zag Settings"),
      input.string("Absolute", "", ["Absolute", "Percent"], inline = "priceRev", group = "Zig Zag Settings"),
      true)
 )

zigZag.update(), change = ta.change(line.all.size()) 

var kv      = matrix.new<float>(2, 0)
var pivots  = array.new <float>()
var timeArr = array.new_int(), var curves = matrix.new<float>(2, 0)

cond = switch use 
    
    true => time <= chart.right_visible_bar_time and time >= chart.left_visible_bar_time 
    =>      time >= start

if cond

    if change and line.all.size() > 1
        if line.all.get(line.all.size() - 2).get_x2() >= (use ? chart.left_visible_bar_time : start)
            pivots.push(

                         line.all.get(line.all.size() - 2).get_x2())

    kv.add_col(0, array.from(
                 
                 hlc3,
                 volume

     ))

    timeArr.unshift(time)

    if barstate.islast

        pivots.push(

             line.all.last().get_x2())


if last_bar_index - bar_index <= 15000

    if change and line.all.size() > 1

        curves.add_col(curves.columns(),

                 array.from(
                         line.all.get(line.all.size() - 2).get_x2(),
                         line.all.get(line.all.size() - 2).get_y2()

                         ))

    if barstate.islast 
        
       
        curves.add_col(curves.columns(),

                 array.from(
                    
                         line.all.last().get_x2(),
                         line.all.last().get_y2()

                         ))


        curves.add_col(curves.columns(),

                 array.from(

                         last_bar_time,
                         ohlc4

                         ))



if barstate.islast

    if polyline.all.size() > 0 
        for i = 0 to polyline.all.size() - 1
            polyline.all.shift().delete()

    if pivots.size() > show
        for i = 0 to pivots.size() - show
            pivots.shift()

    if pivots.size() > 0

        for i = 0 to pivots.size() - 1
    
            sumHLC3 = 0.0, sumVol = 0.0
    
            points = array.new<chart.point>()

            for x = timeArr.indexof(int(pivots.get(i))) to 0
            
                sumHLC3 += kv.get(0, x) * kv.get(1, x)
                sumVol  += kv.get(1, x) 
    
                points.push(chart.point.from_time(timeArr.get(x), sumHLC3 / sumVol))

            col = switch sumHLC3 / sumVol >= close 

                true => color.new(rCol, transp)
                =>      color.new(sCol, transp)

            polyline.new(points, curved = false, xloc = xloc.bar_time, line_color = col, line_width = 1, line_style = styl)


    points = array.new<chart.point>()

    for i = 0 to curves.columns() - 1

        points.push(chart.point.from_time(int(curves.get(0, i)), curves.get(1, i)))

    polyline.new(points, curved = curvy , xloc = xloc.bar_time, line_color = curc)

