DMI and ADX bar counter


Category:
0
0

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
215 views
0
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: 4118)
Answered on June 29, 2020 11:05 am
0
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)
0
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)
0
i dont know how to adjust the style to "plot values at low"
( at June 30, 2020 5:58 am)
0
Im trying to get the numbers over the candles on top of the chart
( at June 30, 2020 6:05 am)
0

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: https://www.hahn-tech.com/ans/displaying-values-for-swing-high-and-low-pivots/

( at June 30, 2020 8:51 am)
0
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)
0
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)
0
If you wanted to add a negative argument to the same study how would you do it? def condition1= diPlus > diMinus and diPlus>20 and adx >18 def condition2= diMinus> DiPlus and diPlus > 20 and adx > 18; How would you count the negative cases and the positive ones on the same study?
( at June 30, 2021 8:15 pm)
0
A custom watchlist column is only able to display a single value. So you can either display the number of bars since one condition or the number of bars since the other condition. It is possible to count both and display one as a positive count and the other as a negative count, remember that you still can only print one value. But that would require a bit more complexity than I am able to provide here in this forum post. I have already spent the full 15 minutes providing the original solution.
( at June 30, 2021 8:58 pm)