# How do I word a scan to look for a criteria that happens twice or more than once?

#### jngy2k

##### Member
For example I have the following that I scan for:
(volume>10000000 and close/close[1]>1.04) within 15 bars
It gives me a list of stocks that has volume and is moving by 4% at least one day in the past 15 days.

Now if I want to look for the same criteria (volume>10000000 and close/close[1]>1.04)
BUT to happen TWICE within 15 bars,
then how would I word it???

Ruby:
``````def MyCondition = volume > 10000000 and close / close[1] > 1.04;
def Count = If MyCondition then Count[1] + 1 else Count[1];
plot Result = Count >= 2;``````

Custom study filter, select for "Result" to be "Is True" within 15 bars, or how ever you want it.

Last edited:
Ruby:
``````def MyCondition = volume > 10000000 and close / close[1] > 1.04;
def Count = If MyCondition then Count[1] + 1 else Count[1];
plot Result = Count >= 2;``````

Custom study filter, select for "Result" to be "Is True" within 15 bars, or how ever you want it.
That will work on short-ish lengths, where you might have only one or two hits. If you look at something like daily charts over a year, you may have many many hits, and your counter will only go up and up. It is safer to do it this way:
Ruby:
``````def MyCondition = volume > 10000000 and close / close[1] > 1.04;
def Count = If MyCondition then 1 else 0;
plot Result = sum(Count, 15) >= 2;``````
In this version, we create a series of 0 and 1 in Count which we sum over the desired range (15 bars here). Otherwise you will need to specify in your scan to only look at the last 15 days (which I'm not sure how to do... I don't know what length of time scans look back through).

Take the same approach that @Joshua recommended for the custom study, but you don't need the within 15 bars bit as it's specified in the code (in the count() function)

Two approaches, take whichever one works better for the results you need.

Hope that helps,
Mashume

Thanks @Joshua for the idea and @mashume for the refinement and simplicity I didn't know sum with a condition can be done like this, so simple

He is just being polite, use mashume's version, don't use my version.

Is there is away to show how many 4% brake out as study
On the chart like arrow in daily chart
Thanks

Last edited:
Is there is away to show how many 4% brake out as study
On the chart like arrow in daily chart
Thanks
@Drmoh1800 It is literally on EG website if you take the time to study his guide and do a proper search so I won't be posting it here.

@Drmoh1800 It is literally on EG website if you take the time to study his guide and do a proper search so I won't be posting it here.
I just saw this thread and very interested, so registered on Usethinkscript just now. I looked but couldn't find the url to EG Website to study his guide, could you please post it for us? Thanks for your expertise!

eg is stock bee

Ruby:
``````def MyCondition = volume > 10000000 and close / close[1] > 1.04;
def Count = If MyCondition then Count[1] + 1 else Count[1];
plot Result = Count >= 2;``````

Custom study filter, select for "Result" to be "Is True" within 15 bars, or how ever you want it.
Can some one put this is a indicator??

Just put the code in a lower chart panel...? This will show you if it happened twice or more ever though. If you want it to show if it happens twice or more in a specified period, use the code provided by @mashume

Just put the code in a lower chart panel...? This will show you if it happened twice or more ever though. If you want it to show if it happens twice or more in a specified period, use the code provided by @mashume
Ty, It just works better as a scan

87k+ Posts
318 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?