C-Counter for the Number of Up Bars

Chuck

Moderator
Staff member
Code:
#hint:<b> Counts the number of bars where close > open. </b>\n Yes/No options to show a summary label, count numbers and bar numbers.

input ShowBarNumb = No;#hint ShowBarNumb: Yes show the bar numbers below the low.
input ShowCountNos = yes;#hint ShowCountNos:Yes show the count of when close > open. It shows above the high.
input ShowLabel = yes;#hint ShowLabel:yes shows a summary label of the percent of bars that are up.

Def counter_AnyDay = CompoundValue (1,if close > open then counter_AnyDay[1] + 1 else counter_AnyDay[1],1);

plot count = if close > open  &&  ShowCountNos  then counter_AnyDay else double.nan;
Count.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

###below 2 lines convert to a line plot ###
#Count.SetPaintingStrategy(PaintingStrategy.line);
#Count.SetStyle(Curve.firm);

def BarNum = Barnumber();

plot Bar_Nos = if ShowBarNumb then BarNum else Double.NaN;
Bar_Nos.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);

Addlabel(ShowLabel, counter_AnyDay + " bars (" + (AsPercent(round(counter_AnyDay / barnumber(),2))) + ") out of a total of " + Barnumber() + " bars have close > open",color.cyan);
Comment: This counter can be easily converted to counting whatever you want. For example, if you want to count the number of days that have risen 1%, you would substitute the term 'close > open' with '(close/close[1] > 1.01) '. Naturally the aggregation is set to what you want to count like days, hours, 15 min bars, etc. The below code illustrates how this existing code can be taken and easily modified into another study. Compare the two.
 
Top