Moving Average Breakout

Trigun1127

Member
Could someone edit this code so that the MA only changes colors when there are 2 consecutive green closes above and 2 consecutive red closes below.

plot myindicator=ExpAverage(close,20);
myindicator.SetDefaultColor(Color.White);
myindicator.AssignValueColor(if close>=myindicator then Color.Green else Color.Red);
myindicator.SetStyle(curve.LONG_DASH);
myindicator.SetPaintingStrategy(paintingstrategy.LINE_VS_SQUARES);
myindicator.SetLineWeight(5);
myindicator.setHiding(if close>open(period="DAY") then 0 else 1);
 
Solution
Perhaps there's a misunderstanding for image 1. The Low of the bar I don't believe should be part of the criteria. Just the Close of the bar. 2 consecutive green closes above the moving average. The moving average here was 3859.45 as you pointed out but the close of the bars were C: 3865.25 and C: 3866.25

Changed to 2 bar criteria as noted above.

Ruby:
#1. If the day opens below the EMA Line = Red
#2. If day opens above EMA Line = Green
#3. If above and Green only 2 bar breakouts below to red are possible.
#4. If below and Red only 2 bar breakouts above to green are possible.
#5. Pictures show where these things do not happen
input length1 = 20;
input avg     = AverageType.EXPONENTIAL;
input price1  = close;

plot...
SleepyZ : Thanks for your help with this label count. I did as you suggested and added your script to mine. It does add a count to the label. But unfortunately the count is incorrect. At the time of my posting this reply, it is 8:40 pm CT. My chart is showing 20 cyan ribbon bars. But the count displayed in the label is 71. Hopefully there is just some minor adjustment to your script to be done. I still very much appreciate your help though.

http://tos.mx/jL5iQ2w
 
As the use of [-1] causes the code to wait for the color after the current bar closes, this uses the color of the last closed bar for the current bar until it closes.
Please post the code to add to your script to create an audible alert with a message center alert that includes the stock symbol, current price, and "TREND CHANGE TO GREEN" or "TREND CHANGE TO RED". Thanks
 
Last edited:
SleepyZ : Thanks for your help with this label count. I did as you suggested and added your script to mine. It does add a count to the label. But unfortunately the count is incorrect. At the time of my posting this reply, it is 8:40 pm CT. My chart is showing 20 cyan ribbon bars. But the count displayed in the label is 71. Hopefully there is just some minor adjustment to your script to be done. I still very much appreciate your help though.

http://tos.mx/jL5iQ2w
As your original post referred to trend, the trend is for the whole day and therefore I did the count is for the entire day as cyan. How do you define the trend to limit the count?
 
As your original post referred to trend, the trend is for the whole day and therefore I did the count is for the entire day as cyan. How do you define the trend to limit the count?
I am using this MA ribbon study on a 10 min /ES chart, so the count would have to be for an intra-day chart rather than a daily chart. To answer your above question, I always use the AssignValueColor part of the script to determine the trend. And to determine my label count as well. I made two attempts to create this count script (lines 74 - 86), but both failed. The first script generated an incorrect count. The second script did not produce a count at all.

http://tos.mx/fV3G3jQ
 
Please post the code to add to your script to create an audible alert with a message center alert that includes the stock symbol, current price, and "TREND CHANGE TO GREEN" or "TREND CHANGE TO RED". Thanks

This seems to work

Ruby:
#1. If the day opens below the EMA Line = Red
#2. If day opens above EMA Line = Green
#3. If above and Green only 2 bar breakouts below to red are possible.
#4. If below and Red only 2 bar breakouts above to green are possible.
#5. Pictures show where these things do not happen
input length1 = 20;
input avg     = AverageType.EXPONENTIAL;
input price1  = close;

plot ma1 = MovingAverage(averagetype = avg, price1, length1);

input test = no;

def o = if GetDay() != GetDay()[1] then open else o[1];
def ma = if GetDay() != GetDay()[1] then ma1 else ma[1];
def trend = if SecondsFromTime(1600) <= 0 and o > ma then 1 else if SecondsFromTime(1600) <= 0 and o < ma then -1 else trend[1];
plot xtrend = if !test then Double.NaN else trend;
xtrend.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);


DefineGlobalColor("Long", Color.CYAN);
DefineGlobalColor("Short", Color.YELLOW);
DefineGlobalColor("Else", Color.WHITE);

# 2 consective bars red bars above ma1, including cross above
def currentbarAbove = if close >= open and close > ma1 then 1 else 0;
def onebarafterAbove = if close >= open and currentbarAbove[1] and close > ma1  then 1 else 0;

# 2 consective bars red bars below ma1, including cross below
def currentbarBelow = if close < open and close < ma1 then 1 else  0;
def onebarafterBelow = if currentbarBelow[1] and close < open and close < ma1 then 1 else 0;

def malinecolor = if IsNaN(malinecolor[1])
                  then 0
                  else if GetDay() != GetDay()[1] and trend == 1
                  then 1
                  else if trend == 1 and onebarafterBelow[-1] or onebarafterBelow
                  then 0
                  else if GetDay() != GetDay()[1] and trend == -1
                  then 0
                  else if trend == -1 and onebarafterAbove[-1] or onebarafterAbove
                  then 1                 
                  else malinecolor[1];

def lastbar     = BarNumber() == HighestAll(if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN);
ma1.AssignValueColor(if lastbar and malinecolor[1] == 1 then Color.GREEN else if lastbar and malinecolor[1] == 0 then Color.RED else if malinecolor then Color.GREEN else if malinecolor == 0 then Color.RED else GlobalColor("Else"));
ma1.SetPaintingStrategy(paintingStrategy.LINE_VS_SQUARES);
ma1.SetLineWeight(5);


AssignPriceColor(if !test then Color.CURRENT else if  onebarafterAbove[-1] or onebarafterAbove then GlobalColor("Long") else if onebarafterBelow[-1] or onebarafterBelow then GlobalColor("Short") else Color.CURRENT);

alert(malinecolor[2]==1 and malinecolor[1]==0 , "TREND CHANGE TO RED " + CLOSE, Alert.bar, Sound.Chimes);
alert(malinecolor[2]==0 and malinecolor[1]==1 , "TREND CHANGE TO GREEN " + CLOSE, Alert.bar, Sound.Chimes);
#
caapture.jpg
 
I am using this MA ribbon study on a 10 min /ES chart, so the count would have to be for an intra-day chart rather than a daily chart. To answer your above question, I always use the AssignValueColor part of the script to determine the trend. And to determine my label count as well. I made two attempts to create this count script (lines 74 - 86), but both failed. The first script generated an incorrect count. The second script did not produce a count at all.

http://tos.mx/fV3G3jQ

See if this is what you are looking to count.

I just limited it to the cyan color at this time. The input lookback set to 0 will find and count the cyan colored candles for the day, defined by the getday{} function.

The image shows the count. As [-1] is part of malinecolor logic the count will start at 0 even though it is a cyan candle and then will continue to count until the end where it will pick up the [-1] count even the color of the line may be the other color.

caapture.jpg
Ruby:
# from UseThinkScript.com
# https://usethinkscript.com/threads/moving-average-breakout.12746/?utm_source=threadloom&utm_medium=email&utm_campaign=ed1906&utm_content=iss74#post-108756
# scripted by SleepyZ

#1. If the day opens below the EMA Line = Red
#2. If day opens above EMA Line = Green
#3. If above and Green only 2 bar breakouts below to red are possible.
#4. If below and Red only 2 bar breakouts above to green are possible.
#5. Pictures show where these things do not happen
## Test removed by ZupDog

input paintbars = no;
input Label = yes;
input showAlerts = no;
input length1 = 20;
input avg     = AverageType.EXPONENTIAL;
input price1  = close;

plot ma1 = MovingAverage(averagetype = avg, price1, length1);

#input test = no;

def o = if GetDay() != GetDay()[1] then open else o[1];
def ma = if GetDay() != GetDay()[1] then ma1 else ma[1];
def trend = if SecondsFromTime(1600) <= 0 and o > ma then 1 else if SecondsFromTime(1600) <= 0 and o < ma then -1 else trend[1];
#plot xtrend = if !test then Double.NaN else trend;
#xtrend.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);

DefineGlobalColor("Long", Color.CYAN);
DefineGlobalColor("Short", Color.YELLOW);
DefineGlobalColor("Else", Color.WHITE);

# 2 consective bars red bars above ma1, including cross above
def currentbarAbove = if close >= open and close > ma1 then 1 else 0;
def onebarafterAbove = if close >= open and currentbarAbove[1] and close > ma1  then 1 else 0;

# 2 consective bars red bars below ma1, including cross below
def currentbarBelow = if close < open and close < ma1 then 1 else  0;
def onebarafterBelow = if currentbarBelow[1] and close < open and close < ma1 then 1 else 0;

def malinecolor = if IsNaN(malinecolor[1])
                  then 0
                  else if GetDay() != GetDay()[1] and trend == 1
                  then 1
                  else if trend == 1 and onebarafterBelow[-1] or onebarafterBelow
                  then 0
                  else if GetDay() != GetDay()[1] and trend == -1
                  then 0
                  else if trend == -1 and onebarafterAbove[-1] or onebarafterAbove
                  then 1                
                  else malinecolor[1];

def lastbar     = BarNumber() == HighestAll(if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN);

ma1.DefineColor("Up", GetColor(1));
ma1.DefineColor("Down", GetColor(0));

#ma1.AssignValueColor(if lastbar and malinecolor[1] == 1 then Color.WHITE else if lastbar and malinecolor[1] == 0 then Color.PLUM else if malinecolor then Color.WHITE else if malinecolor == 0 then Color.PLUM else GlobalColor("Else"));

ma1.AssignValueColor(if lastbar and malinecolor[1] == 1 then ma1.Color("Up") else if lastbar and malinecolor[1] == 0 then ma1.Color("Down") else if malinecolor then ma1.Color("Up") else if malinecolor == 0 then ma1.Color("Down") else GlobalColor("Else"));
ma1.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
ma1.SetLineWeight(5);

Alert( ((showAlerts and lastbar and malinecolor[1] crosses above 1 ) ) , " Trigun1127_MA Bullish", Alert.BAR, Sound.Ring );
Alert( ((showAlerts and lastbar and malinecolor[1] crosses below 0 ) ) , " Trigun1127_MA Bearish", Alert.BAR, Sound.Ring );

#AddLabel(Label, "Trigun1127_MA " , if lastbar and malinecolor[1] == 1 then Color.CYAN else Color.YELLOW );

## my 1st label count attempt :

rec countup = if lastbar and malinecolor[1] == 1 then  1 else countup[1] + 1;
rec countdn = if lastbar and malinecolor[1] == 0 then -1 else countdn[1] - 1;

AddLabel(Label, Concat("Trigun1127_MA  ", if lastbar and malinecolor[1] == 1 then  countup else countdn), if lastbar and malinecolor[1] == 1  then Color.GREEN else if lastbar and  malinecolor[1] == 0 then Color.PLUM else Color.WHITE );

## My second label count attempt :

#rec countup = if lastbar and malinecolor[1] crosses above 1 then  1 else countup[1] + 1;
#rec countdn = if lastbar and malinecolor[1] crosses below 0 then -1 else countdn[1] - 1;

#AddLabel(label, Concat("Trigun1127_MA  ", if lastbar and malinecolor[1] > 1 then  countup else countdn), if lastbar and malinecolor[1] > 1  then Color.GREEN else if lastbar and  malinecolor[1] < 0 then Color.PLUM else Color.WHITE );

input lookback = 0;
def ymd        = GetDay();
def candles    = !IsNaN(close);
def capture    = candles and ymd != ymd[1];
def dayCount   = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay    = (HighestAll(dayCount) - dayCount) ;

plot xup = if thisDay > lookback then 0 else if thisDay == lookback and  malinecolor == 1 then 1 else 0;
def x1   = if thisDay == lookback and  GetValue(xup, 1) == 1 then x1[1] + 1 else x1[1];
plot xx  = x1;
xx.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);

AddLabel(1, x1, Color.cyan);
 
Last edited by a moderator:
Is there a way to make a scanner that will scan for stocks that are above the 50 SMA but below the 9 weighted moving average? And also, is there a way to setup a watchlist to do the same?
 
Just a small favor, could you please insert the stock symbol just before "Trend Change to Red/Trend Change to Green"? Thanks

This should do that

Screenshot-2022-10-12-091248.png
Ruby:
#1. If the day opens below the EMA Line = Red
#2. If day opens above EMA Line = Green
#3. If above and Green only 2 bar breakouts below to red are possible.
#4. If below and Red only 2 bar breakouts above to green are possible.
#5. Pictures show where these things do not happen
input length1 = 20;
input avg     = AverageType.EXPONENTIAL;
input price1  = close;

plot ma1 = MovingAverage(averagetype = avg, price1, length1);

input test = no;

def o = if GetDay() != GetDay()[1] then open else o[1];
def ma = if GetDay() != GetDay()[1] then ma1 else ma[1];
def trend = if SecondsFromTime(1600) <= 0 and o > ma then 1 else if SecondsFromTime(1600) <= 0 and o < ma then -1 else trend[1];
plot xtrend = if !test then Double.NaN else trend;
xtrend.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);


DefineGlobalColor("Long", Color.CYAN);
DefineGlobalColor("Short", Color.YELLOW);
DefineGlobalColor("Else", Color.WHITE);

# 2 consective bars red bars above ma1, including cross above
def currentbarAbove = if close >= open and close > ma1 then 1 else 0;
def onebarafterAbove = if close >= open and currentbarAbove[1] and close > ma1  then 1 else 0;

# 2 consective bars red bars below ma1, including cross below
def currentbarBelow = if close < open and close < ma1 then 1 else  0;
def onebarafterBelow = if currentbarBelow[1] and close < open and close < ma1 then 1 else 0;

def malinecolor = if IsNaN(malinecolor[1])
                  then 0
                  else if GetDay() != GetDay()[1] and trend == 1
                  then 1
                  else if trend == 1 and onebarafterBelow[-1] or onebarafterBelow
                  then 0
                  else if GetDay() != GetDay()[1] and trend == -1
                  then 0
                  else if trend == -1 and onebarafterAbove[-1] or onebarafterAbove
                  then 1                
                  else malinecolor[1];

def lastbar     = BarNumber() == HighestAll(if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN);
ma1.AssignValueColor(if lastbar and malinecolor[1] == 1 then Color.GREEN else if lastbar and malinecolor[1] == 0 then Color.RED else if malinecolor then Color.GREEN else if malinecolor == 0 then Color.RED else GlobalColor("Else"));
ma1.SetPaintingStrategy(paintingStrategy.LINE_VS_SQUARES);
ma1.SetLineWeight(5);


AssignPriceColor(if !test then Color.CURRENT else if  onebarafterAbove[-1] or onebarafterAbove then GlobalColor("Long") else if onebarafterBelow[-1] or onebarafterBelow then GlobalColor("Short") else Color.CURRENT);

alert(malinecolor[2]==1 and malinecolor[1]==0 , "TREND CHANGE TO RED " + CLOSE, Alert.bar, Sound.Chimes);
alert(malinecolor[2]==0 and malinecolor[1]==1 , "TREND CHANGE TO GREEN " + CLOSE, Alert.bar, Sound.Chimes);

input showbubble = yes;
input bubble_updown = 2;
addchartBubble(malinecolor[1]==1 and malinecolor==0, ma1 + ticksize() * bubble_updown, getsymbol(), color.red);
addchartBubble(malinecolor[1]==0 and malinecolor==1, ma1 - ticksize() * bubble_updown, getsymbol(), color.green, no);
 
This should do that
Hi, thanks but I'm sorry that I wasn't clear. When the moving average changes color I get a message in the message center that states, "Trend Change to Green (or Red)" and then I have to look through the message, which includes the particulars about the chart and script, for the stock symbol that this message refers to. Instead, I would like the message in the message center to read for example, "AAPL Trend Change to Green" or "AAPL Trend Change to Red", so the stock symbol appears before the trend change message. Thanks for your help.
 
Hi, thanks but I'm sorry that I wasn't clear. When the moving average changes color I get a message in the message center that states, "Trend Change to Green (or Red)" and then I have to look through the message, which includes the particulars about the chart and script, for the stock symbol that this message refers to. Instead, I would like the message in the message center to read for example, "AAPL Trend Change to Green" or "AAPL Trend Change to Red", so the stock symbol appears before the trend change message. Thanks for your help.

Ah! This should do that now

Screenshot-2022-10-12-175546.png
Ruby:
#1. If the day opens below the EMA Line = Red
#2. If day opens above EMA Line = Green
#3. If above and Green only 2 bar breakouts below to red are possible.
#4. If below and Red only 2 bar breakouts above to green are possible.
#5. Pictures show where these things do not happen
input length1 = 20;
input avg     = AverageType.EXPONENTIAL;
input price1  = close;

plot ma1 = MovingAverage(averagetype = avg, price1, length1);

input test = no;

def o = if GetDay() != GetDay()[1] then open else o[1];
def ma = if GetDay() != GetDay()[1] then ma1 else ma[1];
def trend = if SecondsFromTime(1600) <= 0 and o > ma then 1 else if SecondsFromTime(1600) <= 0 and o < ma then -1 else trend[1];
plot xtrend = if !test then Double.NaN else trend;
xtrend.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);


DefineGlobalColor("Long", Color.CYAN);
DefineGlobalColor("Short", Color.YELLOW);
DefineGlobalColor("Else", Color.WHITE);

# 2 consective bars red bars above ma1, including cross above
def currentbarAbove = if close >= open and close > ma1 then 1 else 0;
def onebarafterAbove = if close >= open and currentbarAbove[1] and close > ma1  then 1 else 0;

# 2 consective bars red bars below ma1, including cross below
def currentbarBelow = if close < open and close < ma1 then 1 else  0;
def onebarafterBelow = if currentbarBelow[1] and close < open and close < ma1 then 1 else 0;

def malinecolor = if IsNaN(malinecolor[1])
                  then 0
                  else if GetDay() != GetDay()[1] and trend == 1
                  then 1
                  else if trend == 1 and onebarafterBelow[-1] or onebarafterBelow
                  then 0
                  else if GetDay() != GetDay()[1] and trend == -1
                  then 0
                  else if trend == -1 and onebarafterAbove[-1] or onebarafterAbove
                  then 1               
                  else malinecolor[1];

def lastbar     = BarNumber() == HighestAll(if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN);
ma1.AssignValueColor(if lastbar and malinecolor[1] == 1 then Color.GREEN else if lastbar and malinecolor[1] == 0 then Color.RED else if malinecolor then Color.GREEN else if malinecolor == 0 then Color.RED else GlobalColor("Else"));
ma1.SetPaintingStrategy(paintingStrategy.LINE_VS_SQUARES);
ma1.SetLineWeight(5);


AssignPriceColor(if !test then Color.CURRENT else if  onebarafterAbove[-1] or onebarafterAbove then GlobalColor("Long") else if onebarafterBelow[-1] or onebarafterBelow then GlobalColor("Short") else Color.CURRENT);


alert(malinecolor[2]==1 and malinecolor[1]==0 , getsymbol() + " TREND CHANGE TO RED " + CLOSE, Alert.bar, Sound.Chimes);
alert(malinecolor[2]==0 and malinecolor[1]==1 , getsymbol() + " TREND CHANGE TO GREEN " + CLOSE, Alert.bar, Sound.Chimes);
 
SleepZ: I decided to keep the bubbles with the stock symbol on my 5 min chart, but find the bubbles crowding the candles on my weekly charts. Could you please make it so that I can toggle the bubbles off and on at the chart setup? Thanks

Sure,

Ruby:
#1. If the day opens below the EMA Line = Red
#2. If day opens above EMA Line = Green
#3. If above and Green only 2 bar breakouts below to red are possible.
#4. If below and Red only 2 bar breakouts above to green are possible.
#5. Pictures show where these things do not happen
input length1 = 20;
input avg     = AverageType.EXPONENTIAL;
input price1  = close;

plot ma1 = MovingAverage(averagetype = avg, price1, length1);

input test = no;

def o = if GetDay() != GetDay()[1] then open else o[1];
def ma = if GetDay() != GetDay()[1] then ma1 else ma[1];
def trend = if SecondsFromTime(1600) <= 0 and o > ma then 1 else if SecondsFromTime(1600) <= 0 and o < ma then -1 else trend[1];
plot xtrend = if !test then Double.NaN else trend;
xtrend.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);


DefineGlobalColor("Long", Color.CYAN);
DefineGlobalColor("Short", Color.YELLOW);
DefineGlobalColor("Else", Color.WHITE);

# 2 consective bars red bars above ma1, including cross above
def currentbarAbove = if close >= open and close > ma1 then 1 else 0;
def onebarafterAbove = if close >= open and currentbarAbove[1] and close > ma1  then 1 else 0;

# 2 consective bars red bars below ma1, including cross below
def currentbarBelow = if close < open and close < ma1 then 1 else  0;
def onebarafterBelow = if currentbarBelow[1] and close < open and close < ma1 then 1 else 0;

def malinecolor = if IsNaN(malinecolor[1])
                  then 0
                  else if GetDay() != GetDay()[1] and trend == 1
                  then 1
                  else if trend == 1 and onebarafterBelow[-1] or onebarafterBelow
                  then 0
                  else if GetDay() != GetDay()[1] and trend == -1
                  then 0
                  else if trend == -1 and onebarafterAbove[-1] or onebarafterAbove
                  then 1               
                  else malinecolor[1];

def lastbar     = BarNumber() == HighestAll(if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN);
ma1.AssignValueColor(if lastbar and malinecolor[1] == 1 then Color.GREEN else if lastbar and malinecolor[1] == 0 then Color.RED else if malinecolor then Color.GREEN else if malinecolor == 0 then Color.RED else GlobalColor("Else"));
ma1.SetPaintingStrategy(paintingStrategy.LINE_VS_SQUARES);
ma1.SetLineWeight(5);


AssignPriceColor(if !test then Color.CURRENT else if  onebarafterAbove[-1] or onebarafterAbove then GlobalColor("Long") else if onebarafterBelow[-1] or onebarafterBelow then GlobalColor("Short") else Color.CURRENT);

alert(malinecolor[2]==1 and malinecolor[1]==0 , "TREND CHANGE TO RED " + CLOSE, Alert.bar, Sound.Chimes);
alert(malinecolor[2]==0 and malinecolor[1]==1 , "TREND CHANGE TO GREEN " + CLOSE, Alert.bar, Sound.Chimes);

input showbubble = yes;
input bubble_updown = 2;
addchartBubble(showbubble and malinecolor[1]==1 and malinecolor==0, ma1 + ticksize() * bubble_updown, getsymbol(), color.red);
addchartBubble(showbubble and malinecolor[1]==0 and malinecolor==1, ma1 - ticksize() * bubble_updown, getsymbol(), color.green, no);

input alerts = yes;
alert(alerts and malinecolor[2]==1 and malinecolor[1]==0 , getsymbol() + " TREND CHANGE TO RED " + CLOSE, Alert.bar, Sound.Chimes);
alert(alerts and malinecolor[2]==0 and malinecolor[1]==1 , getsymbol() + " TREND CHANGE TO GREEN " + CLOSE, Alert.bar, Sound.Chimes);
 
Thanks so much! Looking through your code, I realized that you had already made the bubbles an option in your previous edition. Now you have the alerts lines "Trend Change to Green (Red)" listed twice in your latest edition. I removed the first pair as this latest edition includes making these alerts an option in the script setup. This has become my favorite indicator. Thanks again
 
Thanks so much! Looking through your code, I realized that you had already made the bubbles an option in your previous edition. Now you have the alerts lines "Trend Change to Green (Red)" listed twice in your latest edition. I removed the first pair as this latest edition includes making these alerts an option in the script setup. This has become my favorite indicator. Thanks again
SleepyZ: I'm now using a second one of these moving average lines on the same chart for confirmation of the trend and tried to change the colors using the Globals that are at the bottom of the script setup. They are set at white, cyan, and yellow but show up as red and green on the chart. Please make the Globals active so that I can change the colors to the ones I prefer so that I can distinguish the two lines. Thanks
 
Thanks so much! Looking through your code, I realized that you had already made the bubbles an option in your previous edition. Now you have the alerts lines "Trend Change to Green (Red)" listed twice in your latest edition. I removed the first pair as this latest edition includes making these alerts an option in the script setup. This has become my favorite indicator. Thanks again
SleepyZ: I toggled the "showbubble" to No but I can still see the red and green bubbles with the stock symbol on the charts.
 
SleepyZ: I toggled the "showbubble" to No but I can still see the red and green bubbles with the stock symbol on the charts.

Here is the code in post #14 with bubble set to NO and they are not appearing. The original code had the input showbubble, but it was not made part of the bubble code as in post #14.

Also, I changed the moving average to use the global colors. I did not change the red/green for the bubbles. Thanks for notifying me of duplicate of alerts.


Screenshot-2022-10-14-162402.png
Ruby:
#1. If the day opens below the EMA Line = Red
#2. If day opens above EMA Line = Green
#3. If above and Green only 2 bar breakouts below to red are possible.
#4. If below and Red only 2 bar breakouts above to green are possible.
#5. Pictures show where these things do not happen
input length1 = 20;
input avg     = AverageType.EXPONENTIAL;
input price1  = close;

plot ma1 = MovingAverage(averagetype = avg, price1, length1);

input test = no;

def o = if GetDay() != GetDay()[1] then open else o[1];
def ma = if GetDay() != GetDay()[1] then ma1 else ma[1];
def trend = if SecondsFromTime(1600) <= 0 and o > ma then 1 else if SecondsFromTime(1600) <= 0 and o < ma then -1 else trend[1];
plot xtrend = if !test then Double.NaN else trend;
xtrend.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);


DefineGlobalColor("Long", Color.CYAN);
DefineGlobalColor("Short", Color.YELLOW);
DefineGlobalColor("Else", Color.WHITE);

# 2 consective bars red bars above ma1, including cross above
def currentbarAbove = if close >= open and close > ma1 then 1 else 0;
def onebarafterAbove = if close >= open and currentbarAbove[1] and close > ma1  then 1 else 0;

# 2 consective bars red bars below ma1, including cross below
def currentbarBelow = if close < open and close < ma1 then 1 else  0;
def onebarafterBelow = if currentbarBelow[1] and close < open and close < ma1 then 1 else 0;

def malinecolor = if IsNaN(malinecolor[1])
                  then 0
                  else if GetDay() != GetDay()[1] and trend == 1
                  then 1
                  else if trend == 1 and onebarafterBelow[-1] or onebarafterBelow
                  then 0
                  else if GetDay() != GetDay()[1] and trend == -1
                  then 0
                  else if trend == -1 and onebarafterAbove[-1] or onebarafterAbove
                  then 1               
                  else malinecolor[1];

def lastbar     = BarNumber() == HighestAll(if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN);
ma1.AssignValueColor(if lastbar and malinecolor[1] == 1 then globalcolor("Long") else if lastbar and malinecolor[1] == 0 then globalcolor("Short") else if malinecolor then globalcolor("Long") else if malinecolor == 0 then globalcolor("Short") else GlobalColor("Else"));
ma1.SetPaintingStrategy(paintingStrategy.LINE_VS_SQUARES);
ma1.SetLineWeight(5);


AssignPriceColor(if !test then Color.CURRENT else if  onebarafterAbove[-1] or onebarafterAbove then GlobalColor("Long") else if onebarafterBelow[-1] or onebarafterBelow then GlobalColor("Short") else Color.CURRENT);


input showbubble = yes;
input bubble_updown = 2;
addchartBubble(showbubble and malinecolor[1]==1 and malinecolor==0, ma1 + ticksize() * bubble_updown, getsymbol(), color.red);
addchartBubble(showbubble and malinecolor[1]==0 and malinecolor==1, ma1 - ticksize() * bubble_updown, getsymbol(), color.green, no);

input alerts = yes;
alert(alerts and malinecolor[2]==1 and malinecolor[1]==0 , getsymbol() + " TREND CHANGE TO RED " + CLOSE, Alert.bar, Sound.Chimes);
alert(alerts and malinecolor[2]==0 and malinecolor[1]==1 , getsymbol() + " TREND CHANGE TO GREEN " + CLOSE, Alert.bar, Sound.Chimes);
 
Hey SleepyZ Im trying to get this to work but its not plotting. I'm trying to keep everything the exact same but I want to add a third condition after 2 consecutive bull closes Above MA or 2 Consecutive Bears closes Below MA Id like 1 more bull bar If above and prior conditions met to have a OHLC completely above the MA. That bar can come anytime after the prior sequence. If it doesn't happen then the line color doesn't change to its opposite trend. This applies to bear conditions as well. I put my code in bold so you can see how bad I am at doing this.

#1. If the day opens below the EMA Line = Red
#2. If day opens above EMA Line = Green
#3. If above and Green only 2 bar breakouts below to red are possible.
#4. If below and Red only 2 bar breakouts above to green are possible.
#5. Pictures show where these things do not happen
input length1 = 20;
input avg = AverageType.EXPONENTIAL;
input price1 = close;

plot ma1 = MovingAverage(averagetype = avg, price1, length1);

input test = no;

def o = if GetDay() != GetDay()[1] then open else o[1];
def ma = if GetDay() != GetDay()[1] then ma1 else ma[1];
def trend = if SecondsFromTime(1600) <= 0 and o > ma then 1 else if SecondsFromTime(1600) <= 0 and o < ma then -1 else trend[1];
plot xtrend = if !test then Double.NaN else trend;
xtrend.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);


DefineGlobalColor("Long", Color.CYAN);
DefineGlobalColor("Short", Color.YELLOW);
DefineGlobalColor("Else", Color.WHITE);

# 2 consective bars red bars above ma1, including cross above as well as one bull bar thats low
def currentbarAbove = if close >= open and close > ma1 then 1 else 0;
def onebarafterAbove = if close >= open and currentbarAbove[1] and close > ma1 then 1 else 0;
def disconnectCandleAbove = if close >= open and onebarafterAbove[1] or onebarafterabove[2] and low > ma1 then 1 else 0;

# 2 consective bars red bars below ma1, including cross below
def currentbarBelow = if close < open and close < ma1 then 1 else 0;
def onebarafterBelow = if currentbarBelow[1] and close < open and close < ma1 then 1 else 0;
def disconnectCandlebelow = if close >= open and onebarafterBelow[1] or onebarafterBelow[2] and high < ma1 then 1 else 0;

def malinecolor = if IsNaN(malinecolor[1])
then 0
else if GetDay() != GetDay()[1] and trend == 1
then 1
else if trend == 1 and onebarafterBelow[-1] or onebarafterBelow
then 0
else if GetDay() != GetDay()[1] and trend == -1
then 0
else if trend == -1 and onebarafterAbove[-1] or onebarafterAbove
then 1
else malinecolor[1];

def lastbar = BarNumber() == HighestAll(if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN);
ma1.AssignValueColor(if lastbar and malinecolor[1] == 1 then Color.GREEN else if lastbar and malinecolor[1] == 0 then Color.RED else if malinecolor then Color.GREEN else if malinecolor == 0 then Color.RED else GlobalColor("Else"));
ma1.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
ma1.SetLineWeight(5);


AssignPriceColor(if !test then Color.CURRENT else if disconnectCandleabove[-1] or disconnectCandleabove then GlobalColor("Long") else if disconnectCandlebelow[-1] or disconnectCandlebelow then GlobalColor("Short") else Color.CURRENT);
#
 

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