@Jim C Per your request, I have updated the code and added a couple more variables so that it now plots the last 3 instances of spikes above 2.0 standard deviation. To differentiate them I have colored the plots differently.
CYAN = Most recent
YELLOW = 2nd most recent
WHITE = 3rd most recent
You can add more variables if you like, but pretty soon it will clutter your screen. But if that's what you really want, here's version 1.1 of the Relative Volume StDev Horizontal Line study. I have tested this and this is good to out of the box
Code:
# Relative Volume StDev Horizontal Line
# tomsk
# 1.10.2020
# V1.0 - 01.08.2020 - tomsk - Initial release of Relative Volume StDev Horizontal Line
# V1.1 - 01.10.2020 - tomsk - Added 2nd and 3rd most recent instances of spike above 2.0 St Dev.
input length = 60;
input numDev = 2.0;
input allowNegativeValues = no;
def bar = barNumber();
def rawRelVol = (volume - Average(volume, length)) / StDev(volume, length);
def RelVol = if allowNegativeValues then rawRelVol else Max(0, rawRelVol);
def RelVolBar1 = HighestAll(if RelVol >= numDev then bar else 0);
def RelVolBar2 = HighestAll(if RelVol >= numDev and bar < RelVolBar1 then bar else 0);
def RelVolBar3 = HighestAll(if RelVol >= numDev and bar < RelVolBar2 then bar else 0);
def RelVolPrice1 = if bar == RelVolBar1 then close else RelVolPrice1[1];
def RelVolPrice2 = if bar == RelVolBar2 then close else RelVolPrice2[1];
def RelVolPrice3 = if bar == RelVolBar3 then close else RelVolPrice3[1];
plot HLine1 = if bar >= RelVolBar1 then RelVolPrice1 else Double.NaN;
HLine1.SetDefaultColor(Color.Cyan);
HLine1.SetLineWeight(3);
plot HLine2 = if bar >= RelVolBar2 then RelVolPrice2 else Double.NaN;
HLine2.SetDefaultColor(Color.Yellow);
HLine2.SetLineWeight(3);
plot HLine3 = if bar >= RelVolBar3 then RelVolPrice3 else Double.NaN;
HLine3.SetDefaultColor(Color.White);
HLine3.SetLineWeight(3);
# End Relative Volume StDev Horizontal Line