How to COUNT CUMULATIVE or CONSECUTIVE bars on ENTIRE CHART or SPECIFIED TIME

Found this code. Wanted to modify it to a fixed length. Tried a few things with no luck. It seems to change per chart length. Would love to us it in a scan to find the stocks that have the best changes to keep moving up. Also it looks like it could be easily modified to count gap ups. Thanks.

Code:
#hint:<b> Counts the number of bars where close has risen an inputted percent. </b>\n Yes/No options for 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 occurences. It shows above the high.
input ShowLabel = yes;#hint ShowLabel:Yes shows a summary label of the percent of bars that are up by the inputted percentage.

input PctUp = 0.5; #hint PctUp:<b>Show the percent rise desired</b>\nA stock infrequently rises more than  2% in a day.

def PctFactor = 1 + (PctUp /100);

Def counter_AnyDay =if close !=first(close) && (close/close[1] > PctFactor) then counter_AnyDay[1] + 1 else counter_AnyDay[1];

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

plot count = if (close/close[1] > PctFactor)  &&  ShowCountNos  then counter_AnyDay else double.nan;

Count.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

###below 2 lines convert to a line plot  in lieu of showing values ###
#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/close[1] > " + PctUp +  "%",color.cyan);
addlabel(ShowLabel,"PctUp = " +  PctUp + "%", color.pink);
 
@Prison Mike a lot going on there in that code, you said your trying to modify it to a fixed length. modify what to do what ? and to scan against what part of the code to do what?
 
@Prison Mike a lot going on there in that code, you said your trying to modify it to a fixed length. modify what to do what ? and to scan against what part of the code to do what?
What I would like to scan for is the percent of up days( “pctup” in the code) over the last 100 days. Then I would have a list of stocks that say 70% of the time moved X percent up. Maybe I would use a watchlist column to filter further.

I’m not great at explaining myself in writing. I hope this made sense.

Also thanks for all you do for this community and questions you have answered for me personally.
 
cant code anything if you cant explain it. PctUp is just defines and always returns number 0.5 in the code.

define "up days"
 
@Prison Mike Don't take this the wrong way but when are you going to start learning to help yourself with the coding...??? Are you going to ask others to write all of your code for you forever or are you going to make an effort to learn to write the code you want...??? You've been here long enough to get started... Otherwise you could just live with code that is openly available... If nobody says anything you'll just keep taking and taking and I don't see that as fair... If you see it otherwise, please share your thoughts but I call them as I see them and I think this site has been more than generous... And I'm not singling just you out, the same goes for more than a few other members who have zero initiative other than to get others to write the code that makes them money - for free...
 
@Prison Mike
High-Open?
and greater than pctup? (what is pct up)
that returns a number that logically a undefined percentage cant be compared to. you will have to describe it thoroughly.
 
let me put it to you this way. you practically stated:
High minus open is greater than XYZ percent.
high-open does not yield a percent. therefor nullifying your scan.
you will have to THOUROUGHLY explain.
 
Here's what I came up with. The screener finds the stocks with ATR over .75 and that their high-open range is 75% of their high to low range.

So here are my thoughts on how to trade it. With ATR vs DTR indicator pulled up you see that DTR is 25% of ATR and it's still early in the day. Well you know that once price crosses open the high to open is usually 75% of the days range so you may consider trading it up.

Why I like this set up is because its based on probabilities and statistics. For me it's easier to find risk/reward.
For example:
Daily range: $1
High to open instances: 77% over the last 100 days high- low was .75 of total range.
Stock price: $20
So if I trade it open to high: reward .75/$20= 3.75%
risk if I stop out at low 1.25%
And the odds are in my favor for this to happen.

Thanks again @XeoNoX for taking a look at it.
@rad14733 Your suggestion won't fall on deaf ears. Not offended.

Code:
#hint:<b> Counts the number of bars where close has risen an inputted percent. </b>\n Yes/No options for 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 occurences. It shows above the high.
input ShowLabel = yes;#hint ShowLabel:Yes shows a summary label of the percent of bars that are up by the inputted percentage.

input PctUp = 75; #hint PctUp:<b>Show the percent rise desired</b>\nA stock infrequently rises more than  2% in a day.

def PctFactor =(PctUp /100);
def hilowrange= high-low;
def profitrange= high-open ;
def range= profitrange/hilowrange;
Def counter_AnyDay =if open !=first(open) && (range > PctFactor) then counter_AnyDay[1] + 1 else counter_AnyDay[1];

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

plot count = if (range > PctFactor)  &&  ShowCountNos  then counter_AnyDay else double.nan;

Count.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

###below 2 lines convert to a line plot  in lieu of showing values ###
#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);
input length= 100;
Addlabel(ShowLabel, counter_AnyDay + " bars (" + (AsPercent(round(counter_AnyDay / length,2))) + ") out of a total of " + length + " bars have (high-open) > " + PctUp +  "%",color.cyan);
addlabel(ShowLabel,"PctUp = " +  PctUp + "%", color.pink);

SCAN
http://tos.mx/d92jo2U

Using it with this on multiple timeframes (D, W, M) Modified it to include average of open to close.
Code:
# Average Price Movements
# Assembled by BenTen at useThinkScript.com
#Prison Mike Added open to close average
# Converted from https://www.tradingview.com/script/eHhGyI6R-CD-Average-Daily-Range-Zones-highs-and-lows-of-the-day/

input aggregationPeriod = AggregationPeriod.DAY;
def open = open(period = aggregationPeriod);
def high = high(period = aggregationPeriod);
def low = low(period = aggregationPeriod);
def close = close(period = aggregationPeriod);
def dayrange = (high - low);
def openclose= (close- open);

def r1 = dayrange[1];
def r2 = dayrange[2];
def r3 = dayrange[3];
def r4 = dayrange[4];
def r5 = dayrange[5];
def r6 = dayrange[6];
def r7 = dayrange[7];
def r8 = dayrange[8];
def r9 = dayrange[9];
def r10 = dayrange[10];

def rr1 = openclose[1];
def rr2 = openclose[2];
def rr3 = openclose[3];
def rr4 = openclose[4];
def rr5 = openclose[5];
def rr6 = openclose[6];
def rr7 = openclose[7];
def rr8 = openclose[8];
def rr9 = openclose[9];
def rr10 = openclose[10];

def adr_10 = (r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8 + r9 + r10) / 10;
def adr_5 = (r1 + r2 + r3 + r4 + r5) / 5;

def dr_10 = (rr1 + rr2 + rr3 + rr4 + rr5 + rr6 + rr7 + rr8 + rr9 + rr10) / 10;
def dr_5 = (rr1 + rr2 + rr3 + rr4 + rr5) / 5;


def hl1 = (OPEN + (adr_10 / 2));
def ll1 = (OPEN - (adr_10 / 2));
def hl2 = (OPEN + (adr_5 / 2));
def ll2 = (OPEN - (adr_5 / 2));

def oc1 = (OPEN + (dr_10 / 2));
def co1 = (OPEN - (dr_10 / 2));
def oc2 = (OPEN + (dr_5 / 2));
def co2 = (OPEN - (dr_5 / 2));

plot h1 = hl1;
plot l1 = ll1;
plot h2 = hl2;
plot l2 = ll2;

plot o1 = oc1;
plot c1 = co1;
plot o2 = oc2;
plot c2 = co2;

addCloud(h1, h2, color.RED, color.RED);
addCloud(l1, l2, color.GREEN, color.GREEN);

addCloud(o1, c1, color.gray, color.gray);
#addCloud(c1, c2, color.white, color.white);

h1.SetDefaultColor(Color.dark_red);
h2.SetDefaultColor(Color.dark_red);
l1.SetDefaultColor(Color.dark_green);
l2.SetDefaultColor(Color.dark_green);



o1.SetDefaultColor(Color.white);
o2.SetDefaultColor(Color.orange);
o2.SetPaintingStrategy(PaintingStrategy.DASHES);
c1.SetDefaultColor(Color.white);
c2.SetDefaultColor(Color.orange);
c2.SetPaintingStrategy(PaintingStrategy.DASHES);
 
Last edited:
good job... i assume you figured it out.

@Prison Mike .. this doesn't have to do with you, just in general I can understand @rad14733 frustration, some people as of late are asking multiple questions and referencing different things. Similar to asking how does a plane fly, what speed did it land at and how did it land, and what angle were the wheels at when landing. Yet they make no attempt to gather the needed data to figure these things out. and that's just the first red flag, the second red flag is when they cant explain it in detail, and the third and obvious red flag is when what they explained makes little to no sense. @BenTen should start imposing a 3 day wait before a new person can post so that way they are forced to use the SEARCH and it will also limit what i refer to as "One Post Wonders" (non active/one time visitors). The amount of new people that have started self managing their own portfolio's has risen dramatically and so has the amount of uneducated traders leading to a lot of new people coming here asking what are in my opinion... way out of this world trading ideas. In my opinion every major correction you will see dramatic decrease in active traders, its not a matter of "if' but "when"... in the mean time i think a 3-5 day wait before posting would dramatically decrease the strange posts.
 
Last edited:
How to count the cumulative total of bars or the consecutive number of bars on the entire chart and/or the same day

For this example we will use the number of bars that he close is greater than the open (number of green bars)
the default is:
Code:
def var = close > open;
NOTE: "VAR" aka the variable can be changed to the defined study/pattern you want counted.

Remember to leave a thumbs up if you found this post useful



COUNT OF THE TOTAL NUMBER OF CONSECUTIVE GREEN BARS (CLOSE>OPEN)
Code:
declare lower;
# COUNT OF THE TOTAL NUMBER OF CONSECUTIVE GREEN BARS (CLOSE>OPEN)
# By XeoNoX via Usethinkscript.com
def var = close > open;
def barUpCount = CompoundValue(1, if var then barUpCount[1] + 1 else 0, 0);
AddLabel (yes, "COUNT " + barUpCount );


CUMULATIVE COUNT OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN) ON THE ENTIRE CHART
Code:
declare lower;
#( CUMULATIVE ) COUNT OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN)
# ON THE ENTIRE CHART
# By XeoNoX via Usethinkscript.com
def var =close>open;
def count = totalsum(var);
plot scan  = count;
AddLabel (yes, "COUNT " +  (count)  );


CUMULATIVE COUNT OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN) ON THE ENTIRE CHART WITHIN SPECIFIED TIMEFRAME
Code:
#( CUMULATIVE ) COUNT OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN)
# ON THE ENTIRE CHART WITHIN SPECIFIED TIMEFRAME
# By XeoNoX via Usethinkscript.com
input startTime = 0930;
input endTime = 1600;
def Active = SecondsFromTime(startTime) >= 0 and SecondsTillTime(endTime) >= 0;
def var = close>open;
def cumulative = if Active and !Active[1] then var else if Active then compoundValue(1,  (cumulative[1] + var), 0) else cumulative[1];
plot scan  = cumulative;
addLabel(1, "Count = " + scan, color.dark_green);
scan.SetPaintingStrategy(PaintingStrategy.Values_Above);




CUMULATIVE COUNT OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN) ON THE THE CURRENT DAY ONLY ( TODAYS ) CHART
Code:
declare lower;
#  CUMULATIVE  COUNT OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN)
# ON THE THE CURRENT DAY ONLY ( TODAYS ) CHART
# By XeoNoX via Usethinkscript.com
def Active = GetDay() == GetLastDay(); #Today
def var = close>open;
def cumulative = if Active and !Active[1] then var else if Active then cumulative[1] + var else cumulative[1];
plot scan  = cumulative;
AddLabel (yes, "COUNT " +  (scan), color.dark_green  );


COUNT OF THE CUMULATIVE OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN) FROM XYZ BARS AGO
(COUNT CUMULATIVE TOTAL OF GREEN BARS FROM THE LAST / PAST XYZ BARS)
Code:
declare lower;
#COUNT OF THE CUMULATIVE OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN) FROM XYZ BARS AGO
#(COUNT CUMULATIVE TOTAL OF GREEN BARS FROM THE LAST / PAST XYZ BARS)
# By XeoNoX via Usethinkscript.com
input barsago = 15;
def var =close>open;
def count = Sum(var, barsago);
AddLabel (yes, "COUNT " +  (count)  );
I have modified your first code but it is still not counting correctly when I change tickers. It works once and then stops. My code is below: Please help! Thank you

#Count the number of Green Days in a row on the daily chart

def var = close(period = AggregationPeriod.DAY) > open(period = AggregationPeriod.DAY);
def barUpCount = CompoundValue(1, if var then barUpCount[1] + barUpCount[2] + barUpCount[3] + barUpCount[4] + barUpCount[5] + barUpCount[6] + barUpCount[7] + barUpCount[8] + barUpCount[9] + barUpCount[10] + barUpCount[11] + barUpCount[12] + barUpCount[13] + barUpCount[14] + 1 else 0, 0);
#AddLabel (yes, "COUNT " + barUpCount );
AddLabel(4, barUpCount + " Green Days in a Row", (if barUpCount < -0 then Color.LIGHT_RED else if barUpCount >= 1 then Color.LIGHT_GREEN else Color.WHITE));


#Count the number of higher lows in a row on the daily Chart

def var1 = low(period = AggregationPeriod.DAY) > low(period = AggregationPeriod.DAY) [1];
def barUpCount1 = CompoundValue(1, if var1 then barUpCount1[1] + barUpCount1[2] + barUpCount1[3] + barUpCount1[4] + barUpCount1[5] + barUpCount1[6] + barUpCount1[7] + barUpCount1[8] + barUpCount1[9] + barUpCount1[10] + barUpCount1[11] + barUpCount1[12] + barUpCount1[13] + barUpCount1[14] + 1 else 0, 0);
#AddLabel (yes, "COUNT " + barUpCount1 );
AddLabel(4, barUpCount1 + " Days of Higher Lows", (if barUpCount1 < -0 then Color.LIGHT_RED else if barUpCount1 >= 1 then Color.LIGHT_GREEN else Color.WHITE));
 
you use my code and you just change ONE line only.
looking at your code you changed more than one line.
Code:
def var = close(period = AggregationPeriod.DAY) > open(period = AggregationPeriod.DAY);

and

Code:
def var1 = low(period = AggregationPeriod.DAY) > low(period = AggregationPeriod.DAY) [1];

thats the all you have to change to make it count. let me know if you get it to work.
 
How to count the cumulative total of bars or the consecutive number of bars on the entire chart and/or the same day
To count your study you simply just have to alter ONE line. You alter the "def = VAR" line.
For this example we will use the number of bars that he close is greater than the open (number of green bars)
the default is:
Code:
def var = close > open;
NOTE: "VAR" aka the variable can be changed to the defined study/pattern you want counted.

Remember to leave a thumbs up if you found this post useful

...

Great set of codes, thank you!

How would you count the number of consecutive days above/below a moving average on a lower time frame, for example the hourly/30m? I know how to get the moving average and would probably combine with your CONSECUTIVE code snippet, but not sure how to blend both the day and smaller time frames. Thanks.
 
@pine you would have to aggregate whatever scan you have to day using aggregation period.
example: (close(period = AggregationPeriod.MONTH)

then you would have to get the total number of bars in that current aggregation period that equates to 1 full day using getperiod and barnumber.

then you would need to define each aggregation period and tell the script if AggregationPeriod.FOUR_HOURS then divide by xzy (example if 4hr chart that would mean divide by 2 because there are 2 4hr bars. ) you would have to do this for each aggregation you wish to use.

note: as you can see this would be quit a bit of coding and please note that this would work for plotting on charts but not to scan against as the scan would come up with aggregation period error. In my opinion i would recommend just using 2 different charts with the different time frames instead of trying to make a MTF (multitimeframe) indicator/study which tends to be complicated.

the following example below might help get you started on your journey:
Code:
# Archive Name: Label-Chart Aggregation Length and Detail_v02_Johnny Quotron

# Archive Section: Labels

# Suggested Tos Name: ChartAggLengthDetail_v02_JQ

# Archive Date:

# Archive Notes:  10.10.2018  adjusted label to eliminate repetition





# ChartAggLength_JohnnyQuotron

# This label relies on code from StanL as posted in his Snippets

# Thank you Stan for all your efforts

# The purpose of this script is to create a label that will remind the user

# of both Aggregation and chart length





# AggregartionPeriodLabel_StanL

#hint:Defines the aggregation in a label



#    04.19.2018 added Length and barNumber Bars JohnnyQuotron

# Days On Chart

# Mobius

def RTHstart =  SecondsFromTime(0930) == 0;

def daysOnChart = if RTHstart then daysOnChart[1] + 1

                  else daysOnChart[1];

#addLabel(1, "Days on Chart: " + daysOnChart, color.cyan);



def AggPeriod = GetAggregationPeriod();



AddLabel(yes, Concat(" ",

if AggPeriod == AggregationPeriod.MIN then "1 Min"



else if AggPeriod == AggregationPeriod.TWO_MIN then "2 Min"



else if AggPeriod == AggregationPeriod.THREE_MIN then "3 Min"



else if AggPeriod == AggregationPeriod.FOUR_MIN then "4 Min"



else if AggPeriod == AggregationPeriod.FIVE_MIN then "5 Min"



else if AggPeriod == AggregationPeriod.TEN_MIN then "10 Min"



else if AggPeriod == AggregationPeriod.FIFTEEN_MIN then "15 Min"



else if AggPeriod == AggregationPeriod.TWENTY_MIN then "20 Min"



else if AggPeriod == AggregationPeriod.THIRTY_MIN then "30 Min"



else if AggPeriod == AggregationPeriod.HOUR then "1 Hour"



else if AggPeriod == AggregationPeriod.TWO_HOURS then "2 Hour"



else if AggPeriod == AggregationPeriod.FOUR_HOURS then "4 Hour"



else if AggPeriod == AggregationPeriod.DAY then "Daily"



else if AggPeriod == AggregationPeriod.TWO_DAYS then "2 Day"



else if AggPeriod == AggregationPeriod.THREE_DAYS then "3 Day"



else if AggPeriod == AggregationPeriod.FOUR_DAYS then "4 Day"



else if AggPeriod == AggregationPeriod.WEEK then "Weekly"



else if AggPeriod == AggregationPeriod.MONTH then "Monthly"



else "Use time charts only") + " Aggregation : " + BarNumber() + " " +



(if AggPeriod == AggregationPeriod.MIN then "1 Min"



else if AggPeriod == AggregationPeriod.TWO_MIN then "2 Min"



else if AggPeriod == AggregationPeriod.THREE_MIN then "3 Min"



else if AggPeriod == AggregationPeriod.FOUR_MIN then "4 Min"



else if AggPeriod == AggregationPeriod.FIVE_MIN then "5 Min"



else if AggPeriod == AggregationPeriod.TEN_MIN then "10 Min"



else if AggPeriod == AggregationPeriod.FIFTEEN_MIN then "15 Min"



else if AggPeriod == AggregationPeriod.TWENTY_MIN then "20 Min"



else if AggPeriod == AggregationPeriod.THIRTY_MIN then "30 Min"



else if AggPeriod == AggregationPeriod.HOUR then "1 Hour"



else if AggPeriod == AggregationPeriod.TWO_HOURS then "2 Hour"



else if AggPeriod == AggregationPeriod.FOUR_HOURS then "4 Hour"



else if AggPeriod == AggregationPeriod.DAY then "Daily"



else if AggPeriod == AggregationPeriod.TWO_DAYS then "2 Day"



else if AggPeriod == AggregationPeriod.THREE_DAYS then "3 Day"



else if AggPeriod == AggregationPeriod.FOUR_DAYS then "4 Day"



else if AggPeriod == AggregationPeriod.WEEK then "Weekly"



else if AggPeriod == AggregationPeriod.MONTH then "Monthly"



else "Use time charts only") + " Bars " ,

  Color.WHITE);



addlabel( aggPeriod < 86400000, (daysOnChart -1 ) + " Days on Chart ", color.WHITE);
 
Last edited:
@pine you would have to aggregate whatever scan you have to day using aggregation period.
example: (close(period = AggregationPeriod.MONTH)

then you would have to get the total number of bars in that current aggregation period that equates to 1 full day using getperiod and barnumber.

then you would need to define each aggregation period and tell the script if AggregationPeriod.FOUR_HOURS then divide by xzy (example if 4hr chart that would mean divide by 2 because there are 2 4hr bars. ) you would have to do this for each aggregation you wish to use.

note: as you can see this would be quit a bit of coding and please note that this would work for plotting on charts but not to scan against as the scan would come up with aggregation period error. In my opinion i would recommend just using 2 different charts with the different time frames instead of trying to make a MTF (multitimeframe) indicator/study which tends to be complicated.

the following example below might help get you started on your journey:
Code:
# Archive Name: Label-Chart Aggregation Length and Detail_v02_Johnny Quotron

# Archive Section: Labels

# Suggested Tos Name: ChartAggLengthDetail_v02_JQ

# Archive Date:

# Archive Notes:  10.10.2018  adjusted label to eliminate repetition





# ChartAggLength_JohnnyQuotron

# This label relies on code from StanL as posted in his Snippets

# Thank you Stan for all your efforts

# The purpose of this script is to create a label that will remind the user

# of both Aggregation and chart length





# AggregartionPeriodLabel_StanL

#hint:Defines the aggregation in a label



#    04.19.2018 added Length and barNumber Bars JohnnyQuotron

# Days On Chart

# Mobius

def RTHstart =  SecondsFromTime(0930) == 0;

def daysOnChart = if RTHstart then daysOnChart[1] + 1

                  else daysOnChart[1];

#addLabel(1, "Days on Chart: " + daysOnChart, color.cyan);



def AggPeriod = GetAggregationPeriod();



AddLabel(yes, Concat(" ",

if AggPeriod == AggregationPeriod.MIN then "1 Min"



else if AggPeriod == AggregationPeriod.TWO_MIN then "2 Min"



else if AggPeriod == AggregationPeriod.THREE_MIN then "3 Min"



else if AggPeriod == AggregationPeriod.FOUR_MIN then "4 Min"



else if AggPeriod == AggregationPeriod.FIVE_MIN then "5 Min"



else if AggPeriod == AggregationPeriod.TEN_MIN then "10 Min"



else if AggPeriod == AggregationPeriod.FIFTEEN_MIN then "15 Min"



else if AggPeriod == AggregationPeriod.TWENTY_MIN then "20 Min"



else if AggPeriod == AggregationPeriod.THIRTY_MIN then "30 Min"



else if AggPeriod == AggregationPeriod.HOUR then "1 Hour"



else if AggPeriod == AggregationPeriod.TWO_HOURS then "2 Hour"



else if AggPeriod == AggregationPeriod.FOUR_HOURS then "4 Hour"



else if AggPeriod == AggregationPeriod.DAY then "Daily"



else if AggPeriod == AggregationPeriod.TWO_DAYS then "2 Day"



else if AggPeriod == AggregationPeriod.THREE_DAYS then "3 Day"



else if AggPeriod == AggregationPeriod.FOUR_DAYS then "4 Day"



else if AggPeriod == AggregationPeriod.WEEK then "Weekly"



else if AggPeriod == AggregationPeriod.MONTH then "Monthly"



else "Use time charts only") + " Aggregation : " + BarNumber() + " " +



(if AggPeriod == AggregationPeriod.MIN then "1 Min"



else if AggPeriod == AggregationPeriod.TWO_MIN then "2 Min"



else if AggPeriod == AggregationPeriod.THREE_MIN then "3 Min"



else if AggPeriod == AggregationPeriod.FOUR_MIN then "4 Min"



else if AggPeriod == AggregationPeriod.FIVE_MIN then "5 Min"



else if AggPeriod == AggregationPeriod.TEN_MIN then "10 Min"



else if AggPeriod == AggregationPeriod.FIFTEEN_MIN then "15 Min"



else if AggPeriod == AggregationPeriod.TWENTY_MIN then "20 Min"



else if AggPeriod == AggregationPeriod.THIRTY_MIN then "30 Min"



else if AggPeriod == AggregationPeriod.HOUR then "1 Hour"



else if AggPeriod == AggregationPeriod.TWO_HOURS then "2 Hour"



else if AggPeriod == AggregationPeriod.FOUR_HOURS then "4 Hour"



else if AggPeriod == AggregationPeriod.DAY then "Daily"



else if AggPeriod == AggregationPeriod.TWO_DAYS then "2 Day"



else if AggPeriod == AggregationPeriod.THREE_DAYS then "3 Day"



else if AggPeriod == AggregationPeriod.FOUR_DAYS then "4 Day"



else if AggPeriod == AggregationPeriod.WEEK then "Weekly"



else if AggPeriod == AggregationPeriod.MONTH then "Monthly"



else "Use time charts only") + " Bars " ,

  Color.WHITE);



addlabel( aggPeriod < 86400000, (daysOnChart -1 ) + " Days on Chart ", color.WHITE);
This will get me going. Thank you for your help!
 
Hi All,

This is great post, great set of codes for bar counts, thank you all for your share. I was looking through the code to find what I was looking but I could not identify one that would fit my needs. I was wondering if somebody can help modify below code to get bar count from last fired signal, reset bar count when new signal is up/dn. For example in image below, arrow under bubble show uptrend, is it possible to count bar from that point on until trend reverses, once trend reverses, reset count and count again for next trend. Watchlist below give me color for the trend but wonder if bar count could be integrate for the trend direction.

I found another script that counts bar when price hit ORB H and work great, I try modify code to fit my needs and unsuccessful, posting here for reference. Any help would be greatly appreciated.

Chart.png



Code that need to bar count integrated
Code:
#Combo ST and CCI ATR TREND Coloumn
def c = close;
def h = high;
def l = low;
def pricedata = hl2;
input show_inside_bar_count = Yes;

#SUPERTREND
input ST_Atr_Mult = 1.3;
input ST_Length = 60;
input ST_AvgType = AverageType.wilders;
def ATR = MovingAverage(ST_AvgType, TrueRange(high, close, low), ST_Length);
def UP = HL2 + (ST_Atr_Mult* ATR);
def DN = HL2 + (-ST_Atr_Mult * ATR);
def ST = if close < ST[1] then UP else DN;
def SuperTrend = ST;

#CCI_ATR
input lengthCCI = 50;
input lengthATR = 5;
input AtrFactor = 1.0;
def ATRcci = Average(TrueRange(h, c, l), lengthATR) * AtrFactor;
def price = c + l + h;
def linDev = LinDev(price, lengthCCI);
def CCI = if linDev == 0
          then 0
          else (price - Average(price, lengthCCI)) / linDev / 0.015;

def MT1 = if CCI > 0
          then Max(MT1[1], pricedata - ATRcci)
          else Min(MT1[1], pricedata + ATRcci);
def CCI_ATR_TREND = MT1;

plot ST_ATR_COMBO = if C> ST and C>CCI_ATR_TREND then 4 else if c< ST and c<CCI_ATR_TREND then -4 else 0;

AssignBackgroundColor(if ST_ATR_COMBO ==4 then color.cyan else if ST_ATR_COMBO == -4 then color.yellow else color.black);

ST_ATR_COMBO.assignValueColor(if ST_ATR_COMBO ==4 then color.cyan else if ST_ATR_COMBO == -4 then color.yellow else color.black);

Code for reference to see how others watchlist colum count bars
Code:
# Watchlist ORB Status
# Mobius
# V01 using getTIme()
# Note: Column Aggregation MUST be 30min or less
# Pensar - 07/06/2020 - modified to count bars above/below Opening Range
#        - 07/31/2020 - changed code to use plot instead of AddLabel
#                       so that column can be sorted numerically

# The input below will show the bar count when inside the Opening Range
# if set to "yes", otherwise it will display "NaN".
input show_inside_bar_count = Yes;

def Active = getTime() >= RegularTradingStart(getYYYYMMDD()) and
             getTime() <= RegularTradingStart(getYYYYMMDD()) +   
             AggregationPeriod.FIVE_MIN;

def hh = if Active and !Active[1] then high
         else if Active and high > hh[1] then high
         else hh[1];
def ll = if Active and !Active[1] then low
         else if Active and low < ll[1] then low
         else ll[1];
def current = if between(close, ll, hh) then 0
              else if close > hh then 1
              else if close < ll then -1
              else double.nan;
def n1 = current == 1;
def n2 = current == -1;
def n3 = current == 0;
def count_up = if n1 and !n1[1] then 1 else count_up[1]+1;
def count_dn = if n2 and !n2[1] then 1 else count_dn[1]+1;
def count_in = if show_inside_bar_count then
               if n3 and !n3[1] then 1
               else count_in[1]+1
               else double.nan;

plot Number = if n1 then count_up
              else if n2 then count_dn
              else count_in;
#     Number.AssignValueColor(if n1 then color.green
#                             else if n2 then color.red
#                             else color.yellow);
#
     Number.AssignValueColor(if n1 then color.black
                             else if n2 then color.black
                             else color.black);

AssignBackgroundColor(if n1 then createColor(0,255,0)
                      else if n2 then createColor(155,0,0)
                      else createColor(180,180,5));
# End Code ORB Status
 
@XeoNoX Yes, that's exactly what I'm looking for, I tried modify peace code from ORB scripts by defining last signal, not sure I'm doing it right. Right now I got NaN as results, not sure what I'm doing wrong here, any thoughts about how would I need to modify this.

Code:
Def SignalUp =  C > ST and C > CCI_ATR_TREND ;
Def SignalDn = C < ST and C <CCI_ATR_TREND ;

Def LastSignal = IF SignalUp < SignalDn then SignalDn Else SignalUp;

Def current =  if  LastSignal > SignalDn then 1
    Else if LastSignal < SignalUp then -1
    Else Double.Nan;

def n1 = current == 1;
def n2 = current == -1;
def count_up = if n1 and !n1[1] then 1 else count_up[1]+1;
def count_dn = if n2 and !n2[1] then 1 else count_dn[1]+1;

plot Number = if n1 then count_up else count_dn;
     Number.AssignValueColor(color.black);

AssignBackgroundColor(if n1 then createColor(0,255,0) else createColor(155,0,0));
 
i have no clue what you are trying to do as it seems you are trying to do more than just one thing. you may want to try to post this in the general questions section as this relate to more than just counting.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
404 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top