declare lower;
def TimeFrame = AggregationPeriod.FIFTEEN_MIN;
input length = 5;
def wClose =(high(period = TimeFrame) + low(period = TimeFrame) + 2 * close(period=TimeFrame)) * 0.25;
def wCRatio = (wClose - wClose[1]) / Min(wClose, wClose[1]);
def closeRatio = 3 * wClose / Average(Highest(High(period = TimeFrame), 2) - Lowest(Low(period = TimeFrame), 2), length) * AbsValue(wCRatio);
def volumeRatio = Volume(period = TimeFrame) / Average(Volume(period = TimeFrame), 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(period = TimeFrame)) then Double.NaN else if tempDI < 0 then -1 - tempDI else 1 - tempDI;
def ZeroLine = 0;
AddLabel(yes, "DemandIndex: " + DMIndx, if DMIndx>DMIndx[1] then Color.Green else Color.Red);
#DMIndx.setDefaultColor(GetColor(1));
#ZeroLine.SetDefaultColor(GetColor(5));