# Time Segmented Volume for ThinkorSwim

#### SolidChiken

##### New member
2019 Donor
Last edited:
Solution
I attempted to replicate this with the following code:

# Time Segmented Value (TSV)
Declare lower;
input length = 1;

#TSV=(Sum( IIf( C > Ref(C,-1), V * ( C-Ref(C,-1) ),
# IIf( C < Ref(C,-1),-V * ( C-Ref(C,-1) ), 0 ) ) ,18));

def x = (if close > close[1] then volume *(close - close[1]) else 0);

def y = (if close < close[1] then -volume * (close-close[1]) else 0);

plot TSV = sum(x+y, length);

#Plot TSVAvg = Average( TSV,5);
TSV.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

I cannot for the life of me figure out why it is plotting so wrong. It looks nothing like the tradingview version, but to my knowledge, the code I used is the same.

Here is the tradingview version of the code, and it looks great on that...
This was pulled from the TSL archive.

11:07 UpTheCreek: TSV is proprietary to Worden. I dont believe the algo has ever been disclosed.
11:13 Wally: TSV_Replica https://tos.mx/P2Hgh1

Code:
``````# Time Segmented Value (TSV)
Declare lower;
input length = 18;

#TSV=(Sum( IIf( C > Ref(C,-1), V * ( C-Ref(C,-1) ),
# IIf( C < Ref(C,-1),-V * ( C-Ref(C,-1) ), 0 ) ) ,18));

plot TSV = sum( if close > close[1] then volume * (close - close[1]) else if close < close[1] then -1* volume *(close - close[1]) else 0, length);

Plot TSVAvg = Average( TSV,5);``````

Thank you @BenTen . Really appreciate it. Noticed it doesn't look the same as the one on the article. Do you know how to adjust the code to make it have a zero line ?

#### Attachments

• AgmrCM9.png
53.4 KB · Views: 103
Last edited:
@SolidChiken Here is the code for the Zero Line. Add it to the bottom of that script. Keep in mind that what I posted above is a replica of the indicator you wanted. May or may not work the same.

Code:
``````plot ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.GREEN);``````

@SolidChiken You do know you can give any name you want to an indicator. So what he calls TSV may very well be similar to an already existing one. Write it up nice and voila a proprietary indicator to sell your service. Since no calculations are shown we can only guess.
My best guess is OnBalanceVolume.

The one BenTen showed you has a scale starting at 0. No need for a zero line.

@SolidChiken I see now they also have that write up on OBV. So TSV must be different. Bad guess by me.

OBV is not BMP . ToS has a BalanceofMarketPower indicator.

I am going to play with this tomorrow when I have some spare time. I like the mystery of what TSV is. I am wondering about that web site. How they claim the indicators show dark pool activity is beyond me. Maybe someone more an expert on dark pools can help out.

What had me scratching my head was this "The Bollinger Band indicator employs two simple moving averages with two standard deviations. By applying the standard deviations to the moving averages, " . Makes me wonder about anything they say.

@horserider... I've tried the BoMP. However the indicator doesn't correspond to what I want to see in the chart.
The TSV code that we having here looks like not the same as the page. The zero line is staying at the bottom.
Anyone here can help to tweak it ?

@BenTen Awesome.. Thank you Ben.

Can this script work on currency pairs? I am looking for a volume indicator that can work with currency pairs.

Hi @gjervis Once you get in touch with support via the button on top of the screen and after you ask about thinkscript lounge, ask them to set up your free tour of the system with emphasis on currency pairs. You are entitled to a free personal tour. Have 10 questions ready so they stay on track with currency information. I don't recall any currency pair info on this site.

If you would rather speak on the phone, go back to Tutorials. There is a "Secret" phone number that I call for the Advanced Team.

I believe I found the correct indicator for TSV on the Tradingview site. Here is some brief info:

If the histogram (TSV) is greater than zero and greater than the moving average, price should be moving long and there will be a green box below the chart.
If TSV falls below the moving average while still being greater than zero, the trend may be exhausting and has been coded to read Price Action Long - FAILURE with a black x below the chart.

If the histogram (TSV) is less than zero and less than the moving average, price should be moving short and there will be a red box below the chart.
If TSV rises above the moving average while still being less than zero, the trend may be exhausting and has been coded to read Price Action Short - FAILURE with a black x below the chart.

At times, the moving average may be above zero while TSV is below zero or vice versa. In these situations the chart will indicate long or short based on whether or not TSV is greater or less than zero. It is possible a new trend may be forming as the moving average obviously lags, but also possible price is consolidating with little volume and causing TSV to oscillate close to zero.

Here is the code which they say is open source:

//@version=4
// Written by liw0 active on https://www.tradingview.com/u/liw0
// corrected version by vitelot December 2018 -- no charity required
// Updated/Enhanced version by @eylwithsteph inspired by @storma
// CREDITS: http://quant.stackexchange.com/questions/2816/how-to-calculate-time-segmented-volume
study("Enhanced Time Segmented Volume", shorttitle="TSV")

l = input(13, title="TSV Length")
l_ma = input(7, title="MA Length")

t = sum(close > close[1] ? volume * (close - close[1]) : close < close[1] ? volume * (close - close[1]) : 0, l)
m = sma(t, l_ma)

PAL = (t > m) and (t > 0)
PAL_fail = (t < m) and (t > 0)
PAS = (t < m) and (t < 0)
PAS_fail = (t > m) and (t < 0)

plot(t, color=color.red, style=plot.style_histogram, title="TSV")
plot(m, color=color.green, title="MA")

plotshape(PAL, title="Price Action Long", location=location.bottom, style=shape.square, size=size.auto, color=color.green, transp=80)
plotshape(PAS, title="Price Action Short", location=location.bottom, style=shape.square, size=size.auto, color=color.red, transp=80)
plotshape(PAL_fail, title="Price Action Long - FAILURE", location=location.bottom, style=shape.xcross, size=size.auto, color=color.black, transp=60)
plotshape(PAS_fail, title="Price Action Short - FAILURE", location=location.bottom, style=shape.xcross, size=size.auto, color=color.black, transp=60)

Last edited:
I believe I found the correct indicator for TSV on the Tradingview site. Here is some brief info:

If the histogram (TSV) is greater than zero and greater than the moving average, price should be moving long and there will be a green box below the chart.
If TSV falls below the moving average while still being greater than zero, the trend may be exhausting and has been coded to read Price Action Long - FAILURE with a black x below the chart.

If the histogram (TSV) is less than zero and less than the moving average, price should be moving short and there will be a red box below the chart.
If TSV rises above the moving average while still being less than zero, the trend may be exhausting and has been coded to read Price Action Short - FAILURE with a black x below the chart.

At times, the moving average may be above zero while TSV is below zero or vice versa. In these situations the chart will indicate long or short based on whether or not TSV is greater or less than zero. It is possible a new trend may be forming as the moving average obviously lags, but also possible price is consolidating with little volume and causing TSV to oscillate close to zero.

Here is the code which they say is open source:

//@version=4
// Written by liw0 active on https://www.tradingview.com/u/liw0
// corrected version by vitelot December 2018 -- no charity required
// Updated/Enhanced version by @eylwithsteph inspired by @storma
// CREDITS: http://quant.stackexchange.com/questions/2816/how-to-calculate-time-segmented-volume
study("Enhanced Time Segmented Volume", shorttitle="TSV")

l = input(13, title="TSV Length")
l_ma = input(7, title="MA Length")

t = sum(close > close[1] ? volume * (close - close[1]) : close < close[1] ? volume * (close - close[1]) : 0, l)
m = sma(t, l_ma)

PAL = (t > m) and (t > 0)
PAL_fail = (t < m) and (t > 0)
PAS = (t < m) and (t < 0)
PAS_fail = (t > m) and (t < 0)

plot(t, color=color.red, style=plot.style_histogram, title="TSV")
plot(m, color=color.green, title="MA")

plotshape(PAL, title="Price Action Long", location=location.bottom, style=shape.square, size=size.auto, color=color.green, transp=80)
plotshape(PAS, title="Price Action Short", location=location.bottom, style=shape.square, size=size.auto, color=color.red, transp=80)
plotshape(PAL_fail, title="Price Action Long - FAILURE", location=location.bottom, style=shape.xcross, size=size.auto, color=color.black, transp=60)
plotshape(PAS_fail, title="Price Action Short - FAILURE", location=location.bottom, style=shape.xcross, size=size.auto, color=color.black, transp=60)

I attempted to replicate this with the following code:

# Time Segmented Value (TSV)
Declare lower;
input length = 1;

#TSV=(Sum( IIf( C > Ref(C,-1), V * ( C-Ref(C,-1) ),
# IIf( C < Ref(C,-1),-V * ( C-Ref(C,-1) ), 0 ) ) ,18));

def x = (if close > close[1] then volume *(close - close[1]) else 0);

def y = (if close < close[1] then -volume * (close-close[1]) else 0);

plot TSV = sum(x+y, length);

#Plot TSVAvg = Average( TSV,5);
TSV.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

I cannot for the life of me figure out why it is plotting so wrong. It looks nothing like the tradingview version, but to my knowledge, the code I used is the same.

Here is the tradingview version of the code, and it looks great on that platform:

study("Time Segmented Volume", shorttitle="TSV")

l = input(13, title="Length")
l_ma = input(7, title="MA Length")

//t = sum(close>close[1]?volume*close-close[1]:close<close[1]?(volume*-1)*close-close:0,l)
// previous line is non sensical. The correct version follows
t = sum(close>close[1]?volume*(close-close[1]):close<close[1]?volume*(close-close[1]):0,l)
m = sma(t ,l_ma )

plot(t, color=red, style=histogram)
plot(m, color=green)

I attempted to replicate this with the following code:

# Time Segmented Value (TSV)
Declare lower;
input length = 1;

#TSV=(Sum( IIf( C > Ref(C,-1), V * ( C-Ref(C,-1) ),
# IIf( C < Ref(C,-1),-V * ( C-Ref(C,-1) ), 0 ) ) ,18));

def x = (if close > close[1] then volume *(close - close[1]) else 0);

def y = (if close < close[1] then -volume * (close-close[1]) else 0);

plot TSV = sum(x+y, length);

#Plot TSVAvg = Average( TSV,5);
TSV.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

I cannot for the life of me figure out why it is plotting so wrong. It looks nothing like the tradingview version, but to my knowledge, the code I used is the same.

Here is the tradingview version of the code, and it looks great on that platform:

study("Time Segmented Volume", shorttitle="TSV")

l = input(13, title="Length")
l_ma = input(7, title="MA Length")

//t = sum(close>close[1]?volume*close-close[1]:close<close[1]?(volume*-1)*close-close:0,l)
// previous line is non sensical. The correct version follows
t = sum(close>close[1]?volume*(close-close[1]):close<close[1]?volume*(close-close[1]):0,l)
m = sma(t ,l_ma )

plot(t, color=red, style=histogram)
plot(m, color=green)
Thanks for taking the time out to try and figure this out! Maybe some other eyes can figure out where the disconnect is at. I really got intrigued with this indicator due to this youtube:

Thanks for taking the time out to try and figure this out! Maybe some other eyes can figure out where the disconnect is at. I really got intrigued with this indicator due to this youtube:
yeah I figured that's where you learned about it....I saw the same video and it intrigued me as well. I'll let you know if I ever figure it out, Seems to work really well

I believe I found the correct indicator for TSV on the Tradingview site. Here is some brief info:

If the histogram (TSV) is greater than zero and greater than the moving average, price should be moving long and there will be a green box below the chart.
If TSV falls below the moving average while still being greater than zero, the trend may be exhausting and has been coded to read Price Action Long - FAILURE with a black x below the chart.

If the histogram (TSV) is less than zero and less than the moving average, price should be moving short and there will be a red box below the chart.
If TSV rises above the moving average while still being less than zero, the trend may be exhausting and has been coded to read Price Action Short - FAILURE with a black x below the chart.

At times, the moving average may be above zero while TSV is below zero or vice versa. In these situations the chart will indicate long or short based on whether or not TSV is greater or less than zero. It is possible a new trend may be forming as the moving average obviously lags, but also possible price is consolidating with little volume and causing TSV to oscillate close to zero.

Here is the code which they say is open source:

//@version=4
// Written by liw0 active on https://www.tradingview.com/u/liw0
// corrected version by vitelot December 2018 -- no charity required
// Updated/Enhanced version by @eylwithsteph inspired by @storma
// CREDITS: http://quant.stackexchange.com/questions/2816/how-to-calculate-time-segmented-volume
study("Enhanced Time Segmented Volume", shorttitle="TSV")

l = input(13, title="TSV Length")
l_ma = input(7, title="MA Length")

t = sum(close > close[1] ? volume * (close - close[1]) : close < close[1] ? volume * (close - close[1]) : 0, l)
m = sma(t, l_ma)

PAL = (t > m) and (t > 0)
PAL_fail = (t < m) and (t > 0)
PAS = (t < m) and (t < 0)
PAS_fail = (t > m) and (t < 0)

plot(t, color=color.red, style=plot.style_histogram, title="TSV")
plot(m, color=color.green, title="MA")

plotshape(PAL, title="Price Action Long", location=location.bottom, style=shape.square, size=size.auto, color=color.green, transp=80)
plotshape(PAS, title="Price Action Short", location=location.bottom, style=shape.square, size=size.auto, color=color.red, transp=80)
plotshape(PAL_fail, title="Price Action Long - FAILURE", location=location.bottom, style=shape.xcross, size=size.auto, color=color.black, transp=60)
plotshape(PAS_fail, title="Price Action Short - FAILURE", location=location.bottom, style=shape.xcross, size=size.auto, color=color.black, transp=60)

Is it just me, or is the below code doing the exact same thing whether close < close[1], or close >close[1] ???

t = sum(close > close[1] ? volume * (close - close[1]) : close < close[1] ? volume * (close - close[1]) : 0, l)

I just need a sanity check here

I attempted to replicate this with the following code:

# Time Segmented Value (TSV)
Declare lower;
input length = 1;

#TSV=(Sum( IIf( C > Ref(C,-1), V * ( C-Ref(C,-1) ),
# IIf( C < Ref(C,-1),-V * ( C-Ref(C,-1) ), 0 ) ) ,18));

def x = (if close > close[1] then volume *(close - close[1]) else 0);

def y = (if close < close[1] then -volume * (close-close[1]) else 0);

plot TSV = sum(x+y, length);

#Plot TSVAvg = Average( TSV,5);
TSV.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

I cannot for the life of me figure out why it is plotting so wrong. It looks nothing like the tradingview version, but to my knowledge, the code I used is the same.

Here is the tradingview version of the code, and it looks great on that platform:

study("Time Segmented Volume", shorttitle="TSV")

l = input(13, title="Length")
l_ma = input(7, title="MA Length")

//t = sum(close>close[1]?volume*close-close[1]:close<close[1]?(volume*-1)*close-close:0,l)
// previous line is non sensical. The correct version follows
t = sum(close>close[1]?volume*(close-close[1]):close<close[1]?volume*(close-close[1]):0,l)
m = sma(t ,l_ma )

plot(t, color=red, style=histogram)
plot(m, color=green)
Try this.

Ruby:
``````## Time_Segmented_Volume
##
##
## CREDITS
## Requested by @squatsandstonks from orignal source https://www.tradingview.com/chart/BTCUSD/TG3DTxTR-INDICATOR-TIME-SEGMENTED-VOLUME/
##
##
## Removing the header Credit credits and description is not permitted, any modification needs to be shared.
##
##
## V 1.0 :    @cos251 - Initial release per request from usethinkscript forum.  All logic has not been ported, only base idea
##       :

declare lower;

input tsvLength = 13;
input maLength = 7;

def t = Sum(if close > close[1] then (volume * (close-close[1])) else if close < close[1] then (volume *(close-close[1])) else 0,tsvLength);
def m = MovingAverage(AverageType.SIMPLE,t,maLength);

def PAL = (t > m) and (t > 0);
def PAL_Fail = (t < m) and (t > 0);
def PAS = (t <m) and (t < 0);
def PAS_Fail = (t > m) and (t < 0);

plot TSV = t;
TSV.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
TSV.AssignValueColor(if TSV > 0 then Color.MAGENTA else Color.CYAN);
TSV.SetLineWeight(4);
plot TSV_MA = m;
TSV_MA.SetDefaultColor(Color.GREEN);
plot zero = 0;

plot square = 0;
square.SetPaintingStrategy(PaintingStrategy.SQUARES);
square.SetDefaultColor(Color.DARK_GRAY);
square.AssignValueColor(if PAL then Color.GREEN else if PAL_Fail then Color.RED else if PAS then Color.YELLOW else if PAS_Fail then Color.YELLOW else Color.Current);
square.SetLineWeight(1);``````

@SolidChiken Here is the code for the Zero Line. Add it to the bottom of that script. Keep in mind that what I posted above is a replica of the indicator you wanted. May or may not work the same.

Code:
``````plot ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.GREEN);``````
Sorry to bother you, but are you at all famiiar enough with pinescript to know if if does something different with the volume function? The below line is in the tradingview indicator:

t = sum(close > close[1] ? volume * (close - close[1]) : close < close[1] ? volume * (close - close[1]) : 0, l)

it seems to be doing exactly the same thing regardless of whether close > or close < close[1]....and yet it plots the histogram correctly. I can't figure out what's going on and it's going to drive me insane

Is it just me, or is the below code doing the exact same thing whether close < close[1], or close >close[1] ???

t = sum(close > close[1] ? volume * (close - close[1]) : close < close[1] ? volume * (close - close[1]) : 0, l)

I just need a sanity check here
Seems that way to me, I coded it as is, I can modify. I noticed that went porting it. The reference site is also the same. If someone finds the correction please let me know.

Try this.

Ruby:
``````## Time_Segmented_Volume
##
##
## CREDITS
## Requested by @squatsandstonks from orignal source https://www.tradingview.com/chart/BTCUSD/TG3DTxTR-INDICATOR-TIME-SEGMENTED-VOLUME/
##
##
## Removing the header Credit credits and description is not permitted, any modification needs to be shared.
##
##
## V 1.0 :    @cos251 - Initial release per request from usethinkscript forum.  All logic has not been ported, only base idea
##       :

declare lower;

input tsvLength = 13;
input maLength = 7;

def t = Sum(if close > close[1] then (volume * (close-close[1])) else if close < close[1] then (volume *(close-close[1])) else 0,tsvLength);
def m = MovingAverage(AverageType.SIMPLE,t,maLength);

def PAL = (t > m) and (t > 0);
def PAL_Fail = (t < m) and (t > 0);
def PAS = (t <m) and (t < 0);
def PAS_Fail = (t > m) and (t < 0);

plot TSV = t;
TSV.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
TSV.AssignValueColor(if TSV > 0 then Color.MAGENTA else Color.CYAN);
TSV.SetLineWeight(4);
plot TSV_MA = m;
TSV_MA.SetDefaultColor(Color.GREEN);
plot zero = 0;

plot square = 0;
square.SetPaintingStrategy(PaintingStrategy.SQUARES);
square.SetDefaultColor(Color.DARK_GRAY);
square.AssignValueColor(if PAL then Color.GREEN else if PAL_Fail then Color.RED else if PAS then Color.YELLOW else if PAS_Fail then Color.YELLOW else Color.Current);
square.SetLineWeight(1);``````
Hey, I really appreciate you doing this, this is a lot closer than I got. I'm going to investigate further. I wonder if there are data anomalies between tradingview and TOS

Basically I've been looking at the balance of power and time segmented volume indicators. They are favored for their supposed ability to help identify institutional activity on a chart. The formula for time segmented volume involves mutiplying the current bar's volume by the difference between the close of the current bar and the previous bar. My idea is to make this calculation on the differences between the POINT OF CONTROL of each candle to see if it reveals anything additional. Besides this, point of control for each candle would be very valuable obviously

### Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
668 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• Add-ons and resources
• 1 full year of unlimited support

### Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?