//@version=5 indicator("EMA + Price Indicator", overlay = true) //-----------------------------------------------------------------------------{ //Constants //-----------------------------------------------------------------------------{ ema1_length = input.int(20, "EMA Length 1", minval = 2, group="Fast EMAs", inline = "EMA1") ema2_length = input.int(50, "EMA Length 2", minval = 3, group="Fast EMAs", inline = "EMA2") slow_ema_filter = input.bool(false, "EMA Filter", group = "Slow EMAs") ema3_length = input.int(100, "EMA Length 3", minval = 4, group="Slow EMAs", inline = "EMA3") ema4_length = input.int(200, "EMA Length 4", minval = 5, group="Slow EMAs", inline = "EMA4") ema1_tf = input.timeframe("", " ", group="Fast EMAs", inline = "EMA1") ema2_tf = input.timeframe("", " ", group="Fast EMAs", inline = "EMA2") ema3_tf = input.timeframe("", " ", group="Slow EMAs", inline = "EMA3") ema4_tf = input.timeframe("", " ", group="Slow EMAs", inline = "EMA4") ema1_col = input.color(color.blue, " ", group="Fast EMAs", inline = "EMA1") ema2_col = input.color(color.green, " ", group="Fast EMAs", inline = "EMA2") ema3_col = input.color(color.yellow, " ", group="Slow EMAs", inline = "EMA3") ema4_col = input.color(color.red, " ", group="Slow EMAs", inline = "EMA4") ema1 = request.security(syminfo.tickerid, ema1_tf, ta.ema(close, ema1_length)) ema2 = request.security(syminfo.tickerid, ema2_tf, ta.ema(close, ema2_length)) ema3 = request.security(syminfo.tickerid, ema3_tf, ta.ema(close, ema3_length)) ema4 = request.security(syminfo.tickerid, ema4_tf, ta.ema(close, ema4_length)) plot(ema1, "EMA1", ema1_col) plot(ema2, "EMA2", ema2_col) plot(ema3, "EMA3", ema3_col) plot(ema4, "EMA4", ema4_col) //-----------------------------------------------------------------------------} //Functions //-----------------------------------------------------------------------------{ n = bar_index //Display Structure function display_Structure(x, y, css, dashed)=> structure_line = line.new(x, y, n, y , color = css , style = dashed ? line.style_dashed : line.style_solid) //Swings detection/measurements swings(len)=> var os = 0 upper = ta.highest(len) lower = ta.lowest(len) os := high[len] > upper ? 0 : low[len] < lower ? 1 : os[1] top = os == 0 and os[1] != 0 ? high[len] : 0 btm = os == 1 and os[1] != 1 ? low[len] : 0 [top, btm] //-----------------------------------------------------------------------------} //Global variables //-----------------------------------------------------------------------------{ var bool last_bull_fast_ema_cross = false var bool last_bear_fast_ema_cross = false var int last_bull_cross_idx = 0, var int last_bear_cross_idx = 0 var top_y = 0., var top_x = 0 var btm_y = 0., var btm_x = 0 var int trail_up_x = na, var int trail_dn_x = na var top_cross = false, var btm_cross = false var float trail_up = na, var float trail_dn = na //-----------------------------------------------------------------------------} //EMA Logic //-----------------------------------------------------------------------------{ bull_fast_ema_cross = ta.crossover(ema1, ema2) bear_fast_ema_cross = ta.crossunder(ema1, ema2) bull_fast_ema_filter = ema1 > ema2 bear_fast_ema_filter = ema1 < ema2 bull_slow_ema_filter = slow_ema_filter ? ema3 > ema4 : true bear_slow_ema_filter = slow_ema_filter ? ema3 < ema4 : true if bull_fast_ema_cross last_bull_fast_ema_cross := true last_bear_fast_ema_cross := false last_bull_cross_idx := n if bear_fast_ema_cross last_bull_fast_ema_cross := false last_bear_fast_ema_cross := true last_bear_cross_idx := n //-----------------------------------------------------------------------------} //Swings //-----------------------------------------------------------------------------{ length = input.int(5, "Swing Length") [top, btm] = swings(length) //-----------------------------------------------------------------------------} //Pivot High //-----------------------------------------------------------------------------{ var line extend_top = na bear_css = color.red bull_css = color.green if top top_cross := true //Extend recent top to last bar line.delete(extend_top[1]) extend_top := line.new(n-length, top, n, top , color = bear_css) top_y := top top_x := n - length trail_up := top trail_up_x := n - length //Trailing maximum trail_up := math.max(high, trail_up) trail_up_x := trail_up == high ? n : trail_up_x //Set top extension label/line if barstate.islast and trail_up_x < last_bear_cross_idx line.set_xy1(extend_top, trail_up_x, trail_up) line.set_xy2(extend_top, n + 20, trail_up) line.set_style(extend_top, line.style_dashed) //-----------------------------------------------------------------------------} //Pivot Low //-----------------------------------------------------------------------------{ var line extend_btm = na if btm btm_cross := true //Extend recent btm to last bar line.delete(extend_btm[1]) extend_btm := line.new(n - length, btm, n, btm , color = bull_css) btm_y := btm btm_x := n-length trail_dn := btm trail_dn_x := n-length //Trailing minimum trail_dn := math.min(low, trail_dn) trail_dn_x := trail_dn == low ? n : trail_dn_x //Set btm extension label/line if barstate.islast and trail_dn_x < last_bull_cross_idx line.set_xy1(extend_btm, trail_dn_x, trail_dn) line.set_xy2(extend_btm, n + 20, trail_dn) line.set_style(extend_btm, line.style_dashed) bull_signal = false bear_signal = false //Detect bullish Structure if ta.crossover(close, top_y) and top_cross and top_x < last_bear_cross_idx and bull_slow_ema_filter//and last_bear_fast_ema_cross display_Structure(top_x, top_y, bull_css, false) label.new(top_x, top_y, yloc = yloc.abovebar, style = label.style_cross, color = color.green, size = size.tiny) top_cross := false bull_signal := true //Detect bearish Structure if ta.crossunder(close, btm_y) and btm_cross and btm_x < last_bull_cross_idx and bear_slow_ema_filter//and last_bull_fast_ema_cross display_Structure(btm_x, btm_y, bear_css, false) label.new(btm_x, btm_y, yloc = yloc.belowbar, style = label.style_cross, color = color.red, size = size.tiny) btm_cross := false bear_signal := true plotshape(bull_signal, "Bull Signal", style = shape.triangleup, location = location.belowbar, color = color.green, size = size.tiny) plotshape(bear_signal, "Bear Signal", style = shape.triangledown, location = location.abovebar, color = color.red, size = size.tiny) max_line_history = input.int(20, "Dotted Line (Max Bars)", minval = 2) if n - top_x == max_line_history and top_x < last_bear_cross_idx and top_y > ta.highest(max_line_history) and bull_slow_ema_filter display_Structure(top_x, top_y, bull_css, true) label.new(top_x, top_y, yloc = yloc.abovebar, style = label.style_cross, color = color.green, size = size.tiny) if n - btm_x == max_line_history and btm_x < last_bull_cross_idx and btm_y < ta.lowest(max_line_history) and bear_slow_ema_filter display_Structure(btm_x, btm_y, bear_css, true) label.new(btm_x, btm_y, yloc = yloc.belowbar, style = label.style_cross, color = color.red, size = size.tiny)