# plot count candles below highest high

#### DanielManahan

##### Member
I want to plot the count of the number of candles that a stock fails to break above all time highs such that each candle will have a plot point returning a value of zero if a new highest high is made and 1 if it fails, accumulating that count each candle that fails.

so if you look at QQQ as of 1/14/22 it should have a plot point value of 37 because for the last 37 stock trading days it has failed to break over 408.71 the latest highest high on the daily set on 11/22/22

if QQQ breaks over 408.71 the highest high, then the plot point resets to 0; if the next day it breaks again a new highest high then that too would have a plot point of zero.

this will be great to track certain instruments to see how long they take to let bag holders who top tick, cash out and break even.

I am still trying to research the syntax for controlling the count and how to implement Count = count +1;

I have the logic sketched out, I don't get the syntax.

declare lower;
declare once_per_bar;

input length = 252; #number of trading days in one year on the daily chart

def BreakOutTarget = Highest(high[1], length);

plot countFail = if high[0] < BreakOutTarget then countFail = countFail + 1, else 0;

# plots the number of days a stock fails to make a new yearly high on the daily chart. will reset the plot to 0 each day it makes a new high. and count up one point for each day it fails.

Solution
@DanielManahan
Def countFail = if high >= BreakOutTarget then 0 else countFail[1] + 1;
Plot CF = countFail;

#### halcyonguy

##### Well-known member
VIP
I want to plot the count of the number of candles that a stock fails to break above all time highs such that each candle will have a plot point returning a value of zero if a new highest high is made and 1 if it fails, accumulating that count each candle that fails.

so if you look at QQQ as of 1/14/22 it should have a plot point value of 37 because for the last 37 stock trading days it has failed to break over 408.71 the latest highest high on the daily set on 11/22/22

if QQQ breaks over 408.71 the highest high, then the plot point resets to 0; if the next day it breaks again a new highest high then that too would have a plot point of zero.

this will be great to track certain instruments to see how long they take to let bag holders who top tick, cash out and break even.

I am still trying to research the syntax for controlling the count and how to implement Count = count +1;

I have the logic sketched out, I don't get the syntax.

declare lower;
declare once_per_bar;

input length = 252; #number of trading days in one year on the daily chart

def BreakOutTarget = Highest(high[1], length);

plot countFail = if high[0] < BreakOutTarget then countFail = countFail + 1, else 0;

# plots the number of days a stock fails to make a new yearly high on the daily chart. will reset the plot to 0 each day it makes a new high. and count up one point for each day it fails.

Code:
``plot countFail = if high[0] < BreakOutTarget then (countFail[1] + 1)  else 0;``

or this.
i like to check first for changed conditions, then alter the value.
if no conditions are triggered, then the last section passes the previous value onto the current bar.

the first part of the countFail formula checks if the current high is equal to or higher than BreakOutTarget. if it is, then it sets the counter to 0.
if it isn't higher, then it must be lower. the second part of the formula reads the previous value of countFail, by using an offset of 1, and adds 1 to it.

Code:
``````input length = 252;
def BreakOutTarget = Highest(high[1], length);

plot countFail =
if high >= BreakOutTarget then 0
else countFail[1] + 1;``````

#### DanielManahan

##### Member
thank you but if I get the error "Too Early to access CountFail"

what is the syntax error in your code?

almost there I believe. just get past that error.

#### DanielManahan

##### Member
I will try this to fix that error

declare lower;
declare once_per_bar;

input length = 252;
def BreakOutTarget = Highest(high[1], length);

def CountFail = 0;
plot CF = countFail and if high >= BreakOutTarget then 0
else countFail[1] + 1;

# this fixes the error but still produces no result. gives value 0 without doing any counting.

#### Svanoy

##### Active member
@DanielManahan
Def countFail = if high >= BreakOutTarget then 0 else countFail[1] + 1;
Plot CF = countFail;

#### DanielManahan

##### Member
yes that worked and learning from everyone's examples thanks

#### DanielManahan

##### Member
I can't believe how simple the solution is and yet I never tried it. a lot to learn about my thinking and how to see the code in the future.