Didi index setup
// 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 Plus', shorttitle='Didi')

// Inputs for Didi Index
short = input.int(title="Fast average", defval=3, group="Didi index config")
mid = input.int(title="Normal average", defval=8, group="Didi index config")
long = input.int(title="Slow average", defval=20, group="Didi index config")

// Didi index Average normalisation
DidiLonga = (ta.sma(close, long) / ta.sma(close, mid) - 1) * 100
DidiCurta = (ta.sma(close, short) / ta.sma(close, mid) - 1) * 100
DidiPrice = (close / ta.sma(close, mid) - 1) * 100
DidiOpen = (open / ta.sma(close, mid) - 1) * 100
DidiHigh = (high / ta.sma(close, mid) - 1) * 100
DidiLow = (low / ta.sma(close, mid) - 1) * 100

// Plots the candles
plotcandle(DidiOpen, DidiHigh, DidiLow, DidiPrice, title='Candle shadows', color=color.new(open < close ? color.black : color.white, 80), wickcolor=color.new(color.white, 80), editable=false, bordercolor=color.new(color.white, 80))

// 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')

// DMI
// Inputs for DMI
dmi_len = input.int(8, minval=1, title='DI length', group="DMI/ADX config")
dmi_lensig = input.int(8, title='ADX smoothing', minval=1, maxval=50, group="DMI/ADX config")
colorUpTrend = input.color(color.new(color.blue, 85), "Up trend", group="DMI/ADX config")
colorDownTrend = input.color(color.new(#E040FB, 85), "Down trend", group="DMI/ADX config")

[diplus, diminus, adx] = ta.dmi(dmi_len, dmi_lensig)

// Logic to interpret DMI and define if there is trend or not and its strength/acceleration
tendenciaAcelerante() =>
    adx > adx[1] * 1.015 ? true : false
temTendencia() =>
    adx < 32 and tendenciaAcelerante() == false or adx < diplus and adx < diminus ? false : true
tendenciaCompra() =>
    temTendencia() and diplus > diminus
tendenciaVenda() =>
    temTendencia() and diplus < diminus
    
// Plot DMI in background    
plotchar(math.round(adx), 'ADX', '', location.top, editable=false)
bgcolor(tendenciaCompra() ? colorUpTrend : na, title='Up trend', editable=false)
bgcolor(tendenciaAcelerante() and tendenciaCompra() ? colorUpTrend : na, title='Strong Up trend', editable=false)
bgcolor(tendenciaVenda() ? colorDownTrend : na, title='Down trend', editable=false)
bgcolor(tendenciaAcelerante() and tendenciaVenda() ? colorDownTrend : na, title='Strong Down trend', editable=false)



//TRIX
filtrarTrix = input(title='TRIX signal', defval=true, group="TRIX config")
_length = input.int(9, title='TRIX', minval=1, group="TRIX config")
_useEma = true
_ma = input.int(4, title='TRIX average', minval=1, group="TRIX config")
tema(a, b) =>
    ta.ema(ta.ema(ta.ema(a, b), b), b)
trix(a) =>
    (a - a[1]) / a[1] * 10000
_trix = trix(tema(close, _length))
_trixs = _useEma ? ta.ema(_trix, _ma) : ta.sma(_trix, _ma)

// Estocastico
filtrarStoch = input(title='Stochastic signal', defval=true, group="Stochastic config")
Length = input.int(8, minval=1, title='Stochastic length', group="Stochastic config")
k = input.int(3, minval=1, title='Stochastic %K', group="Stochastic config")
d = input.int(3, minval=1, title='Stochastic %D', group="Stochastic config")
Sto = ta.sma(ta.stoch(close, high, low, Length), d)
K = ta.sma(Sto, k)


trixComprado() =>
    filtrarTrix == false ? true : _trix > _trixs ? true : false
stockComprado() =>
    filtrarStoch == false ? true : Sto > K ? true : false

exitShort() =>
    trixComprado() and ta.crossover(Sto, K) or stockComprado() and ta.crossover(_trix, _trixs)
exitLong() =>
    trixComprado() == false and ta.crossunder(Sto, K) or stockComprado() == false and ta.crossunder(_trix, _trixs)

plotshape(exitShort() and (tendenciaVenda() or temTendencia() == false), style=shape.triangleup, location=location.bottom, color=color.new(color.white, 20), size=size.auto, title='Exit short')
plotshape(exitLong() and (tendenciaCompra() or temTendencia() == false), style=shape.triangledown, location=location.top, color=color.new(color.white, 20), size=size.auto, title='Exit long')

//End