Here is how I would do it. I prefer to use the entire code for computing the DI+ and DI- values. That way I don't have to worry about Thinkorswim making changes to their DMI study.
input length = 14;
input averageType = AverageType.WILDERS;
def hiDiff = high - high[1];
def loDiff = low[1] - low;
def plusDM = if hiDiff > loDiff and hiDiff > 0 then hiDiff else 0;
def minusDM = if loDiff > hiDiff and loDiff > 0 then loDiff else 0;
def atr = MovingAverage(averageType, TrueRange(high, close, low), length);
def diPlus = 100 * MovingAverage(averageType, plusDM, length) / atr;
def diMinus= 100 * MovingAverage(averageType, minusDM, length) / atr;
def bullishCrossing = diPlus[1] < diMinus[1] and diPlus > diMinus;
def bearishCrossing = diMinus[1] < diPlus[1] and diMinus > diPlus;
rec countBullishBars = if bullishCrossing then 1 else if diPlus > diMinus then countBullishBars[1] + 1 else 0;
rec countBearishBars = if bearishCrossing then 1 else if diMinus > diPlus then countBearishBars[1] + 1 else 0;
plot data = Max(countBullishBars, countBearishBars);
AssignBackgroundColor(if diPlus > diMinus then Color.GREEN else Color.RED);