MACDLine moving above/below SignalLine then Alert

ponos1207

New member
VIP
Hello, I have a basic study that I would like to use as a baseline study and would like to add an alert to the study. What is that code to add into the script? Thanks!
 
Solution
I would like to have an alert when MACD crosses above or below the signal line. Here is the study that I am working with:

declare lower;

input fast_len = 12;
input slow_len = 26;
input signal_len = 9;
input atr_len = 26;
input overbought_bounds = 150;
input oversold_bounds = -150;
input price = close;

# Indicator calculation
def fast_ema = ExpAverage(price, fast_len);
def slow_ema = ExpAverage(price, slow_len);
def atr = ATR(atr_len);

def macd = ((fast_ema - slow_ema) / atr) * 100;
def signal = ExpAverage(macd, signal_len);
def hist = macd - signal;

# Plotting
plot MACDLine = macd;
plot SignalLine = signal;
plot Histogram = hist;
plot mylineup = 40;
plot mylinedown = -40;


MACDLine.SetDefaultColor(CreateColor(41, 98, 255))...
I would like to have an alert when MACD crosses above or below the signal line. Here is the study that I am working with:

Code:
declare lower;

input fast_len = 12;
input slow_len = 26;
input signal_len = 9;
input atr_len = 26;
input overbought_bounds = 150;
input oversold_bounds = -150;
input price = close;

# Indicator calculation
def fast_ema = ExpAverage(price, fast_len);
def slow_ema = ExpAverage(price, slow_len);
def atr = ATR(atr_len);

def macd = ((fast_ema - slow_ema) / atr) * 100;
def signal = ExpAverage(macd, signal_len);
def hist = macd - signal;

# Plotting
plot MACDLine = macd;
plot SignalLine = signal;
plot Histogram = hist;
plot mylineup = 40;
plot mylinedown = -40;


MACDLine.SetDefaultColor(CreateColor(41, 98, 255)); # Blue
SignalLine.SetDefaultColor(CreateColor(255, 109, 0)); # Orange

Histogram.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Histogram.SetLineWeight(3);
Histogram.AssignValueColor(if hist >= 0 then
(if hist > hist[1] then CreateColor(38, 166, 154) else CreateColor(178, 223, 219))
else
(if hist > hist[1] then CreateColor(255, 205, 210) else CreateColor(255, 82, 82)));

# Overbought and Oversold bounds
plot UpperBand = overbought_bounds;
plot LowerBand = oversold_bounds;


UpperBand.SetDefaultColor(CreateColor(255, 255, 255)); # White
LowerBand.SetDefaultColor(CreateColor(255, 255, 255)); # White
mylineup.SetDefaultColor(CreateColor(255, 255, 255)); # White
mylinedown.SetDefaultColor(CreateColor(255, 255, 255)); # White
UpperBand.SetStyle(Curve.SHORT_DASH);
LowerBand.SetStyle(Curve.SHORT_DASH);

# Filling between the lines and the bounds if overbought or oversold
AddCloud(if macd > overbought_bounds then macd else Double.NaN, overbought_bounds, CreateColor(255, 200, 200), CreateColor(255, 200, 200));
AddCloud(if macd < oversold_bounds then macd else Double.NaN, oversold_bounds, CreateColor(200, 255, 200), CreateColor(200, 255, 200));

# Extending out the boundaries
plot ExtendedUpperBand = overbought_bounds;
plot ExtendedLowerBand = oversold_bounds;

ExtendedUpperBand.SetDefaultColor(CreateColor(200, 200, 200));
ExtendedLowerBand.SetDefaultColor(CreateColor(200, 200, 200));
ExtendedUpperBand.SetStyle(Curve.LONG_DASH);
ExtendedLowerBand.SetStyle(Curve.LONG_DASH);
 
Last edited by a moderator:
I would like to have an alert when MACD crosses above or below the signal line. Here is the study that I am working with:

declare lower;

input fast_len = 12;
input slow_len = 26;
input signal_len = 9;
input atr_len = 26;
input overbought_bounds = 150;
input oversold_bounds = -150;
input price = close;

# Indicator calculation
def fast_ema = ExpAverage(price, fast_len);
def slow_ema = ExpAverage(price, slow_len);
def atr = ATR(atr_len);

def macd = ((fast_ema - slow_ema) / atr) * 100;
def signal = ExpAverage(macd, signal_len);
def hist = macd - signal;

# Plotting
plot MACDLine = macd;
plot SignalLine = signal;
plot Histogram = hist;
plot mylineup = 40;
plot mylinedown = -40;


MACDLine.SetDefaultColor(CreateColor(41, 98, 255)); # Blue
SignalLine.SetDefaultColor(CreateColor(255, 109, 0)); # Orange

Histogram.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Histogram.SetLineWeight(3);
Histogram.AssignValueColor(if hist >= 0 then
(if hist > hist[1] then CreateColor(38, 166, 154) else CreateColor(178, 223, 219))
else
(if hist > hist[1] then CreateColor(255, 205, 210) else CreateColor(255, 82, 82)));

# Overbought and Oversold bounds
plot UpperBand = overbought_bounds;
plot LowerBand = oversold_bounds;


UpperBand.SetDefaultColor(CreateColor(255, 255, 255)); # White
LowerBand.SetDefaultColor(CreateColor(255, 255, 255)); # White
mylineup.SetDefaultColor(CreateColor(255, 255, 255)); # White
mylinedown.SetDefaultColor(CreateColor(255, 255, 255)); # White
UpperBand.SetStyle(Curve.SHORT_DASH);
LowerBand.SetStyle(Curve.SHORT_DASH);

# Filling between the lines and the bounds if overbought or oversold
AddCloud(if macd > overbought_bounds then macd else Double.NaN, overbought_bounds, CreateColor(255, 200, 200), CreateColor(255, 200, 200));
AddCloud(if macd < oversold_bounds then macd else Double.NaN, oversold_bounds, CreateColor(200, 255, 200), CreateColor(200, 255, 200));

# Extending out the boundaries
plot ExtendedUpperBand = overbought_bounds;
plot ExtendedLowerBand = oversold_bounds;

ExtendedUpperBand.SetDefaultColor(CreateColor(200, 200, 200));
ExtendedLowerBand.SetDefaultColor(CreateColor(200, 200, 200));
ExtendedUpperBand.SetStyle(Curve.LONG_DASH);
ExtendedLowerBand.SetStyle(Curve.LONG_DASH);
Add this to the bottom of your script
Ruby:
# === ALERT CONDITIONS ===
def bullishCross = macd crosses above signal;
def bearishCross = macd crosses below signal;

def macdOverbought = macd > overbought_bounds;
def macdOversold = macd < oversold_bounds;

# === ALERT TRIGGERS ===
Alert(bullishCross, "MACD Bullish Cross", Alert.BAR, Sound.Ring);
Alert(bearishCross, "MACD Bearish Cross", Alert.BAR, Sound.Bell);

Alert(macdOverbought, "MACD Overbought", Alert.BAR, Sound.Chimes);
Alert(macdOversold, "MACD Oversold", Alert.BAR, Sound.Ding);
 
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
336 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