Accumulation & Distribution  - Simple
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jadeja_rajdeep

//@version=5
indicator("Acc & Dist - Simple")

no_of_days = input.int(42, "No of Days / Length", minval=5, maxval=252, step=1)

up = 0.0
down =0.0
vsum =0.0
for x=0 to (no_of_days - 1)
    avg=(high[x]+low[x]+close[x])/3
    if(close[x]>=avg)
        up := up + ((close[x] - avg)*volume[x])
    else
        down := down + ((avg - close[x])*volume[x])
    
    vsum := vsum + volume[x]

up := (up / vsum)*100
down := (down / vsum)*100
AccDist=((up-down)*100)/down
colorcode = (AccDist >=0 ? color.green : color.red)
plot(AccDist,color=colorcode,style =plot.style_histogram)

ema = AccDist >= 0 ? AccDist : AccDist/2
half_ema_days=math.ceil(no_of_days * 0.25)
ema1=ta.ema(ema,half_ema_days)
ema2=ta.ema(ema,no_of_days)
plot(ema1, color=color.white)
plot(ema2,color=color.gray)

bgcolor=color.black

if(ema1>=ema2 and AccDist >= ema2)
    bgcolor := color.new(color.green,90)
else if(ema1>=ema2 and AccDist < ema2)
    bgcolor := color.new(color.red,90)
else if(ema1 < ema2 and AccDist >= ema1)
    bgcolor := color.new(color.green,90)
else if(ema1 < ema2 and AccDist < ema1)
    bgcolor := color.new(color.red,90)

bgcolor(bgcolor)

if(AccDist < math.max(ema1,ema2) and AccDist[1] >= math.max(ema1[1],ema2[1]) and AccDist[2] >= math.max(ema1[2],ema2[2]) and AccDist[3] >= math.max(ema1[3],ema2[3]) and AccDist >= 0)
    label.new(bar_index,math.max(high,high[1]),str.tostring(math.round(math.max(high,high[1]),2)),color = color.yellow)


