Alxuse MACD for tutorial
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zarafshani20
//@version=5
indicator(title="Alxuse MACD for tutorial", timeframe = "")
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
upper_Band = input(title = "Upper Band", defval = 100)
lower_Band = input(title = "Lower Band", defval = -100)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
col_grow_above = input(#40af72, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#bdfacc, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#fac7cc, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#ca5b5b, "Fall", group="Histogram", inline="Below")
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
hline(0, "Middle Band", color=color.new(#787B86, 50))
hline(upper_Band, "Upper Band", color=color.new(#fc0202, 50))
hline(lower_Band, "Lower Band", color=color.new(#fc0202, 50))
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
plot(macd, title="MACD", color=color.red)
plot(signal, title="Signal", color=color.green)
Buy_Macd = ta.crossover(macd, signal) ? signal : na
Sell_Macd = ta.crossunder(macd, signal) ? signal : na
Long_Macd = Buy_Macd and Buy_Macd <= lower_Band
Short_Macd = Sell_Macd and Sell_Macd >= upper_Band
plot(Buy_Macd, title="Buy", color=color.green, style=plot.style_circles, linewidth=4)
plot(Sell_Macd, title="Sell", color=color.red, style=plot.style_circles, linewidth=4)
plotchar(Long_Macd, 'Long', '△', location.bottom, color.new(color.lime, 0), size=size.tiny)
plotchar(Short_Macd, 'Short', '▽', location.top, color.new(color.red, 0), size=size.tiny)
alertcondition(Long_Macd, 'Circle Green and Lower Band (Long)', 'Long, Crossover MACD <= Lower Band (Triangles Green)')
alertcondition(Short_Macd, 'Circle Red and Upper Band (Short)', 'Short, Crossunder MACD >= Upper Band (Triangles Red)')
alertcondition(Buy_Macd, 'Circle Green(Buy)', 'Buy, Crossover MACD')
alertcondition(Sell_Macd, 'Circle Red (Sell)', 'Sell, Crossunder MACD')




