Untitled
unknown
plain_text
10 months ago
12 kB
14
Indexable
//@version=5
indicator("Chandelier Exit + Target Trend [Combined]", overlay=true, max_lines_count=40)
// ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
// Chandelier Exit
// ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
var string calcGroup = 'Calculation'
length = input.int(title='ATR Period', defval=22, group=calcGroup)
mult = input.float(title='ATR Multiplier', step=0.1, defval=3.0, group=calcGroup)
useClose = input.bool(title='Use Close Price for Extremums', defval=true, group=calcGroup)
var string visualGroup = 'Visuals'
showLabels = input.bool(title='Show Buy/Sell Labels', defval=true, group=visualGroup)
highlightState = input.bool(title='Highlight State', defval=true, group=visualGroup)
var string alertGroup = 'Alerts'
awaitBarConfirmation = input.bool(title="Await Bar Confirmation", defval=true, group=alertGroup)
atr = mult * ta.atr(length)
longStop = (useClose ? ta.highest(close, length) : ta.highest(length)) - atr
longStopPrev = nz(longStop[1], longStop)
longStop := close[1] > longStopPrev ? math.max(longStop, longStopPrev) : longStop
shortStop = (useClose ? ta.lowest(close, length) : ta.lowest(length)) + atr
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := close[1] < shortStopPrev ? math.min(shortStop, shortStopPrev) : shortStop
var int dir = 1
dir := close > shortStopPrev ? 1 : close < longStopPrev ? -1 : dir
var color longColor = color.green
var color shortColor = color.red
var color longFillColor = color.new(color.green, 90)
var color shortFillColor = color.new(color.red, 90)
var color textColor = color.new(color.white, 0)
longStopPlot = plot(dir == 1 ? longStop : na, title='Long Stop', style=plot.style_linebr, linewidth=2, color=color.new(longColor, 0))
buySignal = dir == 1 and dir[1] == -1
plotshape(buySignal ? longStop : na, title='Long Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(longColor, 0))
plotshape(buySignal and showLabels ? longStop : na, title='Buy Label', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(longColor, 0), textcolor=textColor)
shortStopPlot = plot(dir == 1 ? na : shortStop, title='Short Stop', style=plot.style_linebr, linewidth=2, color=color.new(shortColor, 0))
sellSignal = dir == -1 and dir[1] == 1
plotshape(sellSignal ? shortStop : na, title='Short Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(shortColor, 0))
plotshape(sellSignal and showLabels ? shortStop : na, title='Sell Label', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(shortColor, 0), textcolor=textColor)
midPricePlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0, display=display.none, editable=false)
longStateFillColor = highlightState ? dir == 1 ? longFillColor : na : na
shortStateFillColor = highlightState ? dir == -1 ? shortFillColor : na : na
fill(midPricePlot, longStopPlot, title='Long State Filling', color=longStateFillColor)
fill(midPricePlot, shortStopPlot, title='Short State Filling', color=shortStateFillColor)
await = awaitBarConfirmation ? barstate.isconfirmed : true
alertcondition(dir != dir[1] and await, title='Alert: CE Direction Change', message='Chandelier Exit has changed direction!')
alertcondition(buySignal and await, title='Alert: CE Buy', message='Chandelier Exit Buy!')
alertcondition(sellSignal and await, title='Alert: CE Sell', message='Chandelier Exit Sell!')
// ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
// Target Trend
// ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
length_tt = input.int(10, "Trend Length")
target_tt = input.int(0, "Set Targets")
var bool trend_tt = na
float trend_value_tt = na
color up_color_tt = #06b690
color dn_color_tt = color.rgb(182, 112, 6)
series float atr_value_tt = ta.sma(ta.atr(200), 200) * 0.8
series float sma_high_tt = ta.sma(high, length_tt) + atr_value_tt
series float sma_low_tt = ta.sma(low, length_tt) - atr_value_tt
color plot_color_tt = color.new(chart.fg_color, 80)
type TrendTargets
line[] lines
label[] labels
var TrendTargets targets_up_tt = TrendTargets.new(array.new_line(), array.new_label())
var TrendTargets targets_down_tt = TrendTargets.new(array.new_line(), array.new_label())
if ta.crossover(close, sma_high_tt) and barstate.isconfirmed
trend_tt := true
if ta.crossunder(close, sma_low_tt) and barstate.isconfirmed
trend_tt := false
trend_value_tt := trend_tt ? sma_low_tt : sma_high_tt
trend_color_tt = trend_tt ? up_color_tt : dn_color_tt
bool signal_up_tt = ta.change(trend_tt) and not trend_tt[1]
bool signal_down_tt = ta.change(trend_tt) and trend_tt[1]
method draw_targets(TrendTargets targets, bool signal1, bool signal2, bool direction)=>
float base = direction ? sma_low_tt : sma_high_tt
float atr_multiplier = atr_value_tt * (direction ? 1 : -1)
var int count_up = 0
var int count_down = 0
if trend_tt
count_down := 0
count_up += 1
if not trend_tt
count_down += 1
count_up := 0
int count = direction ? count_up : count_down
if signal1
float target_len1 = atr_multiplier * (5 + target_tt)
float target_len2 = atr_multiplier * (10 + target_tt * 2)
float target_len3 = atr_multiplier * (15 + target_tt * 3)
for line_i in targets.lines
int i = targets.lines.indexof(line_i)
label.delete(targets.labels.get(i))
line.delete(line_i)
array.clear(targets.lines)
array.clear(targets.labels)
line stop_loss_line = line.new(bar_index, base, bar_index + 20, base)
line entry_line = line.new(bar_index, close, bar_index + 20, close)
line target1_line = line.new(bar_index, close + target_len1, bar_index + 20, close + target_len1)
line target2_line = line.new(bar_index, close + target_len2, bar_index + 20, close + target_len2)
line target3_line = line.new(bar_index, close + target_len3, bar_index + 20, close + target_len3)
linefill.new(stop_loss_line, entry_line, color.new(dn_color_tt, 75))
linefill.new(entry_line, target3_line, color.new(up_color_tt, 75))
label stop_loss_label = label.new(bar_index + 20, base, str.tostring(math.round(base, 2)))
label entry_label = label.new(bar_index + 20, close, str.tostring(math.round(close, 2)))
label target1_label = label.new(bar_index + 20, close + target_len1, "1 - " + str.tostring(math.round(close + target_len1, 2)))
label target2_label = label.new(bar_index + 20, close + target_len2, "2 - " + str.tostring(math.round(close + target_len2, 2)))
label target3_label = label.new(bar_index + 20, close + target_len3, "3 - " + str.tostring(math.round(close + target_len3, 2)))
targets.lines.push(stop_loss_line)
targets.lines.push(entry_line)
targets.lines.push(target1_line)
targets.lines.push(target2_line)
targets.lines.push(target3_line)
targets.labels.push(stop_loss_label)
targets.labels.push(entry_label)
targets.labels.push(target1_label)
targets.labels.push(target2_label)
targets.labels.push(target3_label)
for lbl in targets.labels
int idx = targets.labels.indexof(lbl)
line line_ref = targets.lines.get(idx)
lbl.set_style(label.style_label_left)
lbl.set_color(chart.fg_color)
lbl.set_textcolor(chart.bg_color)
line_ref.set_color(chart.fg_color)
if signal2
for line_i in targets.lines
int i = targets.lines.indexof(line_i)
label.delete(targets.labels.get(i))
line.delete(line_i)
array.clear(targets.lines)
array.clear(targets.labels)
for line_i in targets.lines
int idx = targets.lines.indexof(line_i)
label lbl_ref = targets.labels.get(idx)
label first_label = targets.labels.first()
line entry_line = targets.lines.get(1)
label entry_label = targets.labels.get(1)
if high >= line.get_y2(line_i) and low <= line.get_y2(line_i) and count > 1
lbl_ref.set_style(label.style_label_left)
lbl_ref.set_color(chart.fg_color)
lbl_ref.set_text(" ✔ ")
lbl_ref.set_textcolor(#16ac09)
line_i.set_style(line.style_dashed)
line_i.set_color(plot_color_tt)
if high >= line.get_y2(targets.lines.first()) and low <= line.get_y2(targets.lines.first()) and count > 1
first_label.set_text(" ✖ ")
if direction ? trend_tt : not trend_tt
first_label.set_textcolor(#db1e1e)
line_i.set_x2(bar_index + 20)
targets.lines.first().set_color(#db1e1e)
label.set_x(targets.labels.get(idx), bar_index + 20)
entry_line.set_style(line.style_solid)
entry_line.set_color(up_color_tt)
entry_label.set_text("◉ " + str.tostring(math.round(line.get_y2(entry_line), 2)))
entry_label.set_textcolor(#1d80dd)
targets_down_tt.draw_targets(signal_down_tt, signal_up_tt, false)
targets_up_tt.draw_targets(signal_up_tt, signal_down_tt, true)
plotcandle(open, high, low, close, title='Title', color=trend_color_tt, wickcolor=trend_color_tt, bordercolor=trend_color_tt)
p1_tt = plot(trend_tt ? trend_value_tt : na, style=plot.style_linebr, color=plot_color_tt)
p2_tt = plot(not trend_tt ? trend_value_tt : na, style=plot.style_linebr, color=plot_color_tt)
p0_tt = plot(hl2, display=display.none, editable=false)
fill(p1_tt, p0_tt, trend_value_tt, hl2, color.new(chart.fg_color, 90), na)
fill(p2_tt, p0_tt, trend_value_tt, hl2, color.new(chart.fg_color, 90), na)
float sigUp_tt = signal_up_tt ? low - atr_value_tt * 2 : na
float sigDn_tt = signal_down_tt ? high + atr_value_tt * 2 : na
plotshape(sigUp_tt, "", shape.triangleup, location.absolute, up_color_tt, size=size.tiny)
plotshape(sigUp_tt, "", shape.triangleup, location.absolute, color.new(up_color_tt, 80), size=size.small)
plotshape(sigDn_tt, "", shape.triangledown, location.absolute, dn_color_tt, size=size.tiny)
plotshape(sigDn_tt, "", shape.triangledown, location.absolute, color.new(dn_color_tt, 80), size=size.small)Editor is loading...
Leave a Comment