Reputation: 1
Im relatively new to coding and am trying to figure out how to tell Pine Script to take the first instance the macD line is > and < the signal line to make an alert. Currently it shows a label every candle it is.
Trying to make "long" false after the first iteration, until "short" is true. To make only one alert per cross over.
Thank You to those who help. Ill give who ever solves this problem 15 xrp.
long = macd > signal
short = macd < signal
alertcondition(long, "Macd Long open", "message")
alertcondition(short, "Macd short open", "message")
if(long)
l = label.new(bar_index, high)
label.set_text(l, "buy@\n" + str.tostring(close))
label.set_color(l, color.green)
label.set_yloc(l, yloc.belowbar)
label.set_style(l, label.style_label_up)
if(short)
l = label.new(bar_index, high)
label.set_text(l, "sell@\n" + str.tostring(close))
label.set_color(l, color.red)
label.set_yloc(l, yloc.belowbar)
label.set_style(l, label.style_label_down)
'''
Upvotes: 0
Views: 313
Reputation: 122
You have to write the MACD calculation formula, so I create a list
[macdline, singalline, macdhist] = ta.macd(close, 12, 26, 9)
The basic equation is:
fast_length = input.int(title="Fast Length", defval=12)
slow_length = input.int(title="Slow Length", defval=26)
signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9)
// Calculating
fast_ma = ta.ema(fast_length)
slow_ma =ta.ema(slow_length)
macd = fast_ma - slow_ma
signal = ta.sma(macd, signal_length)
hist = macd - signal
plot(hist, title="Histogram")
plot(macd, title="MACD")
plot(signal, title="Signal")
I solved your code problem
//@version=5
indicator("My script", overlay=true)
[macdline, singalline, macdhist] = ta.macd(close, 12, 26, 9)
long = macdline > singalline
short = macdline < singalline
if(long)
l = label.new(bar_index, high)
label.set_text(l, "buy@\n" + str.tostring(close))
label.set_color(l, color.green)
label.set_yloc(l, yloc.belowbar)
label.set_style(l, label.style_label_up)
if(short)
l = label.new(bar_index, high)
label.set_text(l, "sell@\n" + str.tostring(close))
label.set_color(l, color.red)
label.set_yloc(l, yloc.abovebar)
label.set_style(l, label.style_label_down)
alertcondition(long, "Macd Long open", "message")
alertcondition(short, "Macd short open", "message")
Upvotes: 0