Conditionally Weighted and Ranked Averages for ThinkorSwim

Jman831

Member
Hey y'all, just wanted to share some example codes of what I call "Conditionally Weighted Averages" and "Conditionally Ranked Weighted Averages", a simple concept I came up with that can really allow anyone to come up with their own unique average indicators that are weighted on whatever conditions they deem most important. I honestly believe it will truly allow many people to find their own niche of an edge by coming up with their own conditions that they deem important in the weighting of price (or any data point for that matter). This isn't necessarily an entirely "new" concept, but rather an expansion on previous concepts of weighted averages like a "volume weighted average" or a "variable average", for example. So, we'll get right to it:

Q: What is a "Conditionally Weighted Average"/"Conditionally Ranked Weighted Average"?

A: A "Conditionally Weighted Average" is an average that weighs price based on specified conditions being either true or false within either the last so many bars specified by the user or every bar within the entire chart aggregation and length. A "Conditionally Ranked Weighted Average" is basically the same thing as a "Conditionally Weighted Average", the only difference being that each condition has it's own "rank" that affects the overall "rank" of the price being weighted.

Example code of a "Conditionally Weighted Average":
Code:
input PriceType = close;
input ConditionalLength = 12;

def cond1 = close > close[1];
def cond2 = volume > volume[1];
def cond3 = TrueRange(high, close, low) > TrueRange(high, close, low)[1];

def condtot = cond1 + cond2 + cond3 + 1;
def condwghtdprice = PriceType * condtot;
def avgcondtot = sum(condtot, ConditionalLength);
def avgpricetot = sum(condwghtdprice, ConditionalLength);
def condavg = avgpricetot / avgcondtot;

plot ConditionallyWeightedAverage = condavg;

This code represents a simple Conditionally Weighted Average where each condition is equal in weight. What the code does is it multiplies the price by the number of conditions that are true plus 1. The "+ 1" allows the average to function normally when none of the conditions are met, but if a single condition out of all 3 conditions is met, it adds 1. So if none of the defined conditions are met, then the code simply uses the selected PriceType by itself (PriceType * 1). If any of the conditions are met, it adds 1 for each condition, so if only a single condition is met then it will multiply the selected PriceType by 2 (number of conditions met + 1). If all conditions are met then it multiplies it by a max of 4 (number of conditions met + 1). You then get the fully weighted average by adding up each price that has been multiplied by it's sum of (conditions met + 1) with the last so many bars specified by the user and dividing that total by the total amount of (conditions met + 1) within those same bars. This really adds a dynamic to how you can weight an average based simply off of conditions that have or have not been met. And you can use this type of average to weight any kind of data, not just price (like volume or ATR for example).

Example code of a "Conditionally Ranked Weighted Average":
Code:
input PriceType = close;
input ConditionalLength = 12;

def cond1 = if close > close[1] then 3 else 0;
def cond2 = if volume > volume[1] then 2 else 0;
def cond3 = if TrueRange(high, close, low) > TrueRange(high, close, low)[1] then 1 else 0;

def condtot = cond1 + cond2 + cond3 + 1;
def condwghtdprice = PriceType * condtot;
def avgcondtot = sum(condtot, ConditionalLength);
def avgpricetot = sum(condwghtdprice, ConditionalLength);
def condavg = avgpricetot / avgcondtot;

plot ConditionallyWeightedAverage = condavg;

A "Conditionally Ranked Weighted Average" is the same thing, the only difference being that each condition has it's own weight within all of the conditions. In this example, "cond1" is ranked the highest with a rank of 3, meaning the selected PriceType is going to be multiplied by 3 + 1 = 4 (and divided partially by 4 within the total conditions met) if "cond1" is met, making "cond1" the most weighted condition out of all the conditions. "cond3" is ranked the lowest (besides 0 conditions being met which would equal 1) with only a multiplier of 1 + 1 = 2. This adds even more of a dynamic where you can rank your conditions accordingly to what you deem most important to least important. You can even give multiple conditions the same rank, while other conditions have a different rank.

As you can hopefully see, the possibilities are truly endless with these two types of averages, as you can weight anything based off of as many conditions (or non-conditions for that matter) as you please with as many different ranks as you deem necessary.

I'm truly excited to see what this community's response will be to these ideas and whether or not it will spark some new ideas within all of us individually. I actually stumbled upon this idea myself when I stumbled upon a built-in indicator for ThinkorSwim called "PriceVolumeRank" that gives each bar a rank based on the advance or decline of the price in relation to the advance or decline of the volume. I then made a custom average that was weighted based on the PriceVolumeRank which made me realize you could really do the same thing with any amount of conditions you personally specify.

Happy Trading!
- JMan
 
Last edited:

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