Well you can take the code provided in that previous post and plot it directly on a chart. Don’t know if you realize that. In my videos I have shown this many times. In fact whenever I am building a scan I tend to build the code in a chart study so I can clearly see what results the scan is returning.
So. This code is used as is:
input percent = 50.0;
def percentCloseWithinRange = (close - low) / (high - low);
plot scan = percentCloseWithinRange * 100 <= percent;
When plotted on the price graph, you will need to go into the study settings and adjust the plot to display as Boolean values. Screenshot below shows how to set that.
To add the chart label will take a few more lines. We need to count the number of qualifying bars. Then compute the percentage.
def sum = TotalSum(scan);
def percentWeak = Round(sum / BarNumber(), 3) * 100;
AddLabel(yes, Concat("Prct Weak: ", Concat(percentWeak, "%")), if percentWeak < 50.0 then Color.RED else if percentWeak > 70.0 then Color.GREEN else Color.WHITE);
Add those lines of code to the original batch and the indicator is complete. Second screenshot below shows the results. I suspect you have your percentages backwards. I suspect you actually want Less Than 50% to be green and Greater Than 70% to be weak. Just modify the AddLabel statement by exchanging the Color.RED and Color.GREEN.
Works great, one finally question.
On some stocks such as KOSS and STAF, the calculation is N/A and the label color is black?
What does that mean?
Thank you
Interesting. So what happens there is you have candles where the high and low are equal. And I did not include a check to prevent divide by zero. (Dividing by zero is impossible and results in an error). You can correct this by replacing the second line of code with this:
def percentCloseWithinRange = if (high – low) 0 then (close – low) / (high – low) else 0;
Hello Hahn, I am getting a invalid statement def at 2:1
Can you please provide the entire code complied again.
Best
For some reason my website decided to modify the text I posted. It removed the double equals sign in that last line of code I provided. I am not sure if this is going to happen again but I will try to post it here in it’s entirety. If the website breaks this again I’ll try to edit my answer and provide the updated code there.
input percent = 50.0;
def percentCloseWithinRange = if (high – low) == 0 then 0 else (close – low) / (high – low) ;
plot scan = percentCloseWithinRange * 100 <= percent;
def sum = TotalSum(scan);
def percentWeak = Round(sum / BarNumber(), 3) * 100;
AddLabel(yes, Concat("Prct Weak: ", Concat(percentWeak, "%")), if percentWeak < 50.0 then Color.RED else if percentWeak > 70.0 then Color.GREEN else Color.WHITE);
Sorry, while using this code for another post I discovered and error in my previous comment. It has been corrected.