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

//@version=5

indicator(title='WMACD', overlay=true, max_bars_back=2000, max_lines_count=500)

source = close
fastLength = input.int(3, minval=1)
slowLength = input.int(8, minval=1)
fast = ta.ema(source, fastLength)
slow = ta.ema(source, slowLength)
vr = fast - slow
macd_lookback =100, oo =500,mult =50,zz = 35,ui = 1,macd_show = true
b_color = color.gray,cop = vr<= 0 ? color.red : color.lime
//
float macd_up = 0.0,int macd_upId = 0,int macd_n_b = oo,var int macd_fs = time

a1 = array.new_float(macd_n_b + 1, 0.0),b1 = array.new_float(macd_n_b, 0.0),c1 = array.new_float(macd_n_b, 0.0),d1 = array.new_int(macd_n_b, 0)

float macd_high = ta.highest(high, macd_lookback),float macd_low = ta.lowest(low, macd_lookback)

if barstate.islast
    float m_hl = (macd_high - macd_low) / macd_n_b
    for pp = 1 to macd_n_b + 1 by 1
        array.set(a1, pp - 1, macd_low + m_hl * pp)
    for i = 0 to macd_lookback + 1 by 1
        int cc = 0
        array.fill(c1, 0.0)
        for pp = 0 to macd_n_b - 1 by 1
            float tx = array.get(a1, pp)
            if low[i] < tx and high[i] > tx 
                float trep = array.get(c1, pp)
                float dtrep = trep + nz(vr[i])
                array.set(c1, pp, dtrep)
                cc += 2
                cc
        for pp = 0 to macd_n_b - 1 by 1
            float jj = array.get(b1, pp)
            float trep = array.get(c1, pp)
            float djj = jj + (cc >= 0 ? trep / cc : 0.0)
            array.set(b1, pp, djj)
    macd_up := array.max(b1)
    macd_upId := array.indexof(b1, macd_up)
    for pp = 0 to macd_n_b - 1 by 1
        float jj = array.get(b1, pp)
        int tres = math.round(mult * jj / macd_up)
        array.set(d1, pp, tres)

if barstate.isfirst
    macd_fs := time
    macd_fs
yyy = ta.change(time)
yn = timenow + math.round(yyy * zz)

f(n) =>
    x1 = macd_upId == n and macd_show ? math.max(time[macd_lookback], macd_fs) : timenow + math.round(yyy * (zz - array.get(d1, n)))
    ut = array.get(a1, n)
    line.new(x1=x1, y1=ut, x2=yn, y2=ut, xloc=xloc.bar_time, extend=extend.none, color=macd_upId == n ? cop : b_color, style=line.style_solid, width=ui)

if barstate.islast
    for i = 0 to macd_n_b - 1 by 1
        f(i)



length = input(14)
overSold = input(35)
overBought = input(65)
p = close

vrsi = ta.rsi(p, length)
price = close
var bool long = na
var bool short = na

long := ta.crossover(vrsi, overSold)
short := ta.crossunder(vrsi, overBought)

var float last_open_long = na
var float last_open_short = na

last_open_long := long ? close : nz(last_open_long[1])
last_open_short := short ? close : nz(last_open_short[1])


entry_value = last_open_long
entry_value1 = last_open_short

// Rounding levels to min tick
nround(x) =>
    n = math.round(x / syminfo.mintick) * syminfo.mintick
    n
//
disp_panels = input(true, title='Display info panels?')
fibs_label_off = input(5, title='fibs label offset')
fibs_label_size = input.string(size.normal, options=[size.tiny, size.small, size.normal, size.large, size.huge], title='fibs label size')
r1_x = timenow + math.round(ta.change(time) * fibs_label_off)
r1_y = last_open_short
text1 = 'High : ' + str.tostring(nround(last_open_short))
s1_y = last_open_long
text3 = 'low : ' + str.tostring(nround(last_open_long))

R1_label = disp_panels ? label.new(x=r1_x, y=r1_y, text=text1, xloc=xloc.bar_time, yloc=yloc.price, color=color.orange, style=label.style_label_down, textcolor=color.black, size=fibs_label_size) : na
S1_label = disp_panels ? label.new(x=r1_x, y=s1_y, text=text3, xloc=xloc.bar_time, yloc=yloc.price, color=color.lime, style=label.style_label_up, textcolor=color.black, size=fibs_label_size) : na

label.delete(R1_label[1])
label.delete(S1_label[1])
//







