# Alert When Price > 9ema after closing below it, in last 5 minutes.

#### KevinSammy

##### Member
Hello,

I am attempting to create a scanner to buy when shorts are potentially trapped.

My thought is to wait for the price to close below the 9ema, where shorts might jump in.
Then in the next 5 minutes, if the price goes above the 9ema I will buy in hopes that shorts are trapped.

I created the code, but am having trouble with getting the condition to check within the last 5 minutes. Can someone please help with this?

def ema9 = MovAvgExponential(close, 9, 0, no);
def CloseBelowNine = close < ema9;
def LastPrice = close;
def input bars_away = 5;
def input bars_count = yes;

def BuyOverNine = LastPrice crosses above CloseBelowNine and bars_away;

You should try using the within x bars clause...

You should try using the within x bars clause...

Hi @rad14733 - I tried doing it via the "within x bars" code, but unfortunately it didn't work. Do you have any other suggestions, please?

def ema9 = MovAvgExponential(close, 9, 0, no);
def CloseBelowNine = close < ema9;

def BuyOverNine = Price > CloseBelowNine within 5bars;

@KevinSammy I think you're looking for price crosses above ema9 within 5 bars.

@generic - there are two parts to this.
The second part is what you mentioned, where the price is above the 9ema within the previous 5 bars (def BuyOverNine = Price > CloseBelowNine within 5bars) .
The first part, however, is where the price is BELOW the 9ema (def CloseBelowNine). This is the part that's kicking my butt - how to look at the next 5 bars and alert if this condition is met. Thanks.

Last edited by a moderator:
@KevinSammy Crossing above already satisfies the price is below 9ema at some point within the 5 bars.

@KevinSammy Crossing above already satisfies the price is below 9ema at some point within the 5 bars.

No, unfortunately it does not.

If you say so. Are you looking for price to crosses below and above ema9 within 5 bars?

If you say so. Are you looking for price to crosses below and above ema9 within 5 bars?

Yes, the first step is for the price to close below the 9ema. This sets off a timer/counter that checks if the price closes ABOVE the 9ema in the next 5 candles. If it does, then it fires an alert. I can't code the second part correctly, so any help would be greatly appreciated!!

Here's the code, so that you guys/gals don't have to scroll up:

def ema9 = MovAvgExponential(close, 9, 0, no);
def CloseBelowNine = close < ema9;

def BuyOverNine = Price > CloseBelowNine within 5bars;

@KevinSammy Something like this...??? I think your logic was reversed... You want to know when close crosses back above ema9 after it has crossed below ema9 within the past 5 bars...

Ruby:
``````plot buyOver9 = close crosses below ExpAverage(close, 9) within 5 bars and close crosses above ExpAverage(close, 9);

@KevinSammy Something like this...??? I think your logic was reversed... You want to know when close crosses back above ema9 after it has crossed below ema9 within the past 5 bars...

Ruby:
``````plot buyOver9 = close crosses below ExpAverage(close, 9) within 5 bars and close crosses above ExpAverage(close, 9);

Can't believe that I missed that, tunnel vision did me in. Thank you!

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