# Limit Count To # Of Bars

#### greco26

##### Active member
Has anyone tried to create a watchlist column that uses code (like a strategy) to test the % probability of a setup based on historical data of the past xx bars for that ticker? I think you would need to use 'Fold' but I'm not sure. Any thoughts or feedback would be awesome!

Solution
You can play around with this as an upper chart study if you want. It will allow you to see it more visually. Then, once you're satisfied with your work you can delete all the extra junk. The -1 part is just to avoid overlap on the exact bar, when testing to the exact bar.

Ruby:
``````def H = high;
def L = low;
def C = close;
def O = open;

def insidebar =  (H <= H[1] and L >= L[1]);
#def insidebar =  (H < H[1] and L > L[1]) or (H == H[1] and L > L[1]) or (H < H[1] and L == L[1]) or (H == H[1] and L == L[1]);
def outsidebar =  H  > H[1] and L <  L[1];

def insidebarup  = insidebar and O < C;
def twoup  = H > H[1] and L >= L[1];
def neartwoup = c > (H[1]*.999) and L >= L[1];
def outsidebarup  = outsidebar and O < C...``````
How do I restrict the count to only be based on the prior 100 bars as per the input?

Code:
``````def H = high;
def L = low;
def C = close;
def O = open;

def insidebar =  (H <= H[1] and L >= L[1]);
#def insidebar =  (H < H[1] and L > L[1]) or (H == H[1] and L > L[1]) or (H < H[1] and L == L[1]) or (H == H[1] and L == L[1]);
def outsidebar =  H  > H[1] and L <  L[1];

def insidebarup  = insidebar and O < C;
def twoup  = H > H[1] and L >= L[1];
def neartwoup = c > (H[1]*.999) and L >= L[1];
def outsidebarup  = outsidebar and O < C;
def insidebardn  = insidebar and O > C;
def twodown  = H <= H[1] and L < L[1];
def neartwodown  = H <= H[1] and c < (L[1]/.999);
def outsidebardn  = outsidebar and O > C;

input barsago = 100;

def count = if twodown[2] and insidebar[1] and high > high[1] then count[1] + 1 else count[1];
AddLabel (yes, " BUY COUNT: " + count + " ", Color.cyan );``````

It would be a condition like the following.

Ruby:
``BarNumber() > HighestAll(BarNumber()) - 100``

I don't assume you have to check for null bars in this case, this would usually be reserved for right side expansion areas on charts. Watchlist columns likely do not have this issue, but you may want to use this just to be safe.

Ruby:
``BarNumber() > HighestAll(if !isNaN(close) then BarNumber() else double.nan) - 100``

thanks @Joshua! I guess the question I have though is where do I include that in my def count expression to restrict it to only those 100 bars? I'm continuing to research now so I will post if I find my answer but let me know if you see this before my post. thx!!

You can play around with this as an upper chart study if you want. It will allow you to see it more visually. Then, once you're satisfied with your work you can delete all the extra junk. The -1 part is just to avoid overlap on the exact bar, when testing to the exact bar.

Ruby:
``````def H = high;
def L = low;
def C = close;
def O = open;

def insidebar =  (H <= H[1] and L >= L[1]);
#def insidebar =  (H < H[1] and L > L[1]) or (H == H[1] and L > L[1]) or (H < H[1] and L == L[1]) or (H == H[1] and L == L[1]);
def outsidebar =  H  > H[1] and L <  L[1];

def insidebarup  = insidebar and O < C;
def twoup  = H > H[1] and L >= L[1];
def neartwoup = c > (H[1]*.999) and L >= L[1];
def outsidebarup  = outsidebar and O < C;
def insidebardn  = insidebar and O > C;
def twodown  = H <= H[1] and L < L[1];
def neartwodown  = H <= H[1] and c < (L[1]/.999);
def outsidebardn  = outsidebar and O > C;

input barsago = 100;
def WithinBars = BarNumber() > HighestAll(BarNumber())  - barsago - 1; #can delete - 1

def count =
if withinBars ### here ###
and twodown[2]
and insidebar[1]
and high > high[1]
then count[1] + 1
else count[1];
AddLabel (yes, " BUY COUNT: " + count + " ", Color.cyan );

### delete this junk if you want
def x = if count != count[1] then count else double.nan;;
plot y = x;
plot "I cant see this color, ignore this" = double.nan;
plot z = x;
y.setpaintingStrategy(paintingStrategy.VALUES_BELOW);
y.setlineWeight(5);
z.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
z.setlineWeight(5);
plot a = BarNumber() == HighestAll(BarNumber()) - barsago - 1; #can delete - 1
a.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
a.setlineWeight(5);``````

Awesome, thank you so much. I was able to play around with this to figure out what I needed. Much appreciated!!

How do I restrict the count to only be based on the prior 100 bars as per the input?

Code:
``````def H = high;
def L = low;
def C = close;
def O = open;

def insidebar =  (H <= H[1] and L >= L[1]);
#def insidebar =  (H < H[1] and L > L[1]) or (H == H[1] and L > L[1]) or (H < H[1] and L == L[1]) or (H == H[1] and L == L[1]);
def outsidebar =  H  > H[1] and L <  L[1];

def insidebarup  = insidebar and O < C;
def twoup  = H > H[1] and L >= L[1];
def neartwoup = c > (H[1]*.999) and L >= L[1];
def outsidebarup  = outsidebar and O < C;
def insidebardn  = insidebar and O > C;
def twodown  = H <= H[1] and L < L[1];
def neartwodown  = H <= H[1] and c < (L[1]/.999);
def outsidebardn  = outsidebar and O > C;

input barsago = 100;

def count = if twodown[2] and insidebar[1] and high > high[1] then count[1] + 1 else count[1];
AddLabel (yes, " BUY COUNT: " + count + " ", Color.cyan );``````

this will be true during the last x bars on the chart
Code:
``````input barcount = 100;
def x = ( !isnan(close) and isnan(close[-barcount]) );``````

87k+ Posts
237 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?