DMI counting bars after a crosss


Category:
0
0

Hi Pete, I am trying to build a watchlist column to count bars after a DMI “D+” cross over a “D-“. I was able to get it partially working where it tells me the number of bars past the cross in the positive but I can’t figure out how to get it to read the bars in the negative. Would you mind pointing me in the right direction? Thanks

 

def myvariable = DMI().”DI+” is greater than DMI().”DI-“;
assignbackgroundColor(if myvariable then color.dark_green else color.dark_red);
rec countBars = if myvariable and !myvariable[1] then 1 else if myvariable then countBars[1] + 1 else 0;
plot data = countBars;

Attachments:
RESOLVED
Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on January 23, 2025 5:07 pm
6 views
0
Private answer

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

Marked as spam
Posted by (Questions: 37, Answers: 4122)
Answered on January 23, 2025 5:09 pm