Hold Above 9ema/20ema 2% consolidation before breakout?

jonathanharvest

New member
I have a code I wrote and i dunno if its working they way I want to to. I was hoping to have a stock that maintains its self above the 9 ema and or 20 ema for 4 or more bars (within a range of consolidation (maybe 1% or 2%), just before a breakout. Think you could help?

Code:
def openchange = close;

plot x = openchange;

input ema1_len = 9;
input ema2_len = 20;
input averageType = AverageType.EXPONENTIAL;

def ema1 = MovAvgExponential(length=ema1_len);
def ema2 = MovAvgExponential (length=ema2_len);

AssignBackgroundColor(

if openchange >= ema1 within 4 bars and openchange >= ema2 within 5 bars
then color.white
else color.Current);

x.AssignValueColor (

if openchange >= ema1 within 4 bars and openchange >= ema2 within 5 bars
then color.Black
else color.Current);
 
Last edited by a moderator:
So you want to highlight candlesticks that are above the 9 EMA (at least 4 bars) and 20 EMA (at least 5 bars)?

AssignPriceColor(if openchange >= ema1 within 4 bars and openchange >= ema2 within 5 bars then color.White else color.Current);
 
So you want to highlight candlesticks that are above the 9 EMA (at least 4 bars) and 20 EMA (at least 5 bars)?

AssignPriceColor(if openchange >= ema1 within 4 bars and openchange >= ema2 within 5 bars then color.White else color.Current);

I want to scan for stocks that hold a 9ema or a 20 ema that is also above the Vwap almost entering a wedge consolidation, that will create and obvious sign of a breakout.

The 4 or 5 bars was my way of telling the scanner has to hold more than 5 bars on top of the EMA.
 
@jonathanharvest You don't need any custom script for those conditions.

Here is an example: this condition will give me a list of stocks that are above the 9 EMA within the last 4 bars.

ioVHXq1.png


You can add multiple conditions into one scanner. Try it out.
 
So you want to highlight candlesticks that are above the 9 EMA (at least 4 bars) and 20 EMA (at least 5 bars)?

AssignPriceColor(if openchange >= ema1 within 4 bars and openchange >= ema2 within 5 bars then color.White else color.Current);
This is Beautiful . I was using TTM candles , I like this code better . No matter how much it bounce around if its under or over 9 ema it will stay that way for a while . I change it to 2 back up green , 3 bars down red . This way I won't freak out and get out of trade too early .
Thank you so much !!
 
this is an excellent code i used vwap instead of moving avg and getting very close to what im looking for thanks for your generosity of this script, this is really an amazing tool, and it didnt cause me to have a migraine trying to modify it for my particular scan!
 
This is Beautiful . I was using TTM candles , I like this code better . No matter how much it bounce around if its under or over 9 ema it will stay that way for a while . I change it to 2 back up green , 3 bars down red . This way I won't freak out and get out of trade too early .
Thank you so much !!
Mind sharing your full modified code ?
Thanks
 
Mind sharing your full modified code ?
Thanks
Here you go

def openchange = close;

plot x = openchange;

input ema1_len = 9;
input ema2_len = 20;
input averageType = AverageType.EXPONENTIAL;

def ema1 = MovAvgExponential(length=ema1_len);
def ema2 = MovAvgExponential (length=ema2_len);

AssignPriceColor(if openchange >= ema1 within 2 bars and openchange >= ema2 within 3 bars then color.GREEN else color.RED);
 
Here you go

def openchange = close;

plot x = openchange;

input ema1_len = 9;
input ema2_len = 20;
input averageType = AverageType.EXPONENTIAL;

def ema1 = MovAvgExponential(length=ema1_len);
def ema2 = MovAvgExponential (length=ema2_len);

AssignPriceColor(if openchange >= ema1 within 2 bars and openchange >= ema2 within 3 bars then color.GREEN else color.RED);
Thanks.

Trying to understand how this work, if you dont mind explaining.
 
Thanks.

Trying to understand how this work, if you dont mind explaining.
If the Price is above 9/20ema it will turn candles green and red if below 9/20ema. look back is for it to wait to turn green or red . Make sure to make your candles hollow and full bar so you can waitch if going up or down. I have hollow for up and full for down candles. Hope this helps
 
@nitrous

Here is my 9/20 ema cloud moded with chart lable. If above 9/20ema it will display 9/20ema green and red if below. You can change it to whatever ema you want to use.

# Moving Average Crossover With Arrows, Alerts, Crossing Count and Bubble at Cross and trend colors
# Mobius
# Chat Room Request 01.25.2017

input price = close;
input fastLength = 9;
input slowLength = 20;
input averageType = AverageType.EXPONENTIAL;

plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
FastMA.AssignValueColor(if FastMA > SlowMA then color.cyan else color.red);
SlowMA.AssignValueColor(if FastMA > SlowMA then color.green else color.RED);

plot ArrowUp = if FastMA crosses above SlowMA
then low
else double.nan;
ArrowUP.SetPaintingStrategy(PaintingStrategy.Arrow_UP);
ArrowUP.SetLineWeight(3);
ArrowUP.SetDefaultColor(Color.Green);
plot ArrowDN = if FastMA crosses below SlowMA
then high
else double.nan;
ArrowDN.SetPaintingStrategy(PaintingStrategy.Arrow_DOWN);
ArrowDN.SetLineWeight(3);
ArrowDN.SetDefaultColor(Color.Red);
Alert(ArrowUp, " ", Alert.Bar, Sound.Chimes);
Alert(ArrowDN, " ", Alert.Bar, Sound.Bell);
def countUP = if FastMA crosses above SlowMA
then 1
else if FastMA > SlowMA
then countUP[1] + 1
else if ArrowDN
then 0
else countUP[1];

def CrossBar = if FastMA crosses SlowMa
then barNumber()
else double.nan;

AddLabel(yes, if FastMA > SlowMA then " 9/20 EMA" else " 9/20 EMA", if FastMA >SlowMA then Color.GREEN else Color.RED);
AddCloud(FastMA, SlowMA, Color.Green,Color.Red);
# End Code
 
If the Price is above 9/20ema it will turn candles green and red if below 9/20ema. look back is for it to wait to turn green or red . Make sure to make your candles hollow and full bar so you can waitch if going up or down. I have hollow for up and full for down candles. Hope this helps
Thanks, so trying to break down the logic:

Candles will be green if the price stays above 9/20EMA for 2 bars and if they are 3 red bars and below 9/20EMA, candles turn red?

I have my green candles as hollow for up and red filled for down :)
 
Thanks.

Another questions, when you are defining 2 bars, is it the previous 2 bars it looks for price to be above 9/20EMA to paint the candle green?
It looks for the last two bars to confirm green above 9/20 and 3 bars to confirm red for a downtrend. That's what I understand, you can change it to 1 bar which will light up green or red as soon as if price below or above. Hope this helps, play around with settings and see which works best for your trading style.
 
It looks for the last two bars to confirm green above 9/20 and 3 bars to confirm red for a downtrend. That's what I understand, you can change it to 1 bar which will light up green or red as soon as if price below or above. Hope this helps, play around with settings and see which works best for your trading style.
See, thats where the code is throwng me off, maybe I am wrong:

AssignPriceColor(if openchange >= ema1 within 2 bars and openchange >= ema2 within 3 bars then color.GREEN else color.RED);

Im reading the above as, if ema1(9) is within2 bars, and ema2(20) is within 3 bars, then color green.. else red.. So is it combining ema1 2 bars and ema2 3 bars to paint the candle green?
 
See, thats where the code is throwng me off, maybe I am wrong:

AssignPriceColor(if openchange >= ema1 within 2 bars and openchange >= ema2 within 3 bars then color.GREEN else color.RED);

Im reading the above as, if ema1(9) is within2 bars, and ema2(20) is within 3 bars, then color green.. else red.. So is it combining ema1 2 bars and ema2 3 bars to paint the candle green?
When 9/20 ema close above it will look for last 2 candles before it turn green. When 9/20ema close below then it will look for last 3 candles before it turns candles red. @BenTen isn't that how it works?
 

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
532 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