Trying to extract the high and low price for the 2nd highest volume bar. Finding it for the highest is easy enough. I'm using a fold statement to iterate thought the range comparing the volume bars to each other. If there is a better way I'm all ears. Appreciate the help.

I've searched around the forum and found code that leverages highestall and barnumber to get the highest/lowest values. My ask is bit more complex i'm tying to find the 2nd and 3rd highest volume values x bar before and after the swing high of the volume bar.

I leveraged the swing high swing low code to get the bar number and volume of the first highest volume but now I'll looking from that point to look to find the 2nd highest swing high in that same range. I'm assuming the using the barNumber of the highest volume is the key but i'm not sure how you pass that to HighestAll but not sure how you make a range of it. Any help would be appreciated. Link to image below showing the highest and second highest volume with the price highs and lows for a 5 period lookback and lookforward swing high.

So i gave it a shot and was able to get the next highest value prior to the swing high but not the highest value after the swing high. Instead I get some weird number I think it has to do with the -offset calculation but can't seem to figure why it give a weird number for vol2 plot. Any help would be greatly appreciated.

Code:

```
declare lower;
input length = 100;
# highest volume bar used to help compare for 2nd highest volume bar
def vol = highest(volume, length);
#2nd highest volume bar need to extract the high and low values.
def vol2 = fold i = 0 to length with v do if volume[ i ] < vol and volume[ i ] > v then volume[ i ] else v;
# get the high and low values for the highest volume bar.
def highPriceVolhigh = GetValue(high, GetMaxValueOffset(volume, length), length);
def lowPriceVolhigh = GetValue(low, GetMaxValueOffset(volume, length), length);
Addcloud(highPriceVolhigh,lowPriceVolhigh,color.light_gray,color.light_gray,yes);
```

I've searched around the forum and found code that leverages highestall and barnumber to get the highest/lowest values. My ask is bit more complex i'm tying to find the 2nd and 3rd highest volume values x bar before and after the swing high of the volume bar.

I leveraged the swing high swing low code to get the bar number and volume of the first highest volume but now I'll looking from that point to look to find the 2nd highest swing high in that same range. I'm assuming the using the barNumber of the highest volume is the key but i'm not sure how you pass that to HighestAll but not sure how you make a range of it. Any help would be appreciated. Link to image below showing the highest and second highest volume with the price highs and lows for a 5 period lookback and lookforward swing high.

Code:

```
### taken for Robert Paynes swing high low code.
input length = 10;
def bn = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);
# i get this defines the swing high looking back 10 and foward 10 periods changed it for volume
def swingHigh = volume > Highest(volume[1], length - 1) and high == GetValue(Highest(volume, length), -offset);
# identify the very last swing high point
#
def highPointOneBarNumber = HighestAll(if swingHigh then bn else 0);
```

So i gave it a shot and was able to get the next highest value prior to the swing high but not the highest value after the swing high. Instead I get some weird number I think it has to do with the -offset calculation but can't seem to figure why it give a weird number for vol2 plot. Any help would be greatly appreciated.

Code:

```
input length = 10;
def bn = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);
def swingHigh = volume > Highest(volume[1], length - 1) and volume == GetValue(Highest(volume, length), -offset);
def volHighBefore = volume > Highest(volume[1], length - 1);
def volHighAfter = volume == GetValue(Highest(volume, length), -offset);
# identify the very last swing high point
def volHighBarNum = HighestAll(if swingHigh then bn else 0);
def volHighValue = if bn == volHighBarNum then high else volHighValue[1];
plot volHighPrior = if bn < volHighBarNum then Double.NaN else volHighValue;
volHigh.SetDefaultColor(Color.Yellow);
# identify the highest volume for the last X periods before the last swing high point
def volHighBeforeBarNum = HighestAll(if volHighBefore and bn < volHighBarNum then bn else 0);
def volHighBeforeValue = if bn == volHighBeforeBarNum then high else volHighBeforeValue[1];
plot vol1 = if bn < volHighBeforeBarNum then Double.NaN else volHighBeforeValue;
vol1.SetDefaultColor(Color.CYAN);
# identify the highest volume for the last X periods after the last swing high point
def volHighAfterBarNum = HighestAll(if volHighAfter and bn > volHighBarNum then bn else 0);
def volHighAfterValue = if bn == volHighAfterBarNum then high else volHighAfterValue[1];
plot vol2 = if bn < volHighAfterBarNum then Double.NaN else volHighAfterValue;
volHigh.SetDefaultColor(Color.RED);
```

Last edited by a moderator: