1

I am trying to only send BUY signal if signal line < 0 and MACD line crosses over signal line, and SELL signal when signal line > 0 and MACD crosses under signal line. My condition that I added doesn't seem to work.

//@version=5
indicator(title="MACD crossover overlay with alert v5", overlay=true)
fastMA = input.int(title="Fast moving average",  defval = 12, minval = 7)
slowMA = input.int(title="Slow moving average",  defval = 26, minval = 7)
signalLength = input.int(9, minval=1)
MacdControl = input(true, title="MACD/Histogram Control")

[currMacd,_,_] = ta.macd(close[0], fastMA, slowMA, signalLength)
[prevMacd,_,_] = ta.macd(close[1], fastMA, slowMA, signalLength)
signal = ta.ema(currMacd, signalLength)

// === MACD ===
[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)
macdCondbear= MacdControl ? signalLine > 0 ? true : false : true
macdCondbull= MacdControl ? signalLine < 0 ? true : false : true


crossoverBear = ta.cross(currMacd, signal) and currMacd < signal ? math.avg(currMacd, signal) : na and macdCondbear
crossoverBull = ta.cross(currMacd, signal) and currMacd > signal ? math.avg(currMacd, signal) : na and macdCondbull

plotshape(crossoverBear, title='MACD-BEAR', style=shape.triangledown, text='', location=location.abovebar, color=color.red, textcolor=color.black, size=size.tiny) 
plotshape(crossoverBull, title='MACD-BULL', style=shape.triangleup, text='', location=location.belowbar, color=color.green, textcolor=color.black, size=size.tiny) 

alertcondition(crossoverBear, "MACD Bear", "MACD Bearish Crossover")
alertcondition(crossoverBull, "MACD Bull", "MACD Bullish Crossover")

1 Answers1

0

Your conditions in your code have a little more than what you described.

You can simply use ta.crossover() and check if the signalLine is below/above zero.

// === MACD ===
[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)

crossoverBull = ta.crossover(macdLine, signalLine) and (signalLine < 0)
crossoverBear = ta.crossover(signalLine, macdLine) and (signalLine > 0)

plotshape(crossoverBear, title='MACD-BEAR', style=shape.triangledown, text='', location=location.abovebar, color=color.red, textcolor=color.black, size=size.tiny) 
plotshape(crossoverBull, title='MACD-BULL', style=shape.triangleup, text='', location=location.belowbar, color=color.green, textcolor=color.black, size=size.tiny) 

alertcondition(crossoverBear, "MACD Bear", "MACD Bearish Crossover")
alertcondition(crossoverBull, "MACD Bull", "MACD Bullish Crossover")
vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • This enters when signal line crosses over the 0 line. Maybe i wasn't clear in my og post. I want to BUY when signal line is below 0 and MACD crosses over the signal line. For the SELL, id like to first ensure that signal line is above 0 line and then wait for that cross of signal line over MACD. – Ruslan Ali-zade Dec 09 '22 at 18:41