Candle closes in lower half and compute percent of total


Category:
0
0

Hello Hahn,

Similar to this post “Find stocks which close specific percent below the high

The purpose of this post is an expansion of the request above. The question is, how do you create a chart study that labels candles which close specific percent below the high prices?

 

  1. The first part of this request is to create a chart indicator that labels candles that close half way (50%) between the range of the high and low of the daily bar, Aka would be in the middle of its daily range.
    1. If this condition is true, then plot a yellow dot at the top of the candle.

 

  1. The second part of this request is to create a label associated with part 1 (in the same study).
    1. The next label will be called “Percentage of Weak candles”
    2. This chart label will calculate the “Percentage of marked candles” in relation to the total amount of candles in the timeframe (1 year)
    3. If the percent is less than 50%= chart label red
    4. If the percent is greater than 70% chart label= green

 

 

Thank you

Marked as spam
Posted by (Questions: 22, Answers: 63)
Asked on August 22, 2018 11:55 am
355 views
0
Private answer

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.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4117)
Answered on August 23, 2018 8:23 am
0

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

( at August 23, 2018 9:11 am)
0

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;

( at August 23, 2018 9:44 am)
0

Hello Hahn, I am getting a invalid statement def at 2:1

Can you please provide the entire code complied again.

Best

( at August 24, 2018 12:28 am)
0

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

( at August 24, 2018 6:55 am)
0

Sorry, while using this code for another post I discovered and error in my previous comment. It has been corrected.

( at August 25, 2018 4:59 pm)