Elder Impulse Visualised
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © muaaz2603

//@version=5
indicator("MK Elder impulse", overlay = true)
source = close

// MACD Options
macd_length_fast   = input.int(title="MACD Fast Length", defval=12, minval=1)
macd_length_slow   = input.int(defval=26, minval=1, title="MACD Slow Length")
macd_length_signal = input.int(defval=9,  minval=1, title="MACD Signal Length")
// Calculate MACD
macd_ma_fast       = ta.ema(source, macd_length_fast)
macd_ma_slow       = ta.ema(source, macd_length_slow)
macd               = macd_ma_fast - macd_ma_slow
macd_signal        = ta.ema(macd, macd_length_signal)
macd_histogram     = macd - macd_signal

// EMA Option
ema_length         = input.int(defval=13, minval=1, title="EMA Length")
// Calculate EMA
ema                = ta.ema(source, ema_length)

// Calculate Elder Impulse
elder_bulls        = (ema[0] > ema[1]) and (macd_histogram[0] > macd_histogram[1])
elder_bears        = (ema[0] < ema[1]) and (macd_histogram[0] < macd_histogram[1])
elder_color        = elder_bulls
                     ? color.green //  If Bulls Control Trend and Momentum
                     : elder_bears
                       ? color.red //  If Bears Control Trend and Mementum
                       : color.blue // If Neither Bulls or Bears Control the Market

barcolor(elder_color)

// Create label on the first bar.
print(txt) =>
    var lbl = label.new(bar_index, na, txt, xloc.bar_index, yloc.price, color(na), label.style_none, color.gray, size.large, text.align_left)
    // On next bars, update the label's x and y position, and the text it displays.
    label.set_xy(lbl, bar_index, ta.highest(10)[1])
    label.set_text(lbl, str.tostring(txt))

// k = float(2)/13+1
// prev_ema = ta.ema(close[1], 13)
// needed_close = (prev_ema - prev_ema*(1-k))/k

get_required_close_ema(src, len) =>
    k = float(2)/len+1
    prev_ema = ta.ema(src[1], len)
    needed_close = (prev_ema - prev_ema*(1-k))/k

get_required_close_macdh(flen, slen, siglen) =>
    kf = float(2)/(flen+1)
    ks = float(2)/(slen+1)
    ksig = float(2)/(siglen+1)

    prev_ema_f = ta.ema(close[1], flen)
    prev_ema_s = ta.ema(close[1], slen)
    prev_ema_sig = macd_signal[1]

    //print(str.tostring(macd_signal[1]))

    close_price = (macd_histogram[1] - (prev_ema_f*(1-kf)-prev_ema_s*(1-ks))*(1-ksig) + prev_ema_sig*(1-ksig))/((kf-ks)*(1-ksig))
   //  close_f = get_required_close_ema(close, 12)
   //  close_s = get_required_close_ema(close, 26)
   //  close_macd = get_required_close_ema(macd, 13)

close_needed = get_required_close_ema(close, 13)
macd_close = get_required_close_macdh(12, 26, 13)

max_close = math.max(close_needed, macd_close)
min_close = math.min(close_needed, macd_close)
hit_price = float(0)
float hit_price2 = na
hit_color = color.white

if elder_bulls
    hit_price := min_close
    hit_color := color.lime
else if elder_bears
    hit_price := max_close
    hit_color := color.maroon
else
    hit_price := min_close
    hit_price2 := max_close 

plot(max_close, title="Price_needed_green", linewidth = 2, color = color.green)
print(str.tostring(hit_price))
plot(min_close, title="Price_needed_red", linewidth = 1, color = color.red)

//plotshape(needed_close, title = "mjh", style = shape.cross, show_last = 2, locatio)

// mk=0
// if elder_bulls
//     mk:=2
