Drop Below 9 ema and 20 ema on the Daily Candle

pgoebel6

New member
Hello, I am not familiar with code but I was hoping someone could help me with the process for writing this code. I am looking to scan the market at close for stocks that open above the 9 and 20 ema and closed below both of them. I also want the 9 ema to be higher than the 20 ema. I have attached a couple of screenshots of what I am looking for.
 

Attachments

  • Screen Shot 2023-09-27 at 6.26.53 PM.png
    Screen Shot 2023-09-27 at 6.26.53 PM.png
    186.5 KB · Views: 97
  • Screen Shot 2023-09-27 at 6.26.36 PM.png
    Screen Shot 2023-09-27 at 6.26.36 PM.png
    200.3 KB · Views: 96
  • Screen Shot 2023-09-27 at 6.26.13 PM.png
    Screen Shot 2023-09-27 at 6.26.13 PM.png
    155.8 KB · Views: 93
Hello, I am not familiar with code but I was hoping someone could help me with the process for writing this code. I am looking to scan the market at close for stocks that open above the 9 and 20 ema and closed below both of them. I also want the 9 ema to be higher than the 20 ema. I have attached a couple of screenshots of what I am looking for.

i made an upper study to test.
then made a column study.
then modified it to make a scan.

-------------------

possible scan study (not tested)


Code:
# scan_sell
#https://usethinkscript.com/threads/drop-below-9-ema-and-20-ema-on-the-daily-candle.16786/
#Drop Below 9 ema and 20 ema on the Daily Candle

def na = double.nan;
def bn = barnumber();

def price = close;

#--------------------
# avg 1 & 2

input ma1_len = 9;
def len1 = if ma1_len == 0 then 1 else ma1_len;
input ma1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, len1);

input ma2_len = 20;
def len2 = if ma2_len == 0 then 1 else ma2_len;
input ma2_type =  AverageType.EXPONENTIAL;
def ma2 = Movingaverage(ma2_type, price, len2);

def xup = ma1 crosses above ma2;
def xdwn = ma1 crosses below ma2;
def up = ma1 > ma2;
def dwn = ma1 < ma2;

#  open above the 9 and 20 ema and closed below both of them
def sell_rule1 = (open > ma1 and open > ma2 and close < ma1 and close < ma2);
#  also the 9 ema > 20 ema
def sell_rule2 = (ma1 > ma2);
def sell = (sell_rule1 and sell_rule2);

def buy_rule1 = (open < ma1 and open < ma2 and close > ma1 and close > ma2);
def buy_rule2 = (ma1 < ma2);
def buy = (buy_rule1 and buy_rule2);

plot z = if sell then 1 else 0;
#


--------------------------

test study
column study - set to day

zemax1
http://tos.mx/qmDAlnl

Code:
# zemax1

# mod the upper study for column
# price_below_ema_day_close

#https://usethinkscript.com/threads/drop-below-9-ema-and-20-ema-on-the-daily-candle.16786/
#Drop Below 9 ema and 20 ema on the Daily Candle
#pgoebel6  10/11

# scan the market at close for stocks that,
#  open above the 9 and 20 ema and closed below both of them
#  also the 9 ema > 20 ema


#def lastbar = !isnan(close[0]) and isnan(close[-1]);

def na = double.nan;
def bn = barnumber();

def price = close;

#--------------------
# avg 1 & 2

input ma1_len = 9;
def len1 = if ma1_len == 0 then 1 else ma1_len;
input ma1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, len1);

input ma2_len = 20;
def len2 = if ma2_len == 0 then 1 else ma2_len;
input ma2_type =  AverageType.EXPONENTIAL;
def ma2 = Movingaverage(ma2_type, price, len2);

def xup = ma1 crosses above ma2;
def xdwn = ma1 crosses below ma2;
def up = ma1 > ma2;
def dwn = ma1 < ma2;

#  open above the 9 and 20 ema and closed below both of them
def sell_rule1 = (open > ma1 and open > ma2 and close < ma1 and close < ma2);
#  also the 9 ema > 20 ema
def sell_rule2 = (ma1 > ma2);
def sell = (sell_rule1 and sell_rule2);

def buy_rule1 = (open < ma1 and open < ma2 and close > ma1 and close > ma2);
def buy_rule2 = (ma1 < ma2);
def buy = (buy_rule1 and buy_rule2);

plot z = if buy then 1 else if sell then 0.5 else 0;
z.setdefaultcolor(color.black);

AssignBackgroundColor(if buy then color.green else if sell then color.red else color.gray);
#


------------------------

test study - upper


Code:
#price_below_ema_day_close

#https://usethinkscript.com/threads/drop-below-9-ema-and-20-ema-on-the-daily-candle.16786/
#Drop Below 9 ema and 20 ema on the Daily Candle
#pgoebel6  10/11

# scan the market at close for stocks that,
#  open above the 9 and 20 ema and closed below both of them
#  also the 9 ema > 20 ema


#def lastbar = !isnan(close[0]) and isnan(close[-1]);

def na = double.nan;
def bn = barnumber();

def price = close;

#--------------------
# avg 1 & 2

input ma1_len = 9;
def len1 = if ma1_len == 0 then 1 else ma1_len;
input ma1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, len1);

input ma2_len = 20;
def len2 = if ma2_len == 0 then 1 else ma2_len;
input ma2_type =  AverageType.EXPONENTIAL;
def ma2 = Movingaverage(ma2_type, price, len2);

def xup = ma1 crosses above ma2;
def xdwn = ma1 crosses below ma2;
def up = ma1 > ma2;
def dwn = ma1 < ma2;

#  open above the 9 and 20 ema and closed below both of them
def sell_rule1 = (open > ma1 and open > ma2 and close < ma1 and close < ma2);
#  also the 9 ema > 20 ema
def sell_rule2 = (ma1 > ma2);
def sell = (sell_rule1 and sell_rule2);

def buy_rule1 = (open < ma1 and open < ma2 and close > ma1 and close > ma2);
def buy_rule2 = (ma1 < ma2);
def buy = (buy_rule1 and buy_rule2);

addchartbubble(1, low*0.995,
buy + "\n" +
sell
, (if buy then color.green else if sell then color.red else color.gray), no);


# AssignBackgroundColor

input show_lines = yes;
plot z1 = if show_lines and len1 > 1 then ma1 else na;
z1.setdefaultcolor(getcolor(1));
#z1.setlineweight(1);
z1.hidebubble();

plot z2 = if show_lines and len2 > 1 then ma2 else na;
z2.setdefaultcolor(getcolor(2));
#z2.setlineweight(1);
z2.hidebubble();
#
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
492 Online
Create Post

Similar threads

Similar threads

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