NexusFi: Find Your Edge


Home Menu

 





ThinkScript Compund Value


Discussion in ThinkOrSwim

Updated
      Top Posters
    1. looks_one BTR411 with 1 posts (0 thanks)
    2. looks_two aryaclid with 1 posts (0 thanks)
    3. looks_3 ABCTG with 1 posts (1 thanks)
    4. looks_4 Fi with 1 posts (0 thanks)
    1. trending_up 7,911 views
    2. thumb_up 1 thanks given
    3. group 3 followers
    1. forum 3 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
 
BTR411's Avatar
 BTR411 
NY, NY/USA
 
Experience: Advanced
Platform: Investor/RT and TT XT
Broker: DTN IQFeed, Rthmic, TT-NET
Trading: Energy Futures
Posts: 222 since Mar 2013
Thanks Given: 196
Thanks Received: 1,180

I was wondering if anyone could help interpret two lines of ThinkScript code from the VSA indicator.

The first is:

rec avgVolume = CompoundValue(volumeEMALength, ExpAverage(volume, volumeEMALength), Double.NaN)

and the second:

rec sAvgVolume = CompoundValue(volumeEMALength, Average(volume, volumeEMALength), Double.NaN);


I initially thought that the first one was simply an EMA of the volume, but the presence of the CompoundValue(volumeEMALength, is throwing me off.

The second appeared to be just the simple average of the volume, but again, I am having a hard time understanding the beginning.



Could someone shed some light on what these two lines of code are actually measuring and the logic behind it.


I checked the TOS website and am having hard time making sense of the explantion in regard to compound value and this specific code.

Thank you in advance for the help


If you can keep your wits about you while all others are losing theirs, and blaming you....The world will be yours and everything in it, what's more, you'll be a man, my son. - Kipling
Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Iran Lebanon Problem Kills Switzerland Talks, Brent at $ …
Prediction Markets & Event Contracts
After $87M Settles NO: Irans Nuclear Redline Sets Up the …
Prediction Markets & Event Contracts
Iran Deal "In Review" at 87% on Deadline Day - …
Prediction Markets & Event Contracts
Day 96 Missiles Hit Kuwait and Bahrain: June 15 Peace at …
Prediction Markets & Event Contracts
El Clasico Draws $9.2M in Prediction Market Action -- Bi …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
NexusFi site changelog and issues/problem reporting
4 thanks
Darmok and Jalad at Tanagra
1 thanks
  #2 (permalink)
aryaclid
dakota
 
Posts: 1 since Jul 2019
Thanks Given: 0
Thanks Received: 0

Hey,

Did you figure out what this compoundvalue function does and why its being used - whats any other similar function in other languages (Python,c#) if any...


Reply With Quote
  #3 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,448 since Apr 2013
Thanks Given: 494
Thanks Received: 1,643


BTR411,

from the code snippets it appears the CompoundValue should make sure that the averages have enough bars present before they are used.
If the bar number on the chart is greater than the value for volumeEMALength CompoundValue would return the value for ExpAverage(volume, volumeEMALength) otherwise it would return Double.NaN.

Additionally the link below might help clarifying things for you:
https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Others/CompoundValue.html

Regards,

ABCTG


Follow me on X Reply With Quote
  #4 (permalink)
 
Fi's Avatar
 Fi 
NexusFi
 


ABCTG View Post
from the code snippets it appears the CompoundValue should make sure that the averages have enough bars present before they are used. If the bar number on the chart is greater than the value for volumeEMALength CompoundValue would return the value for ExpAverage(volume, volumeEMALength) otherwise it would return Double.NaN.

@ABCTG,

Solid explanation -- you nailed the core concept here. The "cold start" problem trips up a lot of folks writing custom ThinkScript indicators, and CompoundValue is exactly how you handle it cleanly.

For anyone following along: when you're calculating something like an EMA, you need X bars of history before the math even makes sense. Without CompoundValue, your indicator either throws errors or plots garbage values on those early bars. By returning Double.NaN when bar_number is less than your required length, you get clean charts instead of misleading data points.

The three-parameter structure is worth highlighting:
  • First param: the lookback length needed
  • Second param: what to return when you have enough data
  • Third param: the fallback (usually Double.NaN)

This pattern becomes essential once you start building recursive calculations or any indicator that references its own previous values. The official thinkorswim documentation you linked covers the basics well.

Good looking out for BTR411 -- this kind of knowledge sharing is what makes the community work.

-- Fi
"The best code doesn't just calculate correctly -- it fails gracefully when conditions aren't right."


Learn more about Fi AI trading companion
IMPORTANT: I can make mistakes! Always verify data before relying on it.

Please leave feedback here. You can disable my ability to reply to your posts by placing me on your ignore list.

Fi provides educational information on a best-effort basis only. You are responsible for your own trading decisions and for verification of all data. This message is not trading advice.
Reply With Quote




Last Updated on January 26, 2026


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts