Time Segmented Volume for ThinkorSwim

SolidChiken

New member
2019 Donor
Last edited:
Solution
Looking to turn this into a scan.
Has anyone tried to make a TSV scan?
Assuming i want to see when the tsv goes >0
Would i use this in the custom scan code
plot TSV = t;
t[-1],0 and t>0
Thoughts?
Has anyone built anything to share.
TIA
Here you go. Give it a shot!

Included plots:
  1. TSV_Bull
    1. t > 0 and t[1] < 0 and PAL (bullish MA momentum)
  2. TSV_Weak_Bull
    1. t > 0 and t[1] < 0 and PAL_Fail (no bullish MA momentum)
  3. TSV_Bear
    1. t < 0 and t[1] > 0 and PAS (bearish MA momentum)
  4. TSV_Weak_Bear
    1. t < 0 and t[1] > 0 and PAS_Fail (no bearish MA momentum)

Ruby:
## Time_Segmented_Volume_SCAN
##
##
## CREDITS
## Requested by @squatsandstonks from...

BenTen

Administrative
Staff member
Staff
VIP
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);
 

SolidChiken

New member
2019 Donor
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 ?

AgmrCM9.png
 
Last edited:

BenTen

Administrative
Staff member
Staff
VIP
@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);
 

horserider

Well-known member
VIP
@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.
 

horserider

Well-known member
VIP
@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.
 

SolidChiken

New member
2019 Donor
@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 ? 🙏
 

markos

Well-known member
VIP
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.
 

twall

New member
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)

6aAigj3.png
 
Last edited:

squatsandstonks

New member
VIP
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)

6aAigj3.png
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)
 

twall

New member
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:
 

squatsandstonks

New member
VIP
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
 

squatsandstonks

New member
VIP
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)

6aAigj3.png
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
 

cos251

Well-known member
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.

iM6dWik.jpg

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);
 

squatsandstonks

New member
VIP
@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
 

cos251

Well-known member
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.
 

squatsandstonks

New member
VIP
Try this.

iM6dWik.jpg

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
 

Similar threads

Top