# indicator / paintbar -> standard deviation volume bars

## Recommended Posts

Is it possible to make an indicator or as a paintbar, like a standard deviation volume bar, like:
https://toslc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/R-S/RelativeVolumeStDev

With color coded volume bars according to how many standard deviations the volume of the bar is.
The look back period should be large, like 200 bars from the regular sessions only. The indicator is to be used on a 1-min chart.

In mathematical sense, the Relative Volume StDev is calculated as the ratio of the volume to its simple moving average (SMA), expressed in standard deviations. This value is plotted as a histogram as how many standard deviations the volume of that candle is; its bars are highlighted when the volume is unusually large.

Grey: less then 1 standard deviation from the mean
Blue: between 1 and 1,5 standard deviation from the mean
Light blue:  Between 1,5 and 2,5 standard deviation from the mean
Fuchsia, more then 2,5 standard deviations from the mean

C# generated script from chatgpt….. :

using System;
using System.Linq;
using System.Drawing;
using System.Collections.Generic;

public class RelativeVolumeStDev
{
private int lookback = 21; // Time Frame Base
private int signalLength = 8; // Signal Oscillation Length
private int lengthBand = 55; // Base Oscillation Length
private List<double> volumes = new List<double>();
private List<double> pervolList = new List<double>();

public void OnBarUpdate(double volume, double close, double open)
{

if (volumes.Count < lookback)
return;

// Calculate average volume
double avvol = volumes.Skip(Math.Max(0, volumes.Count - lookback)).Sum() / lookback;

// Calculate percentage volume deviation
double pervol = (volume - avvol) / avvol * 100;

// Calculate standard deviation of percentage volume deviation
double vstd = CalculateStandardDeviation(pervolList.Skip(Math.Max(0, pervolList.Count - lookback)).ToArray());

// Determine the color of the volume bar based on standard deviation
Color barColor = Color.Gray; // Default color for < 1 standard deviation
if (pervol >= 1 * vstd && pervol < 1.5 * vstd)
barColor = Color.Blue;
else if (pervol >= 1.5 * vstd && pervol < 2.5 * vstd)
barColor = Color.LightBlue;
else if (pervol >= 2.5 * vstd)
barColor = Color.Fuchsia;

// Plot the colored volume bar
PlotVolumeBar(volume, barColor);

// Optional: Plot the signal line and standard deviation bands
if (pervolList.Count >= signalLength)
{
double signal = pervolList.Skip(Math.Max(0, pervolList.Count - signalLength)).Average();
PlotSignalLine(signal, Color.Red);

if (pervolList.Count >= lengthBand)
{
double ma = pervolList.Skip(Math.Max(0, pervolList.Count - lengthBand)).Average();
double offs = 1.644 * CalculateStandardDeviation(pervolList.Skip(Math.Max(0, pervolList.Count - lengthBand)).ToArray());
double offs2 = 0.994 * CalculateStandardDeviation(pervolList.Skip(Math.Max(0, pervolList.Count - lengthBand)).ToArray());
double up = ma + offs;
double dn = ma - offs;
double up2 = ma + offs2;
double dn2 = ma - offs2;

PlotBand(up, dn, ma, up2, dn2);
}
}
}

private double CalculateStandardDeviation(double[] values)
{
double avg = values.Average();
return Math.Sqrt(values.Sum(v => Math.Pow(v - avg, 2)) / values.Length);
}

private void PlotVolumeBar(double volume, Color color)
{
// Implement your plotting logic here to draw the volume bar with the given color
Console.WriteLine(\$"Volume: {volume}, Color: {color}");
}

private void PlotSignalLine(double signal, Color color)
{
// Implement your plotting logic here to draw the signal line
Console.WriteLine(\$"Signal Line: {signal}, Color: {color}");
}

private void PlotBand(double up, double dn, double ma, double up2, double dn2)
{
// Implement your plotting logic here to draw the bands
Console.WriteLine(\$"Bands: up={up}, dn={dn}, ma={ma}, up2={up2}, dn2={dn2}");
}
}

Integration:
Plotting: You need to implement the PlotVolumeBar, PlotSignalLine, and PlotBand methods to integrate this code with your charting platform.
Data Feeds: Ensure that the OnBarUpdate method is called for each new bar with the correct volume, close, and open prices.

How's this:

##### Share on other sites

Yeah that could definitely work!

Maybe change the colouring scheme a bit.
Like anything below 1 st.dev. less of a signal color like light grey maybe.
Because we are saying that from a volume-perspective the associated candle is not significant.
And volumes between st.dev. 1.01 and 1.99 a signal colour, and 2.0 and above a signal colour?

And I was wondering what you have as a look back period to calculate the rel.vol.st.dev.
Is it all loaded candles on the chart? Or a big portion of it?

##### Share on other sites

The look back is the same as the SMA period. You can set it as an indicator parameter.

I guess I could make it a 3-color thing.

##### Share on other sites

Will be in next beta

Will be in next beta

##### Share on other sites

Posted (edited)

Thanks Mike!!
Looking forward to using it

Edited by dennishh2024

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.