CompoundValue Function

darkejo

Member
This question may not seem like it, but I have spent many hours trying to figure this function out. Just when I think I have a firm understanding, and I'm retrieving exactly what I'd expect, the next moment I write a new script and the data is close, but too far off for me to feel comfortable using it.

Can someone please explain to me, without using code logic, exactly what the attached code is doing? Please do not refer me to any of the tutorials on here or the learning center at https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/CompoundValue. I have spent HOURS trying to fully understand this. Really all I need is for someone to correct what is wrong with my interpretation.

My interpretation:

Sets 'postmarket_volume' equal to what 'postmarket_volume' previously was PLUS the current minute's total volume. Because 'postmarket_volume' was never set to anything, due to 'after_hours_start' being 1600, the result should be the exact amount of volume from 1600 to the current time in the same day.

What I assume is happening, using 1m aggregation:

An iteration through the day, starting at 0400, looping through each minute. As soon as 1600 is hit (>=0 seconds from 1600 using the if statement), 'postmarket_volume' is set to 'volume+postmarket_volume[1]', with 'postmarket_volume' previously being 0. Since the else statement is never true, the else statement should never be used.

Code:
input after_hours_start = 1600;


rec postmarket_volume = CompoundValue(1, if secondsfromtime(after_hours_start) >=0 then volume+postmarket_volume[1] else postmarket_volume[1],0);


Thank you in advance.
 
Solution
This question may not seem like it, but I have spent many hours trying to figure this function out. Just when I think I have a firm understanding, and I'm retrieving exactly what I'd expect, the next moment I write a new script and the data is close, but too far off for me to feel comfortable using it.

Can someone please explain to me, without using code logic, exactly what the attached code is doing? Please do not refer me to any of the tutorials on here or the learning center at https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/CompoundValue. I have spent HOURS trying to fully understand this. Really all I need is for someone to correct what is wrong with my interpretation.

My interpretation:

Sets...
This question may not seem like it, but I have spent many hours trying to figure this function out. Just when I think I have a firm understanding, and I'm retrieving exactly what I'd expect, the next moment I write a new script and the data is close, but too far off for me to feel comfortable using it.

Can someone please explain to me, without using code logic, exactly what the attached code is doing? Please do not refer me to any of the tutorials on here or the learning center at https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/CompoundValue. I have spent HOURS trying to fully understand this. Really all I need is for someone to correct what is wrong with my interpretation.

My interpretation:

Sets 'postmarket_volume' equal to what 'postmarket_volume' previously was PLUS the current minute's total volume. Because 'postmarket_volume' was never set to anything, due to 'after_hours_start' being 1600, the result should be the exact amount of volume from 1600 to the current time in the same day.

What I assume is happening, using 1m aggregation:

An iteration through the day, starting at 0400, looping through each minute. As soon as 1600 is hit (>=0 seconds from 1600 using the if statement), 'postmarket_volume' is set to 'volume+postmarket_volume[1]', with 'postmarket_volume' previously being 0. Since the else statement is never true, the else statement should never be used.

Code:
input after_hours_start = 1600;


rec postmarket_volume = CompoundValue(1, if secondsfromtime(after_hours_start) >=0 then volume+postmarket_volume[1] else postmarket_volume[1],0);


Thank you in advance.


when i'm stuck, i add a chartbubble to display some variable values.
sometimes you see things you didn't expect.

yellow, the volume is the same, after midnight to day closing ( 3pm est) .
cyan, the volume after 3pm to midnight, changes bar to bar.

stating the obvious, just in case,
pick a chart with several days and extended hours on , like 5days 5min

Ruby:
def bn = barnumber();
input after_hours_start = 1600;

def postmarket_volume = CompoundValue(1, if secondsfromtime(after_hours_start) >=0 then volume+postmarket_volume[1] else postmarket_volume[1],0);

def y1 = highestall(high);
def y2 = lowestall(low);
def y3 = (y1+y2)/2;

def same = ( postmarket_volume[1] == postmarket_volume);

addchartbubble(!isnan(close), y3,
bn + "\n" +
postmarket_volume + "\n" +
(if same then "same vol" else "-")
, ( if same then color.yellow else color.cyan), no);
#


EOxPEVv.jpg



this might help with compoundvalue
https://usethinkscript.com/threads/help-with-script-cant-understand-how-it-works.9922/#post-89795
 
Solution
Thanks for the response.


This is exactly very similar to what I've been doing. My scripts work, I just exchange the variables until I'm seeing the data I know I should see (adding up the total volume and comparing it to my results). That leads me to believe I fully understand how it works. Using the same logic that gave me the results I expected to see in a previous script, eventually ends up giving me a different result in the next. I was just hoping someone would do a better job explaining the logic of this function than what is available on the learning center, or by trial and error. As I said, I've literally spent hours, got to be close to days now, cumulatively, doing this. Using the code you attached, it does appear to be working correctly, but it doesn't answer the question I have as to why I'm getting double the count when used in a watchlist.

It could just be the stocks I'm using these scripts on are unique and the data is just unavailable or inaccurately reported, as I'm using penny stocks. I'm really hoping someone can just confirm each parameter used in this function is being understood correctly.
 
Specifically, this is really all I need confirmed:

What I assume is happening, using 1m aggregation:

An iteration through the day, starting at 0400, looping through each minute. As soon as 1600 is hit (>=0 seconds from 1600 using the if statement), 'postmarket_volume' is set to 'volume+postmarket_volume[1]', with 'postmarket_volume' previously being 0. Since the else statement is never true, the else statement should never be used."

Edit: Your script actually just confirmed one thing: I don't quite have as good of a grasp on the code as a whole. My issue is not with compoundvalue, it's with HOW thinkscript works. I was under the impression the data was reset once a new day started and I had to use [1] to reference a previous day. I now see the value your post code has to offer and will head back to the learning center.
 
Last edited:
when i'm stuck, i add a chartbubble to display some variable values.
sometimes you see things you didn't expect.

yellow, the volume is the same, after midnight to day closing ( 3pm est) .
cyan, the volume after 3pm to midnight, changes bar to bar.

stating the obvious, just in case,
pick a chart with several days and extended hours on , like 5days 5min

Ruby:
def bn = barnumber();
input after_hours_start = 1600;

def postmarket_volume = CompoundValue(1, if secondsfromtime(after_hours_start) >=0 then volume+postmarket_volume[1] else postmarket_volume[1],0);

def y1 = highestall(high);
def y2 = lowestall(low);
def y3 = (y1+y2)/2;

def same = ( postmarket_volume[1] == postmarket_volume);

addchartbubble(!isnan(close), y3,
bn + "\n" +
postmarket_volume + "\n" +
(if same then "same vol" else "-")
, ( if same then color.yellow else color.cyan), no);
#


EOxPEVv.jpg



this might help with compoundvalue
https://usethinkscript.com/threads/help-with-script-cant-understand-how-it-works.9922/#post-89795
Thank you. I now understand.
 
Thank you. I now understand.

glad to hear you are making progress.
-------------------
in post3 you mentioned watchlist for the time. that is kind of important, to describe what environment you are working in.
i'm not sure why you see 2x data.
----------------

here is another chart ver, that can reset each day. it uses if then instead of compoundvalue

Code:
def bn = barnumber();
input after_hours_start = 1600;

def diffday = if getday() != getday()[1] then 1 else 0;

input reset_volume_each_day = yes;

def postmarket_volume = if bn == 1 then 0
 else if (diffday and reset_volume_each_day) then 0
 else if secondsfromtime(after_hours_start) >= 0 then (volume + postmarket_volume[1])
 else postmarket_volume[1];

#def postmarket_volume = CompoundValue(1, if secondsfromtime(after_hours_start) >=0 then volume+postmarket_volume[1] else postmarket_volume[1],0);

def y1 = highestall(high);
def y2 = lowestall(low);
def y3 = (y1+y2)/2;

def same = ( postmarket_volume[1] == postmarket_volume);

addchartbubble(!isnan(close), y3,
bn + "\n" +
postmarket_volume + "\n" +
(if same then "same vol" else "-")
, ( if same then color.yellow else color.cyan), no);
 

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
332 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