Didi index alerts
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vitorleo

//@version=5
indicator('Didi index alerts', shorttitle='Didi alerts')

// Inputs for Didi Index 
curta = input.int(title="Fast average", defval=3, group="Didi index config")
media = input.int(title="Normal average", defval=8, group="Didi index config")
longa = input.int(title="Slow average", defval=20, group="Didi index config")
distMinCurta = input.float(title="Fast average", defval=-0.5, step=0.1, group="Minimum neddle distance", tooltip="Minimum number of previous bars where the needle occured.\nNegative and decimal numbers are allowed.\nDefault:-0.5")
distMinLonga = input.float(title="Slow average", defval=-0.5, step=0.1, group="Minimum neddle distance", tooltip="Minimum number of previous bars where the needle occured.\nNegative and decimal numbers are allowed.\nDefault:-0.5")
distMaxCurta = input.float(title="Fast average", defval=1.6, step=0.1, group="Maximum neddle distance", tooltip="Maximum number of previous bars where the needle occured.\nNegative and decimal numbers are allowed.\nDefault:1.6")
distMaxLonga = input.float(title="Slow average", defval=1.8, step=0.1, group="Maximum neddle distance", tooltip="Maximum number of previous bars where the needle occured.\nNegative and decimal numbers are allowed.\nDefault:1.8")

// Didi index Average normalisation
DidiLonga = (ta.sma(close, longa) / ta.sma(close, media) - 1) * 100
DidiCurta = (ta.sma(close, curta) / ta.sma(close, media) - 1) * 100
DidiPrice = (close / ta.sma(close, media) - 1) * 100

// Plots the lines
plot(DidiCurta, color=color.new(color.blue, 0), linewidth=2, title='Fast average')
plot(0, color=color.new(color.white, 0), title='Normal average')
plot(DidiLonga, color=color.new(color.fuchsia, 0), linewidth=2, title='Slow average')


// Alerts
longaUp = DidiLonga > DidiLonga[1]*1.03
longaDown = DidiLonga < DidiLonga[1]*1.03
alertaCompra = ta.crossover(DidiCurta, 0)
alertaVenda = ta.crossunder(DidiCurta, 0)
confirmaCompra = ta.crossunder(DidiLonga, 0)
confirmaVenda = ta.crossover(DidiLonga, 0)

// Fake long and short
compraFalsa = alertaCompra and longaUp and DidiLonga > DidiCurta 
vendaFalsa = alertaVenda and longaDown and DidiLonga < DidiCurta 

// Needle conditions
// Classic needles
agulhadaCruzamento = (alertaCompra and confirmaCompra) or (alertaVenda and confirmaVenda)

// Projected Needles
projCurta = DidiCurta[1]/(DidiCurta-DidiCurta[1])
projLonga = DidiLonga[1]/(DidiLonga-DidiLonga[1])
agulhadaProjCurta = projCurta > distMinCurta and projCurta < distMaxCurta
agulhadaProjLonga = projLonga > distMinLonga and projLonga < distMaxLonga
agulhadaProj = agulhadaProjCurta and agulhadaProjLonga

// Projected and crossing
projCrossLong = (confirmaCompra and agulhadaProjCurta) or (alertaCompra and agulhadaProjLonga)
projCrossShort = (confirmaVenda and agulhadaProjCurta) or (alertaVenda and agulhadaProjLonga)

// Direction of needles
didiComprado = DidiCurta > 0 and DidiLonga < 0
didiVendido = DidiCurta < 0 and DidiLonga > 0

//or (agulhadaProjLonga and alertaCompra) or (agulhadaProjCurta and confirmaCompra)
agulhadaCompra = didiComprado and (agulhadaProj or agulhadaCruzamento or projCrossLong)
agulhadaVenda =  didiVendido and (agulhadaProj or agulhadaCruzamento or projCrossShort)

plotchar(projCurta, 'Fast average projection', '', location.top, color.blue)
plotchar(projLonga, 'Slow average projection', '', location.top, color.red)

plotshape(agulhadaVenda, 'Short Needle', shape.triangledown, location.bottom, color.new(color.red, 20), size=size.tiny)
plotshape(agulhadaCompra, 'Long Needle', shape.triangleup, location.bottom, color.new(color.green, 20), size=size.tiny)

bgcolor(agulhadaCompra ? color.new(color.green, 60) : na, title='Long needles background')
bgcolor(agulhadaVenda ? color.new(color.red, 60) : na, title='Short needles background')

bgcolor(vendaFalsa ? color.new(color.green, 75) : na, title='Fake long background')
bgcolor(compraFalsa ? color.new(color.red, 75) : na, title='Fake short background')

alertcondition(agulhadaVenda, title='Short Needle (Agulhada de venda)', message='Short Needle')
alertcondition(agulhadaCompra, title='Long Needle  (Agulhada de compra)', message='Long Needle')
alertcondition(compraFalsa, title='Fake long (Compra falsa)', message='Fake long')
alertcondition(vendaFalsa, title='Fake short (Venda falsa)', message='Fake short') 