D-BoT Alpha Volume Spike
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © atnx


//@version=5
indicator(shorttitle='Alpha Vol Spk', title='D-BoT Alpha Volume Spike', overlay = true)

// User Settings
overbought_limit = input(70, title="Overbought")
oversold_limit = input(30, title="Oversold")
volume_multiplier = 1
volume_ma_length = 100
volume_spike_multiple = 2
lookback_period = 720
use_extremities_confirmation = input(defval = false, title = 'Aşırılık Onayı Al?')

//// Calculate Supertrend
// User Settings
supertrend_atr_period1 = input(1, "Supertrend ATR Length 1")
supertrend_atr_period2 = input(2, "Supertrend ATR Length 2")
supertrend_atr_period3 = input(3, "Supertrend ATR Length 3")
supertrend_factor1 = input.float(1.5, "Factor", step = 0.01)
supertrend_factor2 = input.float(1.2, "Factor", step = 0.01)
supertrend_factor3 = input.float(0.9, "Factor", step = 0.01)

// Calculate Supertrends
[supertrend_value1, trend_direction1] = ta.supertrend(supertrend_factor1, supertrend_atr_period1)
[supertrend_value2, trend_direction2] = ta.supertrend(supertrend_factor2, supertrend_atr_period2)
[supertrend_value3, trend_direction3] = ta.supertrend(supertrend_factor3, supertrend_atr_period3)

//
// Candle calculations
high_ = high[1] > high[2] and high[1] > high[0]
low_ = low[1] < low[2] and low[1] < low[0]

// Volume check

// Is volume sufficiently greater than its MA?
vol_ = volume > ta.sma(volume, volume_ma_length) * volume_multiplier 

isBearish = high_ and vol_[1]
isBullish = low_ and vol_[1]

//Inputs

//ADX

ADX_length = input(14, title="ADX Length")

trueRange = math.max(math.max(high - low, math.abs(high - nz(close[1]))), math.abs(low - nz(close[1])))
dmp = high - nz(high[1]) > nz(low[1]) - low ? math.max(high - nz(high[1]), 0) : 0
dmn = nz(low[1]) - low > high - nz(high[1]) ? math.max(nz(low[1]) - low, 0) : 0

smoothedTrueRange = ta.sma(trueRange, ADX_length)
smoothedDMP = ta.sma(dmp, ADX_length)
smoothedDMN = ta.sma(dmn, ADX_length)

DIPlus = smoothedDMP / smoothedTrueRange * 100
DIMinus = smoothedDMN / smoothedTrueRange * 100

DIPlusMinus = DIPlus - DIMinus
diPlusMinusFiltered = ta.sma(DIPlusMinus, ADX_length)

diPlus = ta.sma(DIPlus, ADX_length)
diMinus = ta.sma(DIMinus, ADX_length)



//Volume Spikes
vol = ta.median(volume, lookback_period)
up = false
down = false

if volume > vol*volume_spike_multiple and diPlus > diMinus
    up := true
else if volume > vol*volume_spike_multiple and diMinus > diPlus
    down := true




////////ek filtre RSI MFI

//"Relative Strength Index"
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI-MFI Filter")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
up_rsi = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down_rsi = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down_rsi == 0 ? 100 : up_rsi == 0 ? 0 : 100 - (100 / (1 + up_rsi / down_rsi))

//Money Flow Index
length = input.int(title="MFI Length", defval=14, minval=1, maxval=2000)
src = hlc3
mfi = ta.mfi(src, length)

// Combined Al-Sat Signals{
trendBuy = false
trendSell = false

if use_extremities_confirmation
    if isBullish and up and not (trend_direction2 < 0) and (rsi<oversold_limit)    //and (mfi>rsi) 
        trendBuy := true
    else if use_extremities_confirmation == false
        if isBullish and up and not (trend_direction2 < 0) //and (mfi>rsi) 
            trendBuy := true


if use_extremities_confirmation
    if isBearish and down and (mfi<rsi) and (trend_direction2 < 0) and (rsi>overbought_limit)
        trendSell := true
else if use_extremities_confirmation == false
    if isBearish and down and (mfi<rsi) and (trend_direction2 < 0) 
        trendSell := true
// Plot Shapes
plotshape(trendBuy, title = "Al Sinyali", style = shape.labelup, location = location.belowbar, color = color.new(#5ae64d, 13), size = size.small)
plotshape(trendSell, title = "Sat Sinyali", style = shape.labeldown, location = location.abovebar, color = color.new(#fb4646, 18), size = size.small)



/// Calculate Transparency Levels
distance1 = math.abs(close - supertrend_value1)
transparency_level1 = 98
if (distance1 < 1) 
    transparency_level1 := 95
else if (distance1 < 2) 
    transparency_level1 := 90

distance2 = math.abs(close - supertrend_value2)
transparency_level2 = 95
if (distance2 < 1) 
    transparency_level2 := 95
else if (distance2 < 2) 
    transparency_level2 := 90

distance3 = math.abs(close - supertrend_value3)
transparency_level3 = 92
if (distance3 < 1) 
    transparency_level3 := 95
else if (distance3 < 2) 
    transparency_level3 := 90

// Plot Supertrend Lines
bodyMiddle = plot((open + close) / 2, display=display.none)

uptrend_line1 = plot(trend_direction1 < 0 ? supertrend_value1 : na, "Up Trend", color = color.new(color.green, transparency_level1), style=plot.style_steplinebr)
downtrend_line1 = plot(trend_direction1 < 0? na : supertrend_value1, "Down Trend", color = color.new(color.red, transparency_level1), style=plot.style_steplinebr)
fill(bodyMiddle, uptrend_line1, color.new(color.green, transparency_level1), fillgaps=false)
fill(bodyMiddle, downtrend_line1, color.new(color.red, transparency_level1), fillgaps=false)

uptrend_line2 = plot(trend_direction2 < 0 ? supertrend_value2 : na, "Up Trend", color = color.new(color.green, transparency_level2), style=plot.style_linebr)
downtrend_line2 = plot(trend_direction2 < 0? na : supertrend_value2, "Down Trend", color = color.new(color.red, transparency_level2), style=plot.style_linebr)
fill(bodyMiddle, uptrend_line2, color.new(color.green, transparency_level2), fillgaps=false)
fill(bodyMiddle, downtrend_line2, color.new(color.red, transparency_level2), fillgaps=false)

uptrend_line3 = plot(trend_direction3 < 0 ? supertrend_value3 : na, "Up Trend", color = color.new(color.green, transparency_level3), style=plot.style_linebr)
downtrend_line3 = plot(trend_direction3 < 0? na : supertrend_value3, "Down Trend", color = color.new(color.red, transparency_level3), style=plot.style_linebr)
fill(bodyMiddle, uptrend_line3, color.new(color.green, transparency_level3), fillgaps=false)
fill(bodyMiddle, downtrend_line3, color.new(color.red, transparency_level3), fillgaps=false)
