# Looking for flat bar

#### Bentley

##### Member
Hi Guys,
I've written some code for finding flat bar which is FlatBar = high == low and signal me when 2 adjacent flatbar difference is 0.01. However, it's a bit of an hit and miss situation... Here is the code... Looking for idea to improve... Thanks
Code:
``````# This is to find Flat bar of current and previous bar that is difference by 0.01

Declare Lower;
Input Decimal = 2;
Input Diff = 0.01;
Def FlatBar = high == low;
Def FlatBarValue = GetValue(low, FlatBar, 0);
def FlatBarDiff = Round(absValue(FlatBarValue - FlatBarValue[1]),Decimal) == Diff;

Plot FBD = FlatBarDiff;
FBD.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
FBD.SetDefaultColor(GetColor(2));
FBD.SetLineWeight(2);``````

Solution
Your code is looking at the current flatbar and the PREVIOUS flatbar.
And, you're comparing if the lows of the bars are within 1 cent of each other.

What you appear to want is
1. the flatbar CONDITION to be TRUE for currentbar
2. AND the flatbar condition to be true for previousbar

Try this?
Code:
``````# This is to find Flat bar of current and previous bar that is difference by 0.01
Declare Lower;
Input Decimal = 2;
Input Diff = 0.01;
Def FlatBarCurrent = high == low;
def FlatBarPrevious = high[1] == low[1];

# are flat bars next to each other?
def FlatBarConsecutive = FlatBarCurrent && FlatBarPrevious;
# Are the flat bars 1 cent apart?
def FlatBarDiff = Round(absValue(low- low[1]),Decimal) == Diff;

Plot FBD =if FlatBarConsecutive &&...``````
Your code is looking at the current flatbar and the PREVIOUS flatbar.
And, you're comparing if the lows of the bars are within 1 cent of each other.

What you appear to want is
1. the flatbar CONDITION to be TRUE for currentbar
2. AND the flatbar condition to be true for previousbar

Try this?
Code:
``````# This is to find Flat bar of current and previous bar that is difference by 0.01
Declare Lower;
Input Decimal = 2;
Input Diff = 0.01;
Def FlatBarCurrent = high == low;
def FlatBarPrevious = high[1] == low[1];

# are flat bars next to each other?
def FlatBarConsecutive = FlatBarCurrent && FlatBarPrevious;
# Are the flat bars 1 cent apart?
def FlatBarDiff = Round(absValue(low- low[1]),Decimal) == Diff;

Plot FBD =if FlatBarConsecutive && FlatBarDiff  then FlatBarDiff else double.NaN;
FBD.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
FBD.SetDefaultColor(GetColor(2));
FBD.SetLineWeight(2);``````

I ran this as a scan against daily time frame, and it picked up 230 stocks just now (morning of 10/27/2021, pre-market).

Here's some of those stocks:

Hi there,
Hit it on the nail... totally what I was looking for...

Thought that there's something wrong with my definition of previous period flatbar but just couldn't put my finger how to correctly define them...
Totally wouldn't have think of it if without your help...
Wow, small detail but big differences...
Really learn something new today...
Look obvious until u try it...

Million thanks...

Looking for flat base then breakout scan Questions 1
Looking For Highs And Lows Questions 3
Looking for tick script Questions 2
Looking For Vix Cantango Questions 1
Looking at cumulative volume Questions 1

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