Demand Index For ThinkOrSwim

Mike

Member
VIP
Hello, Can anyone help me add a moving average (various types and lengths) to the Demand Index Indicator?
 
Last edited by a moderator:

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

Here you go

Code:
declare lower;
input length = 5;
input DMIndx_averageType = AverageType.EXPONENTIAL;
input DMIndx_average_length = 10;
def wClose = (high + low + 2 * close) * 0.25;
def wCRatio = (wClose - wClose[1]) / Min(wClose, wClose[1]);
def closeRatio = 3 * wClose / Average(Highest(high, 2) - Lowest(low, 2), length) * AbsValue(wCRatio);
def volumeRatio = volume / Average(volume, length);
def volumePerClose = volumeRatio / Exp(Min(88, closeRatio));
def buyP;
def sellP;
if (wCRatio > 0) {
buyP = volumeRatio;
sellP = volumePerClose;
} else {
buyP = volumePerClose;
sellP = volumeRatio;
}
def buyPres = if IsNaN(buyPres[1]) then 0 else ((buyPres[1] * (length - 1)) + buyP) / length;
def sellPres = if IsNaN(sellPres[1]) then 0 else ((sellPres[1] * (length - 1)) + sellP) / length;
def tempDI;
if ((((sellPres[1] * (length - 1)) + sellP) / length - ((buyPres[1] * (length - 1)) + buyP) / length) > 0) {
tempDI = - if (sellPres != 0) then buyPres / sellPres else 1;
} else {
tempDI = if (buyPres != 0) then sellPres / buyPres else 1;
}
plot DMIndx = if IsNaN(close) then Double.NaN else if tempDI < 0 then -1 - tempDI else 1 - tempDI;
plot ZeroLine = 0;
plot DMIndx_Average = Movingaverage(DMIndx_averageType, DMIndx, DMIndx_average_length);
DMIndx.DefineColor("Above", Color.UPTICK);
DMIndx.DefineColor("Below", Color.DOWNTICK);
DMIndx.AssignValueColor(if DMIndx > DMIndx_Average then DMIndx.Color("Above") else DMIndx.Color("Below"));
 
Curious at to what you find the best input length for the demand index is when trading on a 3m chart? TIA
 
Hi Team,

In the following study, I am trying to add a white vertical line at the point where the Demand Index line crosses the zero line (both to the upside and downside). Can somebody help?

Code:
#
# Charles Schwab & Co. (c) 2010-2025
#

declare lower;

input length = 5;

def wClose = (high + low + 2 * close) * 0.25;
def wCRatio = (wClose - wClose[1]) / Min(wClose, wClose[1]);
def closeRatio = 3 * wClose / Average(Highest(High, 2) - Lowest(Low, 2), length) * AbsValue(wCRatio);
def volumeRatio = Volume / Average(Volume, length);
def volumePerClose = volumeRatio / exp(Min(88, closeRatio));
def buyP;
def sellP;
if (wCRatio > 0) {
buyP = volumeRatio;
sellP = volumePerClose;
} else {
buyP = volumePerClose;
sellP = volumeRatio;
}
def buyPres = if IsNaN(buyPres[1]) then 0 else ((buyPres[1] * (length - 1)) + buyP) / length;
def sellPres = if IsNaN(sellPres[1]) then 0 else ((sellPres[1] * (length - 1)) + sellP) / length;
def tempDI;
if ((((sellPres[1] * (length - 1)) + sellP) / length - ((buyPres[1] * (length - 1)) + buyP) / length) > 0) {
tempDI = - if (sellPres != 0) then buyPres / sellPres else 1;
} else {
tempDI = if (buyPres != 0) then sellPres / buyPres else 1;
}
plot DMIndx = if IsNaN(close) then Double.NaN else if tempDI < 0 then -1 - tempDI else 1 - tempDI;
plot ZeroLine = 0;

DMIndx.setDefaultColor(GetColor(1));
ZeroLine.SetDefaultColor(GetColor(5));
 
Last edited by a moderator:
Add this to the bottom of the script

Code:
AddverticalLine(DMIndx crosses Zeroline);
 
This is awesome! Thank you so much, @BenTen.

Would it be possible to help me customize the script so it becomes an “upper study” that shows the “vertical line” only?
 
Last edited:
This is awesome! Thank you so much, @BenTen.

Would it be possible to help me customize the script so it becomes an “upper study” that shows the “vertical line” only?

Sure thing, here's the code

Code:
#
# Charles Schwab & Co. (c) 2010-2025
#

input length = 5;

def wClose = (high + low + 2 * close) * 0.25;
def wCRatio = (wClose - wClose[1]) / Min(wClose, wClose[1]);
def closeRatio = 3 * wClose / Average(Highest(High, 2) - Lowest(Low, 2), length) * AbsValue(wCRatio);
def volumeRatio = Volume / Average(Volume, length);
def volumePerClose = volumeRatio / exp(Min(88, closeRatio));
def buyP;
def sellP;
if (wCRatio > 0) {
buyP = volumeRatio;
sellP = volumePerClose;
} else {
buyP = volumePerClose;
sellP = volumeRatio;
}
def buyPres = if IsNaN(buyPres[1]) then 0 else ((buyPres[1] * (length - 1)) + buyP) / length;
def sellPres = if IsNaN(sellPres[1]) then 0 else ((sellPres[1] * (length - 1)) + sellP) / length;
def tempDI;
if ((((sellPres[1] * (length - 1)) + sellP) / length - ((buyPres[1] * (length - 1)) + buyP) / length) > 0) {
tempDI = - if (sellPres != 0) then buyPres / sellPres else 1;
} else {
tempDI = if (buyPres != 0) then sellPres / buyPres else 1;
}
def DMIndx = if IsNaN(close) then Double.NaN else if tempDI < 0 then -1 - tempDI else 1 - tempDI;
def ZeroLine = 0;

AddverticalLine(DMIndx crosses Zeroline);
 
Is there away to differentiate between bull and bearish?

Sadly no. It is not coded for bull and bearish.

The Demand Index is an oscillator. Like all oscillators, it is designed to detect trend/momentum.
The Demand Index blends price and volume to spot trend shifts as a forward-looking indicator.

Here is a summary for how the demand index was written to be used:
  • Divergence – if the Demand Index moves the opposite way of price. Possible reversal?
  • Big peaks, big moves – a high spike in the index could foreshadow a fresh price high.
  • Hidden weakness – price rising while the index drops? Possible market top?
  • Zero-line cross = when the index flips above or below zero, momentum change, but not necessarily direction change.
  • Flatlining around zero? – is weak price action, probably not sustainable.
  • Long-term divergence means big pivots – If the index and price part ways over time, a major top or bottom is brewing.
Like all oscillators, these zones are more likely to be meaningful when price is near support or resistance. Otherwise, like all oscillators, these identified points are generally not significant.
 
Last edited:
can we add an input for both UCL (upper control limit) and LCL (lower control limit) with red color upper and green color lower
 
can we add an input for both UCL (upper control limit) and LCL (lower control limit) with red color upper and green color lower

add this code snippet to your study:
Ruby:
input OB = .2;
input OS = -.2;

plot pOB = if IsNaN(close) then Double.NaN else OB;
       pOB.SetDefaultColor(color.red);

plot pOS = if IsNaN(close) then Double.NaN else OS;
       pOS.SetDefaultColor(color.green);
 
Sure thing, here's the code

Code:
#
# Charles Schwab & Co. (c) 2010-2025
#

input length = 5;

def wClose = (high + low + 2 * close) * 0.25;
def wCRatio = (wClose - wClose[1]) / Min(wClose, wClose[1]);
def closeRatio = 3 * wClose / Average(Highest(High, 2) - Lowest(Low, 2), length) * AbsValue(wCRatio);
def volumeRatio = Volume / Average(Volume, length);
def volumePerClose = volumeRatio / exp(Min(88, closeRatio));
def buyP;
def sellP;
if (wCRatio > 0) {
buyP = volumeRatio;
sellP = volumePerClose;
} else {
buyP = volumePerClose;
sellP = volumeRatio;
}
def buyPres = if IsNaN(buyPres[1]) then 0 else ((buyPres[1] * (length - 1)) + buyP) / length;
def sellPres = if IsNaN(sellPres[1]) then 0 else ((sellPres[1] * (length - 1)) + sellP) / length;
def tempDI;
if ((((sellPres[1] * (length - 1)) + sellP) / length - ((buyPres[1] * (length - 1)) + buyP) / length) > 0) {
tempDI = - if (sellPres != 0) then buyPres / sellPres else 1;
} else {
tempDI = if (buyPres != 0) then sellPres / buyPres else 1;
}
def DMIndx = if IsNaN(close) then Double.NaN else if tempDI < 0 then -1 - tempDI else 1 - tempDI;
def ZeroLine = 0;

AddverticalLine(DMIndx crosses Zeroline);
This is perfect! Thank you so much, @BenTen. Have a great weekend.
 
add this code snippet to your study:
Ruby:
input OB = .2;
input OS = -.2;

plot pOB = if IsNaN(close) then Double.NaN else OB;
       pOB.SetDefaultColor(color.red);

plot pOS = if IsNaN(close) then Double.NaN else OS;
       pOS.SetDefaultColor(color.green);
THANK YOU!!!
1739670461524.png
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
697 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