Relative Bar Value Indicator for ThinkorSwim

a3ctrader

New member
Hello All,

This indicator displays the relative bar value for volume, range, volume per range, trades, trades per range, and volume per trades. DataProfile is used to determine MaxRelativeBarValue, NormalRangeHigh, and NormalRangeLow. Hopefully others can get some use and/or ideas out of it. Please feel free to provide me with any feedback and/or suggestions.

Updated: 01/24/21

Code:
declare lower;
declare hide_on_daily;

input ValueData = {
default "Volume",
        "Range",
        "Volume per Range",
        "Trades",
        "Trades per Range",
        "Volume per Trades"};
def Value;
def Range = if High - Low == 0 then TickSize() else High - Low;
switch (ValueData) {
case "Volume":
    Value = Volume;
case "Range":
    Value = Range;
case "Volume per Range":
    Value = Volume / Range;
case "Trades":
    Value = Tick_Count;
case "Trades per Range":
    Value = Tick_Count / Range;
case "Volume per Trades":
    Value = Volume / Tick_Count;}

input OpenTime = 1800;
def DSTSpringForwardOpenTime = OpenTime - 100;
input CloseBarTime = 1640;
#hint DSTSpringForwardDate: If viewing futures with extended-hours trading sessions, enter "20200309" for 2020 or "20210315" for 2021. Otherwise enter "99999999".
input DSTSpringForwardDate = 20200309;
rec BarCounter = if SecondsTillTime(OpenTime) == 0 and GetYYYYMMDD() != DSTSpringForwardDate then 1 else if SecondsTillTime(DSTSpringForwardOpenTime) == 0 and GetYYYYMMDD() == DSTSpringForwardDate then 1 else BarCounter[1] + 1;

def Bar1  = if BarCounter == 1  then Value else 0;
def Bar2  = if BarCounter == 2  then Value else 0;
def Bar3  = if BarCounter == 3  then Value else 0;
def Bar4  = if BarCounter == 4  then Value else 0;
def Bar5  = if BarCounter == 5  then Value else 0;
def Bar6  = if BarCounter == 6  then Value else 0;
def Bar7  = if BarCounter == 7  then Value else 0;
def Bar8  = if BarCounter == 8  then Value else 0;
def Bar9  = if BarCounter == 9  then Value else 0;
def Bar10 = if BarCounter == 10 then Value else 0;
def Bar11 = if BarCounter == 11 then Value else 0;
def Bar12 = if BarCounter == 12 then Value else 0;
def Bar13 = if BarCounter == 13 then Value else 0;
def Bar14 = if BarCounter == 14 then Value else 0;
def Bar15 = if BarCounter == 15 then Value else 0;
def Bar16 = if BarCounter == 16 then Value else 0;
def Bar17 = if BarCounter == 17 then Value else 0;
def Bar18 = if BarCounter == 18 then Value else 0;
def Bar19 = if BarCounter == 19 then Value else 0;
def Bar20 = if BarCounter == 20 then Value else 0;
def Bar21 = if BarCounter == 21 then Value else 0;
def Bar22 = if BarCounter == 22 then Value else 0;
def Bar23 = if BarCounter == 23 then Value else 0;
def Bar24 = if BarCounter == 24 then Value else 0;
def Bar25 = if BarCounter == 25 then Value else 0;
def Bar26 = if BarCounter == 26 then Value else 0;
def Bar27 = if BarCounter == 27 then Value else 0;
def Bar28 = if BarCounter == 28 then Value else 0;
def Bar29 = if BarCounter == 29 then Value else 0;
def Bar30 = if BarCounter == 30 then Value else 0;
def Bar31 = if BarCounter == 31 then Value else 0;
def Bar32 = if BarCounter == 32 then Value else 0;
def Bar33 = if BarCounter == 33 then Value else 0;
def Bar34 = if BarCounter == 34 then Value else 0;
def Bar35 = if BarCounter == 35 then Value else 0;
def Bar36 = if BarCounter == 36 then Value else 0;
def Bar37 = if BarCounter == 37 then Value else 0;
def Bar38 = if BarCounter == 38 then Value else 0;
def Bar39 = if BarCounter == 39 then Value else 0;
def Bar40 = if BarCounter == 40 then Value else 0;
def Bar41 = if BarCounter == 41 then Value else 0;
def Bar42 = if BarCounter == 42 then Value else 0;
def Bar43 = if BarCounter == 43 then Value else 0;
def Bar44 = if BarCounter == 44 then Value else 0;
def Bar45 = if BarCounter == 45 then Value else 0;
def Bar46 = if BarCounter == 46 then Value else 0;
def Bar47 = if BarCounter == 47 then Value else 0;
def Bar48 = if BarCounter == 48 then Value else 0;
def Bar49 = if BarCounter == 49 then Value else 0;
def Bar50 = if BarCounter == 50 then Value else 0;
def Bar51 = if BarCounter == 51 then Value else 0;
def Bar52 = if BarCounter == 52 then Value else 0;
def Bar53 = if BarCounter == 53 then Value else 0;
def Bar54 = if BarCounter == 54 then Value else 0;
def Bar55 = if BarCounter == 55 then Value else 0;
def Bar56 = if BarCounter == 56 then Value else 0;
def Bar57 = if BarCounter == 57 then Value else 0;
def Bar58 = if BarCounter == 58 then Value else 0;
def Bar59 = if BarCounter == 59 then Value else 0;
def Bar60 = if BarCounter == 60 then Value else 0;
def Bar61 = if BarCounter == 61 then Value else 0;
def Bar62 = if BarCounter == 62 then Value else 0;
def Bar63 = if BarCounter == 63 then Value else 0;
def Bar64 = if BarCounter == 64 then Value else 0;
def Bar65 = if BarCounter == 65 then Value else 0;
def Bar66 = if BarCounter == 66 then Value else 0;
def Bar67 = if BarCounter == 67 then Value else 0;
def Bar68 = if BarCounter == 68 then Value else 0;
def Bar69 = if BarCounter == 69 then Value else 0;

rec DayCounter = if SecondsTillTime(OpenTime) == 0 then 1 else 0;

def AvgBar1  = TotalSum(Bar1)  / TotalSum(DayCounter);
def AvgBar2  = TotalSum(Bar2)  / TotalSum(DayCounter);
def AvgBar3  = TotalSum(Bar3)  / TotalSum(DayCounter);
def AvgBar4  = TotalSum(Bar4)  / TotalSum(DayCounter);
def AvgBar5  = TotalSum(Bar5)  / TotalSum(DayCounter);
def AvgBar6  = TotalSum(Bar6)  / TotalSum(DayCounter);
def AvgBar7  = TotalSum(Bar7)  / TotalSum(DayCounter);
def AvgBar8  = TotalSum(Bar8)  / TotalSum(DayCounter);
def AvgBar9  = TotalSum(Bar9)  / TotalSum(DayCounter);
def AvgBar10 = TotalSum(Bar10) / TotalSum(DayCounter);
def AvgBar11 = TotalSum(Bar11) / TotalSum(DayCounter);
def AvgBar12 = TotalSum(Bar12) / TotalSum(DayCounter);
def AvgBar13 = TotalSum(Bar13) / TotalSum(DayCounter);
def AvgBar14 = TotalSum(Bar14) / TotalSum(DayCounter);
def AvgBar15 = TotalSum(Bar15) / TotalSum(DayCounter);
def AvgBar16 = TotalSum(Bar16) / TotalSum(DayCounter);
def AvgBar17 = TotalSum(Bar17) / TotalSum(DayCounter);
def AvgBar18 = TotalSum(Bar18) / TotalSum(DayCounter);
def AvgBar19 = TotalSum(Bar19) / TotalSum(DayCounter);
def AvgBar20 = TotalSum(Bar20) / TotalSum(DayCounter);
def AvgBar21 = TotalSum(Bar21) / TotalSum(DayCounter);
def AvgBar22 = TotalSum(Bar22) / TotalSum(DayCounter);
def AvgBar23 = TotalSum(Bar23) / TotalSum(DayCounter);
def AvgBar24 = TotalSum(Bar24) / TotalSum(DayCounter);
def AvgBar25 = TotalSum(Bar25) / TotalSum(DayCounter);
def AvgBar26 = TotalSum(Bar26) / TotalSum(DayCounter);
def AvgBar27 = TotalSum(Bar27) / TotalSum(DayCounter);
def AvgBar28 = TotalSum(Bar28) / TotalSum(DayCounter);
def AvgBar29 = TotalSum(Bar29) / TotalSum(DayCounter);
def AvgBar30 = TotalSum(Bar30) / TotalSum(DayCounter);
def AvgBar31 = TotalSum(Bar31) / TotalSum(DayCounter);
def AvgBar32 = TotalSum(Bar32) / TotalSum(DayCounter);
def AvgBar33 = TotalSum(Bar33) / TotalSum(DayCounter);
def AvgBar34 = TotalSum(Bar34) / TotalSum(DayCounter);
def AvgBar35 = TotalSum(Bar35) / TotalSum(DayCounter);
def AvgBar36 = TotalSum(Bar36) / TotalSum(DayCounter);
def AvgBar37 = TotalSum(Bar37) / TotalSum(DayCounter);
def AvgBar38 = TotalSum(Bar38) / TotalSum(DayCounter);
def AvgBar39 = TotalSum(Bar39) / TotalSum(DayCounter);
def AvgBar40 = TotalSum(Bar40) / TotalSum(DayCounter);
def AvgBar41 = TotalSum(Bar41) / TotalSum(DayCounter);
def AvgBar42 = TotalSum(Bar42) / TotalSum(DayCounter);
def AvgBar43 = TotalSum(Bar43) / TotalSum(DayCounter);
def AvgBar44 = TotalSum(Bar44) / TotalSum(DayCounter);
def AvgBar45 = TotalSum(Bar45) / TotalSum(DayCounter);
def AvgBar46 = TotalSum(Bar46) / TotalSum(DayCounter);
def AvgBar47 = TotalSum(Bar47) / TotalSum(DayCounter);
def AvgBar48 = TotalSum(Bar48) / TotalSum(DayCounter);
def AvgBar49 = TotalSum(Bar49) / TotalSum(DayCounter);
def AvgBar50 = TotalSum(Bar50) / TotalSum(DayCounter);
def AvgBar51 = TotalSum(Bar51) / TotalSum(DayCounter);
def AvgBar52 = TotalSum(Bar52) / TotalSum(DayCounter);
def AvgBar53 = TotalSum(Bar53) / TotalSum(DayCounter);
def AvgBar54 = TotalSum(Bar54) / TotalSum(DayCounter);
def AvgBar55 = TotalSum(Bar55) / TotalSum(DayCounter);
def AvgBar56 = TotalSum(Bar56) / TotalSum(DayCounter);
def AvgBar57 = TotalSum(Bar57) / TotalSum(DayCounter);
def AvgBar58 = TotalSum(Bar58) / TotalSum(DayCounter);
def AvgBar59 = TotalSum(Bar59) / TotalSum(DayCounter);
def AvgBar60 = TotalSum(Bar60) / TotalSum(DayCounter);
def AvgBar61 = TotalSum(Bar61) / TotalSum(DayCounter);
def AvgBar62 = TotalSum(Bar62) / TotalSum(DayCounter);
def AvgBar63 = TotalSum(Bar63) / TotalSum(DayCounter);
def AvgBar64 = TotalSum(Bar64) / TotalSum(DayCounter);
def AvgBar65 = TotalSum(Bar65) / TotalSum(DayCounter);
def AvgBar66 = TotalSum(Bar66) / TotalSum(DayCounter);
def AvgBar67 = TotalSum(Bar67) / TotalSum(DayCounter);
def AvgBar68 = TotalSum(Bar68) / TotalSum(DayCounter);
def AvgBar69 = TotalSum(Bar69) / TotalSum(DayCounter);

def AvgValue =
     if BarCounter == 1  then AvgBar1
else if BarCounter == 2  then AvgBar2
else if BarCounter == 3  then AvgBar3
else if BarCounter == 4  then AvgBar4
else if BarCounter == 5  then AvgBar5
else if BarCounter == 6  then AvgBar6
else if BarCounter == 7  then AvgBar7
else if BarCounter == 8  then AvgBar8
else if BarCounter == 9  then AvgBar9
else if BarCounter == 10 then AvgBar10
else if BarCounter == 11 then AvgBar11
else if BarCounter == 12 then AvgBar12
else if BarCounter == 13 then AvgBar13
else if BarCounter == 14 then AvgBar14
else if BarCounter == 15 then AvgBar15
else if BarCounter == 16 then AvgBar16
else if BarCounter == 17 then AvgBar17
else if BarCounter == 18 then AvgBar18
else if BarCounter == 19 then AvgBar19
else if BarCounter == 20 then AvgBar20
else if BarCounter == 21 then AvgBar21
else if BarCounter == 22 then AvgBar22
else if BarCounter == 23 then AvgBar23
else if BarCounter == 24 then AvgBar24
else if BarCounter == 25 then AvgBar25
else if BarCounter == 26 then AvgBar26
else if BarCounter == 27 then AvgBar27
else if BarCounter == 28 then AvgBar28
else if BarCounter == 29 then AvgBar29
else if BarCounter == 30 then AvgBar30
else if BarCounter == 31 then AvgBar31
else if BarCounter == 32 then AvgBar32
else if BarCounter == 33 then AvgBar33
else if BarCounter == 34 then AvgBar34
else if BarCounter == 35 then AvgBar35
else if BarCounter == 36 then AvgBar36
else if BarCounter == 37 then AvgBar37
else if BarCounter == 38 then AvgBar38
else if BarCounter == 39 then AvgBar39
else if BarCounter == 40 then AvgBar40
else if BarCounter == 41 then AvgBar41
else if BarCounter == 42 then AvgBar42
else if BarCounter == 43 then AvgBar43
else if BarCounter == 44 then AvgBar44
else if BarCounter == 45 then AvgBar45
else if BarCounter == 46 then AvgBar46
else if BarCounter == 47 then AvgBar47
else if BarCounter == 48 then AvgBar48
else if BarCounter == 49 then AvgBar49
else if BarCounter == 50 then AvgBar50
else if BarCounter == 51 then AvgBar51
else if BarCounter == 52 then AvgBar52
else if BarCounter == 53 then AvgBar53
else if BarCounter == 54 then AvgBar54
else if BarCounter == 55 then AvgBar55
else if BarCounter == 56 then AvgBar56
else if BarCounter == 57 then AvgBar57
else if BarCounter == 58 then AvgBar58
else if BarCounter == 59 then AvgBar59
else if BarCounter == 60 then AvgBar60
else if BarCounter == 61 then AvgBar61
else if BarCounter == 62 then AvgBar62
else if BarCounter == 63 then AvgBar63
else if BarCounter == 64 then AvgBar64
else if BarCounter == 65 then AvgBar65
else if BarCounter == 66 then AvgBar66
else if BarCounter == 67 then AvgBar67
else if BarCounter == 68 then AvgBar68
else if BarCounter == 69 then AvgBar69
else Double.NaN;

def Data = (Value / AvgValue) * 100;
def Condition = BarNumber() == 1;

def NormalValueAreaPercent = 68;
profile DataProfileVA = DataProfile(data = Data, pricePerRow = PricePerRow.TICKSIZE, startNewProfile = Condition, onExpansion = Yes, "value area percent" = NormalValueAreaPercent);
def VAH = DataProfileVA.GetHighestValueArea();
def VAL = DataProfileVA.GetLowestValueArea();

def MaxValueAreaPercent = 95;
profile DataProfileMax = DataProfile(data = Data, pricePerRow = PricePerRow.TICKSIZE, startNewProfile = Condition, onExpansion = Yes, "value area percent" = MaxValueAreaPercent);
def MaxVAH = DataProfileMax.GetHighestValueArea();

def MaxRelativeBarValue = MaxVAH;
def NormalRangeHigh = VAH;
def NormalRangeLow = VAL;

plot RelativeBarValue = if Round((Value / AvgValue) * 100, 0) > MaxRelativeBarValue then MaxRelativeBarValue else Round((Value / AvgValue) * 100, 0);
RelativeBarValue.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
RelativeBarValue.SetLineWeight(3);
RelativeBarValue.HideBubble();
RelativeBarValue.AssignValueColor(
     if close > open and RelativeBarValue <   NormalRangeLow  then CreateColor(102, 102, 255)
else if close > open and RelativeBarValue <=  NormalRangeHigh then CreateColor(0,   0,   255)
else if close > open                                          then CreateColor(0,   0,   153)
else if close < open and RelativeBarValue <   NormalRangeLow  then CreateColor(255, 102, 102)
else if close < open and RelativeBarValue <=  NormalRangeHigh then CreateColor(255, 0,   0)
else if close < open                                          then CreateColor(153, 0,   0)
else if                  RelativeBarValue <   NormalRangeLow  then CreateColor(128, 128, 128)
else if                  RelativeBarValue <=  NormalRangeHigh then CreateColor(64,  64,  64)
else                                                               CreateColor(0,   0,   0));

AssignPriceColor(
     if close > open and RelativeBarValue <   NormalRangeLow  then CreateColor(102, 102, 255)
else if close > open and RelativeBarValue <=  NormalRangeHigh then CreateColor(0,   0,   255)
else if close > open                                          then CreateColor(0,   0,   153)
else if close < open and RelativeBarValue <   NormalRangeLow  then CreateColor(255, 102, 102)
else if close < open and RelativeBarValue <=  NormalRangeHigh then CreateColor(255, 0,   0)
else if close < open                                          then CreateColor(153, 0,   0)
else if                  RelativeBarValue <   NormalRangeLow  then CreateColor(128, 128, 128)
else if                  RelativeBarValue <=  NormalRangeHigh then CreateColor(64,  64,  64)
else                                                               CreateColor(0,   0,   0));

def CumulativeRelativeBarValue = if SecondsTillTime(OpenTime) == 0 and GetYYYYMMDD() != DSTSpringForwardDate then (Value / AvgValue) * 100 else if SecondsTillTime(DSTSpringForwardOpenTime) == 0 and GetYYYYMMDD() == DSTSpringForwardDate then (Value / AvgValue) * 100 else CumulativeRelativeBarValue[1] + ((Value / AvgValue) * 100);
def AvgCumulativeRelativeBarValueRawData = Round(CumulativeRelativeBarValue / BarCounter, 0);
input ShowAvgCumRelBarValue = Yes;
plot AvgCumulativeRelativeBarValue = if AvgCumulativeRelativeBarValueRawData > MaxRelativeBarValue then MaxRelativeBarValue else AvgCumulativeRelativeBarValueRawData;
AvgCumulativeRelativeBarValue.SetLineWeight(2);
AvgCumulativeRelativeBarValue.HideBubble();
AvgCumulativeRelativeBarValue.SetDefaultColor(Color.DARK_GRAY);
AvgCumulativeRelativeBarValue.SetHiding(!ShowAvgCumRelBarValue);
AddChartBubble(SecondsTillTime(CloseBarTime) == 0 and ShowAvgCumRelBarValue, AvgCumulativeRelativeBarValue, AvgCumulativeRelativeBarValueRawData, if AvgCumulativeRelativeBarValueRawData > MaxRelativeBarValue then Color.DARK_ORANGE else Color.LIGHT_GRAY, if AvgCumulativeRelativeBarValueRawData > NormalRangeHigh then No else Yes);

plot ExtremeDot = if RelativeBarValue == MaxRelativeBarValue then MaxRelativeBarValue else Double.NaN;
ExtremeDot.SetPaintingStrategy(PaintingStrategy.SQUARES);
ExtremeDot.SetLineWeight(5);
ExtremeDot.HideBubble();
ExtremeDot.SetDefaultColor(CreateColor(255, 255, 0));
ExtremeDot.HideTitle();

plot AverageLine = 100;
AverageLine.SetLineWeight(5);
AverageLine.HideBubble();
AverageLine.SetDefaultColor(Color.GRAY);
AverageLine.HideTitle();

AddCloud(NormalRangeHigh, NormalRangeLow, Color.DARK_GRAY);

Iss5Euq.png
 
Last edited:

a3ctrader

New member
What does the red and black bar signify?

The color of each relative bar is based on its close vs open and if it's below, within, or above the normal range. See code below.

Code:
RelativeBarValue.AssignValueColor(
     if close > open and RelativeBarValue <   NormalRangeLow  then CreateColor(102, 102, 255)
else if close > open and RelativeBarValue <=  NormalRangeHigh then CreateColor(0,   0,   255)
else if close > open                                          then CreateColor(0,   0,   153)
else if close < open and RelativeBarValue <   NormalRangeLow  then CreateColor(255, 102, 102)
else if close < open and RelativeBarValue <=  NormalRangeHigh then CreateColor(255, 0,   0)
else if close < open                                          then CreateColor(153, 0,   0)
else if                  RelativeBarValue <   NormalRangeLow  then CreateColor(128, 128, 128)
else if                  RelativeBarValue <=  NormalRangeHigh then CreateColor(64,  64,  64)
else                                                               CreateColor(0,   0,   0));
 

a3ctrader

New member
Does anyone know how to correct this indicator (Post #1) for fall Daylight Saving Time (DST) for 23-hour index futures?
 
Last edited:

nduckste

New member
I'm new to ThinkScript. What defines "normal range" in your indicator?

To be transparent, I'm looking for a script that plots the average volume for the current time bar based on the average for that same time bar over the last x days. So if the current bar is 09:30, I want to plot the average for the 09:30 bar over the last x days. Is that what this is doing?

I've done this in NinjaScript simply by looping through the last x days for a particular bar, summing those values, and then dividing by x days. That is very procedural (NinjaScript is C# based) but I'm guessing there is a more elegant way in ThinkScript.

Thanks,

Nick
 

a3ctrader

New member
I'm new to ThinkScript. What defines "normal range" in your indicator?

To be transparent, I'm looking for a script that plots the average volume for the current time bar based on the average for that same time bar over the last x days. So if the current bar is 09:30, I want to plot the average for the 09:30 bar over the last x days. Is that what this is doing?

I've done this in NinjaScript simply by looping through the last x days for a particular bar, summing those values, and then dividing by x days. That is very procedural (NinjaScript is C# based) but I'm guessing there is a more elegant way in ThinkScript.

Thanks,

Nick
Nick,

This indicator uses DataProfile to automatically set the upper and lower "normal range" at one standard deviation or 68%. The chart lookback period will determine the amount of data that's used in the calculation. I use this indicator for overnight index futures with 20 minutes being the lowest timeframe. A lower timeframe can be used on day only session futures/stocks. The relative bar value is (Value / AvgValue) * 100.
 

Similar threads

Top