 # Plot Trend Lines for Rel Volume Std Dev?

#### Jim C

##### New member
First time posting here.
I've recently been studying the Relative Volume Standard Deviation Study for ToS for identifying supply and demand pools for support and resistance.

I've been searching for a script to plot a line on the close of the price candle where the indicator spikes above 2. If anyone could help me it would be greatly appreciated.

Here is the Td Ameritrade Script:
Code:
``````#
# TD Ameritrade IP Company, Inc. (c) 2014-2019
#

declare lower;
declare zerobase;

input length = 60;
input numDev = 2.0;
input allowNegativeValues = no;

def rawRelVol = (volume - Average(volume, length)) / StDev(volume, length);
plot RelVol = if allowNegativeValues then rawRelVol else Max(0, rawRelVol);
plot StDevLevel = numDev;

RelVol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
RelVol.SetLineWeight(3);
RelVol.DefineColor("Above", GetColor(0));
RelVol.DefineColor("Below", GetColor(2));
RelVol.AssignValueColor(if RelVol >= numDev then RelVol.Color("Above") else RelVol.Color("Below"));
StDevLevel.SetDefaultColor(GetColor(7));
StDevLevel.SetStyle(Curve.SHORT_DASH);``````

Jim

#### tomsk

##### Well-known member
VIP
@Jim C Per your request, here is a study that plots a horizontal line from the candle where the Relative Volume StDev indicator spikes above 2.0. Note that this is an upper study. In order to verify the results for yourself, also load the standard TOS Relative Volume StDev that displays on a lower chart. You'll see that the signals match up nicely.

Code:
``````# Relative Volume StDev Horizontal Line
# tomsk
# 1.8.2020

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 RelVolBar = if RelVol >= numDev then bar else RelVolBar;
def RelVolPrice = if bar == HighestAll(RelVolBar) then close else RelVolPrice;

plot HLine = if bar >= HighestAll(RelVolBar) then RelVolPrice else Double.NaN;
HLine.SetDefaultColor(Color.Cyan);
HLine.SetLineWeight(3);
# End Relative Volume StDev Horizontal Line``````

• Jonas99, J007RMC and markos

#### Jim C

##### New member
Thank you @tomsk for the quick response and the help!!

Is there a way to plot a line for every bar that is greater than the numDev?

Last edited by a moderator:

#### tomsk

##### Well-known member
VIP
I have designed it to only plot a horizontal line at the most recent instance of the spike above 2.0 standard deviation. Note that this uses a single variable. Now if you'd like to plot the last 20 occurrences, you'll have to use 20 different variables to maintain it which can get rather cumbersome to manage. On top of that the screen would look very cluttered. I've given you the method so all you need is to replicate that. Personally though I prefer a cleaner approach of just the last instance of that spike

#### Jim C

##### New member
I have designed it to only plot a horizontal line at the most recent instance of the spike above 2.0 standard deviation. Note that this uses a single variable. Now if you'd like to plot the last 20 occurrences, you'll have to use 20 different variables to maintain it which can get rather cumbersome to manage. On top of that the screen would look very cluttered. I've given you the method so all you need is to replicate that. Personally though I prefer a cleaner approach of just the last instance of that spike

Ok cool. I’m not great at writing these scripts but I can bird dog my way through them with some direction. If I wanted to create say 3 more variables what would that look like?

#### tomsk

##### Well-known member
VIP
@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;
def RelVolPrice2 = if bar == RelVolBar2 then close else RelVolPrice2;
def RelVolPrice3 = if bar == RelVolBar3 then close else RelVolPrice3;

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``````

#### Jim C

##### New member
@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;
def RelVolPrice2 = if bar == RelVolBar2 then close else RelVolPrice2;
def RelVolPrice3 = if bar == RelVolBar3 then close else RelVolPrice3;

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``````

Tom , thank you again. This helps me a lot. I see now you just number the extra variables. Thank you for taking the time to modify this.