DMI and ADX bar counter


Im trying to create a custom column on my watch list that will tell me how many candles in a row meet a certain criteria The counter would naturally start at 1 and keep going every 2 hr candle close it keeps meeting the criteria

The criteria is:  DMI+ > 25    DMI – < 20 and ADX is > 18

Any help on this would be appreciated

Marked as spam
Posted by (Questions: 4, Answers: 1)
Asked on June 29, 2020 9:54 am
Private answer

The following will display the number of bars since condition was first true regardless of what time frame you select.

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 dx = if (diPlus + diMinus > 0) then 100 * AbsValue(diPlus - diMinus) / (diPlus + diMinus) else 0;
def adx = MovingAverage(averageType, dx, length);
def condition = diPlus > 25 and diMinus < 20 and adx > 18;
rec countBars = if condition and !condition[1] then 1 else if condition then countBars[1] + 1 else 0;
plot data = countBars;

Marked as spam
Posted by (Questions: 37, Answers: 3223)
Answered on June 29, 2020 11:05 am
that works great thank you so much is there a way that I can put the numbers over the candles and also make them stay there even if the count gets reset back to zero So for instance if there is a stock that has 7 bars in a row that meet the parameters those 7 candles in a row would be marked 1 2 3 4 5 6 7 and then it stopped and those numbers would still stay there and show up again if a new count starts
( at June 29, 2020 6:30 pm)
Did you try it yet? Just add it to a new custom chart study and adjust the style to "plot values at low". Should work fine but I haven't tried it.
( at June 29, 2020 7:31 pm)
i dont know how to adjust the style to "plot values at low"
( at June 30, 2020 5:58 am)
Im trying to get the numbers over the candles on top of the chart
( at June 30, 2020 6:05 am)

Sorry but that is not even an option. Best you can get is value at low or value at high. This is a setting available to all plot on all chart studies. You may want to view the following post for a clue:

( at June 30, 2020 8:51 am)
got it this is exactly what i wanted you a genius now for perfection how can I make the color turn green in my watch list if the number is 1 and 2
( at June 30, 2020 7:33 pm)
In the future you need to include the full set of specifications up front. I have a limited amount of time available for free solutions in the Q&A forum and going back to modify code to meet changing specifications requires more time than I allow myself to spend.
( at July 1, 2020 8:22 am)