# weis_wave_3peaks_00d
#https://usethinkscript.com/threads/display-values-of-last-3-peaks-of-weis-wave.14482/
#Questions UnAnswered Graveyard
#UnAnswered Display values of last 3 peaks of Weis Wave
#SimpleStock Feb 14, 2023
#Would it be possible to display values of last 3 peaks/valleys of price and volume with Addlabel or any other methods in the Weis Wave Zig Zag with Volume indicator ?
#https://usethinkscript.com/threads/weis-wave-volume-indicator-for-thinkorswim.72/#post-308
#BTW it will help us with identifying divergences easily. Three higher peaks with lower volumes and lower valleys with lower volume.
#Attached is ES from 2/13/23
#-------------------------------
#https://usethinkscript.com/threads/weis-wave-volume-indicator-for-thinkorswim.72/#post-308
#Weis Wave Volume Indicator for ThinkorSwim
#BenTen Feb 25, 2019
#Here is the Weis Wave Volume indicator for ThinkorSwim converted from the Lazy Bear's version over at TradingView. In addition, I also found another indicator called Weis Wave Volume with ZigZag so I'll include that in this post as well.
#---------------------------------
# Weis Wave Zig Zag with Volume
#Note: The Weis Wave indicator shows price waves associated with the Weis Wave Volume lower indicator.
#Note: The reversalAmount: Indicate the minimum amount price must reverse before an inflection point can be formed. This value should be set the same on the lower Weis Wave Volume indicator.
declare upper;
input price = hlc3;
input reversalAmount = 1.0;
input reversalMode = {default price, percent};
input displayMode = {default volume, barNumber, barTotal, none};
def mode = if reversalMode == reversalMode.price then ZigZagTrendSign(price = price, reversalAmount=reversalAmount) else ZigZagTrendPercent(price = price, reversalAmount=reversalAmount);
def inflection = if reversalMode == reversalMode.price then if !isNan(ZigZagSign(price = price, reversalAmount=reversalAmount)) then 1 else 0 else if !isNan(ZigZagPercent(price = price, reversalAmount=reversalAmount)) then 1 else 0;
rec trend = if inflection==1 and mode ==-1 then 1 else if inflection==1 and mode==1 then -1 else trend[1];
plot wave = if reversalMode == reversalMode.price then ZigZagSign(price = price, reversalAmount=reversalAmount) else ZigZagPercent(price = price, reversalAmount=reversalAmount);
wave.EnableApproximation();
wave.AssignValueColor(if trend[1] == 1 then color.green else color.red);
wave.SetLineWeight(3);
rec upWaveVolume = if inflection == 1 and trend == 1 and close > open then volume else if inflection == 1 and trend == 1 and close <= open then 0 else if trend == 1 or (inflection == 1 and trend == -1 and close >= open) then upWaveVolume[1] + volume else 0;
rec downWaveVolume = if inflection == 1 and trend == -1 and close < open then volume else if inflection == 1 and trend == -1 and close >= open then 0 else if trend == -1 or (inflection == 1 and trend == 1 and close <= open) then downWaveVolume[1] + volume else 0;
rec barsUp = if inflection == 1 and trend == 1 and close > open then 1 else if inflection == 1 and trend == 1 and close <= open then 0 else if trend == 1 or (inflection == 1 and trend == -1 and close >= open) then barsUp[1] + 1 else 0;
rec barsDown = if inflection == 1 and trend == -1 and close < open then 1 else if inflection == 1 and trend == -1 and close >= open then 0 else if trend == -1 or (inflection == 1 and trend == 1 and close <= open) then barsDown[1] + 1 else 0;
plot uCount = if (displayMode == displayMode.barNumber and barsUp) then barsUp else double.nan;
uCount.SetPaintingStrategy(paintingStrategy.VALUES_BELOW);
uCount.SetDefaultColor(color.dark_green);
plot dCount = if (displayMode == displayMode.barNumber and barsDown) then barsDown else double.nan;
dCount.SetPaintingStrategy(paintingStrategy.VALUES_ABOVE);
dCount.SetDefaultColor(color.red);
plot uCountTot = if (displayMode == displayMode.barTotal and barsDown == 1 and barsUp==0) then barsUp[1] else double.nan;
uCountTot.SetPaintingStrategy(paintingStrategy.VALUES_ABOVE);
uCountTot.SetDefaultColor(color.dark_green);
plot dCountTot = if (displayMode == displayMode.barTotal and barsDown==0 and barsUp==1) then barsDown[1] else double.nan;
dCountTot.SetPaintingStrategy(paintingStrategy.VALUES_BELOW);
dCountTot.SetDefaultColor(color.red);
plot uVol = if (displayMode == displayMode.volume and barsDown == 1 and barsUp==0) then upWaveVolume[1] else double.nan;
uVol.SetPaintingStrategy(paintingStrategy.VALUES_ABOVE);
uVol.SetDefaultColor(color.dark_green);
plot dVol = if (displayMode == displayMode.volume and barsUp == 1 and barsDown==0) then downWaveVolume[1] else double.nan;
dVol.SetPaintingStrategy(paintingStrategy.VALUES_BELOW);
dVol.SetDefaultColor(color.red);
#------------------------------------------
# display values of last 3 peaks/valleys of price and volume
# trend:
# peak = -1
# valley = +1
# on last bar = can be either +or-
def na = double.nan;
def bn = barnumber();
def lastbn = HighestAll(If(IsNaN(close), 0, bn));
def lastbar = if (bn == lastbn) then 1 else 0;
#def lastbar = !isnan(close[0]) and isnan(close[-1]);
def peakcnt = if bn == 1 then 0
else if isnan(wave) or isnan(trend) then peakcnt[1]
else if lastbar then peakcnt[1]
else if !isnan(wave) and trend == -1 then peakcnt[1] + 1
else peakcnt[1];
def valleycnt = if bn == 1 then 0
else if isnan(wave) or isnan(trend) then valleycnt[1]
else if lastbar then valleycnt[1]
else if !isnan(wave) and trend == 1 then valleycnt[1] + 1
else valleycnt[1];
def peakmax = highestall(peakcnt);
def valleymax = highestall(valleycnt);
def peakrevcnt = peakmax - peakcnt + 1;
def valleyrevcnt = valleymax - valleycnt + 1;
input qty_of_last_peaks_valleys = 2;
def q = qty_of_last_peaks_valleys;
# max of 4
def p1pr = if bn == 1 then 0 else if q >= 1 and peakrevcnt == 1 and peakrevcnt[1] != 1 then close else p1pr[1];
def p2pr = if bn == 1 then 0 else if q >= 2 and peakrevcnt == 2 and peakrevcnt[1] != 2 then close else p2pr[1];
def p3pr = if bn == 1 then 0 else if q >= 3 and peakrevcnt == 3 and peakrevcnt[1] != 3 then close else p3pr[1];
def p4pr = if bn == 1 then 0 else if q >= 4 and peakrevcnt == 4 and peakrevcnt[1] != 4 then close else p4pr[1];
def p1vol = if bn == 1 then 0 else if q >= 1 and peakrevcnt == 1 and peakrevcnt[1] != 1 then volume else p1vol[1];
def p2vol = if bn == 1 then 0 else if q >= 2 and peakrevcnt == 2 and peakrevcnt[1] != 2 then volume else p2vol[1];
def p3vol = if bn == 1 then 0 else if q >= 3 and peakrevcnt == 3 and peakrevcnt[1] != 3 then volume else p3vol[1];
def p4vol = if bn == 1 then 0 else if q >= 4 and peakrevcnt == 4 and peakrevcnt[1] != 4 then volume else p4vol[1];
def v1pr = if bn == 1 then 0 else if q >= 1 and valleyrevcnt == 1 and valleyrevcnt[1] != 1 then close else v1pr[1];
def v2pr = if bn == 1 then 0 else if q >= 2 and valleyrevcnt == 2 and valleyrevcnt[1] != 2 then close else v2pr[1];
def v3pr = if bn == 1 then 0 else if q >= 3 and valleyrevcnt == 3 and valleyrevcnt[1] != 3 then close else v3pr[1];
def v4pr = if bn == 1 then 0 else if q >= 4 and valleyrevcnt == 4 and valleyrevcnt[1] != 4 then close else v4pr[1];
def v1vol = if bn == 1 then 0 else if q >= 1 and valleyrevcnt == 1 and valleyrevcnt[1] != 1 then volume else v1vol[1];
def v2vol = if bn == 1 then 0 else if q >= 2 and valleyrevcnt == 2 and valleyrevcnt[1] != 2 then volume else v2vol[1];
def v3vol = if bn == 1 then 0 else if q >= 3 and valleyrevcnt == 3 and valleyrevcnt[1] != 3 then volume else v3vol[1];
def v4vol = if bn == 1 then 0 else if q >= 4 and valleyrevcnt == 4 and valleyrevcnt[1] != 4 then volume else v4vol[1];
def bubx = 2;
def bub = !isnan(close[bubx+1]) and isnan(close[bubx]);
addchartbubble(bub, close[bubx+1],
"Peaks\n" +
"1 " + round(p1pr, 2) + " " + p1vol + "\n" +
"2 " + round(p2pr, 2) + " " + p2vol + "\n" +
"3 " + round(p3pr, 2) + " " + p3vol + "\n" +
"4 " + round(p4pr, 2) + " " + p4vol + "\n"
, color.yellow, yes);
addchartbubble(bub, close[bubx+1],
"Valleys\n" +
"1 " + round(v1pr, 2) + " " + v1vol + "\n" +
"2 " + round(v2pr, 2) + " " + v2vol + "\n" +
"3 " + round(v3pr, 2) + " " + v3vol + "\n" +
"4 " + round(v4pr, 2) + " " + v4vol + "\n"
, color.yellow, no);
#------------------------------------
input test1 = no;
addchartbubble(test1, low*0.995,
wave + " w\n" +
trend + " t\n" +
peakmax + " pm\n" +
peakcnt + " pc\n" +
peakrevcnt + " pr\n" +
"----\n" +
valleymax + " vm\n" +
valleycnt + " v\n" +
valleyrevcnt + " vr"
, color.yellow, no);
input test2 = no;
addchartbubble(test2, low*0.995,
wave + " w\n" +
trend + " t\n" +
#peakmax + " pm\n" +
#peakcnt + " pc\n" +
peakrevcnt + " pr\n" +
#close + "\n" +
"----\n" +
#valleymax + " vm\n" +
#valleycnt + " v\n" +
valleyrevcnt + " vr"
, color.yellow, no);
#
#