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:

BenTen

Administrative
Staff member
Staff
VIP
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);
 

jonathanharvest

New member
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.
 

BenTen

Administrative
Staff member
Staff
VIP
@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.
 

s1111

Active member
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 !!
 

jaythedaytrader

New member
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!
 

nitrous

Member
VIP
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
 

s1111

Active member
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);
 

nitrous

Member
VIP
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.
 

s1111

Active member
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
 

s1111

Active member
@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
 

nitrous

Member
VIP
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 :)
 

s1111

Active member
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.
 

nitrous

Member
VIP
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?
 

s1111

Active member
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?
 
Top