Time Based Volume Indicator for ThinkorSwim

BenTen

Administrative
Staff member
Staff
VIP
Volume is the second most important trading indicator, according to CustomizedTrading. Here is a quick snippet of their introductory to the indicator.

Time Based Volume completely eliminates the high volume distortion that occurs after the open of a market, just before the close of a market, and the low volume that occurs at market lunch times. Since the Time Based Volume Indicator is time based it will work on any intraday time based chart and symbol (including Forex).

Robert from Researchtrade was able to convert the Time Based Volume indicator over to ThinkorSwim. Here is the full script.

Notes:
  • Cyan = Bullish
  • Darker blue = Bullish smaller
  • Magenta = Bearish
  • Darker purple = Bearish smaller

ekndLKf.png


thinkScript Code

Rich (BB code):
# Time Based Volume
# Robert Payne
declare lower;
input LookBack = 20;
def nMinutes = GetAggregationPeriod() / 60000;
def nBars = RoundUp(390 / nMinutes, 0);

def pvSum = fold idx = 1 to LookBack + 1 with a=0 do a + GetValue(volume, idx * nBars, LookBack * nBars);

def pvAvg = pvSum / LookBack;
def VolPct = (volume / pvAvg) * 100;

plot avgLine = 100;
avgLine.SetDefaultColor(Color.GRAY);
avgLine.SetStyle(Curve.LONG_DASH);

def lastUp = if IsNaN(lastUp[1]) then 0 else if (close > open) then VolPct else lastUp[1];
def lastDn = if IsNaN(lastDn[1]) then 0 else if (close < open) then VolPct else lastDn[1];

plot Vol = VolPct;
Vol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Vol.DefineColor("Bullish", Color.CYAN);
Vol.DefineColor("Bullish Smaller", CreateColor(0, 128, 128));
Vol.DefineColor("Bearish", Color.MAGENTA);
Vol.DefineColor("Bearish Smaller", CreateColor(128, 0, 128));
Vol.AssignValueColor(if (close > open) and (VolPct > lastUp[1]) then Vol.Color("bullish" ) else if close > open then Vol.Color("bullish smaller" ) else if close < open and VolPct > lastDn[1] then Vol.Color("bearish" ) else Vol.Color("bearish smaller" ));
Vol.SetLineWeight(3);

Shareable Link

https://tos.mx/nxOBq1
 
Last edited:

netarchitech

Well-known member
VIP
Thanks, BenTen :) Given the importance of the relationship between Price and Volume, here is time based Volume and Price linked together by color:

AMif78D.png


Rich (BB code):
# source: Time Based Volume
# original author: Robert Payne

declare lower;

input LookBack = 20;
def nMinutes = GetAggregationPeriod() / 60000;
def nBars = RoundUp(390 / nMinutes, 0);

def pvSum = fold idx = 1 to LookBack + 1 with a=0 do a + GetValue(volume, idx * nBars, LookBack * nBars);

def pvAvg = pvSum / LookBack;
def VolPct = (volume / pvAvg) * 100;

plot avgLine = 100;
avgLine.SetDefaultColor(Color.GRAY);
avgLine.SetStyle(Curve.LONG_DASH);

def lastUp = if IsNaN(lastUp[1]) then 0 else if (close > open) then VolPct else lastUp[1];
def lastDn = if IsNaN(lastDn[1]) then 0 else if (close < open) then VolPct else lastDn[1];

plot Vol = VolPct;
Vol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Vol.SetLineWeight(3);
Vol.DefineColor("Bullish", Color.GREEN);
Vol.DefineColor("Bullish Smaller", Color.DARK_GREEN);
Vol.DefineColor("Bearish", Color.RED);
Vol.DefineColor("Bearish Smaller", Color.DARK_RED);
Vol.AssignValueColor(if (close > open) and (VolPct > lastUp[1]) then Vol.Color("bullish" ) else if close > open then Vol.Color("bullish smaller" ) else if close < open and VolPct > lastDn[1] then Vol.Color("bearish" ) else Vol.Color("bearish smaller" ));
AssignPriceColor(if (close > open) and (VolPct > lastUp[1]) then Vol.Color("bullish" ) else if close > open then Vol.Color("bullish smaller" ) else if close < open and VolPct > lastDn[1] then Vol.Color("bearish" ) else Vol.Color("bearish smaller" ));
Hope this helps!

Good Luck and Good Trading :)

 
Last edited:

San

Member
2019 Donor
Ben/Netarchitech, Can you confirm this will work 1day 1mintues? I see only on 5mts time frame.

 
Last edited:

San

Member
2019 Donor
I remove all my indicator and reset my chart setting till i don't see in 1 mts chart window. Attached snap shot.
8UzClYv.jpg


 
Last edited:

BenTen

Administrative
Staff member
Staff
VIP
@San I just checked again and it doesn't work for me on the 1m timeframe either. Sorry about that.

 
Last edited:

xfitzz

New member
Hi all. I am looking for assistance for code that scans the first hour of the day for the past twenty days and compares it to the first hour of today's volume.

Example: 9:30-10:30 volume for today, 9:30-10:30 volume for last Friday, etc. etc. for the past twenty (or specified) days. This way you can plot to see what % of volume today is returning compared to other opening days. Not just the last twenty hours.

Thank you.
 

xfitzz

New member
This plot only compares the last twenty hours. It does not compare the first hour of the day for the past twenty days. Any help?
 

xfitzz

New member
Use FB as an example on the 20D 1H plot. If you go into Excel and plot the volume from 9:30 to 10:30 for the past twenty days and average them out, you'll get an average of 4,537,000 (rounded). If this code is true, then today's volume from 9:30-10:30 would have been 140% (this morning we saw approximately 6,368,000 shares traded). BUT, this plot gives us a value of 333%...odd. So, I recorded hourly volume for the past 20 hours and took an average, which was 1,912,794. Today's morning volume? 3.33x that number. So, that proves that this is not taking the first hour of each day, but the last 20 hours. Read through the code and you can confirm this with the fold idx loop. Any help would be appreciated @BenTen
 

xfitzz

New member
Use FB as an example on the 20D 1H plot. If you go into Excel and plot the volume from 9:30 to 10:30 for the past twenty days and average them out, you'll get an average of 4,537,000 (rounded). If this code is true, then today's volume from 9:30-10:30 would have been 140% (this morning we saw approximately 6,368,000 shares traded). BUT, this plot gives us a value of 333%...odd. So, I recorded hourly volume for the past 20 hours and took an average, which was 1,912,794. Today's morning volume? 3.33x that number. So, that proves that this is not taking the first hour of each day, but the last 20 hours. Read through the code and you can confirm this with the fold idx loop. Any help would be appreciated @BenTen
@horserider any ideas here?
 

halcyonguy

Active member
VIP
This plot only compares the last twenty hours. It does not compare the first hour of the day for the past twenty days. Any help?

short answer, try turning off extended hours.

long answer
I think robert's study is working as intended. In my modified version of it, values are displayed that match up to values of previous bars.

Most stocks will have a different quantity of bars (trades) in after hours, from one day to the next. Because of this you can't count x bars back and get to the same time of day. Roberts volume study looks back x number of bars, it doesn't check for time of day. It is looking back multiples of 390, to find previous days data. It uses the chart time to determine how many bars exist during a day on the chart. A trading day has 390 min, 6.5 hrs.

I modified robert's study, so it will show values in bubbles, that can be compared. I changed the length from 20 to 3. That should be enough values to see what is going on. In the bubbles are several variables. It reads the volume from 3 bars in the past. I divided all the numbers by
1000, to reduce the quantity of digits displayed. The values should be unique enough to verify the data. A day has 6.5 hours. so if a chart is set to 1 hour, nbars will be 7 , 6.5 rounded up. Every 7th bar, the bubbles are yellow, to make it easier to find and compare the values.

the variables are, starting from the top,
volume0 , current bar volume
volume1 , a bar that is [1 * nbars] ago , using getvalue
volume2 , a bar that is [2 * nbars] ago , using getvalue
volume3 , a bar that is [3 * nbars] ago , using getvalue
sum of 3 volumes shown above
pvsum variable
pvavg variable
volpct

VolumeTimeBasedcompare_01
http://tos.mx/6iFLCn7

pic from 3/19/2021
VK5TxLI.jpg
 
Last edited:

Jeff1228

New member
short answer, try turning off extended hours.

long answer
I think robert's study is working as intended. In my modified version of it, values are displayed that mactch up to values of previous bars.

Most stocks will have a different quantity of bars (trades) in after hours, from one day to the next. Because of this you can't count x bars back and get to the same time of day. Roberts volume study looks back x number of bars, it doesn't check for time of day. It is looking back multiples of 390, to find previous days data. It uses the chart time to determine how many bars exist during a day on the chart. A trading day has 390 min, 6.5 hrs.

I modified robert's study, so it will show values in bubbles, that can be compared. I changed the length from 20 to 3. That should be enough values to see what is going on. In the bubbles are several variables. It reads the volume from 3 bars in the past. I divided all the numbers by
1000, to reduce the quantity of digits displayed. The values should be unique enough to verify the data. A day has 6.5 hours. so if a chart is set to 1 hour, nbars will be 7 , 6.5 rounded up. Every 7th bar, the bubbles are yellow, to make it easier to find and compare the values.

the variables are, starting from the top,
volume0 , current bar volume
volume1 , a bar that is [1 * nbars] ago , using getvalue
volume2 , a bar that is [2 * nbars] ago , using getvalue
volume3 , a bar that is [3 * nbars] ago , using getvalue
sum of 3 volumes shown above
pvsum variable
pvavg variable
volpct

VolumeTimeBasedcompare_01
http://tos.mx/6iFLCn7

pic from 3/19/2021
VK5TxLI.jpg


So in order to use Robert's study correctly, one has to set AggregationPeriod to DAY, and turn off extended hours, correct? How frequent is the code executed, real-time? Can I use it in an intraday scanner?

Thanks
 

Similar threads

Top