LinReg-MACD Alerts For ThinkOrSwim

https://www.tradingview.com/script/X9FzFyxt-LinReg-MACD-Alerts/

Code:
//@version=4
study(title="LinReg-MACD Alerts", shorttitle="LinReg-MACD Alerts", format=format.price, precision=4, overlay=true)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 200, defval = 11)
sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=true)
lin_reg = input(title="Lin Reg", type=input.bool, defval=true)
linreg_length = input(title="Linear Regression Length", type=input.integer, minval = 1, maxval = 200, defval = 11)
bopen = lin_reg ? linreg(open, linreg_length, 0) : open
bhigh = lin_reg ? linreg(high, linreg_length, 0) : high
blow = lin_reg ? linreg(low, linreg_length, 0) : low
bclose = lin_reg ? linreg(close, linreg_length, 0) : close
r = bopen < bclose
signal = sma_signal ? sma(bclose, signal_length) : ema(bclose, signal_length)

//---------------------------------------------
Cond2 = input(true,"Condition for the 2nd candle")
Cond3 = input(true,"Condition for 3rd candle")
//---------------------------------------------
m_fast_length = 12
m_slow_length = 26
m_src = close
m_signal_length = 9
m_fast_ma = ema(m_src, m_fast_length)
m_slow_ma = ema(m_src, m_slow_length)
m_macd = m_fast_ma - m_slow_ma
m_signal = ema(m_macd, m_signal_length)
m_hist = m_macd - m_signal
//-------------------------------------------------------------------------------------------
srcLSMA = input(close, title = "LSMA Source")
UseLSMA = input(false, title="Use LSMA filter")
lengthLSMA = input(title="LSMA Length", defval=25)
lsma = linreg(srcLSMA, lengthLSMA,0)
//---------------------------------------------
rr = not r
isBuy = (lsma > lsma[1] or not UseLSMA)  and crossover(m_hist,0) and ( (r and not r[1]) or (r and r[1] and not r[2] and Cond2 ) or (r and r[1] and r[2] and not r[3] and Cond3 ))
isSell = (lsma < lsma[1] or not UseLSMA) and crossunder(m_hist,0) and ( (rr and not rr[1]) or (rr and rr[1] and not rr[2] and Cond2 ) or (rr and rr[1] and rr[2] and not rr[3] and Cond3 ))

plotshape(isBuy ? 1 : na, style=shape.labelup, location=location.belowbar, size=size.normal, color=color.green, text="Buy",textcolor=color.white)
plotshape(isSell ? 1 : na, style=shape.labeldown, location=location.abovebar, size=size.normal, color=color.red, text="Sell",textcolor=color.white)
alertcondition(isBuy, "Buy Signal", "Buy Signal")
alertcondition(isSell, "Sell Signal", "Sell Signal")
check below.
CSS:
#https://www.tradingview.com/v/X9FzFyxt/
#//@barrettdenning
#study(title="LinReg-MACD Alerts", shorttitle="LinReg-MACD Alerts",
# Converted by Sam4Cok@Samer800     - 03/2023

input signal_length = 11;    # "Signal Smoothing"
input sma_signal = yes;      # "Simple MA (Signal Line)"
input lin_reg = yes;         # "Lin Reg"
input linreg_length = 11;    # "Linear Regression Length"
input ConditionFor2NdCandle = yes;           # "Condition for the 2nd candle"
input ConditionFor3RdCandle = yes;           # "Condition for 3rd candle"
input m_fast_length = 12;
input m_slow_length = 26;
input m_src = close;
input m_signal_length = 9;
input srcLSMA = close;       # "LSMA Source"
input UseLSMA = no;          # "Use LSMA filter"
input lengthLSMA = 25;       # "LSMA Length", defval=25

def na = Double.NaN;
def Cond2 = ConditionFor2ndCandle;
def Cond3 = ConditionFor3RdCandle;
def bopen  = if lin_reg then Inertia(open, linreg_length) else open;
def bhigh  = if lin_reg then Inertia(high, linreg_length) else high;
def blow   = if lin_reg then Inertia(low, linreg_length) else low;
def bclose = if lin_reg then Inertia(close, linreg_length) else close;

def signal = if sma_signal then Average(bclose, signal_length) else ExpAverage(bclose, signal_length);

#//---------------------------------------------

def m_fast_ma = ExpAverage(m_src, m_fast_length);
def m_slow_ma = ExpAverage(m_src, m_slow_length);
def m_macd = m_fast_ma - m_slow_ma;
def m_signal = ExpAverage(m_macd, m_signal_length);
def m_hist = m_macd - m_signal;
#//-------------------------------------------------------------------------------------------
def lsma = Inertia(srcLSMA, lengthLSMA);

#//---------------------------------------------
def r = bopen < bclose;
def rr = !r;
def isBuy = (lsma > lsma[1] or !UseLSMA)  and (m_hist crosses above 0) and ((r and !r[1]) or (r and r[1] and !r[2] and Cond2 ) or (r and r[1] and r[2] and !r[3] and Cond3 ));
def isSell = (lsma < lsma[1] or !UseLSMA) and (m_hist crosses below 0) and ( (rr and !rr[1]) or (rr and rr[1] and !rr[2] and Cond2 ) or (rr and rr[1] and rr[2] and !rr[3] and Cond3 ));


AddChartBubble(isBuy, low, "Buy", Color.GREEN, no);
AddChartBubble(isSell, high, "Sell", Color.RED, yes);

#--- END of CODE
 
https://www.tradingview.com/script/X9FzFyxt-LinReg-MACD-Alerts/

Code:
//@version=4
study(title="LinReg-MACD Alerts", shorttitle="LinReg-MACD Alerts", format=format.price, precision=4, overlay=true)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 200, defval = 11)
sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=true)
lin_reg = input(title="Lin Reg", type=input.bool, defval=true)
linreg_length = input(title="Linear Regression Length", type=input.integer, minval = 1, maxval = 200, defval = 11)
bopen = lin_reg ? linreg(open, linreg_length, 0) : open
bhigh = lin_reg ? linreg(high, linreg_length, 0) : high
blow = lin_reg ? linreg(low, linreg_length, 0) : low
bclose = lin_reg ? linreg(close, linreg_length, 0) : close
r = bopen < bclose
signal = sma_signal ? sma(bclose, signal_length) : ema(bclose, signal_length)

//---------------------------------------------
Cond2 = input(true,"Condition for the 2nd candle")
Cond3 = input(true,"Condition for 3rd candle")
//---------------------------------------------
m_fast_length = 12
m_slow_length = 26
m_src = close
m_signal_length = 9
m_fast_ma = ema(m_src, m_fast_length)
m_slow_ma = ema(m_src, m_slow_length)
m_macd = m_fast_ma - m_slow_ma
m_signal = ema(m_macd, m_signal_length)
m_hist = m_macd - m_signal
//-------------------------------------------------------------------------------------------
srcLSMA = input(close, title = "LSMA Source")
UseLSMA = input(false, title="Use LSMA filter")
lengthLSMA = input(title="LSMA Length", defval=25)
lsma = linreg(srcLSMA, lengthLSMA,0)
//---------------------------------------------
rr = not r
isBuy = (lsma > lsma[1] or not UseLSMA)  and crossover(m_hist,0) and ( (r and not r[1]) or (r and r[1] and not r[2] and Cond2 ) or (r and r[1] and r[2] and not r[3] and Cond3 ))
isSell = (lsma < lsma[1] or not UseLSMA) and crossunder(m_hist,0) and ( (rr and not rr[1]) or (rr and rr[1] and not rr[2] and Cond2 ) or (rr and rr[1] and rr[2] and not rr[3] and Cond3 ))

plotshape(isBuy ? 1 : na, style=shape.labelup, location=location.belowbar, size=size.normal, color=color.green, text="Buy",textcolor=color.white)
plotshape(isSell ? 1 : na, style=shape.labeldown, location=location.abovebar, size=size.normal, color=color.red, text="Sell",textcolor=color.white)
alertcondition(isBuy, "Buy Signal", "Buy Signal")
alertcondition(isSell, "Sell Signal", "Sell Signal")

check below.
CSS:
#https://www.tradingview.com/v/X9FzFyxt/
#//@barrettdenning
#study(title="LinReg-MACD Alerts", shorttitle="LinReg-MACD Alerts",
# Converted by Sam4Cok@Samer800     - 03/2023

input signal_length = 11;    # "Signal Smoothing"
input sma_signal = yes;      # "Simple MA (Signal Line)"
input lin_reg = yes;         # "Lin Reg"
input linreg_length = 11;    # "Linear Regression Length"
input ConditionFor2NdCandle = yes;           # "Condition for the 2nd candle"
input ConditionFor3RdCandle = yes;           # "Condition for 3rd candle"
input m_fast_length = 12;
input m_slow_length = 26;
input m_src = close;
input m_signal_length = 9;
input srcLSMA = close;       # "LSMA Source"
input UseLSMA = no;          # "Use LSMA filter"
input lengthLSMA = 25;       # "LSMA Length", defval=25

def na = Double.NaN;
def Cond2 = ConditionFor2ndCandle;
def Cond3 = ConditionFor3RdCandle;
def bopen  = if lin_reg then Inertia(open, linreg_length) else open;
def bhigh  = if lin_reg then Inertia(high, linreg_length) else high;
def blow   = if lin_reg then Inertia(low, linreg_length) else low;
def bclose = if lin_reg then Inertia(close, linreg_length) else close;

def signal = if sma_signal then Average(bclose, signal_length) else ExpAverage(bclose, signal_length);

#//---------------------------------------------

def m_fast_ma = ExpAverage(m_src, m_fast_length);
def m_slow_ma = ExpAverage(m_src, m_slow_length);
def m_macd = m_fast_ma - m_slow_ma;
def m_signal = ExpAverage(m_macd, m_signal_length);
def m_hist = m_macd - m_signal;
#//-------------------------------------------------------------------------------------------
def lsma = Inertia(srcLSMA, lengthLSMA);

#//---------------------------------------------
def r = bopen < bclose;
def rr = !r;
def isBuy = (lsma > lsma[1] or !UseLSMA)  and (m_hist crosses above 0) and ((r and !r[1]) or (r and r[1] and !r[2] and Cond2 ) or (r and r[1] and r[2] and !r[3] and Cond3 ));
def isSell = (lsma < lsma[1] or !UseLSMA) and (m_hist crosses below 0) and ( (rr and !rr[1]) or (rr and rr[1] and !rr[2] and Cond2 ) or (rr and rr[1] and rr[2] and !rr[3] and Cond3 ));


AddChartBubble(isBuy, low, "Buy", Color.GREEN, no);
AddChartBubble(isSell, high, "Sell", Color.RED, yes);

#--- END of CODE
you are awesome @samer800 thank you some much
 
check below.
CSS:
#https://www.tradingview.com/v/X9FzFyxt/
#//@barrettdenning
#study(title="LinReg-MACD Alerts", shorttitle="LinReg-MACD Alerts",
# Converted by Sam4Cok@Samer800     - 03/2023

input signal_length = 11;    # "Signal Smoothing"
input sma_signal = yes;      # "Simple MA (Signal Line)"
input lin_reg = yes;         # "Lin Reg"
input linreg_length = 11;    # "Linear Regression Length"
input ConditionFor2NdCandle = yes;           # "Condition for the 2nd candle"
input ConditionFor3RdCandle = yes;           # "Condition for 3rd candle"
input m_fast_length = 12;
input m_slow_length = 26;
input m_src = close;
input m_signal_length = 9;
input srcLSMA = close;       # "LSMA Source"
input UseLSMA = no;          # "Use LSMA filter"
input lengthLSMA = 25;       # "LSMA Length", defval=25

def na = Double.NaN;
def Cond2 = ConditionFor2ndCandle;
def Cond3 = ConditionFor3RdCandle;
def bopen  = if lin_reg then Inertia(open, linreg_length) else open;
def bhigh  = if lin_reg then Inertia(high, linreg_length) else high;
def blow   = if lin_reg then Inertia(low, linreg_length) else low;
def bclose = if lin_reg then Inertia(close, linreg_length) else close;

def signal = if sma_signal then Average(bclose, signal_length) else ExpAverage(bclose, signal_length);

#//---------------------------------------------

def m_fast_ma = ExpAverage(m_src, m_fast_length);
def m_slow_ma = ExpAverage(m_src, m_slow_length);
def m_macd = m_fast_ma - m_slow_ma;
def m_signal = ExpAverage(m_macd, m_signal_length);
def m_hist = m_macd - m_signal;
#//-------------------------------------------------------------------------------------------
def lsma = Inertia(srcLSMA, lengthLSMA);

#//---------------------------------------------
def r = bopen < bclose;
def rr = !r;
def isBuy = (lsma > lsma[1] or !UseLSMA)  and (m_hist crosses above 0) and ((r and !r[1]) or (r and r[1] and !r[2] and Cond2 ) or (r and r[1] and r[2] and !r[3] and Cond3 ));
def isSell = (lsma < lsma[1] or !UseLSMA) and (m_hist crosses below 0) and ( (rr and !rr[1]) or (rr and rr[1] and !rr[2] and Cond2 ) or (rr and rr[1] and rr[2] and !rr[3] and Cond3 ));


AddChartBubble(isBuy, low, "Buy", Color.GREEN, no);
AddChartBubble(isSell, high, "Sell", Color.RED, yes);

#--- END of CODE
hi @samer800 do you think it is possible to changes the Buy and Sell labels for a arrow? thank you in advance.
 
hi @samer800 do you think it is possible to changes the Buy and Sell labels for a arrow? thank you in advance.
use this.
CSS:
#https://www.tradingview.com/v/X9FzFyxt/
#//@barrettdenning
#study(title="LinReg-MACD Alerts", shorttitle="LinReg-MACD Alerts",
# Converted by Sam4Cok@Samer800     - 03/2023
input SignalStyle   = {Default Arrows, Bubbles};
input signal_length = 11;    # "Signal Smoothing"
input sma_signal = yes;      # "Simple MA (Signal Line)"
input lin_reg = yes;         # "Lin Reg"
input linreg_length = 11;    # "Linear Regression Length"
input ConditionFor2NdCandle = yes;           # "Condition for the 2nd candle"
input ConditionFor3RdCandle = yes;           # "Condition for 3rd candle"
input m_fast_length = 12;
input m_slow_length = 26;
input m_src = close;
input m_signal_length = 9;
input srcLSMA = close;       # "LSMA Source"
input UseLSMA = no;          # "Use LSMA filter"
input lengthLSMA = 25;       # "LSMA Length", defval=25

def na = Double.NaN;
def arrow = SignalStyle==SignalStyle.Arrows;
def Cond2 = ConditionFor2ndCandle;
def Cond3 = ConditionFor3RdCandle;
def bopen  = if lin_reg then Inertia(open, linreg_length) else open;
def bhigh  = if lin_reg then Inertia(high, linreg_length) else high;
def blow   = if lin_reg then Inertia(low, linreg_length) else low;
def bclose = if lin_reg then Inertia(close, linreg_length) else close;

def signal = if sma_signal then Average(bclose, signal_length) else ExpAverage(bclose, signal_length);

#//---------------------------------------------

def m_fast_ma = ExpAverage(m_src, m_fast_length);
def m_slow_ma = ExpAverage(m_src, m_slow_length);
def m_macd = m_fast_ma - m_slow_ma;
def m_signal = ExpAverage(m_macd, m_signal_length);
def m_hist = m_macd - m_signal;
#//-------------------------------------------------------------------------------------------
def lsma = Inertia(srcLSMA, lengthLSMA);

#//---------------------------------------------
def r = bopen < bclose;
def rr = !r;
def isBuy = (lsma > lsma[1] or !UseLSMA)  and (m_hist crosses above 0) and ((r and !r[1]) or (r and r[1] and !r[2] and Cond2 ) or (r and r[1] and r[2] and !r[3] and Cond3 ));
def isSell = (lsma < lsma[1] or !UseLSMA) and (m_hist crosses below 0) and ( (rr and !rr[1]) or (rr and rr[1] and !rr[2] and Cond2 ) or (rr and rr[1] and rr[2] and !rr[3] and Cond3 ));

plot buyArrow = if arrow and isBuy then low else na;
buyArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
buyArrow.SetDefaultColor(Color.CYAN);
plot sellArrow = if arrow and isSell then high else na;
sellArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
sellArrow.SetDefaultColor(Color.MAGENTA);

AddChartBubble(!arrow and isBuy, low, "Buy", Color.GREEN, no);
AddChartBubble(!arrow and isSell, high, "Sell", Color.RED, yes);

#--- END of CODE
 
Can somebody help me with this code? It used to work. It shows the level of the zero line on the MACD if it is above it or below it and by how much. It used to work when I first started using it, but now it says "exactly one plot expected."

#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;

plot Value = MACD(fastLength, slowLength, MACDLength, averageType).Value;
plot ZeroLine = 0;
plot CrossUp = if Value crosses above Zeroline then Zeroline else double.NaN ;
plot CrossDn = if Value crosses below Zeroline then Zeroline else double.NaN ;

Value.SetDefaultColor(GetColor(1));
ZeroLine.SetDefaultColor(GetColor(0));
CrossUp.SetDefaultColor(color.blue);
CrossDn.SetDefaultColor(color.dark_orange);

CrossUp.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
CrossDn.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);

# Alerts
Alert(Value crosses above 0, "Cross above MACD Alert", Alert.Bar, Sound.Chimes);
Alert(Value crosses below 0, "Cross below MACD Alert", Alert.Bar, Sound.Bell);
 
Can somebody help me with this code? It used to work. It shows the level of the zero line on the MACD if it is above it or below it and by how much. It used to work when I first started using it, but now it says "exactly one plot expected."

#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;

plot Value = MACD(fastLength, slowLength, MACDLength, averageType).Value;
plot ZeroLine = 0;
plot CrossUp = if Value crosses above Zeroline then Zeroline else double.NaN ;
plot CrossDn = if Value crosses below Zeroline then Zeroline else double.NaN ;

Value.SetDefaultColor(GetColor(1));
ZeroLine.SetDefaultColor(GetColor(0));
CrossUp.SetDefaultColor(color.blue);
CrossDn.SetDefaultColor(color.dark_orange);

CrossUp.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
CrossDn.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);

# Alerts
Alert(Value crosses above 0, "Cross above MACD Alert", Alert.Bar, Sound.Chimes);
Alert(Value crosses below 0, "Cross below MACD Alert", Alert.Bar, Sound.Bell);
try this

CSS:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;


plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot ZeroLine = 0;
def cUp = (Value crosses above 0);
def cDn = (Value crosses below 0);

plot CrossUp = if cUp then Zeroline else double.NaN ;
plot CrossDn = if cDn then Zeroline else double.NaN ;

CrossUp.SetLineWeight(2);
CrossDn.SetLineWeight(2);
Value.SetDefaultColor(GetColor(1));
ZeroLine.SetDefaultColor(GetColor(0));
CrossUp.SetDefaultColor(color.CYAN);
CrossDn.SetDefaultColor(color.dark_orange);

CrossUp.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
CrossDn.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);

# Alerts
Alert(cUp, "Cross above MACD Alert", Alert.Bar, Sound.Chimes);
Alert(cDn, "Cross below MACD Alert", Alert.Bar, Sound.Bell);
 
try this

CSS:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;


plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot ZeroLine = 0;
def cUp = (Value crosses above 0);
def cDn = (Value crosses below 0);

plot CrossUp = if cUp then Zeroline else double.NaN ;
plot CrossDn = if cDn then Zeroline else double.NaN ;

CrossUp.SetLineWeight(2);
CrossDn.SetLineWeight(2);
Value.SetDefaultColor(GetColor(1));
ZeroLine.SetDefaultColor(GetColor(0));
CrossUp.SetDefaultColor(color.CYAN);
CrossDn.SetDefaultColor(color.dark_orange);

CrossUp.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
CrossDn.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);

# Alerts
Alert(cUp, "Cross above MACD Alert", Alert.Bar, Sound.Chimes);
Alert(cDn, "Cross below MACD Alert", Alert.Bar, Sound.Bell);
It's the same. I think you copied and pasted the same code. I didn't see any difference in the code when I skimmed through it, but might have missed something. Regardless, it still says "Exactly one plot expected."
 
It's the same. I think you copied and pasted the same code. I didn't see any difference in the code when I skimmed through it, but might have missed something. Regardless, it still says "Exactly one plot expected."
I think you copied the code in the "scan". Such error usually appears if you have more than one plot in the scan.
 
I think you copied the code in the "scan". Such error usually appears if you have more than one plot in the scan.
No, I had this in a watchlist and it worked and I just noticed it stopped working. Is there a way to fix it to work in the watchlist? I just need something that shows if it is above or below the purple zero line on the MACD and by what amount. I had gotten it somewhere else, but don't remember where.
 

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