Distribution Days
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Neeshant112
//@version=5

indicator(title='Distribution', shorttitle='Distribution Days', format=format.volume, overlay=true)

//Input Parameters
distributionDayThreshold = input.float(title='Distribution Threshold (%)', defval=-0.2, minval=-3.14, maxval=-0.1, step=0.05)
length = input.int(title='Distrbution Period (in focus)', defval=25, minval=10, maxval=100, step=1)
showFor = input.int(title='Distrbution Period (perspective)', defval=100, minval=10, maxval=100, step=1)
negateDistribution = input.float(title='Negate Distribution (%)', defval=5.0, minval=1.0, maxval=10.0, step=0.25)
negateDistribution := 1 + negateDistribution / 100


//generic functions
truncate(number, decimals) =>
    factor = math.pow(10, decimals)
    int(number * factor) / factor

//Variables -------------------------------------
var line hline = line.new(na, na, na, na, extend=extend.right, color=#ff6361)
var label negateDistribution_label = label.new(na, na, 'text')
var int bar_last_distribution = na
var float close_last_distribution = na

//Functions -------------------------------------
hasVolumeIncreased = ta.change(volume) > 0
priceChangePercent = ta.change(close) / close[1] * 100
barIndex = bar_index

//Check if it's a distribution day (excluding negation logic)
isDistributionDay = priceChangePercent < distributionDayThreshold and hasVolumeIncreased ? 1 : 0

var test_y = -1.0
var test_x1 = -1.0


//Draw line for distribution negation level
if isDistributionDay and bar_index <= barIndex and bar_index > barIndex - showFor  //and lowest(close, 100) //if lowest candle is considered for negation
    y = close * negateDistribution
    line.set_xy1(hline, bar_index - 1, y)
    line.set_xy2(hline, na, y)
    test_y := y
    test_x1 := bar_index
    test_x1
line.set_x2(hline, bar_index)

crossover1 = ta.crossover(close, test_y) ? 1 : 0

//var isNegated = 0
//if (crossover1 and (bar_index > line.get_x1(hline)))
//    line.delete(hline) // also trigger alert 
//    label.delete(negateDistribution_label)

//if(line.get_x1(hline))
//    isDistributionDay := 0 

//Count number of distribution days during the loopback period
calculateDistributions() =>
    count = 0
    for _i = 0 to length - 1 by 1
        count += isDistributionDay[_i]
        count
countDist = calculateDistributions()


//cross = crossover(close, test_y) ? 1:0
//ToDo: if cross == 1 when bar_index > test_x1 ---> negated distribution


if barstate.islast
    label.set_x(negateDistribution_label, bar_index)
    label.set_y(negateDistribution_label, test_y)
    label.set_text(negateDistribution_label, str.tostring(truncate(countDist, 0)) + ' Distributions negate at ' + str.tostring(truncate(test_y, 2)))
    label.set_textcolor(negateDistribution_label, color.white)
    label.set_color(negateDistribution_label, color=#F95D6A)


//Paint all distribution days
bgcolor(isDistributionDay ? color.red : na, title='DistributionDay', show_last=showFor, transp=90)
plot(crossover1, 'cross', display=display.none)
plot(test_x1, 'X-axis', display=display.none)
//plot(test_y, "Y-axis")
//plot(bar_index, "bar_index")
plot(crossover1 and bar_index > test_x1 ? 1 : 0, 'cross+bar', display=display.none)



