ATH Drawdown Indicator by Atilla Yurtseven
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AtillaYurtseven

//@version=5
indicator("ATH Drawdown // Atilla Yurtseven", shorttitle="ADD")

show_as_candles = input.bool(false, title="Show as Candles")
show_mean = input.bool(true, title="Show Mean")
green = input.color(#27a69a, title="Green", group="Colors")
red = input.color(#f05350, title="Red", group="Colors")
var float ath = na
var bool is_ath_bar = false
var float total_dd = 0.0
var int bar = na

// functions
drawdown(val) => (val / ath - 1) * 100

// reset
if na(bar)
    bar := 0
if na(total_dd)
    total_dd := 0.0

bar += 1

is_ath_bar := false
if na(ath) or high > ath
    ath := high
    is_ath_bar := true

dd = is_ath_bar?0.0:drawdown(low)

total_dd += dd
mean = total_dd / bar

o = show_as_candles?drawdown(open):na
h = show_as_candles?drawdown(high):na
l = show_as_candles?drawdown(low):na
c = show_as_candles?drawdown(close):na

clr = c > o?green:red

plot(show_as_candles?na:dd, color=green)
plotcandle(o, h, l, c, color=clr, bordercolor=clr)
plot(mean, title="Mean")

hline(0)
hline(-25)
hline(-50)
hline(-75)
hline(-100)
