Weis V5 zigzag jayy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jayy

//@version=5
//First published privately April 22, 2021  https://www.tradingview.com/script/jrZQBZ5D-Weis-zigzag-v4-jayy/
// v5 published privately Feb 2, 2022
// Somehow, I deleted version 5 of the zigzag script.  Same name.  I have added some older notes describing how the Weis Wave works.
// I have also changed the date restriction that stopped the script from working after Dec 31, 2022.
// What you see here is the Weis zigzag wave plotted directly on the price chart.  This script is the companion to the Weis cumulative wave volume script.
//  What is a Weis wave?  David Weis has been recognized as a Wyckoff method analyst he has written two books one of which, Trades About to Happen, 
// describes the evolution of the now-popular Weis wave.  The method employed by Weis is to identify waves of price action and to compare the strength of
// the waves on characteristics of wave strength.  Chief among the characteristics of strength is the cumulative volume of the wave.  
// There are other markers that Weis uses as well for example how the actual price difference between the start of the Weis wave from start to finish.  Weis also uses time, 
// particularly when using a Renko chart 
// David Weis did a futures  io video which is a popular source of information about his method. (Search David Weis and futures.io.  I strongly suggest you also read “Trades About to Happen”
//  by David Weis.
// This will get you up and running more quickly when studying charts.  However, you should choose the Traditional method to be true to David Weis technique as described in his book 
// "Trades About to Happen" and in the Futures IO Webcast featuring David Weis
// https://www.tradingview.com/script/k4X18NJK-Weis-Pip-Wave-jayy/ .  
// The Weis pip zigzag wave shows how far in terms of bar close price a Weis wave has traveled through the duration of a Weis wave.  The Weis zigzag wave is used in combination with the
//  Weis cumulative volume wave.  The two waves should be set to the same "wave size".

// To use this script, you must set the wave size: Using the traditional Weis method simply enter the desired wave size in the box "How should wave size be calculated", 
// in this example I am using a traditional wave size of .25.  Each wave for each security and each timeframe requires its own wave size.  Although not the traditional method devised 
// by David Weis a more automatic way to set wave size would be to use Average True Range (ATR).  Using ATR is not the true Weis method but it does give you similar waves and, importantly, 
// without the hassle described above. Once the Weis wave size is set then the zigzag wave will be shown with volume. Because Weis used the closing price of a wave to define waves a 
// line Bar highs and bar lows are not captured by the Weis Wave.  The default script setting is now cumulative volume waves using an ATR of 7 and a multiplication factor of .5.  
// To display volume in a way that does not crowd out neighbouring volumes Weis displayed volume as a maximum of 3 digits (usually).  Consider two Weis Wave volumes 
// 176,895,570 and 2,654,763,889. To display wave volume as three digits it is necessary to take a number such as 176,895,570 and truncate it.  176,895,570 can be represented as 
// 177 X 10 to the power of 6.  The number displayed must also be relative to other numbers in the field. If the highest volume on the page is: 2,654,763,889 and with only three 
// numbers available to display the result the value shown must be 265 (265 X 10 to the power of 7).  Since 176,895,570 is an order of magnitude smaller than 2,654,763,889 therefore
//  175,895,570 must be shown as 18 instead of 177.  In this way, the relative magnitudes of the two volumes can be understood. All numbers in the field of view must be truncated by the 
// same order of magnitude to make the relative volumes understandable. The script attempts to calculate the order of magnitude value automatically. If you see a red number in the field 
// of view it means the script has failed to do the calculation automatically and you should use the manual method – use the dialogue box “Calculate truncated wave value automatically or manually”. 
//  Scroll down from the automatic method and select manual.  Once "manual" is selected the values displayed become the power values or multipliers for each wave.
// Using the manual method you will select a “Multiplier” in the next dialogue box.  Scan the field and select the largest value in the field of view (visible chart) is the multiplier of interest.
//   If you select a lower number than the maximum value will see at least one red “up”.  If you are too high you will see at least one red “down”.  Scroll in the direction recommended or the
//  values on the screen will be totally incorrect. With volume truncated to the highest order values, the eye can quickly get a feel for relative volumes. It also reduces the crowding and
//  overlapping of values on the screen.  You can opt to show the full volume to help get a sense of the magnitude of the true volumes.
// How does the script determine if a Weis wave is continuing to grow or not?
// The script evaluates the closing price of each new bar relative to the "Weis wave size".  Suppose the current bar closes at a new low close, within the current down wave, at $30.00. 
// If the Weis wave size is $0.10 then the algorithm will remember the $30.00 close and compare it to the close of the next bar.  If the bar close price does not close equal to or lower 
// than $30.00 or close equal to or higher than $30.10 then the wave is still a down wave with a current low of $30.00.  This is true even if the bar low is less than $30.00 or the bar 
// high is greater than 30.10 – only the bar’s closing price matters.  If a bar's closing price climbs back up to a close of $30.11 then because the closing price has moved more than $0.10 
// (the Weis wave size) then that is a wave reversal with a new up-trending wave.  In the above example if there was currently a downward trending wave and the bar closes were as follows $30.00,
//  $30.09, $30.01, $30.05, $30.10 The wave direction would continue to stay downward trending until the close of $30.10 was achieved.  As such $30.00 would be the low and the following closes 
// $30.09, $30.01, $30.05 would be allocated to the new upward-trending wave. If however There was a series of bar closes like this $30.00, $30.09, $30.01, $30.05, $29.99 since none of the closes
//  was equal to above the 10-cent reversal target of $30.10 but instead, a new Weis wave low was achieved  ($29.99).  As such the closes of  $30.09, $30.01, $30.05 would all be attributed to the
//  continued down-trending wave with a current low of $29.99, even though the closing price for the interim bars was above $30.00.  Now that the Weis Wave low is now 429.99 then, in order to 
// reverse this continued downtrend price will need to close at or above $30.09 on subsequent bar closes assuming now new low bar close is achieved.  With large wave sizes, wave direction can be 
// in limbo for many bars before a close either renews wave direction or reverses it and confirms wave direction as either a reversal or a continuation. On the zig-zag, a wave line and its volume
//  will not be "printed" until a wave reversal is confirmed. 
// The wave attribution is similar when using other methods to define wave size. If ATR is used for wave size instead of a traditional wave constant size such as $0.10 or $2 or 2000 pips or ... 
// then the wave size is calculated based on current ATR instead of the Weis wave constant (Traditional selected value).
// I have the option to display pseudo-Ord volume.  In truth, Ord used more traditional zig-zag pivots of bar highs and lows. Waves using closes as pivots can have some significant differences.  
// This difference can be lessened by using smaller time frames and larger wave sizes.
// There other options such to display the delta price or pip size of a Weis Wave, the number of bars in a wave 

indicator('Weis V5 zigzag jayy', overlay=true, max_labels_count=500, max_lines_count=500, max_bars_back=4999)
shokey = input(false, title='Show key variables after last bar')
KO = time > timestamp(syminfo.timezone, 2032, 12, 31, 23, 59) ? false : true
//KO=time>timestamp(syminfo.timezone, 2021, 04, 09, 12, 48)?false:true
wavechoice_ = input.string(title='Choose wave type, default is cumulative volume, try Weis cumulative volume/delta price to help identify change of behaviour ', defval='Weis volume', options=['Weis volume', 'delta price or delta pip', 'wave time - time based charts only', 'Weis cumulative volume/delta price', 'pseudo-Ord volume', 'Weis volume and # bars per wave', 'Weis wave volume and Ord volume'])
// sensitivity_= input(title="calculate truncated wave value automatically or manually, any red numbers means the manual method must be used or try a different sensitivity", defval="Automatically calculate",  options=["Automatically calculate", 
//      "Manually set multiplier","sensitivity 1", " sensitivity 2", " sensitivity 3"])
// calcmult_a=sensitivity_=="Calculate automatically"?4:sensitivity_=="Manually set multiplier"?5:sensitivity_=="sensitivity 1"?1: sensitivity_==" sensitivity 2"?2:sensitivity_== " sensitivity 3"?3:na
sensitivity_ = input.string(title='calculate truncated wave value automatically or manually, any red numbers means the manual method must be used or try a different sensitivity', defval='Automatically calculate', options=['Automatically calculate', 'Manually set multiplier', 'sensitivity 1', ' sensitivity 2', ' sensitivity 3', 'alternative auto method'])
calcmult_a = sensitivity_ == 'Calculate automatically' ? 4 : sensitivity_ == 'Manually set multiplier' ? 5 : sensitivity_ == 'sensitivity 1' ? 1 : sensitivity_ == ' sensitivity 2' ? 2 : sensitivity_ == ' sensitivity 3' ? 3 : sensitivity_ == 'alternative auto method' ? 6 : na
// sensitivity_= input(title="calculate truncated wave value automatically or manually, any red numbers means the manual method must be used or try a different sensitivity", defval="Automatically calculate",  options=["Automatically calculate", 
//      "Manually set multiplier","Automatic X 10", "Automatic X 100", "Automatic X 0.1", "alternative auto method","alternative auto method x10" ])
// calcmult_a=sensitivity_=="Calculate automatically"?4:sensitivity_=="Manually set multiplier"?5:sensitivity_=="Automatic X 10"?1: sensitivity_=="Automatic X 100"?2:
//      sensitivity_== "Automatic X 0.1"?3:sensitivity_== "alternative auto method"?6:sensitivity_== "alternative auto method x10"?7: na

shovol_ = input.string(title='Show volume in the truncated way Weis did, show actual volume or hide volume', defval='Weis truncated volume', options=['Weis truncated volume', 'Show true wave volume', 'Show waves without volume', 'Automatic X 10', 'Automatic X 100', 'Automatic X 0.1', 'Automatic X 0.01'])

// shovol_= input(title="Show volume in the truncated way Weis did, show actual volume or hide volume", defval="Weis truncated volume",  
//      options=["Weis truncated volume", "Show true wave volume","Show waves without volume" ])  //,"Automatic X 10", "Automatic X 100", "Automatic X 0.1"
shovol = shovol_ == 'Show waves without volume' ? false : true  // input(true, title="show volume?")
//calcmult = calcmult_a==5 or calcmult_a==6 or calcmult_a==7?false:true //if manual method is selected then calcmult will be false ie auto method when calcmult is true
calcmult = calcmult_a == 5 ? false : true
mul = input.int(0, minval=-12, title='Multiplier: Manual method increase to highest multiplier on waves shown when 0 displayed here. or hit up arrow then follow up or down instructions.')
mult = mul >= 1 or mul < 0 ? mul : na
wavecalc = input.string(title='How should wave size be defined and calculated, default is ATR for quick set up. Use traditional for the true David Weis method', defval='Average true range and multiplier', options=['traditional Weis wave size ie price', 'Average true range and multiplier', 'percent of last leg', ' percent of average price'])
minrang = input(.05000000000000, title='Select traditional Weis wave size, to ball park try 1/1000th of price then scale up or down by factors of 10 ')
truerange = wavecalc == 'Average true range and multiplier' ? true : false  // input(true, title="Use average true range?")
trralength = input.int(7, minval=2, title='Average true range length, larger values will smooth out the wave to wave variations of wave confirmation points. Try 7 or 70')
treafactor = input(.5000, title='Average true range multiple factor adjusts the wave size to pick up or eliminate smaller price moves')
percentw = wavecalc == 'percent of last leg' ? true : false  //  input(false, title="Use percent of last leg?")
percent = input(9.000, title='What percent of last leg to determine reverse')
minrangpw = wavecalc == ' percent of average price' ? true : false  //   input(false, title="Use percent of avg price to set wave size")
minrangprice = input(5.000, title='What percent of avg price to set wave size.  The value can vary widely from small fractions of a percent to 10% or more. This zigzag is close to close not high to low')
minrangp = minrangprice / 100
//sensitivity = calcmult_a==1?1:calcmult_a==1?1:calcmult_a==1?1:0 //input(0, title="sensitivity of the automatic wave volume calculator 1 is high sensitivity, 2 medium and three low sensitivity", minval=0, maxval=3)
sensitivity = calcmult_a == 1 ? 1 : calcmult_a == 2 ? 2 : calcmult_a == 3 ? 3 : 0
rf = input.int(0, minval=0, maxval=8, title='Move wave labels away from bars?')
linewidth = input.int(1, 'Wave Line Width', minval=1, maxval=5)
ts = input.string(title='Text size, auto does not seem to work', defval='normal', options=['tiny', 'small', 'normal', 'large', 'huge', 'auto'])
oldwave = input(false, title='show Pinescript Version 1 wave')
showweiswave = input(false, title='Show Version 3 weis wave (lagged pivots) unclick to show the pinescript V1 no lag version?')
colorb = input(title='Colour of text', defval=color.black)
colorr = input(title='Colour of text when warning of need to manually calculate mult factor', defval=color.red)
colorc = input(title='Colour of actual vol delta price and # bars', defval=color.navy)
colourupwave = input(title='Colour of up wave', defval=color.black)
colourdownwave = input(title='Colour of down wave', defval=color.black)
//size_=input(title="size of text", type=input=label.set.size, defval=size.auto)
pricehloc = input(title='Source, almost always it should be left as the default: close', defval=close)
//rf= input(0,minval=0, maxval=8, title="Move wave labels away from bars?")
wavechoice = wavechoice_ == 'Weis volume' ? 1 : wavechoice_ == 'delta price or delta pip' ? 2 : wavechoice_ == 'wave time - time based charts only' ? 3 : wavechoice_ == 'Weis cumulative volume/delta price' ? 4 : wavechoice_ == 'Ord volume' ? 5 : wavechoice_ == 'Weis volume and # bars per wave' ? 6 : wavechoice_ == 'Weis wave volume and Ord volume' ? 7 : na  // ord style vol per bar for each wave
//wavechoice:=wavechoice==5 or wavechoice==6?1:wavechoice

avspread = false  //input(false, title= "Use avg high low spread?")
spreadlength = 10  //input(10,  title= "Average Spread length")
spreadfactor = .5  //input(.5,  title= "Multiple of spread factor")
trange = treafactor * ta.atr(trralength)
minrangetr = spreadfactor * ta.ema(high - low, spreadlength)
var count = 0
count := nz(count[1]) + 1
tickid = ticker.new(syminfo.prefix, syminfo.ticker, session.regular)

dopen = request.security(syminfo.tickerid, 'D', open)


sma_1 = ta.sma(close, 1)
sma_2 = ta.sma(close, 2)
sma_3 = ta.sma(close, 3)
sma_4 = ta.sma(close, 4)
sma_5 = ta.sma(close, 5)
sma_6 = ta.sma(close, 6)
sma_7 = ta.sma(close, 7)
sma_8 = ta.sma(close, 8)
sma_9 = ta.sma(close, 9)
sma_10 = ta.sma(close, 10)
sma_11 = ta.sma(close, 11)
sma_12 = ta.sma(close, 13)
sma_13 = ta.sma(close, 14)
sma_14 = ta.sma(close, 15)
sma_15 = ta.sma(close, 16)
sma_16 = ta.sma(close, 17)
sma_17 = ta.sma(close, 18)
sma_18 = ta.sma(close, 19)
sma_19 = ta.sma(close, 20)
avgprice = count == 1 ? sma_1 : count == 2 ? sma_2 : count == 3 ? sma_3 : count == 4 ? sma_4 : count == 5 ? sma_5 : count == 6 ? sma_6 : count == 7 ? sma_7 : count == 8 ? sma_8 : count == 9 ? sma_9 : count == 10 ? sma_10 : count == 11 ? sma_11 : count == 12 ? sma_12 : count == 14 ? sma_13 : count == 15 ? sma_14 : count == 16 ? sma_15 : count == 17 ? sma_16 : count == 18 ? sma_17 : count == 19 ? sma_18 : sma_19
var minrangee = 0.0
minrange = minrangpw ? nz(avgprice) * nz(minrangp) : percentw and count <= 1 ? avgprice * .0001 : percentw ? nz(minrangee[1]) : minrangpw == false and percentw == false and truerange == false and avspread == false ? minrang : nz(minrangee[1])  // count==2? nz(minrangee[1]):0

shorenbar = false  //input(false, title="Show individual bar time")
renkotrend = 0
renkowave = 0
bar1 = 0.0
Rangebartime = 0.0
Rangebartime_ = 0.0
renkoup = 0.0
renkodown = 0.0
rbt = 0.0
r = 0.0
mins = 0.0


bf = true  //input(true, title=" recolour a limbo wave once direction confirmed?")

//Rangebartime := r // na(t3) ? bar1 - bar1[1] : bar1 - bar1[1] - t5
Rangebartime := timeframe.isintraday ? time_close / 60000 - time / 60000 : timeframe.isdaily ? time_close / 3600000 - time / 3600000 : timeframe.isdwm ? 1 : na
volumm = wavechoice == 1 or wavechoice == 4 or wavechoice == 5 or wavechoice == 6 or wavechoice == 7 ? volume : wavechoice == 2 ? close - close[1] : wavechoice == 3 ? Rangebartime : na
//volumme = wavechoice == 3 and volumm == 0 ? r : volumm
volumme = wavechoice == 3 and volumm == 0 ? Rangebartime : volumm

linetra = 40  //input(40, title='wave line transparency 100 is transparent, 0 is not')
var hi = 0.0
var lo = 0.0
Diffu = nz(lo[1]) + minrange - (nz(hi[1]) - minrange)  ////1
Diff = Diffu > 0 ? Diffu : 0

showconfirmedpivots = false  // input(true, title="Show the minimum wave confirmation?")
showallmult = false  //input(true, title="Show factors for rez of volume?")
//calcmult= input(false, title="Let script calc multi factor, red 0s means factor changed on last bar?")
showmulti = mul == 0 and calcmult == false ? true : false
//count= nz(count[1]) + 1
//calcmult = automan == true ? true : false
//lookback = lookbac == 0 ? periods : lookbac
var trendd = 0
var trend = 0
//pricehloc:= high

hi := pricehloc >= nz(hi[1]) - minrange + Diff and pricehloc[1] <= nz(lo[1]) + minrange - Diff and pricehloc - nz(lo[1]) >= minrange or nz(trendd[1]) == -1 and nz(hi[1]) - nz(lo[1]) < 2 * minrange and pricehloc[1] <= nz(lo[1]) + minrange and pricehloc[0] >= nz(lo[1]) + minrange ? pricehloc : nz(trendd[0]) == 1 and pricehloc >= nz(hi[1]) - minrange + Diff and pricehloc[1] <= nz(lo[1]) + minrange - Diff and pricehloc[0] >= nz(lo[1]) + minrange ? pricehloc : pricehloc >= nz(hi[1]) - minrange ? math.max(pricehloc[0], nz(hi[1])) : pricehloc >= nz(lo[1]) + minrange and (nz(trend[1]) == -2 or nz(trend[1]) == -1) ? pricehloc : nz(hi[1])
//pricehloc:=low
trenddo = pricehloc >= nz(hi[0]) - minrange ? 2 : pricehloc >= nz(lo[1]) + minrange ? 1 : pricehloc <= nz(lo[1]) + minrange ? -1 : pricehloc <= nz(hi[0]) - minrange ? -2 : na

lo := pricehloc <= nz(lo[1]) + minrange - Diff and pricehloc[1] >= nz(hi[0]) - minrange + Diff and nz(hi[0]) - pricehloc >= minrange or nz(trendd[1]) == 1 and hi[0] - nz(lo[1]) < 2 * minrange and pricehloc[1] > hi[0] - minrange and pricehloc[0] <= hi[0] - minrange ? pricehloc : pricehloc <= nz(lo[1]) + minrange ? math.min(pricehloc[0], nz(lo[1])) : pricehloc <= nz(hi[0]) - minrange and (nz(trenddo[0]) == 2 or nz(trenddo[0]) == 1) ? pricehloc : nz(lo[1])

trend := pricehloc >= nz(hi[0]) - minrange ? 2 : pricehloc >= nz(lo[0]) + minrange ? 1 : pricehloc <= nz(lo[0]) + minrange ? -1 : pricehloc <= nz(hi[0]) - minrange ? -2 : na

trendd := hi[0] != hi[1] or hi[0] == pricehloc and nz(trendd[1]) == -1 ? 1 : lo[0] != lo[1] or lo[0] == pricehloc and nz(trendd[1]) == 1 ? -1 : nz(trendd[1]) == 1 and not(lo[0] > lo[1] or lo[0] == pricehloc and nz(trendd[1]) == 1) ? 1 : nz(trendd[1]) == -1 and not(hi[0] < hi[1] or hi[0] == pricehloc and nz(trendd[1]) == -1) ? -1 : 0

ct = nz(trendd[0]) == 1 and nz(trendd[1]) == -1 ? 1 : nz(trendd[0]) == -1 and nz(trendd[1]) == -1 ? -2 : nz(trendd[0]) == 1 and nz(trendd[1]) == 1 ? 2 : nz(trendd[0]) == -1 and nz(trendd[1]) == 1 ? -1 : na

maxpivot = trendd == 1 and nz(hi[0]) == pricehloc ? pricehloc : na
mphigh = maxpivot ? high : na
minpivot = trendd == -1 and nz(lo[0]) == pricehloc ? pricehloc : na
maxpivottf = maxpivot == pricehloc ? true : false
minpivottf = minpivot == pricehloc ? true : false
var limboup = 0
var lvolup = 0.0
var limbodown = 0
var lvoldown = 0.0
limboup := pricehloc < nz(hi[0]) and nz(trendd[0]) == 1 ? nz(limboup[1]) + 1 : nz(trendd[0]) == 1 and nz(trendd[1]) == -1 ? nz(limboup[1]) : 0
lvolup := nz(limboup[0]) == 0 ? 0 : nz(limboup[0]) >= 1 ? nz(lvolup[1]) + volumme : volumme

limbodown := pricehloc > nz(lo[0]) and nz(trendd[0]) == -1 ? nz(limbodown[1]) + 1 : nz(trendd[0]) == -1 and nz(trendd[1]) == 1 ? nz(limbodown[1]) : 0
lvoldown := nz(limbodown[0]) == 0 ? 0 : nz(limbodown[0]) >= 1 ? nz(lvoldown[1]) + volumme : volumme



// use limbo up cont to calc volume
var Counttrendup = 0
var Counttrenddown = 0
var volup = 0.0
var voldown = 0.0
Counttrendup := nz(trendd[0]) == 1 and nz(trendd[1]) == -1 ? 1 + nz(limbodown[1]) : nz(trendd[0]) == -1 and nz(trendd[1]) == 1 ? na : nz(trendd[0]) == 1 and nz(trendd[1]) == 1 ? nz(Counttrendup[1]) + 1 : 0
Counttrenddown := nz(trendd[0]) == -1 and nz(trendd[1]) == 1 ? 1 + nz(limboup[1]) : nz(trendd[0]) == 1 and nz(trendd[1]) == -1 ? na : nz(trendd[0]) == -1 and nz(trendd[1]) == -1 ? nz(Counttrenddown[1]) + 1 : 0

// direction = nz(Counttrendup[0]) >= 1 and nz(limboup[0]) == 0 ? 2 : 
//   nz(Counttrenddown[0]) >= 1 and nz(limbodown[0]) == 0 ? -2 : 
//   nz(Counttrendup[0]) >= 1 and nz(limboup[0]) > 0 ? 1 : 
//   nz(Counttrenddown[0]) >= 1 and nz(limbodown[0]) > 0 ? -1 : na

volup := nz(limbodown[0]) == 0 and nz(Counttrendup[0]) == 0 ? 0 : nz(limbodown[0]) >= 1 and nz(Counttrendup[0]) == 0 ? nz(volup[1]) + volumme : nz(limbodown[1]) >= 1 and nz(Counttrendup[0]) >= 1 and nz(trendd[0]) == 1 and nz(trendd[1]) == -1 ? nz(volup[1]) + volumme : nz(limbodown[1]) == 0 and nz(Counttrendup[0]) >= 1 and nz(trendd[0]) == 1 and nz(trendd[1]) == -1 ? volumme : nz(limbodown[1]) == 0 and nz(Counttrendup[0]) >= 1 and nz(trendd[0]) == 1 and nz(trendd[1]) == 1 ? nz(volup[1]) + volumme : na

voldown := nz(limboup[0]) == 0 and nz(Counttrenddown[0]) == 0 ? 0 : nz(limboup[0]) >= 1 and nz(Counttrenddown[0]) == 0 ? nz(voldown[1]) + volumme : nz(limboup[1]) >= 1 and nz(Counttrenddown[0]) >= 1 and nz(trendd[0]) == -1 and nz(trendd[1]) == 1 ? nz(voldown[1]) + volumme : nz(limboup[1]) == 0 and nz(Counttrenddown[0]) >= 1 and nz(trendd[0]) == -1 and nz(trendd[1]) == 1 ? volumme : nz(limboup[1]) == 0 and nz(Counttrenddown[0]) >= 1 and nz(trendd[0]) == -1 and nz(trendd[1]) == -1 ? nz(voldown[1]) + volumme : na
// ct==1 change to up, ct== 2 stay up, ct= =-1 change to down, ct== 2 stay down

toppe = limboup[1] == 0 and ct == -1 ? maxpivot[1] : limboup[1] == 1 and ct == -1 ? maxpivot[2] : limboup[1] == 2 and ct == -1 ? maxpivot[3] : limboup[1] == 3 and ct == -1 ? maxpivot[4] : limboup[1] == 4 and ct == -1 ? maxpivot[5] : limboup[1] == 5 and ct == -1 ? maxpivot[6] : limboup[1] == 6 and ct == -1 ? maxpivot[7] : limboup[1] == 7 and ct == -1 ? maxpivot[8] : limboup[1] == 8 and ct == -1 ? maxpivot[9] : limboup[1] == 9 and ct == -1 ? maxpivot[10] : limboup[1] == 10 and ct == -1 ? maxpivot[11] : limboup[1] == 11 and ct == -1 ? maxpivot[12] : limboup[1] == 12 and ct == -1 ? maxpivot[13] : limboup[1] == 13 and ct == -1 ? maxpivot[14] : limboup[1] == 14 and ct == -1 ? maxpivot[15] : limboup[1] == 15 and ct == -1 ? maxpivot[16] : limboup[1] == 16 and ct == -1 ? maxpivot[17] : limboup[1] == 17 and ct == -1 ? maxpivot[18] : limboup[1] == 18 and ct == -1 ? maxpivot[19] : limboup[1] == 19 and ct == -1 ? maxpivot[20] : limboup[1] == 20 and ct == -1 ? maxpivot[21] : limboup[1] == 21 and ct == -1 ? maxpivot[22] : limboup[1] == 22 and ct == -1 ? maxpivot[23] : limboup[1] == 23 and ct == -1 ? maxpivot[24] : limboup[1] == 24 and ct == -1 ? maxpivot[25] : limboup[1] == 25 and ct == -1 ? maxpivot[26] : limboup[1] == 26 and ct == -1 ? maxpivot[27] : limboup[1] == 27 and ct == -1 ? maxpivot[28] : limboup[1] == 28 and ct == -1 ? maxpivot[29] : limboup[1] == 29 and ct == -1 ? maxpivot[30] : limboup[1] == 30 and ct == -1 ? maxpivot[31] : limboup[1] == 31 and ct == -1 ? maxpivot[32] : limboup[1] == 32 and ct == -1 ? maxpivot[33] : limboup[1] == 33 and ct == -1 ? maxpivot[34] : limboup[1] == 34 and ct == -1 ? maxpivot[35] : limboup[1] == 35 and ct == -1 ? maxpivot[36] : limboup[1] == 36 and ct == -1 ? maxpivot[37] : limboup[1] == 37 and ct == -1 ? maxpivot[38] : limboup[1] == 38 and ct == -1 ? maxpivot[39] : limboup[1] == 39 and ct == -1 ? maxpivot[40] : limboup[1] == 40 and ct == -1 ? maxpivot[41] : limboup[1] == 41 and ct == -1 ? maxpivot[42] : limboup[1] == 42 and ct == -1 ? maxpivot[43] : limboup[1] == 43 and ct == -1 ? maxpivot[44] : limboup[1] == 44 and ct == -1 ? maxpivot[45] : limboup[1] == 45 and ct == -1 ? maxpivot[46] : limboup[1] == 46 and ct == -1 ? maxpivot[47] : limboup[1] == 47 and ct == -1 ? maxpivot[48] : limboup[1] == 48 and ct == -1 ? maxpivot[49] : limboup[1] == 49 and ct == -1 ? maxpivot[40] : limboup[1] == 50 and ct == -1 ? maxpivot[51] : limboup[1] == 51 and ct == -1 ? maxpivot[52] : limboup[1] == 52 and ct == -1 ? maxpivot[53] : limboup[1] == 53 and ct == -1 ? maxpivot[54] : limboup[1] == 54 and ct == -1 ? maxpivot[55] : limboup[1] == 55 and ct == -1 ? maxpivot[56] : limboup[1] == 56 and ct == -1 ? maxpivot[57] : limboup[1] == 57 and ct == -1 ? maxpivot[58] : limboup[1] == 58 and ct == -1 ? maxpivot[59] : limboup[1] == 59 and ct == -1 ? maxpivot[60] : limboup[1] == 60 and ct == -1 ? maxpivot[61] : limboup[1] == 61 and ct == -1 ? maxpivot[62] : limboup[1] == 62 and ct == -1 ? maxpivot[63] : limboup[1] == 63 and ct == -1 ? maxpivot[64] : limboup[1] == 64 and ct == -1 ? maxpivot[65] : limboup[1] == 65 and ct == -1 ? maxpivot[66] : limboup[1] == 66 and ct == -1 ? maxpivot[67] : limboup[1] == 67 and ct == -1 ? maxpivot[68] : limboup[1] == 68 and ct == -1 ? maxpivot[69] : limboup[1] == 69 and ct == -1 ? maxpivot[70] : limboup[1] == 70 and ct == -1 ? maxpivot[71] : limboup[1] == 71 and ct == -1 ? maxpivot[72] : na


bottomme = limbodown[1] == 0 and ct == 1 ? minpivot[1] : limbodown[1] == 1 and ct == 1 ? minpivot[2] : limbodown[1] == 2 and ct == 1 ? minpivot[3] : limbodown[1] == 3 and ct == 1 ? minpivot[4] : limbodown[1] == 4 and ct == 1 ? minpivot[5] : limbodown[1] == 5 and ct == 1 ? minpivot[6] : limbodown[1] == 6 and ct == 1 ? minpivot[7] : limbodown[1] == 7 and ct == 1 ? minpivot[8] : limbodown[1] == 8 and ct == 1 ? minpivot[9] : limbodown[1] == 9 and ct == 1 ? minpivot[10] : limbodown[1] == 10 and ct == 1 ? minpivot[11] : limbodown[1] == 11 and ct == 1 ? minpivot[12] : limbodown[1] == 12 and ct == 1 ? minpivot[13] : limbodown[1] == 13 and ct == 1 ? minpivot[14] : limbodown[1] == 14 and ct == 1 ? minpivot[15] : limbodown[1] == 15 and ct == 1 ? minpivot[16] : limbodown[1] == 16 and ct == 1 ? minpivot[17] : limbodown[1] == 17 and ct == 1 ? minpivot[18] : limbodown[1] == 18 and ct == 1 ? minpivot[19] : limbodown[1] == 19 and ct == 1 ? minpivot[20] : limbodown[1] == 20 and ct == 1 ? minpivot[21] : limbodown[1] == 21 and ct == 1 ? minpivot[22] : limbodown[1] == 22 and ct == 1 ? minpivot[23] : limbodown[1] == 23 and ct == 1 ? minpivot[24] : limbodown[1] == 24 and ct == 1 ? minpivot[25] : limbodown[1] == 25 and ct == 1 ? minpivot[26] : limbodown[1] == 26 and ct == 1 ? minpivot[27] : limbodown[1] == 27 and ct == 1 ? minpivot[28] : limbodown[1] == 28 and ct == 1 ? minpivot[29] : limbodown[1] == 29 and ct == 1 ? minpivot[30] : limbodown[1] == 30 and ct == 1 ? minpivot[31] : limbodown[1] == 31 and ct == 1 ? minpivot[32] : limbodown[1] == 32 and ct == 1 ? minpivot[33] : limbodown[1] == 33 and ct == 1 ? minpivot[34] : limbodown[1] == 34 and ct == 1 ? minpivot[35] : limbodown[1] == 35 and ct == 1 ? minpivot[36] : limbodown[1] == 36 and ct == 1 ? minpivot[37] : limbodown[1] == 37 and ct == 1 ? minpivot[38] : limbodown[1] == 38 and ct == 1 ? minpivot[39] : limbodown[1] == 39 and ct == 1 ? minpivot[40] : limbodown[1] == 40 and ct == 1 ? minpivot[41] : limbodown[1] == 41 and ct == 1 ? minpivot[42] : limbodown[1] == 42 and ct == 1 ? minpivot[43] : limbodown[1] == 43 and ct == 1 ? minpivot[44] : limbodown[1] == 44 and ct == 1 ? minpivot[45] : limbodown[1] == 45 and ct == 1 ? minpivot[46] : limbodown[1] == 46 and ct == 1 ? minpivot[47] : limbodown[1] == 47 and ct == 1 ? minpivot[48] : limbodown[1] == 48 and ct == 1 ? minpivot[49] : limbodown[1] == 49 and ct == 1 ? minpivot[50] : limbodown[1] == 50 and ct == 1 ? minpivot[51] : limbodown[1] == 51 and ct == 1 ? minpivot[52] : limbodown[1] == 52 and ct == 1 ? minpivot[53] : limbodown[1] == 53 and ct == 1 ? minpivot[54] : limbodown[1] == 54 and ct == 1 ? minpivot[55] : limbodown[1] == 55 and ct == 1 ? minpivot[56] : limbodown[1] == 56 and ct == 1 ? minpivot[57] : limbodown[1] == 57 and ct == 1 ? minpivot[58] : limbodown[1] == 58 and ct == 1 ? minpivot[59] : limbodown[1] == 59 and ct == 1 ? minpivot[60] : limbodown[1] == 60 and ct == 1 ? minpivot[61] : limbodown[1] == 61 and ct == 1 ? minpivot[62] : limbodown[1] == 62 and ct == 1 ? minpivot[63] : limbodown[1] == 63 and ct == 1 ? minpivot[64] : limbodown[1] == 64 and ct == 1 ? minpivot[65] : limbodown[1] == 65 and ct == 1 ? minpivot[66] : limbodown[1] == 66 and ct == 1 ? minpivot[67] : limbodown[1] == 67 and ct == 1 ? minpivot[68] : limbodown[1] == 68 and ct == 1 ? minpivot[69] : limbodown[1] == 69 and ct == 1 ? minpivot[70] : limbodown[1] == 70 and ct == 1 ? minpivot[71] : limbodown[1] == 71 and ct == 1 ? minpivot[72] : na


// time estimate bars
var deltaclose = 0.0
begin = toppe > 0 ? Counttrendup[1] + 1 : bottomme > 0 ? Counttrenddown[1] + 1 : na
end = toppe > 0 ? limboup[1] + 1 : bottomme > 0 ? limbodown[1] + 1 : na
time_ = (begin - end) * r  //new algo does not use this uses time_close instead
bars_ = begin - end  // use for ord type avg wave volume/bar
// startclose=close[begin]
// endclose=close[end]
deltaclose := toppe or bottomme ? math.abs(close[begin] - close[end]) : na
//plot(deltaclose,style=cross, color=orange, linewidth=4 )



alertcondition(toppe, title='a top confirmed', message='Weis wave top confirmed!')
alertcondition(bottomme, title='a bottom confirmed', message='Weis wave bottom confirmed!')


plotvolup1 = ct == -1 ? volup[1] - lvolup[1] : na  //maxpivottf==true and
//plotvolup =  wavechoice == 5 ?plotvolup1 / bars_: wavechoice == 4 ? deltaclose/plotvolup1  : plotvolup1
plotvolup = wavechoice == 5 ? plotvolup1 / bars_ : wavechoice == 4 ? plotvolup1 / deltaclose : plotvolup1  // speed index style could mult x 1.53
//plotvolup =  wavechoice == 5 ?plotvolup1 / bars_: wavechoice == 4 ? plotvolup1 / (plottoppeclose - plotbottommeclose) : plotvolup1
// should replace with log10 truncated with out decimal round(log10(plotvolup))-log10(plotvolup) truncate to whole number without rounding up

//old way  #13 now 44 or 43 Weis V4 jayy

cpvabs = math.round(math.log10(plotvolup) - .5)

cpv = cpvabs >= 19 and wavechoice == 3 ? 1 : cpvabs
var cpvtoppe = 0
var cpvdbottomme = 0

cpvtoppe := toppe ? cpv : nz(cpvtoppe[1])
plotvoldown1 = ct == 1 ? math.abs(voldown[1] - lvoldown[1]) : na  //maxpivottf==true and
//(bottomme?plotvoldown/-(plottoppeclose-plotbottommeclose):toppe?plotvolup/(plottoppeclose-plotbottommeclose)
//plotvoldown = wavechoice == 5 ?abs(plotvoldown1) / bars_: wavechoice == 4 ?  deltaclose/abs(plotvoldown1): abs(plotvoldown1)
plotvoldown = wavechoice == 5 ? math.abs(plotvoldown1) / bars_ : wavechoice == 4 ? math.abs(plotvoldown1) / deltaclose : math.abs(plotvoldown1)  // speed index style
//plotvoldown = wavechoice == 5 ?abs(plotvoldown1) / bars_: wavechoice == 4 ? abs(plotvoldown1) / (plottoppeclose - plotbottommeclose) : 
//   abs(plotvoldown1)   
// cpvdabs= log10(plotvoldown)>0 and round(log10(plotvoldown))-log10(plotvoldown)>0?round(log10(plotvoldown))-1:
//      log10(plotvoldown)<0 and round(log10(plotvoldown))-log10(plotvoldown)<0?round(log10(plotvoldown)):round(log10(plotvoldown))
cpvdabs = math.round(math.log10(plotvoldown) - .5)  //round(log10(plotvoldown))-log10(plotvoldown)>0?round(log10(plotvoldown))-1:round(log10(plotvoldown))


cpvd = cpvdabs == 19 and wavechoice == 3 ? 1 : cpvdabs
k = cpvd > 20 or cpv > 20 ? true : false
cpvdbottomme := bottomme ? cpvd : nz(cpvdbottomme[1])

var cpvhe = 0.0
highest_1 = ta.highest(cpvtoppe, 50)
highest_2 = ta.highest(cpvdbottomme, 50)
highest_3 = ta.highest(cpvtoppe, 50)
highest_4 = ta.highest(cpvdbottomme, 50)
highest_5 = ta.highest(cpvtoppe, 250)
highest_6 = ta.highest(cpvdbottomme, 250)
highest_7 = ta.highest(cpvtoppe, 250)
highest_8 = ta.highest(cpvdbottomme, 250)
highest_9 = ta.highest(cpvtoppe, 500)
highest_10 = ta.highest(cpvdbottomme, 500)
highest_11 = ta.highest(cpvtoppe, 500)
highest_12 = ta.highest(cpvdbottomme, 500)
highest_13 = ta.highest(cpvtoppe, 10)
highest_14 = ta.highest(cpvdbottomme, 10)
highest_15 = ta.highest(cpvtoppe, 10)
highest_16 = ta.highest(cpvdbottomme, 10)
highest_17 = ta.highest(cpvtoppe, 30)
highest_18 = ta.highest(cpvdbottomme, 30)
highest_19 = ta.highest(cpvtoppe, 30)
highest_20 = ta.highest(cpvdbottomme, 30)
highest_21 = ta.highest(cpvtoppe, 50)
highest_22 = ta.highest(cpvdbottomme, 50)
highest_23 = ta.highest(cpvtoppe, 50)
highest_24 = ta.highest(cpvdbottomme, 50)
highest_25 = ta.highest(cpvtoppe, 100)
highest_26 = ta.highest(cpvdbottomme, 100)
highest_27 = ta.highest(cpvtoppe, 100)
highest_28 = ta.highest(cpvdbottomme, 100)
highest_29 = ta.highest(cpvtoppe, 200)
highest_30 = ta.highest(cpvdbottomme, 200)
highest_31 = ta.highest(cpvtoppe, 200)
highest_32 = ta.highest(cpvdbottomme, 200)
highest_33 = ta.highest(cpvtoppe, 500)
highest_34 = ta.highest(cpvdbottomme, 500)
highest_35 = ta.highest(cpvtoppe, 500)
highest_36 = ta.highest(cpvdbottomme, 500)
highest_37 = ta.highest(cpvtoppe, 1000)
highest_38 = ta.highest(cpvdbottomme, 1000)
highest_39 = ta.highest(cpvtoppe, 1000)
highest_40 = ta.highest(cpvdbottomme, 1000)
highest_41 = ta.highest(cpvtoppe, 4000)
highest_42 = ta.highest(cpvdbottomme, 4000)
highest_43 = ta.highest(cpvtoppe, 4000)
highest_44 = ta.highest(cpvdbottomme, 4000)
cpvhe := calcmult_a == 1 and count > 50 ? nz(highest_1) > nz(highest_2) ? nz(highest_3) : nz(highest_4) : calcmult_a == 2 and count > 250 ? nz(highest_5) > nz(highest_6) ? nz(highest_7) : nz(highest_8) : calcmult_a == 3 and count > 500 ? nz(highest_9) > nz(highest_10) ? nz(highest_11) : nz(highest_12) : count >= 10 and count < 30 ? nz(highest_13) > nz(highest_14) ? nz(highest_15) : nz(highest_16) : count >= 30 and count < 50 ? nz(highest_17) > nz(highest_18) ? nz(highest_19) : nz(highest_20) : count >= 50 and count < 100 ? nz(highest_21) > nz(highest_22) ? nz(highest_23) : nz(highest_24) : count >= 100 and count < 200 ? nz(highest_25) > nz(highest_26) ? nz(highest_27) : nz(highest_28) : count >= 200 and count < 500 and (timeframe.period == '1' or timeframe.period == '2' or timeframe.period == '3' or timeframe.period == '4' or timeframe.period == '5') ? nz(highest_29) > nz(highest_30) ? nz(highest_31) : nz(highest_32) : count >= 500 and count < 1000 and (timeframe.period == '1' or timeframe.period == '2' or timeframe.period == '3' or timeframe.period == '4' or timeframe.period == '5') ? nz(highest_33) > nz(highest_34) ? nz(highest_35) : nz(highest_36) : count >= 1000 and count < 4000 and (timeframe.period == '1' or timeframe.period == '2' or timeframe.period == '3' or timeframe.period == '4' or timeframe.period == '5') ? nz(highest_37) > nz(highest_38) ? nz(highest_39) : nz(highest_40) : count >= 4000 and (timeframe.period == '1' or timeframe.period == '2' or timeframe.period == '3' or timeframe.period == '4' or timeframe.period == '5') ? nz(highest_41) > nz(highest_42) ? nz(highest_43) : nz(highest_44) : count >= 200 ? nz(highest_29) > nz(highest_30) ? nz(highest_31) : nz(highest_32) : na

aa = cpvtoppe  //aa=cpvtoppe, bb=cpvdbottomme  //aa=lowest(cpvtoppe, 2000), bb=lowest(cpvdbottomme, 2000)
bb = cpvdbottomme
var famult = 0.0
ab = 0.0
ba = 0.0
abh = 0.0
bal = 0.0
ab := nz(aa[0]) >= 1 and nz(aa[1]) < 1 ? nz(aa[0]) : aa[0] < nz(ab[1]) ? nz(aa[0]) : aa[0] > nz(aa[1]) ? nz(ab[1]) : aa[0] == nz(aa[1]) ? nz(ab[1]) : nz(ab[1])  //aa[0]>=nz(aa[1])?nz(aa[1]):aa[0]==0?nz(ab[1]):9  //lowest(cpvtoppe, 2000), bb=lowest(cpvdbottomme, 2000)
abh := aa[0] > nz(abh[1]) ? nz(aa[0]) : aa[0] < nz(aa[1]) ? nz(abh[1]) : aa[0] == nz(aa[1]) ? nz(abh[1]) : nz(abh[1])
//nz(aa[0])>=1 and nz(aa[1])<1? nz(aa[0]):
ba := nz(bb[0]) >= 1 and nz(bb[1]) < 1 ? nz(bb[0]) : bb[0] < nz(ba[1]) ? nz(bb[0]) : bb[0] > nz(bb[1]) ? nz(ba[1]) : bb[0] == nz(bb[1]) ? nz(ba[1]) : nz(ba[1])
bal := bb[0] > nz(bal[1]) ? nz(bb[0]) : bb[0] < nz(bb[1]) ? nz(bal[1]) : bb[0] == nz(bb[1]) ? nz(bal[1]) : nz(bal[1])
//nz(bb[0])>=1 and nz(bb[1])<1? nz(bb[0]):
famult := abh < 50 or bal < 50 ? math.max(abh, bal) : cpvhe
//calcmult and 
// multi =  calcmult_a==6?pow(10,famult-2): calcmult_a==7?pow(10,famult-3):calcmult ? pow(10, cpvh - 2) : 
//   pow(10, (wavechoice == 1 or wavechoice == 4 or wavechoice==5 or wavechoice==6 or wavechoice==7? mult : (wavechoice==2 or wavechoice == 3) and cpvh>=0 ? mult -2 : mult) - 2)
calcmultm = calcmult and calcmult_a == 6 ? true : false
calcmult_m = calcmultm or calcmult ? true : false



cpvh = 0.0
cpvh := calcmultm ? famult : cpvhe  //(timeframe.isdaily or timeframe.period == "4H") and cpvhe == -10 and 


multsen = calcmult_m and shovol_ == 'Automatic X 10' ? 1 : calcmult_m and shovol_ == 'Automatic X 100' ? 0 : calcmult_m and shovol_ == 'Automatic X 0.1' ? 3 : calcmult_m and shovol_ == 'Automatic X 0.01' ? 4 : calcmult ? 2 : 4
multi = calcmult_m ? math.pow(10, cpvh - multsen) : math.pow(10, (wavechoice == 1 or wavechoice == 4 or wavechoice == 5 or wavechoice == 6 or wavechoice == 7 ? mult : (wavechoice == 2 or wavechoice == 3) and cpvh >= 0 ? mult - 2 : mult) - 2)

cpvh_new = calcmultm ? famult + 2 - multsen : calcmult ? cpvh + 2 - multsen : na

var cpvhm = 0.0
cpvhm := calcmult and calcmult_a == 6 ? famult - 2 : calcmult_a == 7 ? famult - 3 : calcmult ? cpvh - 2 : wavechoice == 1 or wavechoice == 4 or wavechoice == 5 or wavechoice == 6 or wavechoice == 7 ? cpvh - 2 : wavechoice == 3 ? cpvh : cpvh

var cpvmult = 0.0
cpvmult := math.max(aa, bb)
// multi = calcmult ? pow(10, cpvh - 2) : 
//   pow(10, (wavechoice == 1 or wavechoice == 4 or wavechoice==5 or wavechoice==6 or wavechoice==7? mult : wavechoice == 3 ? mult - 10 : mult - 10) - 2)
change = multi != nz(multi[1]) ? true : false

arreg = plotvolup / multi
ar = calcmult_m ? arreg >= 1000 ? arreg / 10 : arreg : showmulti and wavechoice == 2 and cpvh >= 0 ? cpv + 2 : (wavechoice == 1 or wavechoice == 4 or wavechoice == 5 or wavechoice == 6 or wavechoice == 7) and showmulti ? cpv : (wavechoice == 2 or wavechoice == 3) and showmulti and cpvh >= 0 ? cpv + 2 : showmulti and (wavechoice == 2 or wavechoice == 3) and cpvh < 0 ? cpv + 1 : arreg >= 1000 ? arreg / 10 : arreg  //   arreg //(arreg<1?arreg*10:arreg>=1000?arreg/10:arreg)
var armult = 0.0
armult := nz(ar[0]) > 0 ? nz(ar[0]) : nz(armult[1]) != nz(ar[0]) and nz(ar[0]) > 0 and nz(armult[1]) > 0 ? nz(ar[0]) : nz(armult[1])
y = ar < 1 ? 0 : na
A3i = math.round((math.round(ar) - 5) / 10) * 10
A3 = math.round(ar) - A3i
A2i = math.round((A3i / 10 - 5) / 10) * 10
A2 = A3i / 10 < 10 ? A3i / 10 : ar < 10 ? 0 : A3i / 10 - A2i
A1i = math.round((A2i / 10 - 5) / 10) * 10
A1 = A2i / 10 < 10 ? A2i / 10 : A3i < 10 ? 0 : A2i / 10 - A1i
T = A1i / 10 > 0 and A1i / 10 < 10 ? A1i / 10 : na

ardreg = plotvoldown / multi
ard = calcmult_m ? ardreg >= 1000 ? ardreg / 10 : ardreg : showmulti and wavechoice == 2 and cpvh >= 0 ? cpvd + 2 : (wavechoice == 1 or wavechoice == 4 or wavechoice == 5 or wavechoice == 6 or wavechoice == 7) and showmulti ? cpvd : (wavechoice == 2 or wavechoice == 3) and showmulti and cpvh >= 0 ? cpvd + 2 : showmulti and (wavechoice == 2 or wavechoice == 3) and cpvh < 0 ? cpvd + 1 : ardreg >= 1000 ? ardreg / 10 : ardreg
yd = ard < 1 ? 0 : na
A3id = math.round((math.round(ard) - 5) / 10) * 10
A3D = math.round(ard) - A3id
A2id = math.round((A3id / 10 - 5) / 10) * 10
A2D = A3id / 10 < 10 ? A3id / 10 : ard < 10 ? 0 : A3id / 10 - A2id
A1id = math.round((A2id / 10 - 5) / 10) * 10
A1D = A2id / 10 < 10 ? A2id / 10 : A3id < 10 ? 0 : A2id / 10 - A1id
Td = A1id / 10 > 0 and A1id / 10 < 10 ? A1id / 10 : na



topp = 0.0
bottomm = 0.0
multtoppelast = 0.0
multbottommelast = 0.0
interimtop = 0.0
interimbottom = 0.0

topp := toppe > 0 ? nz(toppe) : nz(topp[1])
bottomm := bottomme > 0 ? nz(bottomme) : nz(bottomm[1])
multtoppelast := toppe ? multi : nz(multtoppelast[1])
multbottommelast := bottomme ? multi : nz(multbottommelast[1])
interimtop := maxpivottf ? maxpivot : nz(interimtop[1])
interimbottom := minpivottf ? minpivot : nz(interimbottom[1])

minrangee := minrangpw == true ? avgprice * minrangp : truerange ? trange : avspread ? minrangetr : percentw and nz(ct) == 2 ? (nz(interimtop) - nz(bottomm[0])) * (percent / 100) : percentw and nz(ct) == 1 ? (nz(interimtop) - nz(bottomm[0])) * (percent / 100) : percentw and nz(ct) == -2 ? (nz(topp[0]) - nz(interimbottom)) * (percent / 100) : percentw and nz(ct) == -1 ? (nz(topp[0]) - nz(interimbottom)) * (percent / 100) : minrangpw == false and percentw == false and truerange == false and avspread == false ? minrang : na  //nz(ct)==1 or 
lbwvv = false
plot(oldwave ? not lbwvv ? ct == -1 and showweiswave ? toppe : ct == 1 and showweiswave ? bottomme : maxpivottf == true and not showweiswave ? maxpivot : minpivottf == true and not showweiswave ? minpivot : na : maxpivottf == true ? maxpivot : minpivottf == true ? minpivot : na : na, color=not lbwvv ? showweiswave ? ct == -1 ? color.green : ct == 1 ? color.red : na : maxpivottf == true ? color.green : color.red : maxpivottf == true ? color.green : color.red, offset=not lbwvv ? showweiswave ? -1 : 0 : -1, linewidth=3)


pivote = maxpivottf == true ? maxpivot : minpivottf == true ? minpivot : na
txtsize = ts == 'tiny' ? size.tiny : ts == 'small' ? size.small : ts == 'normal' ? size.normal : ts == 'large' ? size.large : ts == 'huge' ? size.huge : ts == 'auto' ? size.auto : size.auto
line weisline = na


need_manual_calc = false
shocalc = false
shocalc := shovol_ == 'Weis truncated volume' or shovol_ == 'Automatic X 10' or shovol_ == 'Automatic X 100' or shovol_ == 'Automatic X 0.1' or shovol_ == 'Automatic X 0.01' ? true : false

shotruvol = shovol_ == 'Show true wave volume' ? true : shocalc ? false : na
need_manual_calc := shotruvol ? false : arreg <= .1 and (mult > 0 or mult < 0) or ardreg < .1 and (mult > 0 or mult < 0) or arreg >= 10000 and (mult > 0 or mult < 0) or ardreg >= 10000 and (mult > 0 or mult < 0) or k or limboup > 40 or limbodown > 40 or toppe and multbottommelast != multi or bottomme and multtoppelast != multi ? true : false
//need_manual_calc:=shovol_=="Show full true wave volume"?false:need_manual_calc[1]

art = 0.0
ardt = 0.0
art := shovol_ == 'Show true wave volume' ? plotvolup : shocalc ? ar : na
ardt := shovol_ == 'Show true wave volume' ? plotvoldown : shocalc ? ard : na
var cpvh_ = string(na)

//shovol_== "Automatic X 10" or shovol_== "Automatic X 100"
cpvh_ := shovol_ != 'Show true wave volume' ? cpvh == 4 ? '#' : cpvh == 3 ? '#' : cpvh == 2 ? '#' : cpvh == 1 ? '#.#' : cpvh == 0 ? '#.###' : cpvh == -1 ? '#.####' : cpvh == -2 ? '#.#####' : na : na  //'#.#######' //

// limitcph_ to 3 digits? cpvh_:= shovol_!= "Show true wave volume"?(cpvh==3?'#':cpvh==2?'#':cpvh==1?'#.#':cpvh==0?'#.##':cpvh==-1?'#.###':na):na //'#.#######' //pace 



artbar = art / bars_
ardtbar = ardt / bars_
arbar = ar / bars_
ardbar = ard / bars_

artsigdig = artbar >= 100 ? '#' : artbar >= 10 ? '#.#' : artbar >= 1 ? '#.##' : artbar >= .1 ? '#.###' : artbar >= .01 ? '#.####' : na
ardtsigdig = ardtbar >= 100 ? '#' : ardtbar >= 10 ? '#.#' : ardtbar >= 1 ? '#.##' : ardtbar >= .1 ? '#.###' : ardtbar >= .01 ? '#.####' : na
// if vol over ord vol has too few digits employ this
arsigdig = arbar >= 100 ? '#' : arbar >= 10 ? '#.#' : arbar >= 1 ? '#.##' : arbar >= .1 ? '#.###' : arbar >= .01 ? '#.####' : na
ardsigdig = ardbar >= 100 ? '#' : ardbar >= 10 ? '#.#' : ardbar >= 1 ? '#.##' : ardbar >= .1 ? '#.###' : ardbar >= .01 ? '#.####' : na

upsigdig = plotvolup >= 100 ? '#' : plotvolup >= 10 ? '#.#' : plotvolup >= 1 ? '#.##' : plotvolup >= .1 ? '#.###' : plotvolup >= .01 ? '#.####' : plotvolup >= .001 ? '#.#####' : plotvolup >= .0001 ? '#.######' : plotvolup >= .00001 ? '#.#######' : plotvolup >= .000001 ? '#.########' : plotvolup >= .0000001 ? '#.########' : plotvolup >= .00000001 ? '#.##########' : plotvolup >= .000000001 ? '#.###########' : plotvolup >= .0000000001 ? '#.############' : plotvolup >= .00000000001 ? '#.#############' : plotvolup >= .000000000001 ? '#.##############' : na
downsigdig = plotvoldown >= 100 ? '#' : plotvoldown >= 10 ? '#.#' : plotvoldown >= 1 ? '#.##' : plotvoldown >= .1 ? '#.###' : plotvoldown >= .01 ? '#.####' : plotvoldown >= .001 ? '#.#####' : plotvoldown >= .0001 ? '#.######' : plotvoldown >= .00001 ? '#.#######' : plotvoldown >= .000001 ? '#.########' : plotvoldown >= .0000001 ? '#.########' : plotvoldown >= .00000001 ? '#.##########' : plotvoldown >= .000000001 ? '#.###########' : plotvoldown >= .0000000001 ? '#.############' : plotvoldown >= .00000000001 ? '#.#############' : plotvoldown >= .000000000001 ? '#.##############' : na
//210

nspace = string(na)
nspace := rf == 0 ? na : rf == 1 ? '\n' : rf == 2 ? '\n\n' : rf == 3 ? '\n\n\n' : rf == 4 ? '\n\n\n\n' : rf == 5 ? '\n\n\n\n\n' : rf == 6 ? '\n\n\n\n\n\n' : rf == 7 ? '\n\n\n\n\n\n\n' : rf == 8 ? '\n\n\n\n\n\n\n\n' : na

//replace in both below textcolor=need_manual_calc and shovol_!= "Show true wave volume"?colorr:(wavechoice== 2 or wavechoice==3)?colorc:colorb

dnlbl = KO ? bottomme and shovol ? label.new(bar_index[limbodown[1] + 1], bottomme - 1, style=label.style_none, yloc=yloc.belowbar, textcolor=need_manual_calc and shovol_ != 'Show true wave volume' ? colorr : wavechoice == 2 or wavechoice == 3 ? colorc : colorb, size=txtsize, text=shotruvol == false ? (wavechoice == 2 or wavechoice == 3) and calcmult_m and cpvh >= -1 and cpvh <= 3 ? nspace + str.tostring(plotvoldown, cpvh_) : ardreg < .1 and (mult > 0 or mult < 0) ? nspace + 'down' : ardreg > 9999 and (mult > 0 or mult < 0) ? nspace + 'up' : wavechoice != 6 and wavechoice != 7 ? ardreg > 999 and ardreg <= 9999 ? nspace + str.tostring(math.round(ard * 10)) : nspace + str.tostring(math.round(ard)) : wavechoice == 6 ? ardreg > 999 and ardreg <= 9999 ? nspace + str.tostring(math.round(ard * 10)) + '\n' + str.tostring(10 * bars_) : nspace + str.tostring(math.round(ard)) + '\n' + str.tostring(bars_) : ardreg > 999 and ardreg <= 9999 ? nspace + str.tostring(math.round(ard * 10)) + '\n' + str.tostring(math.round(10 * ard / bars_)) : nspace + str.tostring(math.round(ard)) + '\n' + str.tostring(math.round(ard / bars_)) : wavechoice != 6 and wavechoice != 7 ? ardreg > 999 and ardreg <= 9999 ? nspace + str.tostring(ardt * 10, downsigdig) : nspace + str.tostring(ardt, downsigdig) : wavechoice == 6 ? ardreg > 999 and ardreg <= 9999 ? nspace + str.tostring(ardt * 10, downsigdig) + '\n' + str.tostring(10 * bars_) : nspace + str.tostring(ardt, downsigdig) + '\n' + str.tostring(bars_) : ardreg > 999 and ardreg <= 9999 ? nspace + str.tostring(math.round(ardt)) + '\n' + str.tostring(ardtbar * 10, ardtsigdig) : nspace + str.tostring(ardt, downsigdig) + '\n' + str.tostring(ardtbar, ardtsigdig)) : na : na



uplbl = KO ? toppe and shovol ? label.new(bar_index[limboup[1] + 1], toppe + 1, style=label.style_none, yloc=yloc.abovebar, textcolor=need_manual_calc and shovol_ != 'Show true wave volume' ? colorr : wavechoice == 2 or wavechoice == 3 ? colorc : colorb, size=txtsize, text=shotruvol == false ? (wavechoice == 2 or wavechoice == 3) and calcmult_m and cpvh >= -1 and cpvh <= 3 ? str.tostring(plotvolup, cpvh_) + nspace : arreg < .1 and (mult > 0 or mult < 0) ? 'down' + nspace : arreg > 9999 and (mult > 0 or mult < 0) ? 'up' + nspace : wavechoice != 6 and wavechoice != 7 ? arreg > 999 and arreg <= 9999 ? str.tostring(math.round(ar * 10)) + nspace : str.tostring(math.round(ar)) + nspace : wavechoice == 6 ? arreg > 999 and arreg <= 9999 ? str.tostring(math.round(ar * 10)) + '\n' + str.tostring(10 * bars_) + nspace : str.tostring(math.round(ar)) + '\n' + str.tostring(bars_) + nspace : arreg > 999 and arreg <= 9999 ? str.tostring(math.round(ar * 10)) + '\n' + str.tostring(math.round(10 * ar / bars_)) + nspace : str.tostring(math.round(ar)) + '\n' + str.tostring(math.round(ar / bars_)) : wavechoice != 6 and wavechoice != 7 ? arreg > 999 and arreg <= 9999 ? str.tostring(art * 10, upsigdig) + nspace : str.tostring(art, upsigdig) + nspace : wavechoice == 6 ? arreg > 999 and arreg <= 9999 ? str.tostring(art * 10, upsigdig) + '\n' + str.tostring(10 * bars_) + nspace : str.tostring(art, upsigdig) + '\n' + str.tostring(bars_) + nspace : arreg > 999 and arreg <= 9999 ? str.tostring(art * 10, upsigdig) + '\n' + str.tostring(10 * artbar, artsigdig) + nspace : str.tostring(art, upsigdig) + '\n' + str.tostring(artbar, artsigdig) + nspace) : na : na  //size=size.auto,



// wavechoice==8?(ardreg)>999 and (ardreg)<=9999?tostring(round(ard*10))+ " " + tostring(10*deltaclose) + " " + tostring(10*bars_) +"\n" + tostring(round(ard*10/deltaclose)): tostring(round(ard))+ " " + tostring(deltaclose) + " " + tostring(bars_) +"\n" + tostring(round(ard/deltaclose)):             
label.set_textalign(dnlbl, text.align_right)
label.set_textalign(uplbl, text.align_right)
// 235 base pre weis/ ord          


weisline := line.new(bar_index - begin, pricehloc[begin], bar_index - end, pricehloc[end], width=linewidth, color=KO ? toppe ? colourupwave : colourdownwave : na)
// see #349 for the original notes

var tmult = 0
tmult := timeframe.ismonthly ? 2000 : timeframe.isweekly ? 1000 : timeframe.isdaily ? 300 : r < 10 ? 10 : r < 60 ? 60 : timeframe.isintraday ? 200 : not timeframe.isintraday ? 750 : 500
milliseconds_in_5days = 1000 * 60 * 60 * 24 * tmult  // millisecs * secs * min * hours * days
leftborder = timenow - time < milliseconds_in_5days  // true or na when false
rightborder = barstate.islast

max = float(na)
max := not leftborder ? na : na(max[1]) ? cpvmult : max[1]

if cpvmult > max  // we have a new high
    max := cpvmult  // change variable "max" to use current bar's high value
    max
multval = rightborder ? max[1] : na
var WeisLabel1 = label.new(x=na, y=na, xloc=xloc.bar_time, color=color.white, textcolor=color.black)
var WeisLabel2 = label.new(x=na, y=na, xloc=xloc.bar_time, color=color.white, textcolor=color.black)
// Update the label on the chart's right
var minran_ = string(na)
var minranp = string(na)
wavesizepercent = minrangee / close * 100
//cpvh>=0? cpvd + 1

minran_ := minrangee >= 100 ? '#' : minrangee >= 10 ? '#.#' : minrangee >= 1 ? '#.##' : minrangee >= .1 ? '#.###' : minrangee >= .01 ? '#.####' : minrangee >= .001 ? '#.#####' : minrangee >= .0001 ? '#.######' : minrangee >= .00001 ? '#.#######' : minrangee >= .000001 ? '#.########' : minrangee >= .0000001 ? '#.########' : minrangee >= .00000001 ? '#.##########' : minrangee >= .000000001 ? '#.###########' : minrangee >= .0000000001 ? '#.############' : minrangee >= .00000000001 ? '#.#############' : minrangee >= .000000000001 ? '#.##############' : na  //

minranp := wavesizepercent >= 100 ? '#' : wavesizepercent >= 10 ? '#.#' : wavesizepercent >= 1 ? '#.##' : wavesizepercent >= .1 ? '#.###' : wavesizepercent >= .01 ? '#.####' : wavesizepercent >= .001 ? '#.#####' : wavesizepercent >= .0001 ? '#.######' : wavesizepercent >= .00001 ? '#.#######' : wavesizepercent >= .000001 ? '#.########' : wavesizepercent >= .0000001 ? '#.########' : wavesizepercent >= .00000001 ? '#.##########' : wavesizepercent >= .000000001 ? '#.###########' : wavesizepercent >= .0000000001 ? '#.############' : wavesizepercent >= .00000000001 ? '#.#############' : wavesizepercent >= .000000000001 ? '#.##############' : na  //
var multfactor = 0.0
var expo = 0.0
// cpvhh=(wavechoice==2 or wavechoice ==3) and cpvh>=0? cpvh + 2:cpvh_new   // cpvh_new =(if alternate famult or if not then  cpvh)+ multsen +2  
// multfactor :=calcmult_m and  cpvh>=-1 and cpvh_new<=3?1:pow(10, cpvh_new - 2)
// cpvh:= (wavechoice==2 or wavechoice ==3) and cpvh>=-1 and cpvh<=3?2:cpvh_new  //multsen + 2   
cpvhh = (wavechoice == 2 or wavechoice == 3) and cpvh >= 0 ? cpvh + 2 : cpvh  // cpvh_new =(if alternate famult or if not then  cpvh)+ multsen +2
multfactor := calcmult and cpvh >= -1 and cpvh <= 3 ? 1 : math.pow(10, cpvh - multsen)
cpvh := (wavechoice == 2 or wavechoice == 3) and cpvh >= -1 and cpvh <= 3 ? 2 : cpvh  //multsen + 2 
timeframe_ = timeframe.isintraday ? '- minutes' : timeframe.isdaily ? '- hours' : '- bars in wave'
ran = 0.0
k2 = timeframe.isdaily ? 300 : 1  // if isintraday ? add time to compensate open - time_close
//Rangebartime:= timeframe.isintraday?time_close[1]/60000 - time/60000:timeframe.isdaily?time_close[1]/3600000 - time/3600000:timeframe.isdwm?1:na
run = timeframe.period == '1' ? 1 : timeframe.period == '2' ? 2 : timeframe.period == '3' ? 3 : timeframe.period == '4' ? 4 : timeframe.period == '5' ? 5 : timeframe.period == '6' ? 6 : timeframe.period == '7' ? 7 : timeframe.period == '8' ? 8 : timeframe.period == '9' ? 9 : timeframe.period == '10' ? 10 : timeframe.period == '11' ? 11 : timeframe.period == '12' ? 12 : timeframe.period == '13' ? 13 : timeframe.period == '14' ? 14 : timeframe.period == '15' ? 15 : timeframe.period == '16' ? 16 : timeframe.period == '17' ? 17 : timeframe.period == '18' ? 18 : timeframe.period == '19' ? 19 : timeframe.period == '20' ? 20 : timeframe.period == '21' ? 21 : timeframe.period == '22' ? 22 : timeframe.period == '23' ? 23 : timeframe.period == '24' ? 24 : timeframe.period == '25' ? 25 : timeframe.period == '26' ? 26 : timeframe.period == '27' ? 27 : timeframe.period == '28' ? 28 : timeframe.period == '29' ? 29 : timeframe.period == '30' ? 30 : timeframe.period == '31' ? 31 : timeframe.period == '32' ? 32 : timeframe.period == '33' ? 33 : timeframe.period == '34' ? 34 : timeframe.period == '35' ? 35 : timeframe.period == '36' ? 36 : timeframe.period == '37' ? 37 : timeframe.period == '38' ? 38 : timeframe.period == '39' ? 39 : timeframe.period == '40' ? 40 : timeframe.period == '45' ? 45 : timeframe.period == '60' ? 60 : timeframe.period == '65' ? 65 : timeframe.period == '120' ? 120 : timeframe.period == '130' ? 130 : timeframe.period == '180' ? 180 : timeframe.period == '195' ? 195 : timeframe.period == '225' ? 225 : timeframe.period == '240' ? 240 : timeframe.period == '260' ? 260 : timeframe.period == '480' ? 480 : 0

//ran:= timeframe.isintraday and run<=5 and abs(time_close[1]/60000 - time/60000)>5? abs(time_close[1]/60000 - time/60000) :timeframe.isdaily?time_close[1]/3600000 - time/3600000:timeframe.isdwm?1:na


if barstate.islast and shokey
    labelText1 = wavechoice == 1 ? 'Type:  Weis volume ' + '\nWeis wave size: ' + str.tostring(minrangee, minran_) + '\nWave size as a percent' + '\nof last close:  ' + str.tostring(wavesizepercent, minranp) + '%' + '\nManual Multiplier:  ' + str.tostring(cpvhh) + '\nPower Exponent:  ' + str.tostring(cpvh - multsen) + '\nMultiply by:  ' + str.tostring(multfactor) : wavechoice == 2 ? 'Type:  delta price or delta pip ' + '\nWeis wave size: ' + str.tostring(minrangee, minran_) + '\nWave size as a percent' + '\nof last close:  ' + str.tostring(wavesizepercent, minranp) + '%' + '\nManual Multiplier:  ' + str.tostring(cpvhh) + '\nPower Exponent:  ' + str.tostring(cpvh - multsen) + '\nMultiply by:  ' + str.tostring(multfactor) : wavechoice == 3 ? 'wave time ' + timeframe_ + '\nWeis wave size: ' + str.tostring(minrangee, minran_) + '\nWave size as a percent' + '\nof last close:  ' + str.tostring(wavesizepercent, minranp) + '%' + '\nManual Multiplier:  ' + str.tostring(cpvhh) + '\nPower Exponent:  ' + str.tostring(cpvh - multsen) + '\nMultiply by:  ' + str.tostring(multfactor) : wavechoice == 4 ? 'Weis cumulative volume/delta price ' + '\nWeis wave size: ' + str.tostring(minrangee, minran_) + '\nWave size as a percent' + '\nof last close:  ' + str.tostring(wavesizepercent, minranp) + '%' + '\nManual Multiplier:  ' + str.tostring(cpvhh) + '\nPower Exponent:  ' + str.tostring(cpvh - multsen) + '\nMultiply by::  ' + str.tostring(multfactor) : wavechoice == 5 ? 'Ord style volume ' + '\nWeis wave size: ' + str.tostring(minrangee, minran_) + '\nWave size as a percent' + '\nof last close:  ' + str.tostring(wavesizepercent, minranp) + '%' + '\nManual Multiplier:  ' + str.tostring(cpvhh) + '\nPower Exponent:  ' + str.tostring(cpvh - multsen) + '\nMultiply by:  ' + str.tostring(multfactor) : wavechoice == 6 ? 'Weis volume over # bars per wave ' + '\nWeis wave size: ' + str.tostring(minrangee, minran_) + '\nWave size as a percent' + '\nof last close:  ' + str.tostring(wavesizepercent, minranp) + '%' + '\nManual Multiplier:  ' + str.tostring(cpvhh) + '\nPower Exponent:  ' + str.tostring(cpvh - multsen) + '\nMultiply by:  ' + str.tostring(multfactor) : wavechoice == 7 ? 'Weis wave volume over Ord style volume ' + '\nWeis wave size: ' + str.tostring(minrangee, minran_) + '\nWave size as a percent' + '\nof last close:  ' + str.tostring(wavesizepercent, minranp) + '%' + '\nManual Multiplier:  ' + str.tostring(cpvhh) + '\nPower Exponent:  ' + str.tostring(cpvh - multsen) + '\nMultiply by:  ' + str.tostring(multfactor) : na


    label.set_y(id=WeisLabel1, y=close[1])
    label.set_x(id=WeisLabel1, x=time + 1800000 * k2)  //80)bar_index
    //label.set_x(id=WeisLabel1, x=bar_index )//80)bar_index
    label.set_text(id=WeisLabel1, text=labelText1)
    label.set_style(id=WeisLabel1, style=label.style_label_lower_left)
    label.set_textalign(WeisLabel1, text.align_left)



if barstate.islast and shokey
    labelText2 = sensitivity == 1 ? 'sensitivity 1' : sensitivity == 2 ? 'sensitivity 2' : sensitivity == 3 ? 'sensitivity 3' : calcmult_a == 6 ? 'alternative auto method' : calcmult_a == 7 ? 'alternative auto method x10' : calcmult ? 'Calculate Automatically' : calcmult == false ? 'Manually set multiplier' : na

    label.set_y(id=WeisLabel2, y=close[1])
    label.set_x(id=WeisLabel2, x=time + 1800000 * k2)  //80)bar_index
    label.set_text(id=WeisLabel2, text=labelText2)
    label.set_style(id=WeisLabel2, style=label.style_label_upper_left)
    label.set_textalign(WeisLabel2, text.align_left)
// plot(lo+minrangee,color=color.red)
// plot(hi-minrangee, color=color.lime)
// plot(close, color=color.black, linewidth=2, style=plot.style_circles)

