Impulse Alerts - Riccardo Di Giacomo
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Riccardo Di Giacomo

//@version=5
indicator("Impulse Alerts - Riccardo Di Giacomo", "Impulse Alerts - Riccardo Di Giacomo", overlay=true)
ema260len = input.int(230, minval=1, title="Length")
ema260src = input(close, title="Source")
ema260offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)
ema260out = ta.ema(ema260src, ema260len)
colorBasis = low >= ema260out ? color.blue : color.rgb(249, 27, 27)
plot(ema260out, title="EMA", color=colorBasis, offset=ema260offset)

ema(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

typeEMA = input.string(title = "Method", defval = "EMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Smoothing")
ema260smoothingLength = input.int(title = "Length", defval = 5, minval = 1, maxval = 100, group="Smoothing")

ema260 = ema(ema260out, ema260smoothingLength, typeEMA)


//EMA9

ma9len = input.int(9, minval=1, title="Length")
ma9src = input(close, title="Source")
ma9offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)
ma9out = ta.ema(ma9src, ma9len)
plot(ma9out, color=color.rgb(45, 162, 240), title="EMA9", offset=ma9offset)

ma9(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

typeMA9 = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Smoothing")
ma9smoothingLength = input.int(title = "Length", defval = 5, minval = 1, maxval = 100, group="Smoothing")

ma9smoothingLine = ma9(ma9out, ma9smoothingLength, typeMA9)

//EMA21

ema21len = input.int(21, minval=1, title="Length")
ema21src = input(close, title="Source")
ema21offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)
ema21out = ta.ema(ema21src, ema21len)
plot(ema21out, color=color.rgb(245, 64, 64), title="MA", offset=ema21offset)

ema21(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

typeMA21 = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Smoothing")
ema21smoothingLength = input.int(title = "Length", defval = 5, minval = 1, maxval = 100, group="Smoothing")

ema21smoothingLine = ema21(ema21out, ema21smoothingLength, typeMA21)

//bollinger bands
bbsrc = input(close)
bblength = input.int(20, minval=1)
mult = input.float(2.0, minval=0.001, maxval=50)

basis = ta.sma(bbsrc, bblength)
dev = ta.stdev(bbsrc, bblength)
dev2 = mult*dev

upper1 = basis + dev
lower1 = basis - dev
upper2 = basis + dev2
lower2 = basis - dev2


pUpper1 = plot(upper1, color=color.rgb(127, 128, 133, 80), style=plot.style_line)
pUpper2 = plot(upper2, color=color.rgb(127, 128, 133, 80))
pLower1 = plot(lower1, color=color.rgb(127, 128, 133, 80), style=plot.style_line)
pLower2 = plot(lower2, color=color.rgb(127, 128, 133, 80))

fill(pUpper1, pUpper2, color=color.rgb(127, 128, 133, 80))
fill(pLower1, pLower2, color=color.rgb(127, 128, 133, 80))



//stoch

periodK = input.int(5, title="%K Length", minval=1)
smoothK = input.int(1, title="%K Smoothing", minval=1)
periodD = input.int(3, title="%D Smoothing", minval=1)
k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
d = ta.sma(k, periodD)
upperbandstoch = 80.0
lowerbandstoch = 20.0

//RSI


rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="MA Length", group="MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
isBB = maTypeInput == "Bollinger Bands"

//STOCASTICO RAGGIUNTO
buy_stoch = k <= lowerbandstoch
sell_stoch = k >= upperbandstoch


//SELL SETUP
sell_setup = false
 
if close < ema260 and close < ema21smoothingLine and ema21smoothingLine < ema260 and ma9smoothingLine < ema21smoothingLine and k > upperbandstoch and rsi < 50
    alert("Impulse - Sell Setup", alert.freq_once_per_bar)
    sell_setup := not sell_setup[1] and not sell_stoch[1] ? true : false
else 
    sell_setup := false

plotshape(sell_setup, style=shape.arrowdown, location=location.abovebar, color=color.red)

//BUY SETUP
buy_setup = false

if close > ema260 and close > ema21smoothingLine and ema21smoothingLine > ema260 and ma9smoothingLine > ema21smoothingLine and k < lowerbandstoch and rsi > 50
    alert("Impulse - Buy Setup", alert.freq_once_per_bar)
    buy_setup := not buy_setup[1] and not buy_stoch[1] ? true : false
else 
    buy_setup := false

plotshape(buy_setup, style=shape.arrowup, location=location.belowbar, color=color.blue)