Demand Index w/Moving Average

M

Mike

New member
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

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

tenacity11

Active member
2019 Donor
Curious at to what you find the best input length for the demand index is when trading on a 3m chart? TIA
 
markos

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

Mike

New member
@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"));
 

Similar threads

Top