Demand Index w/Moving Average

Mike

New member
VIP
Hello, Can anyone help me with the following thinkscript attempt to add a moving average (various types and lengths) to the Demand Index Indicator. My modified version of the Demand Index does not work correctly when changing input selections. Thanks!

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 = Average(DMIndx);

DMIndx.DefineColor("Above", Color.UPTICK);
DMIndx.DefineColor("Below", Color.DOWNTICK);
DMIndx.AssignValueColor(if DMIndx > DMIndx_Average then DMIndx.Color("Above") else DMIndx.Color("Below"));
 

YungTraderFromMontana

Well-known member
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"));
 

markos

Well-known member
VIP
@Mike Please edit the script above and insert the Header Information. We all need to know where it came from & the developer needs credit for their work. Thanks.
 

Mike

New member
VIP
@Mike Please edit the script above and insert the Header Information. We all need to know where it came from & the developer needs credit for their work. Thanks.

#YungTraderFromMontana

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

123 man

New member
Code:
#YungTraderFromMontana

#The DMI is especially useful for trend trading strategies because it differentiates between strong and weak trends, allowing the trader to enter only the ones with real momentum. DMI works on all time frames

#updated /  paint candles

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.RED);
DMIndx.AssignValueColor(if DMIndx > DMIndx_Average then DMIndx.Color("Above") else DMIndx.Color("Below"));


assignPriceColor(if   DMIndx < DMIndx_Average  then Color.RED else Color.UpTick);
 
Last edited:

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.
Top