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

//@version=5
indicator("Accumulation & Distribution", overlay=true)

// INPUTs
fbs =   input(true, "Filter by Sma")    // Filter by Sma 
len =   input(10, "Sma Length")         // Sma Length 
thr =   input(2, "Thresold")            // Thresold

// MAs
ma  =   ta.sma(close, len)              // Simple Moving Average Price
//Vma =   ta.sma(volume, len)           // Simple Moving Average Volume

// Moving Average
sumOfVolumes = 0.0
for offset = 0 to len - 1
    sumOfVolumes := sumOfVolumes + nz(volume[offset])
Vma = sumOfVolumes / len

// VARIABLEs
float   accumulation   =   na           //
float   distribution   =   na           //
float   sma = fbs ? ma : 0              // Sma Value

// MAIN
// Display Accumulation 
accumulation := if (close > close[1] and volume > volume[1])
    if (fbs == true)
        (close > sma) ? low[1] : na
    else if (fbs == false)
        low[1]
// Display Distribution 
distribution := if (close < close[1] and volume > volume[1])
    if (fbs == true)
        (close < sma) ? high[1] : na
    else if (fbs == false)
        high[1]

// COLORs
Pup = volume > Vma * thr ? color.new(color.blue, 10) : volume > Vma ? color.new(color.blue, 40) :   color.new(color.blue, 70)              // Palette Up
Pdo = volume > Vma * thr ? color.new(color.fuchsia, 10) : volume > Vma ? color.new(color.fuchsia, 40)  :   color.new(color.fuchsia, 70)    // Palette Down

// PLOTs
plotshape(accumulation, title="Accumulation", style=shape.triangleup, location=location.belowbar, color=Pup, size=size.auto)
plotshape(distribution, title="Distribution", style=shape.triangledown, location=location.abovebar, color=Pdo, size=size.auto)