Weis Wave Volume - Simple labels and comparison
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

// Original version of this script from @the_MarketWhisperer and some other pieces of the script from @LucF. This version makes changes to the way the waves are calculated as well as changes to how it is displayed for a cleaner chart.
// Modifications by @TonioVolcano

//@version=5
indicator('Weis Wave Volume - Simple labels and comparisson', 'Weis-W', overlay=true)

// Inputs
trendReversalLength = input.int(2, 'Trend reversal length', minval=2, group='Wave Calc')
priceSource = input(close, 'Price source for trend detection', group='Wave Calc')
lastshown = input.int(defval=50, title='Labels for X last bars', group='Display')
isShowMarkers = input.bool(false, 'Show Pivot Markers', group='Display')
colorUpWaveVol = input.color(color.new(#29b13b, 0), 'Up Volume', group='Display')
colorDnWaveVol = input.color(color.new(#ff4800, 0), 'Down Volume', group='Display')

// Detect Price Waves
var trend = 0
var wave = 0
upOrDn = priceSource > priceSource[1] ? 1 : priceSource < priceSource[1] ? -1 : 0 
trend := upOrDn != 0 and upOrDn != upOrDn[1] ? upOrDn : trend 
reversalTrend = ta.rising(priceSource, trendReversalLength) or ta.falling(priceSource, trendReversalLength) 
wave := trend != wave and reversalTrend ? trend : wave 
bullWave = wave == 1
bearWave = wave == -1
sameWave = wave == wave[1]

poschange = wave[1] == -1 and wave == 1
negchange = wave[1] == 1 and wave == -1

// Wave calculations
var cumVolume = 0.0
cumVolume := sameWave ? cumVolume + volume : volume
var waveBars = 0
var lastWaveBars = 0
waveBars := sameWave ? waveBars + 1 : 1
averageWaveVolume = cumVolume / waveBars
var increasingBars = 0
increasingBars := sameWave ? averageWaveVolume > nz(averageWaveVolume[1]) ? increasingBars + 1 : 0 : 0
var wavesTot = 0
var wavesTotVol = 0.0
var wavesTotAvg = 0.0
var lastWaveHi = 0.0

// Update numbers when changing waves
if not sameWave
    lastWaveBars := nz(waveBars[1])
    lastWaveHi := cumVolume[1]
    wavesTot += 1
    wavesTotVol += nz(cumVolume[1])
    wavesTotAvg := wavesTotVol / wavesTot
    wavesTotAvg

// Label variables
C_DEFAULT = color.silver
wavestren = lastWaveHi > lastWaveHi[1] ? "o" : "u"
wavestren2 = cumVolume > lastWaveHi ? "[o]" : "[u]"
var label currentWaveLabel = na
var color labelColor = C_DEFAULT
var color textColor = C_DEFAULT
var string labelText = ''
var string labelTool = ''
labelStyle = label.style_none
xloc = bar_index
yloc = yloc.abovebar

label.delete(currentWaveLabel[lastshown])

if poschange
    labelText := str.tostring(wavestren)
    labelTool := str.tostring(int(lastWaveHi))
    labelStyle := isShowMarkers ? label.style_arrowup : label.style_none
    labelColor := colorDnWaveVol
    textColor := colorDnWaveVol
    xloc := bar_index - 1
    yloc := yloc.belowbar
    currentWaveLabel := label.new(xloc, close, yloc=yloc, color=labelColor, textcolor=textColor, style=labelStyle, text=labelText, tooltip=labelTool)
    currentWaveLabel
else if negchange
    labelText := str.tostring(wavestren)
    labelTool := str.tostring(int(lastWaveHi))
    labelStyle := isShowMarkers ? label.style_arrowdown : label.style_none
    labelColor := colorUpWaveVol
    textColor := colorUpWaveVol
    xloc := bar_index - 1
    yloc := yloc.abovebar
    currentWaveLabel := label.new(xloc, close, yloc=yloc, color=labelColor, textcolor=textColor, style=labelStyle, text=labelText, tooltip=labelTool)
    currentWaveLabel

// Label for current wave cumulative vol
colorWaveVol = bullWave ? colorUpWaveVol : bearWave ? colorDnWaveVol : na

var label currentWaveLabel2 = na
var color labelColor2 = C_DEFAULT
var color textColor2 = C_DEFAULT
var string labelText2 = ''
labelStyle2 = label.style_none
xloc2 = bar_index
yloc2 = yloc.price

label.delete(currentWaveLabel2)

if cumVolume > 0  
    labelText2 := str.tostring(wavestren2) + str.tostring(int(cumVolume))
    labelStyle2 := label.style_none
    labelColor2 := colorWaveVol
    textColor2 := colorWaveVol
    xloc2 := bar_index+3
    yloc2 := yloc.price
    currentWaveLabel2 := label.new(xloc2, close, yloc=yloc2, color=labelColor2, textcolor=textColor2, style=labelStyle2, text=labelText2)
    currentWaveLabel2
