Add bar count to column?


Category:
0
0

see attached study.  currently returns a price number.  i’d much prefer a number indicating periods since last buy/sell change.  tried to do it on my own, using code of yours i found, but failed.  can you help?

would have put code here but it exceeds your onerous 1,200 character max.

Attachments:
Marked as spam
Posted by (Questions: 6, Answers: 3)
Asked on January 17, 2021 6:21 pm
108 views
0
Private answer

This question has already been asked multiple times in the forum. The process of counting the number of bars since a boolean condition was last true is the same regardless of what that boolean condition is based from.

So even though the following solution deals with a Hull Moving average crossover, the method to count bars since the condition was true is exactly the same:

https://www.hahn-tech.com/ans/display-bars-since-cross-above-hma/

The real trick, is to determine where that boolean variable is, or how it is defined.

I took a few minutes to review the code you provided but I was not able to determine exactly how this "buy/sell change" is determined. The only hint I had was the line of code that sets the background color of the custom watchlist column. So we have "close > halftrend" and "close < halftrend". If that is the condition that you consider "buy/sell change" then you can add the following lines to count the bars since each change and display that value in the column in place of the value.

Important Note: you will need to find the plot statement for the variable named halftrend and change that from plot to def.

def uptrend = close > halftrend;
def downtrend = close < halftrend;
rec countUp = if uptrend and !uptrend[1] then 1 else if uptrend then countUp[1] + 1 else 0;
rec countDown = if downtrend and !downtrend[1] then -1 else if downtrend then countDown[1] - 1 else 0;
plot data = if countUp <> 0 then countUp else if countDown <> 0 then countDown else 0;

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on January 18, 2021 10:26 am