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

I tried to modify this code to allow for a higher aggregation period to be used on a lower time frame chart (e.g., a two minute period on a one minute chart). I couldn't get it to work. This is what I tried:

Code:
## 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;

input agg = "aggregationperiod.two_min";
def t = Sum(if close(period="agg") > close(period="agg")[1] then (volume(period="agg") * (close(period="agg")-close(period="agg")[1])) else if close(period="agg") < close(period="agg")[1] then (volume(period="agg") *(close(period="agg")-close(period="agg")[1])) else 0,tsvLength);


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

Does anyone know why it's not working with this higher time frame modification?
 
I tried to modify this code to allow for a higher aggregation period to be used on a lower time frame chart (e.g., a two minute period on a one minute chart). I couldn't get it to work. This is what I tried:

Code:
## 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;

input agg = "aggregationperiod.two_min";
def t = Sum(if close(period="agg") > close(period="agg")[1] then (volume(period="agg") * (close(period="agg")-close(period="agg")[1])) else if close(period="agg") < close(period="agg")[1] then (volume(period="agg") *(close(period="agg")-close(period="agg")[1])) else 0,tsvLength);


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

Does anyone know why it's not working with this higher time frame modification?

You had unnecessary quotation marks in the following. The code in quotes was treated as text name rather than a timeframe.

Replace this
Ruby:
input agg = "aggregationperiod.two_min";

def t = Sum(if close(period="agg") > close(period="agg")[1] then (volume(period="agg") * (close(period="agg")-close(period="agg")[1])) else if close(period="agg") < close(period="agg")[1] then (volume(period="agg") *(close(period="agg")-close(period="agg")[1])) else 0,tsvLength);
With this
Ruby:
input agg = AggregationPeriod.TWO_MIN;
def t = Sum(if close(period = agg) > close(period = agg)[1] then (volume(period = agg) * (close(period = agg) - close(period = agg)[1])) else if close(period = agg) < close(period = agg)[1] then (volume(period = agg) * (close(period = agg) - close(period = agg)[1])) else 0, tsvLength);
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
393 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • 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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top