2 Indicator Conditions = Alert

sbtyme

New member
Hello,
I'm trying to learn coding but I can't quite get this code to give me the proper alerts. The colors are working fine.

Thanks in advance.

Below is code to color bars based on the hull ma direction and code to color the chart background based on the heiken candle color.

I want the alarm ring to sound every time the hull ma direction and the heiken color background color match up. Example - Assume the hull is bullish but the heiken is bearish, then as soon as the heiken background color of light green (bullish) changes as well, the bell should sound.

Conversely, if the heiken background color is bullish and then the hull changes to bullish (violet), the bell would also sound. Same true on the inverse bearish scenarios.

The alert should only go off once when there is a match not repeat at the start of each new bar in a trend. However, the alert should not disable entirely so that it would sound once again the trend is over and a new alert condition is formed.

My Code:

input price = close;
input length = 9;
input displace = 0;

plot HMA = MovingAverage(AverageType.HULL, price, length)[-displace];

HMA.DefineColor("Up", GetColor(1));
HMA.DefineColor("Down", GetColor(0));
HMA.AssignValueColor(if HMA > HMA[1] then HMA.Color("Up") else HMA.Color("Down"));
def bullish = HMA > HMA[1];
def bearish = HMA < HMA[1];

AssignPriceColor(if bullish then Color.vIOLET else Color.RED);

def hmaAscending = HMA > HMA[1];
def hmaDescending = HMA < HMA[1];

def haClose = ohlc4;
def haOpen = (haOpen[1] + haClose[1]) / 2;
def haTrendUp = if haClose >= haOpen then 1 else 0;
def haTrendDn = if haClose < haOpen then 1 else 0;
def haSignal =
if haTrendUp == 1 and haTrendUp[1] == 1 then 1
else if haTrendDn == 1 and haTrendDn[1] == 1 then -1
else 0
;

AssignBackgroundColor(
if haSignal == 1 then Color.light_GREEN

else if haSignal == -1 then Color.PINK
else Color.WHITE
);

def bull = (haSignal == 1) and (hmaAscending and !hmaAscending[1]) or (hmaAscending) and (haSignal == 1 and !haSignal == 1[1]);
def bear = (haSignal == -1) and (hmaDescending and !hmaDescending[1]) or (haDescending) and (haSignal == -1 and !haSignal == -1[1]);

Alert (bull, text = Alert.BAR, sound = Sound.Ring);
Alert (bear, text = Alert.BAR, Sound = Sound.Ring);
 
Solution
Hello,
I'm trying to learn coding but I can't quite get this code to give me the proper alerts. The colors are working fine.

Thanks in advance.

Below is code to color bars based on the hull ma direction and code to color the chart background based on the heiken candle color.

I want the alarm ring to sound every time the hull ma direction and the heiken color background color match up. Example - Assume the hull is bullish but the heiken is bearish, then as soon as the heiken background color of light green (bullish) changes as well, the bell should sound.

Conversely, if the heiken background color is bullish and then the hull changes to bullish (violet), the bell would also sound. Same true on the inverse bearish scenarios.

The...
Hello,
I'm trying to learn coding but I can't quite get this code to give me the proper alerts. The colors are working fine.

Thanks in advance.

Below is code to color bars based on the hull ma direction and code to color the chart background based on the heiken candle color.

I want the alarm ring to sound every time the hull ma direction and the heiken color background color match up. Example - Assume the hull is bullish but the heiken is bearish, then as soon as the heiken background color of light green (bullish) changes as well, the bell should sound.

Conversely, if the heiken background color is bullish and then the hull changes to bullish (violet), the bell would also sound. Same true on the inverse bearish scenarios.

The alert should only go off once when there is a match not repeat at the start of each new bar in a trend. However, the alert should not disable entirely so that it would sound once again the trend is over and a new alert condition is formed.

My Code:

input price = close;
input length = 9;
input displace = 0;

plot HMA = MovingAverage(AverageType.HULL, price, length)[-displace];

HMA.DefineColor("Up", GetColor(1));
HMA.DefineColor("Down", GetColor(0));
HMA.AssignValueColor(if HMA > HMA[1] then HMA.Color("Up") else HMA.Color("Down"));
def bullish = HMA > HMA[1];
def bearish = HMA < HMA[1];

AssignPriceColor(if bullish then Color.vIOLET else Color.RED);

def hmaAscending = HMA > HMA[1];
def hmaDescending = HMA < HMA[1];

def haClose = ohlc4;
def haOpen = (haOpen[1] + haClose[1]) / 2;
def haTrendUp = if haClose >= haOpen then 1 else 0;
def haTrendDn = if haClose < haOpen then 1 else 0;
def haSignal =
if haTrendUp == 1 and haTrendUp[1] == 1 then 1
else if haTrendDn == 1 and haTrendDn[1] == 1 then -1
else 0
;

AssignBackgroundColor(
if haSignal == 1 then Color.light_GREEN

else if haSignal == -1 then Color.PINK
else Color.WHITE
);

def bull = (haSignal == 1) and (hmaAscending and !hmaAscending[1]) or (hmaAscending) and (haSignal == 1 and !haSignal == 1[1]);
def bear = (haSignal == -1) and (hmaDescending and !hmaDescending[1]) or (haDescending) and (haSignal == -1 and !haSignal == -1[1]);

Alert (bull, text = Alert.BAR, sound = Sound.Ring);
Alert (bear, text = Alert.BAR, Sound = Sound.Ring);


This seems to work after modifying the def statements for bull and bear. Optional bubbles were added to show when the bull/bear defs were equal to 1, causing the alerts to sound.

Screenshot 2023-10-13 133718.png
Code:
input price = close;
input length = 9;
input displace = 0;

plot HMA = MovingAverage(AverageType.HULL, price, length)[-displace];

HMA.DefineColor("Up", GetColor(1));
HMA.DefineColor("Down", GetColor(0));
HMA.AssignValueColor(if HMA > HMA[1] then HMA.Color("Up") else HMA.Color("Down"));
def bullish = HMA > HMA[1];
def bearish = HMA < HMA[1];

AssignPriceColor(if bullish then Color.VIOLET else Color.RED);

def hmaAscending = HMA > HMA[1];
def hmaDescending = HMA < HMA[1];

def haClose = ohlc4;
def haOpen = (haOpen[1] + haClose[1]) / 2;
def haTrendUp = if haClose >= haOpen then 1 else 0;
def haTrendDn = if haClose < haOpen then 1 else 0;
def haSignal =
if haTrendUp == 1 and haTrendUp[1] == 1 then 1
else if haTrendDn == 1 and haTrendDn[1] == 1 then -1
else 0
;

AssignBackgroundColor(if haSignal == 1 then Color.LIGHT_GREEN else if haSignal == -1 then Color.PINK else Color.WHITE );

def bull = (haSignal == 1) and (hmaAscending and !hmaAscending[1]) or (hmaAscending) and (haSignal == 1 and !haSignal[1] == 1);
def bear = (haSignal == -1) and (hmaDescending and !hmaDescending[1]) or (hmaDescending) and (haSignal == -1 and !haSignal[1] == -1);

input test = yes;
AddChartBubble(test and bull, high, bull, Color.WHITE);
AddChartBubble(test and bear, low, bear, Color.WHITE, no);

Alert (bull, text = Alert.BAR, sound = Sound.Ring);
Alert (bear, text = Alert.BAR, Sound = Sound.Ring);
 
Solution

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