Coding Help for Welkin's Advanced Volume Indicator.

fungus12

Member
Hi I have a question on how to code something in Welkin's AVI. Here is the link to the thread for the script https://usethinkscript.com/threads/advanced-volume-indicator-for-thinkorswim.1665/

My question is about the buy/sell str for the second aggregation. First off let me just present two images so you know what I'm talking about.

Here is the indicator with the second aggregation turned off:

D6Z3iet.png


And here is the indicator with the second aggregation turned on:

tQjiaPG.png


My question is, is it possible to have some kind of percentage present in the second image that indicates the percentage of buy vs sell shown in the clouds? For example in that image from 6:30 - 7:00 it would show something like "95% buy/5% sell", then at 7:01 it would show "60% buy/40% sell". Something like that. I would prefer this also be favorable to backtesting. In other words, have the percentages present not only at the most current data but historical data as well.

Also along similar lines, I also would like to change the watchlist column in Welkin's post to show the buy/str for an aggregate (15m, for example) instead of just current bars. Here is the code in the thread for the watchlist:
Code:
#Buy/Sell Strength Column
def o = open;
def h = high;
def l = low;
def c = close;
def PR = h - l;
def BuyStr = ((c - l) / PR) * 100;

AddLabel(1,Round(BuyStr,1)+" | " + Round(100-BuyStr,1)+"%", Color.BLACK);

AssignBackgroundColor(if BuyStr >= 70 then Color.Green else if BuyStr > 50 then Color.DARK_GREEN else if BuyStr <= 30 then Color.RED else if BuyStr < 50 then Color.DARK_RED else Color.GRAY);

I wish Welkin was around to answer these questions but he's been inactive for several months now. If anyone knows how to code these things, please let me know. Thanks in advance.
 

fungus12

Member
Labels only work off of the most recent data? You can't really backtest with labels, unless you use ondemand I guess but that's not really what I'm looking for. Here's a picture of kinda what I'm looking for, apologize for the poor mspaint work.

XBYvyQz.png
 
Last edited by a moderator:

SleepyZ

Well-known member
Hmm..would it be possible to add chart bubbles for each aggregate interval on the lower study itself (not on the upper chart)? Because every fifteen minutes the lower study updates, it's not as if it's constantly updating. So every fifteen minutes it would update with a new bubble.

If that's not possible, I have another question. Firstly, how can I remove that upper label that says "(no,yes,yes,5,20,no,no)"? That obscures the cursor data. And another follow-up on that, so when you hover your cursor over an area in that lower study it gives you the buy volume and sell volume values as you can see in that top-right 1,661 number. This indicator was designed primarily to have single-bar values then the aggregate was added later on. I'm thinking that maybe If you isolate just the aggregate, if you hover your cursor over an area it should give you the volume values of the aggregate rather than the single bars themselves. That is a fine substitution for me, because all I'm looking for is buying volume to be above 50%, and you can eyeball that easily if you just have two numbers.

This plots a buy/sell bubbles at the input bar at 30 min agg in the example at the input bubblemover at 15, thereby centering it on the volsigma2 line. Adjust the inputs to your liking. The code for the bubbles was added to the end of the script.

Capture.jpg

Ruby:
#Advanced Volume Study
#[email protected]
#v5.22.2020
declare on_volume;
input ShowVolumeAsCandlesticks = no;
input ShowBuySellStrengthOnVolumeBars = no;
input ShowBuySellStrength2ndAgg = no;
input AvgDayVolLength = 5;
input AvgVolLength = 20;
input ShowDayVolLabel = yes;
input ShowBarVolLabel = yes;
input ShowEthTotalVol = no;
input ShowBuySellStrength = yes;
input BuySellStrAgg2 = AggregationPeriod.THIRTY_MIN;
def BuySellStrAggregation2 = if GetAggregationPeriod() < BuySellStrAgg2 then BuySellStrAgg2 else GetAggregationPeriod();
AddLabel(if GetAggregationPeriod() < BuySellStrAgg2 then 0 else 1, "Adjust BuySellStrAgg2 in Study Settings", Color.YELLOW);
input VolAverageType = AverageType.SIMPLE;
#if ShowBuySellStrengthOnVolumeBars is toggled on then the following volume bar paint options will not show, only the VolSignal Triangle set at the top of the bars will be painting according to volume average levels.
input PaintAboveAvgVolBars = yes;
input PaintAccordingToRelPrevVol = yes;
input RelativetoPrevVolTolerance = 1.25; #if volume is 1.25x greater than previous bar it will paint even if it is still below the average/sigma2/sigma3
input PaintBelowAvgVol = yes;
input PaintPriceAsVol = no;
input ShowVerticalTickLines = yes;
def ShowVertLines = if ShowVerticalTickLines and GetAggregationPeriod() < AggregationPeriod.DAY then 1 else 0;
input TickLevel = 1000;
input ShowTickLabel = yes;


def NA = Double.NaN;
def PriceRange = high - low;
def TopShadowRange = if open >= close then high - open else high - close;
def BottomShadowRange = if open <= close then open - low else close - low;
def BodyRange = PriceRange - (TopShadowRange + BottomShadowRange);
def VolumeTopShadowValue = (1 - (TopShadowRange / PriceRange)) * volume;
def VolumeBottomShadowValue = ((BottomShadowRange / PriceRange) * volume);
def BodyRangeVolValue = ((BodyRange + BottomShadowRange) / PriceRange) * volume;
#def DayVolAgg = if GetAggregationPeriod() < AggregationPeriod.DAY then AggregationPeriod.DAY else if GetAggregationPeriod() >= AggregationPeriod.DAY then AggregationPeriod.WEEK else GetAggregationPeriod();
def DayVolAgg = if GetAggregationPeriod() < AggregationPeriod.DAY then AggregationPeriod.DAY else GetAggregationPeriod();
def DayVol = volume("period" = DayVolAgg);
def AvgDayVol = Average(DayVol, AvgDayVolLength);
def Start = 0930;
def End = 1600;
def conf = SecondsFromTime(Start) >= 0 and SecondsFromTime(End) <= 0;

plot VolColor = NA;
VolColor.DefineColor("Bullish", Color.GREEN);
VolColor.DefineColor("Bearish", Color.RED);
VolColor.DefineColor("VolAvg", CreateColor(0, 100, 200));
VolColor.DefineColor("VolSigma2", Color.DARK_ORANGE);
VolColor.DefineColor("VolSigma3", Color.MAGENTA);
VolColor.DefineColor("Relative to Prev", Color.YELLOW);
VolColor.DefineColor("Below Average", Color.GRAY);
VolColor.DefineColor("ETH TVOL", Color.GRAY);
VolColor.DefineColor("TICK Vert", Color.GRAY);

#Current Candle Buy and Sell Strength
def BuyStr = ((close - low) / PriceRange) * 100;
def SellStr = ((high - close) / PriceRange) * 100;

def BuyStr2 = ((close("period" = BuySellStrAggregation2) - low("period" = BuySellStrAggregation2)) / (high("period" = BuySellStrAggregation2) - low("period" = BuySellStrAggregation2))) * 100;
def SellStr2 = ((high("period" = BuySellStrAggregation2) - close("period" = BuySellStrAggregation2)) / (high("period" = BuySellStrAggregation2) - low("period" = BuySellStrAggregation2))) * 100;

plot BuyVol = if ShowBuySellStrengthOnVolumeBars then (BuyStr / 100) * volume else NA;
def SellVol = (SellStr / 100) * volume;
def BuyVol2 = (BuyStr2 / 100) * volume("period" = BuySellStrAggregation2);
def SellVol2 = (SellStr2 / 100) * volume("period" = BuySellStrAggregation2);
AddCloud(if ShowBuySellStrength2ndAgg then BuyVol2 else NA, 0, VolColor.Color("Bullish"), VolColor.Color("Bullish"), yes);
AddCloud(if ShowBuySellStrength2ndAgg then volume("period" = BuySellStrAggregation2) else NA, BuyVol2, VolColor.Color("Bearish"), VolColor.Color("Bearish"), yes);

BuyVol.SetDefaultColor(VolColor.Color("Bullish"));
BuyVol.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
#BuyVol2.SetDefaultColor(Color.GREEN);
#BuyVol2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#SellVol2.SetDefaultColor(Color.GREEN);
#SellVol2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot Vol = volume;
plot VolumeBottom = if !ShowVolumeAsCandlesticks or ShowBuySellStrengthOnVolumeBars then NA else VolumeBottomShadowValue;
plot VolumeBody = if !ShowVolumeAsCandlesticks or ShowBuySellStrengthOnVolumeBars then NA else BodyRangeVolValue;

VolumeBottom.HideTitle();
VolumeBody.HideTitle();

plot VolAvg = MovingAverage(VolAverageType, volume, AvgVolLength);

#2Sigma and 3Sigma Vol Filter
def Num_Dev1 = 2.0;
def Num_Dev2 = 3.0;
def averageType = AverageType.SIMPLE;
def sDev = StDev(data = Vol, length = AvgVolLength);

plot VolSigma2 = VolAvg + Num_Dev1 * sDev;
plot VolSigma3 = VolAvg + Num_Dev2 * sDev;


def RelDayVol = DayVol / AvgDayVol[1];
def RelPrevDayVol = DayVol / volume("period" = DayVolAgg)[1];
#Daily aggregation volume labels
AddLabel(if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else if ShowDayVolLabel then 1 else 0, "DayVol: " + DayVol + " / " + Round(RelDayVol, 2) + "x Avg(" + AvgDayVolLength + ") / " + Round(RelPrevDayVol, 2) + "x Prev", if DayVol > AvgDayVol then VolColor.Color("VolAvg") else VolColor.Color("Below Average"));

def RelVol = Vol / VolAvg[1];
def RelPrevVol = volume / volume[1];

#Triangle Vol Signal
plot VolSignal = if Vol > VolSigma3 then volume else if Vol > VolSigma2 then volume else if Vol > VolAvg then volume else if RelPrevVol >= RelativetoPrevVolTolerance then volume else NA;

#current aggregation's volume labels
AddLabel(ShowBarVolLabel, "Vol: " + volume + " / " + Round(RelVol, 2) + "x Avg(" + AvgVolLength + ") / " + Round(RelPrevVol, 2) + "x Prev",  if Vol > VolSigma3 then VolColor.Color("VolSigma3") else if Vol > VolSigma2 then VolColor.Color("VolSigma2") else if Vol > VolAvg then VolColor.Color("VolAvg") else VolColor.Color("Below Average"));

#ETH Total Vol Label
def ETH_VOL = if !conf and conf[1] then volume else if !conf then CompoundValue(1, ETH_VOL[1] + volume, volume) else ETH_VOL[1];

AddLabel(if !ShowEthTotalVol then 0 else if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else 1, "ETH TVOL: " + ETH_VOL, VolColor.Color("ETH TVOL"));

#$TICK Vertical Lines
def tickc = close("$TICK");
def tickh = high("$TICK");
def tickl = low("$TICK");

AddVerticalLine(if ShowVertLines and (tickh > TickLevel) or ShowVertLines and (tickl < -TickLevel) then 1 else 0, if (tickh > TickLevel) then tickh else if (tickl < -TickLevel) then tickl else Double.NaN, VolColor.Color("TICK Vert"));

#$TICK Label
AddLabel(if ShowTickLabel then 1 else 0, "$TICK: " + tickc, if tickc > 0 then Color.GREEN else Color.RED);

#current candle Buy/Sell strength labels
AddLabel(if ShowBuySellStrength then 1 else 0, " ", Color.BLACK);
AddLabel(if ShowBuySellStrength then 1 else 0, "1", Color.GRAY);
AddLabel(if ShowBuySellStrength then 1 else 0, "Sell " + Round(SellStr, 2) + "%", if SellStr > BuyStr then Color.RED else Color.DARK_RED);
AddLabel(if ShowBuySellStrength then 1 else 0, "Buy " + Round(BuyStr, 2) + "%", if BuyStr > SellStr then Color.GREEN else Color.DARK_GREEN);

#2nd Aggregation Buy/Sell strength labels
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 and ShowBuySellStrength then 1 else 0, "Check BuySellAgg2 > Current Agg", Color.YELLOW);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, " ", Color.BLACK);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, "2", Color.GRAY);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, "Sell " + Round(SellStr2, 2) + "%", if SellStr2 > BuyStr2 then Color.RED else Color.DARK_RED);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, "Buy " + Round(BuyStr2, 2) + "%", if BuyStr2 > SellStr2 then Color.GREEN else Color.DARK_GREEN);


VolumeBottom.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
VolumeBottom.AssignValueColor(Color.BLACK);
VolumeBody.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);

VolumeBody.AssignValueColor(if PaintAboveAvgVolBars and Vol > VolSigma3 then VolColor.Color("VolSigma3") else if PaintAboveAvgVolBars and Vol > VolSigma2 then VolColor.Color("VolSigma2") else if PaintAboveAvgVolBars and Vol > VolAvg then VolColor.Color("VolAvg") else if PaintAccordingToRelPrevVol and RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else if  PaintBelowAvgVol and Vol < VolAvg then VolColor.Color("Below Average") else if close > open then VolColor.Color("Bullish") else VolColor.Color("Bearish"));
#VolumeTop.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
#VolumeTop.AssignValueColor(if close > open then Color.Black else Color.Black);

VolAvg.SetDefaultColor(VolColor.Color("VolAvg"));
VolSigma2.SetDefaultColor(VolColor.Color("VolSigma2"));
VolSigma3.SetDefaultColor(VolColor.Color("VolSigma3"));

Vol.SetPaintingStrategy(if !ShowVolumeAsCandlesticks or ShowBuySellStrengthOnVolumeBars then PaintingStrategy.SQUARED_HISTOGRAM else PaintingStrategy.HISTOGRAM);

Vol.SetLineWeight(1);
Vol.AssignValueColor(if ShowBuySellStrengthOnVolumeBars then VolColor.Color("Bearish") else if PaintAboveAvgVolBars and volume > VolSigma3 then VolColor.Color("VolSigma3") else if PaintAboveAvgVolBars and volume > VolSigma2 then VolColor.Color("VolSigma2") else if PaintAboveAvgVolBars and volume > VolAvg then VolColor.Color("VolAvg") else if PaintAccordingToRelPrevVol and RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else if PaintBelowAvgVol and volume < VolAvg then VolColor.Color("Below Average") else if close > open then VolColor.Color("Bullish") else VolColor.Color("Bearish"));

AssignPriceColor(if PaintPriceAsVol and PaintAboveAvgVolBars and volume > VolSigma3 then VolColor.Color("VolSigma3") else if PaintPriceAsVol and PaintAboveAvgVolBars and volume > VolSigma2 then VolColor.Color("VolSigma2") else if PaintPriceAsVol and PaintAboveAvgVolBars and volume > VolAvg then VolColor.Color("VolAvg") else if PaintPriceAsVol and PaintAccordingToRelPrevVol and RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else if PaintPriceAsVol and PaintBelowAvgVol and volume < VolAvg then VolColor.Color("Below Average") else Color.CURRENT);

VolSignal.AssignValueColor(if Vol > VolSigma3 then VolColor.Color("VolSigma3") else if Vol > VolSigma2 then VolColor.Color("VolSigma2") else if Vol > VolAvg then VolColor.Color("VolAvg") else if RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else VolColor.Color("Below Average"));
VolSignal.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
VolAvg.SetLineWeight(2);
VolSignal.SetLineWeight(1);

########################################
#Buy/Sell Bubbles

input show_buy_sell_bubbles = yes;
input begin = 0930;
def bar = if SecondsTillTime(begin) == 0 and
                   SecondsFromTime(begin) == 0
                then 0
                else bar[1] + 1;
input bubblemover = 15;
def b = bubblemover;
def b1 = b + 1;
AddChartBubble(show_buy_sell_bubbles and !IsNaN(close[b]) and bar[b] % ((30) / (GetAggregationPeriod() / 60000)) == 0 , VolSigma2[b], SellStr2, Color.RED);
AddChartBubble(show_buy_sell_bubbles and !IsNaN(close[b]) and bar[b] % ((30) / (GetAggregationPeriod() / 60000)) == 0 , VolSigma2[b], BuyStr2, Color.GREEN);
#######################################
 

fungus12

Member
This plots a buy/sell bubbles at the input bar at 30 min agg in the example at the input bubblemover at 15, thereby centering it on the volsigma2 line. Adjust the inputs to your liking. The code for the bubbles was added to the end of the script.
Wow awesome job dude. Just have a few questions about this, and sorry if I'm asking too many questions.

1) So right now the bubbles pop up 30 minutes after the new interval. Here's a picture:

FIUt7P4.png


The way to fix this is to have its starting point be a minute after market open (6:31 my time, 9:31 NY time) as you can see in that picture. I looked at the code but I'm bad at coding. I tried changing the SecondsFromTime values to == 420 because in the image I provided, the starting point for that day was 6:24, so I tried adding 420 seconds which is 7 minutes from that time but that didn't work.

I think maybe the issue is bubbles like these:

9w3OYT4.png


As you can see, for some reason it plotted a bubble 15 minutes after 7:00 even though the interval is set at 30m. From then on it continued to plot at 30m intervals but I don't know why it plotted the 15m. I think that probably throws it off.

2) Is it possible to have these displayed as percentages? I tried but it kept giving me errors.

3) Is there some way to make the chart bubble aggregation customizable in the study itself? For example if I want to have 3 of these studies up with different aggregations like 10m/15m/30m, I would have to create 3 separate copies of the study with 3 different bubble aggregations in the code. Which honestly isn't a big deal. If there's no workaround to that I'm completely fine with that.

Once again thanks a lot for your help man.
 

SleepyZ

Well-known member
Wow awesome job dude. Just have a few questions about this, and sorry if I'm asking too many questions.

1) So right now the bubbles pop up 30 minutes after the new interval. Here's a picture:

FIUt7P4.png


The way to fix this is to have its starting point be a minute after market open (6:31 my time, 9:31 NY time) as you can see in that picture. I looked at the code but I'm bad at coding. I tried changing the SecondsFromTime values to == 420 because in the image I provided, the starting point for that day was 6:24, so I tried adding 420 seconds which is 7 minutes from that time but that didn't work.

I think maybe the issue is bubbles like these:

9w3OYT4.png


As you can see, for some reason it plotted a bubble 15 minutes after 7:00 even though the interval is set at 30m. From then on it continued to plot at 30m intervals but I don't know why it plotted the 15m. I think that probably throws it off.

2) Is it possible to have these displayed as percentages? I tried but it kept giving me errors.

3) Is there some way to make the chart bubble aggregation customizable in the study itself? For example if I want to have 3 of these studies up with different aggregations like 10m/15m/30m, I would have to create 3 separate copies of the study with 3 different bubble aggregations in the code. Which honestly isn't a big deal. If there's no workaround to that I'm completely fine with that.

Once again thanks a lot for your help man.
I centered the bubble as mentioned above. Set the bubblemover to zero (0) to see it at the start of the 30 minutes. It starts computing when the new 30 minutes starts in ONDEMAND testing. I moved it because your picture showed then havd written values in the middle of the clouds.
 

fungus12

Member
I centered the bubble as mentioned above. Set the bubblemover to zero (0) to see it at the start of the 30 minutes. It starts computing when the new 30 minutes starts in ONDEMAND testing. I moved it because your picture showed then havd written values in the middle of the clouds.
Perfect. bubblemover at 0 fixed it. Thanks man. Do you think it's possible to have these as percentages?
 

SleepyZ

Well-known member
Perfect. bubblemover at 0 fixed it. Thanks man. Do you think it's possible to have these as percentages?
Replace this code for percentages presentation

Ruby:
AddChartBubble(show_buy_sell_bubbles and !IsNaN(close[b]) and bar[b] % ((30) / (GetAggregationPeriod() / 60000)) == 0 , VolSigma2[b], Round(SellStr2) + "%", Color.RED);
AddChartBubble(show_buy_sell_bubbles and !IsNaN(close[b]) and bar[b] % ((30) / (GetAggregationPeriod() / 60000)) == 0 , VolSigma2[b], Round(BuyStr2) + "%", Color.GREEN);
 

fungus12

Member
Replace this code for percentages presentation

Ruby:
AddChartBubble(show_buy_sell_bubbles and !IsNaN(close[b]) and bar[b] % ((30) / (GetAggregationPeriod() / 60000)) == 0 , VolSigma2[b], Round(SellStr2) + "%", Color.RED);
AddChartBubble(show_buy_sell_bubbles and !IsNaN(close[b]) and bar[b] % ((30) / (GetAggregationPeriod() / 60000)) == 0 , VolSigma2[b], Round(BuyStr2) + "%", Color.GREEN);
Awesome. One more question regarding the bubbles, do you think it's possible to set it so the 30m bubble aggregation is changeable in the study menu as opposed to in the code itself? I use the 30m/15m/10m aggregations on 3 charts so if its hardcoded I have to make 3 copies of this study with different bubble aggregations. If it's not possible then that's not a big deal, just curious.
 

SleepyZ

Well-known member
Awesome. One more question regarding the bubbles, do you think it's possible to set it so the 30m bubble aggregation is changeable in the study menu as opposed to in the code itself? I use the 30m/15m/10m aggregations on 3 charts so if its hardcoded I have to make 3 copies of this study with different bubble aggregations. If it's not possible then that's not a big deal, just curious.

Whatever you input at BuySellStrAgg2 will now update the bubbles by replacing the following code.

Ruby:
 ########################################
#Buy/Sell Bubbles

input show_buy_sell_bubbles = yes;
input begin = 0930;
def bar = if SecondsTillTime(begin) == 0 and
                   SecondsFromTime(begin) == 0
                then 0
                else bar[1] + 1;
input bubblemover = 0;
def b = bubblemover;
def b1 = b + 1;
def timeframe =  BuySellStrAgg2 / 60000;

AddChartBubble(show_buy_sell_bubbles and !IsNaN(close[b]) and bar[b] % ((timeframe) / (GetAggregationPeriod() / 60000)) == 0 , VolSigma2[b], Round(SellStr2) + "%", Color.RED);
AddChartBubble(show_buy_sell_bubbles and !IsNaN(close[b]) and bar[b] % ((timeframe) / (GetAggregationPeriod() / 60000)) == 0 , VolSigma2[b], Round(BuyStr2) + "%", Color.GREEN);
#######################################
 

fungus12

Member
Whatever you input at BuySellStrAgg2 will now update the bubbles by replacing the following code.
So this seems to be working but some of the bubbles have pushed forward a few minutes off the intervals. Some are right on point at 30 minute intervals like this:

UM9joCa.png


But there are also segments like this that are delayed:

1NYu3Xx.png
 

SleepyZ

Well-known member
So this seems to be working but some of the bubbles have pushed forward a few minutes off the intervals. Some are right on point at 30 minute intervals like this:

UM9joCa.png


But there are also segments like this that are delayed:

1NYu3Xx.png

Glad you checked. As TOS does not plot a bar if there is no activity, the method used could be off. Here is another way that I think may help fix that.

Ruby:
########################################
#Buy/Sell Bubbles

input show_buy_sell_bubbles = yes;
input begin = 0930;
def bar = if SecondsTillTime(begin) == 0 and
                   SecondsFromTime(begin) == 0
                then 0
                else bar[1] + 1;
input bubblemover = 0;
def b = bubblemover;
def b1 = b + 1;

def periods     = if isnan(close(period=buysellstrAggregation2)) then periods[1] else close(period=buysellstrAggregation2);
def periodCount = CompoundValue(1, if periods then periodCount[1] + 1 else periodCount[1], 0);
def thisperiod  = (HighestAll(periodCount) - periodCount) ;

AddChartBubble(show_buy_sell_bubbles and thisperiod[b1]!=thisperiod[b], VolSigma2[b], Round(SellStr2) + "%", Color.RED);
AddChartBubble(show_buy_sell_bubbles and thisperiod[b1]!=thisperiod[b] , VolSigma2[b], Round(BuyStr2) + "%", Color.GREEN);
#######################################
 

fungus12

Member
Glad you checked. As TOS does not plot a bar if there is no activity, the method used could be off. Here is another way that I think may help fix that.

Ruby:
########################################
#Buy/Sell Bubbles

input show_buy_sell_bubbles = yes;
input begin = 0930;
def bar = if SecondsTillTime(begin) == 0 and
                   SecondsFromTime(begin) == 0
                then 0
                else bar[1] + 1;
input bubblemover = 0;
def b = bubblemover;
def b1 = b + 1;

def periods     = if isnan(close(period=buysellstrAggregation2)) then periods[1] else close(period=buysellstrAggregation2);
def periodCount = CompoundValue(1, if periods then periodCount[1] + 1 else periodCount[1], 0);
def thisperiod  = (HighestAll(periodCount) - periodCount) ;

AddChartBubble(show_buy_sell_bubbles and thisperiod[b1]!=thisperiod[b], VolSigma2[b], Round(SellStr2) + "%", Color.RED);
AddChartBubble(show_buy_sell_bubbles and thisperiod[b1]!=thisperiod[b] , VolSigma2[b], Round(BuyStr2) + "%", Color.GREEN);
#######################################
Man I wish I could offer you something other than likes lol. Genius stuff, it works perfectly. I feel like an ass for asking so many questions from you but the last thing I need is a watchlist column for this aggregation. @Welkin posted this column in the original post:

Code:
#Buy/Sell Strength Column
def o = open;
def h = high;
def l = low;
def c = close;
def PR = h - l;
def BuyStr = ((c - l) / PR) * 100;

AddLabel(1,Round(BuyStr,1)+" | " + Round(100-BuyStr,1)+"%", Color.BLACK);

AssignBackgroundColor(if BuyStr >= 70 then Color.Green else if BuyStr > 50 then Color.DARK_GREEN else if BuyStr <= 30 then Color.RED else if BuyStr < 50 then Color.DARK_RED else Color.GRAY);

However this is for the current bars. Is it possible to have this strength column for aggregates as well?
 

SleepyZ

Well-known member
Man I wish I could offer you something other than likes lol. Genius stuff, it works perfectly. I feel like an ass for asking so many questions from you but the last thing I need is a watchlist column for this aggregation. @Welkin posted this column in the original post:

Code:
#Buy/Sell Strength Column
def o = open;
def h = high;
def l = low;
def c = close;
def PR = h - l;
def BuyStr = ((c - l) / PR) * 100;

AddLabel(1,Round(BuyStr,1)+" | " + Round(100-BuyStr,1)+"%", Color.BLACK);

AssignBackgroundColor(if BuyStr >= 70 then Color.Green else if BuyStr > 50 then Color.DARK_GREEN else if BuyStr <= 30 then Color.RED else if BuyStr < 50 then Color.DARK_RED else Color.GRAY);

However this is for the current bars. Is it possible to have this strength column for aggregates as well?

Try using this in same code in a watchlist column and set the aggregation of the column to for example the 30 minute agg used in the Welkin Volume Code
 

fungus12

Member
Try using this in same code in a watchlist column and set the aggregation of the column to for example the 30 minute agg used in the Welkin Volume Code
Hmm I'm not sure what you mean by "use this in the same code", I tried pasting that watchlist code into the AVI script and got this error:

wLDrhzo.png
 

nab

New member
Scratch that, figured it out. All you had to do was just paste that code and change it to a 30m aggregation. @SleepyZ I can't thank you enough dude. Thanks so much for the help!
@fungus12 could you please post the final code since there was a lots of discussion, if you can make a shareable link that would great for the chart and the watchlist....thanks
 

fungus12

Member
@fungus12 could you please post the final code since there was a lots of discussion, if you can make a shareable link that would great for the chart and the watchlist....thanks
Sure, here's the full code:

Code:
#Advanced Volume Study
#[email protected]
#v5.22.2020
declare on_volume;
input ShowVolumeAsCandlesticks = no;
input ShowBuySellStrengthOnVolumeBars = no;
input ShowBuySellStrength2ndAgg = no;
input AvgDayVolLength = 5;
input AvgVolLength = 20;
input ShowDayVolLabel = yes;
input ShowBarVolLabel = yes;
input ShowEthTotalVol = no;
input ShowBuySellStrength = yes;
input BuySellStrAgg2 = AggregationPeriod.THIRTY_MIN;
def BuySellStrAggregation2 = if GetAggregationPeriod() < BuySellStrAgg2 then BuySellStrAgg2 else GetAggregationPeriod();
AddLabel(if GetAggregationPeriod() < BuySellStrAgg2 then 0 else 1, "Adjust BuySellStrAgg2 in Study Settings", Color.YELLOW);
input VolAverageType = AverageType.SIMPLE;
#if ShowBuySellStrengthOnVolumeBars is toggled on then the following volume bar paint options will not show, only the VolSignal Triangle set at the top of the bars will be painting according to volume average levels.
input PaintAboveAvgVolBars = yes;
input PaintAccordingToRelPrevVol = yes;
input RelativetoPrevVolTolerance = 1.25; #if volume is 1.25x greater than previous bar it will paint even if it is still below the average/sigma2/sigma3
input PaintBelowAvgVol = yes;
input PaintPriceAsVol = no;
input ShowVerticalTickLines = yes;
def ShowVertLines = if ShowVerticalTickLines and GetAggregationPeriod() < AggregationPeriod.DAY then 1 else 0;
input TickLevel = 1000;
input ShowTickLabel = yes;


def NA = Double.NaN;
def PriceRange = high - low;
def TopShadowRange = if open >= close then high - open else high - close;
def BottomShadowRange = if open <= close then open - low else close - low;
def BodyRange = PriceRange - (TopShadowRange + BottomShadowRange);
def VolumeTopShadowValue = (1 - (TopShadowRange / PriceRange)) * volume;
def VolumeBottomShadowValue = ((BottomShadowRange / PriceRange) * volume);
def BodyRangeVolValue = ((BodyRange + BottomShadowRange) / PriceRange) * volume;
#def DayVolAgg = if GetAggregationPeriod() < AggregationPeriod.DAY then AggregationPeriod.DAY else if GetAggregationPeriod() >= AggregationPeriod.DAY then AggregationPeriod.WEEK else GetAggregationPeriod();
def DayVolAgg = if GetAggregationPeriod() < AggregationPeriod.DAY then AggregationPeriod.DAY else GetAggregationPeriod();
def DayVol = volume("period" = DayVolAgg);
def AvgDayVol = Average(DayVol, AvgDayVolLength);
def Start = 0930;
def End = 1600;
def conf = SecondsFromTime(Start) >= 0 and SecondsFromTime(End) <= 0;

plot VolColor = NA;
VolColor.DefineColor("Bullish", Color.GREEN);
VolColor.DefineColor("Bearish", Color.RED);
VolColor.DefineColor("VolAvg", CreateColor(0, 100, 200));
VolColor.DefineColor("VolSigma2", Color.DARK_ORANGE);
VolColor.DefineColor("VolSigma3", Color.MAGENTA);
VolColor.DefineColor("Relative to Prev", Color.YELLOW);
VolColor.DefineColor("Below Average", Color.GRAY);
VolColor.DefineColor("ETH TVOL", Color.GRAY);
VolColor.DefineColor("TICK Vert", Color.GRAY);

#Current Candle Buy and Sell Strength
def BuyStr = ((close - low) / PriceRange) * 100;
def SellStr = ((high - close) / PriceRange) * 100;

def BuyStr2 = ((close("period" = BuySellStrAggregation2) - low("period" = BuySellStrAggregation2)) / (high("period" = BuySellStrAggregation2) - low("period" = BuySellStrAggregation2))) * 100;
def SellStr2 = ((high("period" = BuySellStrAggregation2) - close("period" = BuySellStrAggregation2)) / (high("period" = BuySellStrAggregation2) - low("period" = BuySellStrAggregation2))) * 100;

plot BuyVol = if ShowBuySellStrengthOnVolumeBars then (BuyStr/100)*volume else NA;
def SellVol = (SellStr/100)*volume;
def BuyVol2 = (BuyStr2/100)*volume("period" = BuySellStrAggregation2);
def SellVol2 = (SellStr2/100)*volume("period" = BuySellStrAggregation2);
AddCloud(if ShowBuySellStrength2ndAgg then BuyVol2 else NA, 0, VolColor.Color("Bullish"), VolColor.Color("Bullish"), yes);
AddCloud(if ShowBuySellStrength2ndAgg then volume("period" = BuySellStrAggregation2) else NA, BuyVol2, VolColor.Color("Bearish"), VolColor.Color("Bearish"), yes);

BuyVol.SetDefaultColor(VolColor.Color("Bullish"));
BuyVol.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
#BuyVol2.SetDefaultColor(Color.GREEN);
#BuyVol2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#SellVol2.SetDefaultColor(Color.GREEN);
#SellVol2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot Vol = volume;
plot VolumeBottom = if !ShowVolumeAsCandlesticks or ShowBuySellStrengthOnVolumeBars then NA else VolumeBottomShadowValue;
plot VolumeBody = if !ShowVolumeAsCandlesticks or ShowBuySellStrengthOnVolumeBars then NA else BodyRangeVolValue;

VolumeBottom.HideTitle();
VolumeBody.HideTitle();

plot VolAvg = MovingAverage(VolAverageType, volume, AvgVolLength);

#2Sigma and 3Sigma Vol Filter
def Num_Dev1 = 2.0;
def Num_Dev2 = 3.0;
def averageType = AverageType.SIMPLE;
def sDev = StDev(data = Vol, length = AvgVolLength);

plot VolSigma2 = VolAvg + Num_Dev1 * sDev;
plot VolSigma3 = VolAvg + Num_Dev2 * sDev;


def RelDayVol = DayVol / AvgDayVol[1];
def RelPrevDayVol = DayVol / volume("period" = DayVolAgg)[1];
#Daily aggregation volume labels
AddLabel(if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else if ShowDayVolLabel then 1 else 0, "DayVol: " + DayVol + " / " + Round(RelDayVol, 2) + "x Avg(" + AvgDayVolLength + ") / " + Round(RelPrevDayVol, 2) + "x Prev", if DayVol > AvgDayVol then VolColor.Color("VolAvg") else VolColor.Color("Below Average"));

def RelVol = Vol / VolAvg[1];
def RelPrevVol = volume / volume[1];

#Triangle Vol Signal
plot VolSignal = if Vol > VolSigma3 then volume else if Vol > VolSigma2 then volume else if Vol > VolAvg then volume else if RelPrevVol >= RelativetoPrevVolTolerance then volume else NA;

#current aggregation's volume labels
AddLabel(ShowBarVolLabel, "Vol: " + volume + " / " + Round(RelVol, 2) + "x Avg(" + AvgVolLength + ") / " + Round(RelPrevVol, 2) + "x Prev",  if Vol > VolSigma3 then VolColor.Color("VolSigma3") else if Vol > VolSigma2 then VolColor.Color("VolSigma2") else if Vol > VolAvg then VolColor.Color("VolAvg") else VolColor.Color("Below Average"));

#ETH Total Vol Label
def ETH_VOL = if !conf and conf[1] then volume else if !conf then CompoundValue(1, ETH_VOL[1] + volume, volume) else ETH_VOL[1];

AddLabel(if !ShowEthTotalVol then 0 else if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else 1, "ETH TVOL: " + ETH_VOL, VolColor.Color("ETH TVOL"));

#$TICK Vertical Lines
def tickc = close("$TICK");
def tickh = high("$TICK");
def tickl = low("$TICK");

AddVerticalLine(if ShowVertLines and (tickh > TickLevel) or ShowVertLines and (tickl < -TickLevel) then 1 else 0, if (tickh > TickLevel) then tickh else if (tickl < -TickLevel) then tickl else Double.NaN, VolColor.Color("TICK Vert"));

#$TICK Label
AddLabel(if ShowTickLabel then 1 else 0,"$TICK: " + tickc, if tickc > 0 then Color.GREEN else Color.RED);

#current candle Buy/Sell strength labels
AddLabel(if ShowBuySellStrength then 1 else 0, " ", Color.BLACK);
AddLabel(if ShowBuySellStrength then 1 else 0, "1", Color.GRAY);
AddLabel(if ShowBuySellStrength then 1 else 0, "Sell " + Round(SellStr, 2) + "%", if SellStr > BuyStr then Color.RED else Color.DARK_RED);
AddLabel(if ShowBuySellStrength then 1 else 0, "Buy " + Round(BuyStr, 2) + "%", if BuyStr > SellStr then Color.GREEN else Color.DARK_GREEN);

#2nd Aggregation Buy/Sell strength labels
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 and ShowBuySellStrength then 1 else 0,"Check BuySellAgg2 > Current Agg", Color.YELLOW);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, " ", Color.BLACK);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, "2", Color.GRAY);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, "Sell " + Round(SellStr2, 2) + "%", if SellStr2 > BuyStr2 then Color.RED else Color.DARK_RED);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, "Buy " + Round(BuyStr2, 2) + "%", if BuyStr2 > SellStr2 then Color.GREEN else Color.DARK_GREEN);


VolumeBottom.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
VolumeBottom.AssignValueColor(Color.BLACK);
VolumeBody.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);

VolumeBody.AssignValueColor(if PaintAboveAvgVolBars and Vol > VolSigma3 then VolColor.Color("VolSigma3") else if PaintAboveAvgVolBars and Vol > VolSigma2 then VolColor.Color("VolSigma2") else if PaintAboveAvgVolBars and Vol > VolAvg then VolColor.Color("VolAvg") else if PaintAccordingToRelPrevVol and RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else if  PaintBelowAvgVol and Vol < VolAvg then VolColor.Color("Below Average") else if close > open then VolColor.Color("Bullish") else VolColor.Color("Bearish"));
#VolumeTop.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
#VolumeTop.AssignValueColor(if close > open then Color.Black else Color.Black);

VolAvg.SetDefaultColor(VolColor.Color("VolAvg"));
VolSigma2.SetDefaultColor(VolColor.Color("VolSigma2"));
VolSigma3.SetDefaultColor(VolColor.Color("VolSigma3"));

Vol.SetPaintingStrategy(if !ShowVolumeAsCandlesticks or ShowBuySellStrengthOnVolumeBars then PaintingStrategy.SQUARED_HISTOGRAM else PaintingStrategy.HISTOGRAM);

Vol.SetLineWeight(1);
Vol.AssignValueColor(if ShowBuySellStrengthOnVolumeBars then VolColor.Color("Bearish") else if PaintAboveAvgVolBars and volume > VolSigma3 then VolColor.Color("VolSigma3") else if PaintAboveAvgVolBars and volume > VolSigma2 then VolColor.Color("VolSigma2") else if PaintAboveAvgVolBars and volume > VolAvg then VolColor.Color("VolAvg") else if PaintAccordingToRelPrevVol and RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else if PaintBelowAvgVol and volume < VolAvg then VolColor.Color("Below Average") else if close > open then VolColor.Color("Bullish") else VolColor.Color("Bearish"));

AssignPriceColor(if PaintPriceAsVol and PaintAboveAvgVolBars and volume > VolSigma3 then VolColor.Color("VolSigma3") else if PaintPriceAsVol and PaintAboveAvgVolBars and volume > VolSigma2 then VolColor.Color("VolSigma2") else if PaintPriceAsVol and PaintAboveAvgVolBars and volume > VolAvg then VolColor.Color("VolAvg") else if PaintPriceAsVol and PaintAccordingToRelPrevVol and RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else if PaintPriceAsVol and PaintBelowAvgVol and volume < VolAvg then VolColor.Color("Below Average") else Color.CURRENT);

VolSignal.AssignValueColor(if Vol > VolSigma3 then VolColor.Color("VolSigma3") else if Vol > VolSigma2 then VolColor.Color("VolSigma2") else if Vol > VolAvg then VolColor.Color("VolAvg") else if RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else VolColor.Color("Below Average"));
VolSignal.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
VolAvg.SetLineWeight(2);
VolSignal.SetLineWeight(1);



########################################
#Buy/Sell Bubbles

input show_buy_sell_bubbles = yes;
input begin = 0930;
def bar = if SecondsTillTime(begin) == 0 and
                   SecondsFromTime(begin) == 0
                then 0
                else bar[1] + 1;
input bubblemover = 0;
def b = bubblemover;
def b1 = b + 1;

def periods     = if isnan(close(period=buysellstrAggregation2)) then periods[1] else close(period=buysellstrAggregation2);
def periodCount = CompoundValue(1, if periods then periodCount[1] + 1 else periodCount[1], 0);
def thisperiod  = (HighestAll(periodCount) - periodCount) ;

AddChartBubble(show_buy_sell_bubbles and thisperiod[b1]!=thisperiod[b], VolSigma2[b], Round(SellStr2) + "%", Color.RED);
AddChartBubble(show_buy_sell_bubbles and thisperiod[b1]!=thisperiod[b] , VolSigma2[b], Round(BuyStr2) + "%", Color.GREEN);
#######################################


This is the code for the watchlist:

Code:
#Buy/Sell Strength Column
def o = open;
def h = high;
def l = low;
def c = close;
def PR = h - l;
def BuyStr = ((c - l) / PR) * 100;

AddLabel(1,Round(BuyStr,1)+" | " + Round(100-BuyStr,1)+"%", Color.BLACK);

AssignBackgroundColor(if BuyStr >= 70 then Color.Green else if BuyStr > 50 then Color.DARK_GREEN else if BuyStr <= 30 then Color.RED else if BuyStr < 50 then Color.DARK_RED else Color.GRAY);

To get this to work in a watchlist, right click on "Symbol" on your watchlist. Click customize. Type in "Custom" in the search bar and go to any of the "Custom" scripts there. Click on the script icon, click on thinkscript editor, paste the code there, and at the top where it says "D" for day, change that to whatever aggregation period you want. Make sure to rename it too.
 

BerryMcCawkiner

New member
@SleepyZ
ive been using @Welkin volume labels for a while and im wondering if anyone can help me create what im looking for
welkins labels uses data per candle which means if its 10:00 the 1min 5min 30min 1hr candle will all have the same buy/sell percentage for a short time

im wondering if its possible to count 5 candles every 5 minutes and forget the 5th candle every new minute and so on with 30 candles forgetting the 30th...etc
this will give you a percentage of buy/sell of the last 5/10/15/30 minutes instead of 5min 30min candles it would work more as a volume trend than just volume indicating direction of trend with volume
i believe this would only work on the 1 minute chart accurately because this script idea on a 3min chart counting back 5min would be 15 min buy/ sell volume but itd still work

theres a volume script on here cant seen to find it that plots the average 30 bar volume by adding together 30 volume bars and gives you a value thats kinda the idea im going for with added buy/sell

Code:
#[email protected]
#Volume Labels
input AvgDayVolLength = 5;
input AvgVolLength = 20;
input ShowDayVolLabel = yes;
input ShowBarVolLabel = yes;
input ShowBuySellStrength = yes;
input BuySellStrAggregation2 = AggregationPeriod.thiRTY_MIN;
input VolAverageType = AverageType.SIMPLE;
input BuySellStrAggregation3 = AggregationPeriod.day;
input BuySellStrAggregation4 = AggregationPeriod.hour;

def NA = Double.NaN;
def PriceRange = high - low;
def DayVol = volume("period" = AggregationPeriod.DAY);
def AvgDayVol = Average(DayVol,AvgDayVolLength);

plot VolColor = NA;
VolColor.DefineColor("Bullish", Color.GREEN);
VolColor.DefineColor("Bearish", Color.RED);
VolColor.DefineColor("VolAvg", CreateColor(0,100,200));
VolColor.DefineColor("VolSigma2", Color.DARK_ORANGE);
VolColor.DefineColor("VolSigma3", Color.Magenta);
VolColor.DefineColor("Relative to Prev", Color.YELLOW);
VolColor.DefineColor("Below Average", Color.GRAY);

def Vol = volume;
def VolAvg = MovingAverage(VolAverageType, Volume, AvgVolLength);

#2Sigma and 3Sigma Vol Filter
def Num_Dev1 = 2.0;
def Num_Dev2 = 3.0;
def averageType = AverageType.SIMPLE;
def sDev = StDev(data = Vol, length = AvgVolLength);

def VolSigma2 = VolAvg + Num_Dev1 * sDev;
def VolSigma3 = VolAvg + Num_Dev2 * sDev;

#Current Candle Buy and Sell Strength
def BuyStr = ((close - low)/PriceRange)*100;
def SellStr = ((high - close)/PriceRange)*100;


def BuyStr2 = ((close("period"=BuySellStrAggregation2) - low("period"=BuySellStrAggregation2))/(high("period"=BuySellStrAggregation2) - low("period"=BuySellStrAggregation2)))*100;
def SellStr2 = ((high("period"=BuySellStrAggregation2) - close("period"=BuySellStrAggregation2))/(high("period"=BuySellStrAggregation2) - low("period"=BuySellStrAggregation2)))*100;

def BuyStr3 = ((close("period"=BuySellStrAggregation3) - low("period"=BuySellStrAggregation3))/(high("period"=BuySellStrAggregation3) - low("period"=BuySellStrAggregation3)))*100;
def SellStr3 = ((high("period"=BuySellStrAggregation3) - close("period"=BuySellStrAggregation3))/(high("period"=BuySellStrAggregation3) - low("period"=BuySellStrAggregation3)))*100;

def BuyStr4 = ((close("period"=BuySellStrAggregation4) - low("period"=BuySellStrAggregation4))/(high("period"=BuySellStrAggregation4) - low("period"=BuySellStrAggregation4)))*100;
def SellStr4 = ((high("period"=BuySellStrAggregation4) - close("period"=BuySellStrAggregation4))/(high("period"=BuySellStrAggregation4) - low("period"=BuySellStrAggregation4)))*100;


def RelDayVol = DayVol / AvgDayVol;
def RelPrevDayVol = DayVol / volume("period" = AggregationPeriod.DAY)[1];
#Daily aggregation volume labels
AddLabel(if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else if ShowDayVolLabel then 1 else 0, "DayVol: " + DayVol +" / "+ Round(RelDayVol,2) + "x Avg("+ AvgDayVolLength +") / "+ Round(RelPrevDayVol,2) +"x Prev", if DayVol > AvgDayVol then VolColor.Color("VolAvg") else VolColor.Color("Below Average"));

def RelVol = Vol/VolAvg;
def RelPrevVol = volume / volume[1];

#current aggregation's volume labels
AddLabel(ShowBarVolLabel, "Vol: " + volume +" / "+ Round(RelVol,2) + "x Avg("+ AvgVolLength +") / "+ Round(RelPrevVol,2)+"x Prev",  if vol > VolSigma3 then VolColor.Color("VolSigma3") else if Vol > VolSigma2 then VolColor.Color("VolSigma2") else if Vol > VolAvg then VolColor.Color("VolAvg") else VolColor.Color("Below Average"));

#current candle Buy/Sell strength labels
##AddLabel(if ShowBuySellStrength then 1 else 0, "1", Color.GRAY);
#AddLabel(if ShowBuySellStrength then 1 else 0, "Sell "+Round(SellStr,2) +"%", if SellStr > BuyStr then Color.RED else Color.pink);
#AddLabel(if ShowBuySellStrength then 1 else 0, "Buy "+Round(BuyStr,2) +"%", if BuyStr > SellStr then Color.GREEN else Color.light_GREEN);

#2nd Aggregation Buy/Sell strength labels
#AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, " ", Color.BLACK);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, "5m", Color.GRAY);
#AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, "Sell "+Round(SellStr2,2) +"%", if SellStr2 > BuyStr2 then Color.RED else Color.pink);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, "Buy "+Round(BuyStr2,2) +"%", if BuyStr2 > SellStr2 then Color.cyan else Color.red);

#2nd Aggregation Buy/Sell strength labels
#AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation4 or !ShowBuySellStrength then 0 else 1, " ", Color.BLACK);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation4 or !ShowBuySellStrength then 0 else 1, "1H", Color.GRAY);
#AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation3 or !ShowBuySellStrength then 0 else 1, "DSell "+Round(SellStr3,2) +"%", if SellStr3 > BuyStr3 then Color.RED else Color.pink);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation4 or !ShowBuySellStrength then 0 else 1, "Buy "+Round(BuyStr4,2) +"%", if BuyStr4 > SellStr4 then Color.cyan else Color.red);

#2nd Aggregation Buy/Sell strength labels
#AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation3 or !ShowBuySellStrength then 0 else 1, " ", Color.BLACK);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation3 or !ShowBuySellStrength then 0 else 1, "Day", Color.GRAY);
#AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation3 or !ShowBuySellStrength then 0 else 1, "DSell "+Round(SellStr3,2) +"%", if SellStr3 > BuyStr3 then Color.RED else Color.pink);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation3 or !ShowBuySellStrength then 0 else 1, "Buy "+Round(BuyStr3,2) +"%", if BuyStr3 > SellStr3 then Color.cyan else Color.red);
 
Last edited by a moderator:

SleepyZ

Well-known member
@SleepyZ
ive been using @Welkin volume labels for a while and im wondering if anyone can help me create what im looking for
welkins labels uses data per candle which means if its 10:00 the 1min 5min 30min 1hr candle will all have the same buy/sell percentage for a short time

im wondering if its possible to count 5 candles every 5 minutes and forget the 5th candle every new minute and so on with 30 candles forgetting the 30th...etc
this will give you a percentage of buy/sell of the last 5/10/15/30 minutes instead of 5min 30min candles it would work more as a volume trend than just volume indicating direction of trend with volume
i believe this would only work on the 1 minute chart accurately because this script idea on a 3min chart counting back 5min would be 15 min buy/ sell volume but itd still work

theres a volume script on here cant seen to find it that plots the average 30 bar volume by adding together 30 volume bars and gives you a value thats kinda the idea im going for with added buy/sell

Code:
#[email protected]
#Volume Labels
input AvgDayVolLength = 5;
input AvgVolLength = 20;
input ShowDayVolLabel = yes;
input ShowBarVolLabel = yes;
input ShowBuySellStrength = yes;
input BuySellStrAggregation2 = AggregationPeriod.thiRTY_MIN;
input VolAverageType = AverageType.SIMPLE;
input BuySellStrAggregation3 = AggregationPeriod.day;
input BuySellStrAggregation4 = AggregationPeriod.hour;

def NA = Double.NaN;
def PriceRange = high - low;
def DayVol = volume("period" = AggregationPeriod.DAY);
def AvgDayVol = Average(DayVol,AvgDayVolLength);

plot VolColor = NA;
VolColor.DefineColor("Bullish", Color.GREEN);
VolColor.DefineColor("Bearish", Color.RED);
VolColor.DefineColor("VolAvg", CreateColor(0,100,200));
VolColor.DefineColor("VolSigma2", Color.DARK_ORANGE);
VolColor.DefineColor("VolSigma3", Color.Magenta);
VolColor.DefineColor("Relative to Prev", Color.YELLOW);
VolColor.DefineColor("Below Average", Color.GRAY);

def Vol = volume;
def VolAvg = MovingAverage(VolAverageType, Volume, AvgVolLength);

#2Sigma and 3Sigma Vol Filter
def Num_Dev1 = 2.0;
def Num_Dev2 = 3.0;
def averageType = AverageType.SIMPLE;
def sDev = StDev(data = Vol, length = AvgVolLength);

def VolSigma2 = VolAvg + Num_Dev1 * sDev;
def VolSigma3 = VolAvg + Num_Dev2 * sDev;

#Current Candle Buy and Sell Strength
def BuyStr = ((close - low)/PriceRange)*100;
def SellStr = ((high - close)/PriceRange)*100;


def BuyStr2 = ((close("period"=BuySellStrAggregation2) - low("period"=BuySellStrAggregation2))/(high("period"=BuySellStrAggregation2) - low("period"=BuySellStrAggregation2)))*100;
def SellStr2 = ((high("period"=BuySellStrAggregation2) - close("period"=BuySellStrAggregation2))/(high("period"=BuySellStrAggregation2) - low("period"=BuySellStrAggregation2)))*100;

def BuyStr3 = ((close("period"=BuySellStrAggregation3) - low("period"=BuySellStrAggregation3))/(high("period"=BuySellStrAggregation3) - low("period"=BuySellStrAggregation3)))*100;
def SellStr3 = ((high("period"=BuySellStrAggregation3) - close("period"=BuySellStrAggregation3))/(high("period"=BuySellStrAggregation3) - low("period"=BuySellStrAggregation3)))*100;

def BuyStr4 = ((close("period"=BuySellStrAggregation4) - low("period"=BuySellStrAggregation4))/(high("period"=BuySellStrAggregation4) - low("period"=BuySellStrAggregation4)))*100;
def SellStr4 = ((high("period"=BuySellStrAggregation4) - close("period"=BuySellStrAggregation4))/(high("period"=BuySellStrAggregation4) - low("period"=BuySellStrAggregation4)))*100;


def RelDayVol = DayVol / AvgDayVol;
def RelPrevDayVol = DayVol / volume("period" = AggregationPeriod.DAY)[1];
#Daily aggregation volume labels
AddLabel(if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else if ShowDayVolLabel then 1 else 0, "DayVol: " + DayVol +" / "+ Round(RelDayVol,2) + "x Avg("+ AvgDayVolLength +") / "+ Round(RelPrevDayVol,2) +"x Prev", if DayVol > AvgDayVol then VolColor.Color("VolAvg") else VolColor.Color("Below Average"));

def RelVol = Vol/VolAvg;
def RelPrevVol = volume / volume[1];

#current aggregation's volume labels
AddLabel(ShowBarVolLabel, "Vol: " + volume +" / "+ Round(RelVol,2) + "x Avg("+ AvgVolLength +") / "+ Round(RelPrevVol,2)+"x Prev",  if vol > VolSigma3 then VolColor.Color("VolSigma3") else if Vol > VolSigma2 then VolColor.Color("VolSigma2") else if Vol > VolAvg then VolColor.Color("VolAvg") else VolColor.Color("Below Average"));

#current candle Buy/Sell strength labels
##AddLabel(if ShowBuySellStrength then 1 else 0, "1", Color.GRAY);
#AddLabel(if ShowBuySellStrength then 1 else 0, "Sell "+Round(SellStr,2) +"%", if SellStr > BuyStr then Color.RED else Color.pink);
#AddLabel(if ShowBuySellStrength then 1 else 0, "Buy "+Round(BuyStr,2) +"%", if BuyStr > SellStr then Color.GREEN else Color.light_GREEN);

#2nd Aggregation Buy/Sell strength labels
#AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, " ", Color.BLACK);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, "5m", Color.GRAY);
#AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, "Sell "+Round(SellStr2,2) +"%", if SellStr2 > BuyStr2 then Color.RED else Color.pink);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation2 or !ShowBuySellStrength then 0 else 1, "Buy "+Round(BuyStr2,2) +"%", if BuyStr2 > SellStr2 then Color.cyan else Color.red);

#2nd Aggregation Buy/Sell strength labels
#AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation4 or !ShowBuySellStrength then 0 else 1, " ", Color.BLACK);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation4 or !ShowBuySellStrength then 0 else 1, "1H", Color.GRAY);
#AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation3 or !ShowBuySellStrength then 0 else 1, "DSell "+Round(SellStr3,2) +"%", if SellStr3 > BuyStr3 then Color.RED else Color.pink);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation4 or !ShowBuySellStrength then 0 else 1, "Buy "+Round(BuyStr4,2) +"%", if BuyStr4 > SellStr4 then Color.cyan else Color.red);

#2nd Aggregation Buy/Sell strength labels
#AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation3 or !ShowBuySellStrength then 0 else 1, " ", Color.BLACK);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation3 or !ShowBuySellStrength then 0 else 1, "Day", Color.GRAY);
#AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation3 or !ShowBuySellStrength then 0 else 1, "DSell "+Round(SellStr3,2) +"%", if SellStr3 > BuyStr3 then Color.RED else Color.pink);
AddLabel(if GetAggregationPeriod() >= BuySellStrAggregation3 or !ShowBuySellStrength then 0 else 1, "Buy "+Round(BuyStr3,2) +"%", if BuyStr3 > SellStr3 then Color.cyan else Color.red);

I modified the script to change aggregationperiod2 from 30 miinutes to an input number of bars, currently set to 5.

Screenshot-2021-08-29-163655.jpg
Ruby:
#Advanced Volume Study
#[email protected]
#v5.22.2020
declare on_volume;
input ShowVolumeAsCandlesticks = no;
input ShowBuySellStrengthOnVolumeBars = no;
input ShowBuySellStrength2ndAgg = no;
input AvgDayVolLength = 5;
input AvgVolLength = 20;
input ShowDayVolLabel = yes;
input ShowBarVolLabel = yes;
input ShowEthTotalVol = no;
input ShowBuySellStrength = yes;

#----------------------------------------
input begin = 0930;
input bars  = 5;
def bar     = if SecondsTillTime(begin) == 0 and
                 SecondsFromTime(begin) == 0
              then 0
              else bar[1] + 1;
def BuySellStrAgg2 = bar % bars == 0;# AggregationPeriod.THIRTY_MIN;
#----------------------------------------

input VolAverageType = AverageType.SIMPLE;
#if ShowBuySellStrengthOnVolumeBars is toggled on then the following volume bar paint options will not show, only the VolSignal Triangle set at the top of the bars will be painting according to volume average levels.
input PaintAboveAvgVolBars = yes;
input PaintAccordingToRelPrevVol = yes;
input RelativetoPrevVolTolerance = 1.25; #if volume is 1.25x greater than previous bar it will paint even if it is still below the average/sigma2/sigma3
input PaintBelowAvgVol = yes;
input PaintPriceAsVol = no;
input ShowVerticalTickLines = yes;
def ShowVertLines = if ShowVerticalTickLines and GetAggregationPeriod() < AggregationPeriod.DAY then 1 else 0;
input TickLevel = 1000;
input ShowTickLabel = yes;


def NA = Double.NaN;
def PriceRange = high - low;
def TopShadowRange = if open >= close then high - open else high - close;
def BottomShadowRange = if open <= close then open - low else close - low;
def BodyRange = PriceRange - (TopShadowRange + BottomShadowRange);
def VolumeTopShadowValue = (1 - (TopShadowRange / PriceRange)) * volume;
def VolumeBottomShadowValue = ((BottomShadowRange / PriceRange) * volume);
def BodyRangeVolValue = ((BodyRange + BottomShadowRange) / PriceRange) * volume;
#def DayVolAgg = if GetAggregationPeriod() < AggregationPeriod.DAY then AggregationPeriod.DAY else if GetAggregationPeriod() >= AggregationPeriod.DAY then AggregationPeriod.WEEK else GetAggregationPeriod();
def DayVolAgg = if GetAggregationPeriod() < AggregationPeriod.DAY then AggregationPeriod.DAY else GetAggregationPeriod();
def DayVol = volume("period" = DayVolAgg);
def AvgDayVol = Average(DayVol, AvgDayVolLength);
def Start = 0930;
def End = 1600;
def conf = SecondsFromTime(Start) >= 0 and SecondsFromTime(End) <= 0;

plot VolColor = NA;
VolColor.DefineColor("Bullish", Color.GREEN);
VolColor.DefineColor("Bearish", Color.RED);
VolColor.DefineColor("VolAvg", CreateColor(0, 100, 200));
VolColor.DefineColor("VolSigma2", Color.DARK_ORANGE);
VolColor.DefineColor("VolSigma3", Color.MAGENTA);
VolColor.DefineColor("Relative to Prev", Color.YELLOW);
VolColor.DefineColor("Below Average", Color.GRAY);
VolColor.DefineColor("ETH TVOL", Color.GRAY);
VolColor.DefineColor("TICK Vert", Color.GRAY);

#Current Candle Buy and Sell Strength
def BuyStr = ((close - low) / PriceRange) * 100;
def SellStr = ((high - close) / PriceRange) * 100;

def bar2count = if bar % bars == 0 then bar2count[1] + 1 else bar2count[1];

def high2   = if bar2count != bar2count[1] then Highest(high, bars) else Double.NaN;
def close2  = if bar2count != bar2count[1] then close else Double.NaN;
def low2    = if bar2count != bar2count[1] then Lowest(low, bars) else Double.NaN;
def volume2 = if bar2count != bar2count[1] then Highest(volume, bars) else Double.NaN;
plot x2 = high2;
x2.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
def BuyStr2 = (close2 - low2) / (high2 - low2) * 100;
def SellStr2 = ((high2 - close2) / (high2 - low2)) * 100;

plot BuyVol = if ShowBuySellStrengthOnVolumeBars then (BuyStr / 100) * volume else NA;
def SellVol = (SellStr / 100) * volume;
def BuyVol2 = (BuyStr2 / 100) * volume2;
def SellVol2 = (SellStr2 / 100) * volume2;
AddCloud(if ShowBuySellStrength2ndAgg then BuyVol2 else NA, 0, VolColor.Color("Bullish"), VolColor.Color("Bullish"), yes);
AddCloud(if ShowBuySellStrength2ndAgg then volume2 else NA, BuyVol2, VolColor.Color("Bearish"), VolColor.Color("Bearish"), yes);

BuyVol.SetDefaultColor(VolColor.Color("Bullish"));
BuyVol.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
#BuyVol2.SetDefaultColor(Color.GREEN);
#BuyVol2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#SellVol2.SetDefaultColor(Color.GREEN);
#SellVol2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot Vol = volume;
plot VolumeBottom = if !ShowVolumeAsCandlesticks or ShowBuySellStrengthOnVolumeBars then NA else VolumeBottomShadowValue;
plot VolumeBody = if !ShowVolumeAsCandlesticks or ShowBuySellStrengthOnVolumeBars then NA else BodyRangeVolValue;

VolumeBottom.HideTitle();
VolumeBody.HideTitle();

plot VolAvg = MovingAverage(VolAverageType, volume, AvgVolLength);

#2Sigma and 3Sigma Vol Filter
def Num_Dev1 = 2.0;
def Num_Dev2 = 3.0;
def averageType = AverageType.SIMPLE;
def sDev = StDev(data = Vol, length = AvgVolLength);

plot VolSigma2 = VolAvg + Num_Dev1 * sDev;
plot VolSigma3 = VolAvg + Num_Dev2 * sDev;


def RelDayVol = DayVol / AvgDayVol[1];
def RelPrevDayVol = DayVol / volume("period" = DayVolAgg)[1];
#Daily aggregation volume labels
AddLabel(if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else if ShowDayVolLabel then 1 else 0, "DayVol: " + DayVol + " / " + Round(RelDayVol, 2) + "x Avg(" + AvgDayVolLength + ") / " + Round(RelPrevDayVol, 2) + "x Prev", if DayVol > AvgDayVol then VolColor.Color("VolAvg") else VolColor.Color("Below Average"));

def RelVol = Vol / VolAvg[1];
def RelPrevVol = volume / volume[1];

#Triangle Vol Signal
plot VolSignal = if Vol > VolSigma3 then volume else if Vol > VolSigma2 then volume else if Vol > VolAvg then volume else if RelPrevVol >= RelativetoPrevVolTolerance then volume else NA;

#current aggregation's volume labels
AddLabel(ShowBarVolLabel, "Vol: " + volume + " / " + Round(RelVol, 2) + "x Avg(" + AvgVolLength + ") / " + Round(RelPrevVol, 2) + "x Prev",  if Vol > VolSigma3 then VolColor.Color("VolSigma3") else if Vol > VolSigma2 then VolColor.Color("VolSigma2") else if Vol > VolAvg then VolColor.Color("VolAvg") else VolColor.Color("Below Average"));

#ETH Total Vol Label
def ETH_VOL = if !conf and conf[1] then volume else if !conf then CompoundValue(1, ETH_VOL[1] + volume, volume) else ETH_VOL[1];

AddLabel(if !ShowEthTotalVol then 0 else if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else 1, "ETH TVOL: " + ETH_VOL, VolColor.Color("ETH TVOL"));

#$TICK Vertical Lines
def tickc = close("$TICK");
def tickh = high("$TICK");
def tickl = low("$TICK");

AddVerticalLine(if ShowVertLines and (tickh > TickLevel) or ShowVertLines and (tickl < -TickLevel) then 1 else 0, if (tickh > TickLevel) then tickh else if (tickl < -TickLevel) then tickl else Double.NaN, VolColor.Color("TICK Vert"));

#$TICK Label
AddLabel(if ShowTickLabel then 1 else 0, "$TICK: " + tickc, if tickc > 0 then Color.GREEN else Color.RED);

#current candle Buy/Sell strength labels
AddLabel(if ShowBuySellStrength then 1 else 0, " ", Color.BLACK);
AddLabel(if ShowBuySellStrength then 1 else 0, "1", Color.GRAY);
AddLabel(if ShowBuySellStrength then 1 else 0, "Sell " + Round(SellStr, 2) + "%", if SellStr > BuyStr then Color.RED else Color.DARK_RED);
AddLabel(if ShowBuySellStrength then 1 else 0, "Buy " + Round(BuyStr, 2) + "%", if BuyStr > SellStr then Color.GREEN else Color.DARK_GREEN);

#2nd Aggregation Buy/Sell strength labels
AddLabel(if ShowBuySellStrength then 1 else 0, "Check BuySellAgg2 > Current Agg", Color.YELLOW);
AddLabel(if !ShowBuySellStrength then 0 else 1, " ", Color.BLACK);
AddLabel(if !ShowBuySellStrength then 0 else 1, "2", Color.GRAY);
AddLabel(if !ShowBuySellStrength then 0 else 1, "Sell " + Round(SellStr2, 2) + "%", if SellStr2 > BuyStr2 then Color.RED else Color.DARK_RED);
AddLabel(if !ShowBuySellStrength then 0 else 1, "Buy " + Round(BuyStr2, 2) + "%", if BuyStr2 > SellStr2 then Color.GREEN else Color.DARK_GREEN);

VolumeBottom.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
VolumeBottom.AssignValueColor(Color.BLACK);
VolumeBody.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);

VolumeBody.AssignValueColor(if PaintAboveAvgVolBars and Vol > VolSigma3 then VolColor.Color("VolSigma3") else if PaintAboveAvgVolBars and Vol > VolSigma2 then VolColor.Color("VolSigma2") else if PaintAboveAvgVolBars and Vol > VolAvg then VolColor.Color("VolAvg") else if PaintAccordingToRelPrevVol and RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else if  PaintBelowAvgVol and Vol < VolAvg then VolColor.Color("Below Average") else if close > open then VolColor.Color("Bullish") else VolColor.Color("Bearish"));
#VolumeTop.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
#VolumeTop.AssignValueColor(if close > open then Color.Black else Color.Black);

VolAvg.SetDefaultColor(VolColor.Color("VolAvg"));
VolSigma2.SetDefaultColor(VolColor.Color("VolSigma2"));
VolSigma3.SetDefaultColor(VolColor.Color("VolSigma3"));

Vol.SetPaintingStrategy(if !ShowVolumeAsCandlesticks or ShowBuySellStrengthOnVolumeBars then PaintingStrategy.SQUARED_HISTOGRAM else PaintingStrategy.HISTOGRAM);

Vol.SetLineWeight(1);
Vol.AssignValueColor(if ShowBuySellStrengthOnVolumeBars then VolColor.Color("Bearish") else if PaintAboveAvgVolBars and volume > VolSigma3 then VolColor.Color("VolSigma3") else if PaintAboveAvgVolBars and volume > VolSigma2 then VolColor.Color("VolSigma2") else if PaintAboveAvgVolBars and volume > VolAvg then VolColor.Color("VolAvg") else if PaintAccordingToRelPrevVol and RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else if PaintBelowAvgVol and volume < VolAvg then VolColor.Color("Below Average") else if close > open then VolColor.Color("Bullish") else VolColor.Color("Bearish"));

AssignPriceColor(if PaintPriceAsVol and PaintAboveAvgVolBars and volume > VolSigma3 then VolColor.Color("VolSigma3") else if PaintPriceAsVol and PaintAboveAvgVolBars and volume > VolSigma2 then VolColor.Color("VolSigma2") else if PaintPriceAsVol and PaintAboveAvgVolBars and volume > VolAvg then VolColor.Color("VolAvg") else if PaintPriceAsVol and PaintAccordingToRelPrevVol and RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else if PaintPriceAsVol and PaintBelowAvgVol and volume < VolAvg then VolColor.Color("Below Average") else Color.CURRENT);

VolSignal.AssignValueColor(if Vol > VolSigma3 then VolColor.Color("VolSigma3") else if Vol > VolSigma2 then VolColor.Color("VolSigma2") else if Vol > VolAvg then VolColor.Color("VolAvg") else if RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else VolColor.Color("Below Average"));
VolSignal.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
VolAvg.SetLineWeight(2);
VolSignal.SetLineWeight(1);



########################################
#Buy/Sell Bubbles

input show_buy_sell_bubbles = yes;
input bubblemover = 0;
def b = bubblemover;
def b1 = b + 1;

def periods     = bar % bars == 0;# if isnan(close(period=buysellstrAggregation2)) then periods[1] else close(period=buysellstrAggregation2);
def periodCount = CompoundValue(1, if periods then periodCount[1] + 1 else periodCount[1], 0);
def thisperiod  = (HighestAll(periodCount) - periodCount) ;

AddChartBubble(show_buy_sell_bubbles and thisperiod[b1] != thisperiod[b], VolSigma2[b], Round(SellStr2) + "%", Color.RED);
AddChartBubble(show_buy_sell_bubbles and thisperiod[b1] != thisperiod[b] , VolSigma2[b], Round(BuyStr2) + "%", Color.GREEN);
#######################################
 

SleepyZ

Well-known member
I modified the script to change aggregationperiod2 from 30 miinutes to an input number of bars, currently set to 5.

I have made optimization improvements to the code in the previous post, handled missing data situations, made option for barnumbers to start at first bar on chart or at the first bar of regular trading hours input (0930), and location of and coloring of buy/sell bubbles at the 'bar aggregation'.

Screenshot-2021-08-30-085535.jpg
Ruby:
#Advanced Volume Study
#[email protected]
#v5.22.2020
declare on_volume;
input ShowVolumeAsCandlesticks = no;
input ShowBuySellStrengthOnVolumeBars = no;
input ShowBuySellStrength2ndAgg = no;
input AvgDayVolLength = 5;
input AvgVolLength = 20;
input ShowDayVolLabel = yes;
input ShowBarVolLabel = yes;
input ShowEthTotalVol = no;
input ShowBuySellStrength = yes;

#----------------------------------------
input startcount = {default firstbar, rthbegin};
input begin = 0930;
input bars  = 5;
def bar     = if startcount == startcount.rthbegin and SecondsTillTime(begin) == 0 and
                 SecondsFromTime(begin) == 0
              then 1             
              else if close == First(close)
              then 1
              else bar[1] + 1;
def BuySellStrAgg2 = bar % bars == 0;# AggregationPeriod.THIRTY_MIN;
#----------------------------------------

input VolAverageType = AverageType.SIMPLE;
#if ShowBuySellStrengthOnVolumeBars is toggled on then the following volume bar paint options will not show, only the VolSignal Triangle set at the top of the bars will be painting according to volume average levels.
input PaintAboveAvgVolBars = yes;
input PaintAccordingToRelPrevVol = yes;
input RelativetoPrevVolTolerance = 1.25; #if volume is 1.25x greater than previous bar it will paint even if it is still below the average/sigma2/sigma3
input PaintBelowAvgVol = yes;
input PaintPriceAsVol = no;
input ShowVerticalTickLines = yes;
def ShowVertLines  = if ShowVerticalTickLines and GetAggregationPeriod() < AggregationPeriod.DAY then 1 else 0;
input TickLevel     = 1000;
input ShowTickLabel = yes;


def NA = Double.NaN;
def h  = high;
def l  = low;
def c  = close;
def o  = open;
def v  = volume;
def PriceRange              = h - l;
def TopShadowRange          = if o >= c then h - o else h - c;
def BottomShadowRange       = if o <= c then o - l else c - l;
def BodyRange               = PriceRange - (TopShadowRange + BottomShadowRange);
def VolumeTopShadowValue    = (1 - (TopShadowRange / PriceRange)) * v;
def VolumeBottomShadowValue = ((BottomShadowRange / PriceRange) * v);
def BodyRangeVolValue       = ((BodyRange + BottomShadowRange) / PriceRange) * v;
def DayVolAgg               = if GetAggregationPeriod() < AggregationPeriod.DAY then AggregationPeriod.DAY else GetAggregationPeriod();
def DayVol    = volume("period" = DayVolAgg);
def AvgDayVol = Average(DayVol, AvgDayVolLength);
def Start     = 0930;
def End       = 1600;
def conf      = SecondsFromTime(Start) >= 0 and SecondsFromTime(End) <= 0;

plot VolColor = NA;
VolColor.DefineColor("Bullish", Color.GREEN);
VolColor.DefineColor("Bearish", Color.RED);
VolColor.DefineColor("VolAvg", CreateColor(0, 100, 200));
VolColor.DefineColor("VolSigma2", Color.DARK_ORANGE);
VolColor.DefineColor("VolSigma3", Color.MAGENTA);
VolColor.DefineColor("Relative to Prev", Color.YELLOW);
VolColor.DefineColor("Below Average", Color.GRAY);
VolColor.DefineColor("ETH TVOL", Color.GRAY);
VolColor.DefineColor("TICK Vert", Color.GRAY);

#Current Candle Buy and Sell Strength
def BuyStr    = ((c - l) / PriceRange) * 100;
def SellStr   = ((h - c) / PriceRange) * 100;

def bar2count = if bar % bars == 0
                then bar2count[1] + 1
                else bar2count[1];
def high2     = if IsNaN(h)
                then high2[1]
                else if bar2count != bar2count[1]
                then Highest(h, bars) else Double.NaN;
def close2    = if IsNaN(c)
                then close2[1]
                else if bar2count != bar2count[1]
                then c else Double.NaN;
def low2      = if IsNaN(l)
                then low2[1]
                else  if bar2count != bar2count[1]
                then Lowest(l, bars) else Double.NaN;
def volume2   = if IsNaN(v)
                then volume2[1]
                else if bar2count != bar2count[1]
                then Highest(v, bars) else Double.NaN;
def v2  = volume2;

def BuyStr2  = if IsNaN((close2 - low2) / (high2 - low2) * 100)
               then BuyStr2[1]
               else (close2 - low2) / (high2 - low2) * 100;
def SellStr2 = if IsNaN((high2 - close2) / (high2 - low2) * 100)
               then SellStr2[1]
               else (high2 - close2) / (high2 - low2) * 100;

plot BuyVol = if ShowBuySellStrengthOnVolumeBars then (BuyStr / 100) * v else NA;
def SellVol = (SellStr / 100) * v;
def BuyVol2 = (BuyStr2 / 100) * volume2;
def SellVol2 = (SellStr2 / 100) * volume2;
AddCloud(if ShowBuySellStrength2ndAgg then BuyVol2 else NA, 0, VolColor.Color("Bullish"), VolColor.Color("Bullish"), yes);
AddCloud(if ShowBuySellStrength2ndAgg then v2 else NA, BuyVol2, VolColor.Color("Bearish"), VolColor.Color("Bearish"), yes);

BuyVol.SetDefaultColor(VolColor.Color("Bullish"));
BuyVol.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
#BuyVol2.SetDefaultColor(Color.GREEN);
#BuyVol2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#SellVol2.SetDefaultColor(Color.GREEN);
#SellVol2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot Vol          = v;
plot VolumeBottom = if !ShowVolumeAsCandlesticks or ShowBuySellStrengthOnVolumeBars then NA else VolumeBottomShadowValue;
plot VolumeBody   = if !ShowVolumeAsCandlesticks or ShowBuySellStrengthOnVolumeBars then NA else BodyRangeVolValue;

VolumeBottom.HideTitle();
VolumeBody.HideTitle();

plot VolAvg = MovingAverage(VolAverageType, v, AvgVolLength);

#2Sigma and 3Sigma Vol Filter
def Num_Dev1          = 2.0;
def Num_Dev2          = 3.0;
def averageType       = AverageType.SIMPLE;
def sDev = StDev(data = Vol, length = AvgVolLength);

plot VolSigma2 = VolAvg + Num_Dev1 * sDev;
plot VolSigma3 = VolAvg + Num_Dev2 * sDev;


def RelDayVol     = DayVol / AvgDayVol[1];
def RelPrevDayVol = DayVol / volume("period" = DayVolAgg)[1];
#Daily aggregation volume labels
AddLabel(if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else if ShowDayVolLabel then 1 else 0, "DayVol: " + DayVol + " / " + Round(RelDayVol, 2) + "x Avg(" + AvgDayVolLength + ") / " + Round(RelPrevDayVol, 2) + "x Prev", if DayVol > AvgDayVol then VolColor.Color("VolAvg") else VolColor.Color("Below Average"));

def RelVol = Vol / VolAvg[1];
def RelPrevVol = v / v[1];

#Triangle Vol Signal
plot VolSignal = if Vol > VolSigma3 then v else if Vol > VolSigma2 then v else if Vol > VolAvg then volume else if RelPrevVol >= RelativetoPrevVolTolerance then v else NA;

#current aggregation's volume labels
AddLabel(ShowBarVolLabel, "Vol: " + v + " / " + Round(RelVol, 2) + "x Avg(" + AvgVolLength + ") / " + Round(RelPrevVol, 2) + "x Prev",  if Vol > VolSigma3 then VolColor.Color("VolSigma3") else if Vol > VolSigma2 then VolColor.Color("VolSigma2") else if Vol > VolAvg then VolColor.Color("VolAvg") else VolColor.Color("Below Average"));

#ETH Total Vol Label
def ETH_VOL = if !conf and conf[1] then v else if !conf then CompoundValue(1, ETH_VOL[1] + v, v) else ETH_VOL[1];

AddLabel(if !ShowEthTotalVol then 0 else if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else 1, "ETH TVOL: " + ETH_VOL, VolColor.Color("ETH TVOL"));

#$TICK Vertical Lines
def tickc = close("$TICK");
def tickh = high("$TICK");
def tickl = low("$TICK");

AddVerticalLine(if ShowVertLines and (tickh > TickLevel) or ShowVertLines and (tickl < -TickLevel) then 1 else 0, if (tickh > TickLevel) then tickh else if (tickl < -TickLevel) then tickl else Double.NaN, VolColor.Color("TICK Vert"));

#$TICK Label
AddLabel(if ShowTickLabel then 1 else 0, "$TICK: " + tickc, if tickc > 0 then Color.GREEN else Color.RED);

#current candle Buy/Sell strength labels
AddLabel(if ShowBuySellStrength then 1 else 0, " ", Color.BLACK);
AddLabel(if ShowBuySellStrength then 1 else 0, "1", Color.GRAY);
AddLabel(if ShowBuySellStrength then 1 else 0, "Sell " + Round(SellStr, 2) + "%", if SellStr > BuyStr then Color.RED else Color.DARK_RED);
AddLabel(if ShowBuySellStrength then 1 else 0, "Buy " + Round(BuyStr, 2) + "%", if BuyStr > SellStr then Color.GREEN else Color.DARK_GREEN);

#2nd Aggregation Buy/Sell strength labels
#AddLabel(if ShowBuySellStrength then 1 else 0, "Check BuySellAgg2 > Current Agg", Color.YELLOW);
AddLabel(if !ShowBuySellStrength then 0 else 1, " ", Color.BLACK);
AddLabel(if !ShowBuySellStrength then 0 else 1, "2", Color.GRAY);
AddLabel(if !ShowBuySellStrength then 0 else 1, "Sell " + Round(SellStr2, 2) + "%", if SellStr2 > BuyStr2 then Color.RED else Color.DARK_RED);
AddLabel(if !ShowBuySellStrength then 0 else 1, "Buy " + Round(BuyStr2, 2) + "%", if BuyStr2 > SellStr2 then Color.GREEN else Color.DARK_GREEN);

VolumeBottom.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
VolumeBottom.AssignValueColor(Color.BLACK);
VolumeBody.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);

VolumeBody.AssignValueColor(if PaintAboveAvgVolBars and Vol > VolSigma3 then VolColor.Color("VolSigma3") else if PaintAboveAvgVolBars and Vol > VolSigma2 then VolColor.Color("VolSigma2") else if PaintAboveAvgVolBars and Vol > VolAvg then VolColor.Color("VolAvg") else if PaintAccordingToRelPrevVol and RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else if  PaintBelowAvgVol and Vol < VolAvg then VolColor.Color("Below Average") else if c > o then VolColor.Color("Bullish") else VolColor.Color("Bearish"));
#VolumeTop.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
#VolumeTop.AssignValueColor(if close > open then Color.Black else Color.Black);

VolAvg.SetDefaultColor(VolColor.Color("VolAvg"));
VolSigma2.SetDefaultColor(VolColor.Color("VolSigma2"));
VolSigma3.SetDefaultColor(VolColor.Color("VolSigma3"));

Vol.SetPaintingStrategy(if !ShowVolumeAsCandlesticks or ShowBuySellStrengthOnVolumeBars then PaintingStrategy.SQUARED_HISTOGRAM else PaintingStrategy.HISTOGRAM);

Vol.SetLineWeight(1);
Vol.AssignValueColor(if ShowBuySellStrengthOnVolumeBars then VolColor.Color("Bearish") else if PaintAboveAvgVolBars and v > VolSigma3 then VolColor.Color("VolSigma3") else if PaintAboveAvgVolBars and v > VolSigma2 then VolColor.Color("VolSigma2") else if PaintAboveAvgVolBars and v > VolAvg then VolColor.Color("VolAvg") else if PaintAccordingToRelPrevVol and RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else if PaintBelowAvgVol and v < VolAvg then VolColor.Color("Below Average") else if c > o then VolColor.Color("Bullish") else VolColor.Color("Bearish"));

AssignPriceColor(if PaintPriceAsVol and PaintAboveAvgVolBars and v > VolSigma3 then VolColor.Color("VolSigma3") else if PaintPriceAsVol and PaintAboveAvgVolBars and v > VolSigma2 then VolColor.Color("VolSigma2") else if PaintPriceAsVol and PaintAboveAvgVolBars and v > VolAvg then VolColor.Color("VolAvg") else if PaintPriceAsVol and PaintAccordingToRelPrevVol and RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else if PaintPriceAsVol and PaintBelowAvgVol and v < VolAvg then VolColor.Color("Below Average") else Color.CURRENT);

VolSignal.AssignValueColor(if Vol > VolSigma3 then VolColor.Color("VolSigma3") else if Vol > VolSigma2 then VolColor.Color("VolSigma2") else if Vol > VolAvg then VolColor.Color("VolAvg") else if RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else VolColor.Color("Below Average"));
VolSignal.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
VolAvg.SetLineWeight(2);
VolSignal.SetLineWeight(1);



########################################
#Buy/Sell Bubbles

input show_buy_sell_bubbles = yes;
input bubblemover           = 0;
def b                       = bubblemover;
def b1                      = b + 1;

def periods     = bar % bars == 0;
def periodCount = CompoundValue(1, if periods then periodCount[1] + 1 else periodCount[1], 0);
def thisperiod  = (HighestAll(periodCount) - periodCount) ;

AddChartBubble(show_buy_sell_bubbles and thisperiod[b1] != thisperiod[b], VolAvg[b], Round(SellStr2) + "%", if Round(SellStr2) > Round(BuyStr2) then Color.RED else Color.GRAY);
AddChartBubble(show_buy_sell_bubbles and thisperiod[b1] != thisperiod[b] , VolAvg[b], Round(BuyStr2) + "%", if Round(SellStr2) > Round(BuyStr2) then Color.GRAY else Color.GREEN);
#######################################
 

Similar threads

Top