HZlog 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RafaelZioni

//@version=5
indicator('HZlog ', overlay=true)
src = input(close)

tf = input(3600)
len = timeframe.isintraday and timeframe.multiplier >= 1 ? tf / timeframe.multiplier * 7 : timeframe.isintraday and timeframe.multiplier < 60 ? 60 / timeframe.multiplier * 24 * 7 : 7

ma = ta.ema(src * close, len) / ta.ema(close, len)
//

src1 = ma

z(src1, len) =>
    n = 0.0
    s = 0.0
    for i = 0 to len - 1 by 1
        wr = (len - i) * len
        n += wr
        s += src[i] * wr
        s
    s / n

hm = 2.0 * z(src1, math.floor(len / 2)) - z(src1, len)
zhma = z(hm, math.floor(math.sqrt(len)))
lineColor = zhma > zhma[2] ? color.lime : color.red
plot(zhma, title='ZHMA', color=lineColor, linewidth=3)
hColor = true
vis = true
hu = hColor ? zhma > zhma[2] ? #00ff00 : #ff0000 : #ff9800

vl = zhma[0]
ll = zhma[2]
m1 = plot(vl, color=hu, linewidth=1, transp=60)
m2 = plot(vis ? ll : na, color=hu, linewidth=2, transp=80)

fill(m1, m2, color=hu, transp=70)

c5 = zhma
f1 = 1
f2 = 1000
//
up = c5 - f1 * math.log(f2)
dn = c5 + f1 * math.log(f2)
//

factor = input.float(title='Factor', defval=0.001, minval=0.001, maxval=5, step=0.01)

hb = 0.00
hb := nz(hb[1])
hl = 0.000
hl := nz(hl[1])

lb = 0.00
lb := nz(lb[1])
l1 = 0.000
l1 := nz(l1[1])

c = 0
c := nz(c[1]) + 1

trend = 0
trend := nz(trend[1])
n = dn
x = up


if barstate.isfirst
    c := 0
    lb := n
    hb := x
    l1 := c5
    hl := c5
    hl
if c == 1
    if x >= hb[1]
        hb := x
        hl := c5
        trend := 1
        trend
    else
        lb := n
        l1 := c5
        trend := -1
        trend

if c > 1

    if trend[1] > 0
        hl := math.max(hl[1], c5)
        if x >= hb[1]
            hb := x
            hb
        else


            if n < hb[1] - hb[1] * factor
                lb := n
                l1 := c5

                trend := -1
                trend
    else


        l1 := math.min(l1[1], c5)

        if n <= lb[1]
            lb := n
            lb
        else


            if x > lb[1] + lb[1] * factor
                hb := x
                hl := c5

                trend := 1
                trend



v = trend == 1 ? hb : trend == -1 ? lb : na
//plot(v, color=trend == 1 ? color.blue : color.yellow, style=plot.style_circles, linewidth=1, title='trend', join=true, transp=0)

//

long = trend == 1 and trend[1] == -1
short = trend == -1 and trend[1] == 1
//
last_long = 0.0
last_short = 0.0
last_long := long ? time : nz(last_long[1])
last_short := short ? time : nz(last_short[1])

buy = ta.crossover(last_long, last_short)
sell = ta.crossover(last_short, last_long)


/////////////// Plotting /////////////// 
plotshape(buy, title='buy', text='Buy', color=color.new(color.green, 0), style=shape.labelup, location=location.belowbar, size=size.small, textcolor=color.new(color.white, 0))  //plot for buy icon
plotshape(sell, title='sell', text='Sell', color=color.new(color.red, 0), style=shape.labeldown, location=location.abovebar, size=size.small, textcolor=color.new(color.white, 0))


/////////////// Alerts /////////////// 
alertcondition(buy, title='buy', message='Buy')
alertcondition(sell, title='sell', message='Sell')

