OPEN-SOURCE SCRIPT

MACD Buy Sell with alerts

//version=5
indicator(title="Moving Average Convergence Divergence", shorttitle="MACD", timeframe="", timeframe_gaps=true)

// Getting inputs
fast_length = input(title = "Fast Length", defval = 12)
slow_length = input(title = "Slow Length", defval = 26)
src = input(title = "Source", defval = close)
signal_length = input.int(title = "Signal Smoothing", minval = 1, maxval = 50, defval = 9, display = display.data_window)
sma_source = input.string(title = "Oscillator MA Type", defval = "EMA", options = ["SMA", "EMA"], display = display.data_window)
sma_signal = input.string(title = "Signal Line MA Type", defval = "EMA", options = ["SMA", "EMA"], display = display.data_window)

// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal

// Alert conditions for histogram changes
alertcondition(hist[1] >= 0 and hist < 0, title='Rising to falling', message='The MACD histogram switched from a rising to falling state')
alertcondition(hist[1] <= 0 and hist > 0, title='Falling to rising', message='The MACD histogram switched from a falling to rising state')

// Buy and Sell Signals
buy_signal = (ta.crossover(macd, signal) and macd > 0) or ta.crossover(macd, 0)
sell_signal = (ta.crossunder(macd, signal) and macd < 0) or ta.crossunder(macd, 0)

// Alert conditions for buy and sell signals
alertcondition(buy_signal, title='Buy Alert', message='MACD crossed above the Signal line above zero or zero line - Buy Signal!')
alertcondition(sell_signal, title='Sell Alert', message='MACD crossed below the Signal line below zero or zero line - Sell Signal!')

// Plotting buy and sell signals
plotshape(buy_signal, title='Buy Signal', location=location.bottom, color=color.green, style=shape.labelup, text='BUY', textcolor=color.white, size=size.small)
plotshape(sell_signal, title='Sell Signal', location=location.top, color=color.red, style=shape.labeldown, text='SELL', textcolor=color.white, size=size.small)

// Plotting
hline(0, "Zero Line", color=color.new(#787B86, 50))
plot(hist, title="Histogram", style=plot.style_columns, color=(hist >= 0 ? (hist[1] < hist ? #26A69A : #B2DFDB) : (hist[1] < hist ? #FFCDD2 : #FF5252)))
plot(macd, title="MACD", color=#2962FF)
plot(signal, title="Signal", color=#FF6D00)


alertsetupalertsignalsbuymacdcrossovermacdivergenceMoving AveragesmultitimeframeOscillatorssellsignal

Mã nguồn mở

Theo tinh thần TradingView thực sự, tác giả của tập lệnh này đã xuất bản dưới dạng nguồn mở để các nhà giao dịch có thể hiểu và xác minh. Chúc mừng tác giả! Bạn có thể sử dụng miễn phí. Tuy nhiên, bạn cần sử dụng lại mã này theo Quy tắc nội bộ. Bạn có thể yêu thích nó để sử dụng nó trên biểu đồ.

Bạn muốn sử dụng tập lệnh này trên biểu đồ?

Thông báo miễn trừ trách nhiệm