Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- // Chandelier Exit script may be freely distributed under the terms of the GPL-3.0 license.
- indicator('Chandelier Exit', shorttitle='CE', overlay=true, max_bars_back=500, max_lines_count=400, precision=4, max_labels_count=500)
- length1 = input(title='ATR Period', defval=22)
- mult = input.float(title='ATR Multiplier', step=0.1, defval=3.0)
- showLabels = input(title='Show Buy/Sell Labels ?', defval=true)
- useClose = input(title='Use Close Price for Extremums ?', defval=true)
- highlightState = input(title='Highlight State ?', defval=true)
- atr = mult * ta.atr(length1)
- longStop = (useClose ? ta.highest(close, length1) : ta.highest(length1)) - atr
- longStopPrev = nz(longStop[1], longStop)
- longStop := close[1] > longStopPrev ? math.max(longStop, longStopPrev) : longStop
- shortStop = (useClose ? ta.lowest(close, length1) : ta.lowest(length1)) + 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
- 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=color.new(color.white, 0))
- 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=color.new(color.white, 0))
- midPricePlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0, display=display.none, editable=false)
- longFillColor = highlightState ? dir == 1 ? longColor : na : na
- shortFillColor = highlightState ? dir == -1 ? shortColor : na : na
- fill(midPricePlot, longStopPlot, title='Long State Filling', color=longFillColor, transp=90)
- fill(midPricePlot, shortStopPlot, title='Short State Filling', color=shortFillColor, transp=90)
- changeCond = dir != dir[1]
- alertcondition(changeCond, title='Alert: CE Direction Change', message='Chandelier Exit has changed direction!')
- alertcondition(buySignal, title='Alert: CE Buy', message='Chandelier Exit Buy!')
- alertcondition(sellSignal, title='Alert: CE Sell', message='Chandelier Exit Sell!')
- //*****************************************************************************************************************************************************
- //ema
- //********
- shortest = ta.ema(close, 20)
- short = ta.ema(close, 50)
- longer = ta.ema(close, 100)
- longest = ta.ema(close, 200)
- plot(shortest, color=color.new(color.red, 0))
- plot(short, color=color.new(color.orange, 0))
- plot(longer, color=color.new(color.aqua, 0))
- plot(longest, color=color.new(color.white, 0))
- //**************************************************************************************
- //dynamic risistance and support
- //***********************************
- prd = input.int(defval=10, title='Pivot Period', minval=4, maxval=30, group='Setup')
- ppsrc = input.string(defval='High/Low', title='Source', options=['High/Low', 'Close/Open'], group='Setup')
- maxnumpp = input.int(defval=20, title=' Maximum Number of Pivot', minval=5, maxval=100, group='Setup')
- ChannelW = input.int(defval=10, title='Maximum Channel Width %', minval=1, group='Setup')
- maxnumsr = input.int(defval=5, title=' Maximum Number of S/R', minval=1, maxval=10, group='Setup')
- min_strength = input.int(defval=2, title=' Minimum Strength', minval=1, maxval=10, group='Setup')
- labelloc = input.int(defval=20, title='Label Location', group='Colors', tooltip='Positive numbers reference future bars, negative numbers reference histical bars')
- linestyle = input.string(defval='Dashed', title='Line Style', options=['Solid', 'Dotted', 'Dashed'], group='Colors')
- linewidth = input.int(defval=2, title='Line Width', minval=1, maxval=4, group='Colors')
- resistancecolor = input.color(defval=color.red, title='Resistance Color', group='Colors')
- supportcolor = input.color(defval=color.lime, title='Support Color', group='Colors')
- showpp = input(false, title='Show Point Points')
- float src1 = ppsrc == 'High/Low' ? high : math.max(close, open)
- float src2 = ppsrc == 'High/Low' ? low : math.min(close, open)
- float ph2 = ta.pivothigh(src1, prd, prd)
- float pl2 = ta.pivotlow(src2, prd, prd)
- plotshape(ph2 and showpp, text='H', style=shape.labeldown, color=na, textcolor=color.new(color.red, 0), location=location.abovebar, offset=-prd)
- plotshape(pl2 and showpp, text='L', style=shape.labelup, color=na, textcolor=color.new(color.lime, 0), location=location.belowbar, offset=-prd)
- Lstyle = linestyle == 'Dashed' ? line.style_dashed : linestyle == 'Solid' ? line.style_solid : line.style_dotted
- //calculate maximum S/R channel zone width
- prdhighest = ta.highest(300)
- prdlowest = ta.lowest(300)
- cwidth = (prdhighest - prdlowest) * ChannelW / 100
- var pivotvals = array.new_float(0)
- if ph2 or pl2
- array.unshift(pivotvals, ph2 ? ph2 : pl2)
- if array.size(pivotvals) > maxnumpp // limit the array size
- array.pop(pivotvals)
- get_sr_vals(ind) =>
- float lo = array.get(pivotvals, ind)
- float hi = lo
- int numpp = 0
- for y = 0 to array.size(pivotvals) - 1 by 1
- float cpp = array.get(pivotvals, y)
- float wdth = cpp <= lo ? hi - cpp : cpp - lo
- if wdth <= cwidth // fits the max channel width?
- lo := cpp <= lo ? cpp : lo
- hi := cpp > lo ? cpp : hi
- numpp += 1
- numpp
- [hi, lo, numpp]
- var sr_up_level = array.new_float(0)
- var sr_dn_level = array.new_float(0)
- sr_strength = array.new_float(0)
- find_loc(strength) =>
- ret = array.size(sr_strength)
- for i = ret > 0 ? array.size(sr_strength) - 1 : na to 0 by 1
- if strength <= array.get(sr_strength, i)
- break
- ret := i
- ret
- ret
- check_sr(hi, lo, strength) =>
- ret = true
- for i = 0 to array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na by 1
- //included?
- if array.get(sr_up_level, i) >= lo and array.get(sr_up_level, i) <= hi or array.get(sr_dn_level, i) >= lo and array.get(sr_dn_level, i) <= hi
- if strength >= array.get(sr_strength, i)
- array.remove(sr_strength, i)
- array.remove(sr_up_level, i)
- array.remove(sr_dn_level, i)
- ret
- else
- ret := false
- ret
- break
- ret
- var sr_lines = array.new_line(11, na)
- var sr_labels = array.new_label(11, na)
- for x = 1 to 10 by 1
- rate = 100 * (label.get_y(array.get(sr_labels, x)) - close) / close
- label.set_text(array.get(sr_labels, x), text=str.tostring(label.get_y(array.get(sr_labels, x))) + '(' + str.tostring(rate, '#.##') + '%)')
- label.set_x(array.get(sr_labels, x), x=bar_index + labelloc)
- label.set_color(array.get(sr_labels, x), color=label.get_y(array.get(sr_labels, x)) >= close ? color.red : color.lime)
- label.set_textcolor(array.get(sr_labels, x), textcolor=label.get_y(array.get(sr_labels, x)) >= close ? color.white : color.black)
- label.set_style(array.get(sr_labels, x), style=label.get_y(array.get(sr_labels, x)) >= close ? label.style_label_down : label.style_label_up)
- line.set_color(array.get(sr_lines, x), color=line.get_y1(array.get(sr_lines, x)) >= close ? resistancecolor : supportcolor)
- if ph2 or pl2
- //because of new calculation, remove old S/R levels
- array.clear(sr_up_level)
- array.clear(sr_dn_level)
- array.clear(sr_strength)
- //find S/R zones
- for x = 0 to array.size(pivotvals) - 1 by 1
- [hi, lo, strength] = get_sr_vals(x)
- if check_sr(hi, lo, strength)
- loc = find_loc(strength)
- // if strength is in first maxnumsr sr then insert it to the arrays
- if loc < maxnumsr and strength >= min_strength
- array.insert(sr_strength, loc, strength)
- array.insert(sr_up_level, loc, hi)
- array.insert(sr_dn_level, loc, lo)
- // keep size of the arrays = 5
- if array.size(sr_strength) > maxnumsr
- array.pop(sr_strength)
- array.pop(sr_up_level)
- array.pop(sr_dn_level)
- for x = 1 to 10 by 1
- line.delete(array.get(sr_lines, x))
- label.delete(array.get(sr_labels, x))
- for x = 0 to array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na by 1
- float mid = math.round_to_mintick((array.get(sr_up_level, x) + array.get(sr_dn_level, x)) / 2)
- rate = 100 * (mid - close) / close
- array.set(sr_labels, x + 1, label.new(x=bar_index + labelloc, y=mid, text=str.tostring(mid) + '(' + str.tostring(rate, '#.##') + '%)', color=mid >= close ? color.red : color.lime, textcolor=mid >= close ? color.white : color.black, style=mid >= close ? label.style_label_down : label.style_label_up))
- array.set(sr_lines, x + 1, line.new(x1=bar_index, y1=mid, x2=bar_index - 1, y2=mid, extend=extend.both, color=mid >= close ? resistancecolor : supportcolor, style=Lstyle, width=linewidth))
- f_crossed_over() =>
- ret = false
- for x = 0 to array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na by 1
- float mid = math.round_to_mintick((array.get(sr_up_level, x) + array.get(sr_dn_level, x)) / 2)
- if close[1] <= mid and close > mid
- ret := true
- ret
- ret
- f_crossed_under() =>
- ret = false
- for x = 0 to array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na by 1
- float mid = math.round_to_mintick((array.get(sr_up_level, x) + array.get(sr_dn_level, x)) / 2)
- if close[1] >= mid and close < mid
- ret := true
- ret
- ret
- alertcondition(f_crossed_over(), title='Resistance Broken', message='Resistance Broken')
- alertcondition(f_crossed_under(), title='Support Broken', message='Support Broken')
- //**************************************************************************************************
- length = input.int(14)
- k = input.float(1.,'Slope',minval=0,step=.1)
- method = input.string('Atr','Slope Calculation Method',
- options=['Atr','Stdev','Linreg'])
- show = input(false,'Show Only Confirmed Breakouts')
- //----
- upper = 0.,lower = 0.
- slope_ph = 0.,slope_pl = 0.
- src = close
- n = bar_index
- //----
- ph = ta.pivothigh(length,length)
- pl = ta.pivotlow(length,length)
- slope = switch method
- 'Atr' => ta.atr(length)/length*k
- 'Stdev' => ta.stdev(src,length)/length*k
- 'Linreg' => math.abs(ta.sma(src*bar_index,length)-ta.sma(src,length)*ta.sma(bar_index,length))/ta.variance(n,length)/2*k
- slope_ph := ph ? slope : slope_ph[1]
- slope_pl := pl ? slope : slope_pl[1]
- upper := ph ? ph : upper[1] - slope_ph
- lower := pl ? pl : lower[1] + slope_pl
- //----
- single_upper = 0
- single_lower = 0
- single_upper := src[length] > upper ? 0 : ph ? 1 : single_upper[1]
- single_lower := src[length] < lower ? 0 : pl ? 1 : single_lower[1]
- upper_breakout = single_upper[1] and src[length] > upper and (show ? src > src[length] : 1)
- lower_breakout = single_lower[1] and src[length] < lower and (show ? src < src[length] : 1)
- plotshape(upper_breakout ? low[length] : na,"Upper Break",shape.labelup,location.absolute,#26a69a,-length,text="B",textcolor=color.white,size=size.tiny)
- plotshape(lower_breakout ? high[length] : na,"Lower Break",shape.labeldown,location.absolute,#ef5350,-length,text="B",textcolor=color.white,size=size.tiny)
- //----
- var line up_l = na
- var line dn_l = na
- var label recent_up_break = na
- var label recent_dn_break = na
- if ph[1]
- line.delete(up_l[1])
- label.delete(recent_up_break[1])
- up_l := line.new(n-length-1,ph[1],n-length,upper,color=#26a69a,
- extend=extend.right,style=line.style_dashed)
- if pl[1]
- line.delete(dn_l[1])
- label.delete(recent_dn_break[1])
- dn_l := line.new(n-length-1,pl[1],n-length,lower,color=#ef5350,
- extend=extend.right,style=line.style_dashed)
- if ta.crossover(src,upper-slope_ph*length)
- label.delete(recent_up_break[1])
- recent_up_break := label.new(n,low,'B',color=#26a69a,
- textcolor=color.white,style=label.style_label_up,size=size.small)
- if ta.crossunder(src,lower+slope_pl*length)
- label.delete(recent_dn_break[1])
- recent_dn_break := label.new(n,high,'B',color=#ef5350,
- textcolor=color.white,style=label.style_label_down,size=size.small)
- //----
- plot(upper,'Upper',color = ph ? na : #26a69a,offset=-length)
- plot(lower,'Lower',color = pl ? na : #ef5350,offset=-length)
- alertcondition(ta.crossover(src,upper-slope_ph*length),'Upper Breakout','Price broke upper trendline')
- alertcondition(ta.crossunder(src,lower+slope_pl*length),'Lower Breakout','Price broke lower trendline')
- //***********************************************************************************************************************
- //hull suit
- //***********
- //Basic Hull Ma Pack tinkered by InSilico
- //INPUT
- src3 = input(close, title='Source')
- modeSwitch = input.string('Hma', title='Hull Variation', options=['Hma', 'Thma', 'Ehma'])
- length3 = input(55, title='Length(180-200 for floating S/R , 55 for swing entry)')
- lengthMult = input(1.0, title='Length multiplier (Used to view higher timeframes with straight band)')
- useHtf = input(false, title='Show Hull MA from X timeframe? (good for scalping)')
- htf = input.timeframe('240', title='Higher timeframe')
- switchColor = input(true, 'Color Hull according to trend?')
- candleCol = input(false, title='Color candles based on Hull\'s Trend?')
- visualSwitch = input(true, title='Show as a Band?')
- thicknesSwitch = input(1, title='Line Thickness')
- transpSwitch = input.int(40, title='Band Transparency', step=5)
- //FUNCTIONS
- //HMA
- HMA(_src3, _length3) =>
- ta.wma(2 * ta.wma(_src3, _length3 / 2) - ta.wma(_src3, _length3), math.round(math.sqrt(_length3)))
- //EHMA
- EHMA(_src3, _length3) =>
- ta.ema(2 * ta.ema(_src3, _length3 / 2) - ta.ema(_src3, _length3), math.round(math.sqrt(_length3)))
- //THMA
- THMA(_src3, _length3) =>
- ta.wma(ta.wma(_src3, _length3 / 3) * 3 - ta.wma(_src3, _length3 / 2) - ta.wma(_src3, _length3), _length3)
- //SWITCH
- Mode(modeSwitch, src3, len) =>
- modeSwitch == 'Hma' ? HMA(src3, len) : modeSwitch == 'Ehma' ? EHMA(src3, len) : modeSwitch == 'Thma' ? THMA(src3, len / 2) : na
- //OUT
- _hull = Mode(modeSwitch, src3, int(length3 * lengthMult))
- HULL = useHtf ? request.security(syminfo.ticker, htf, _hull) : _hull
- MHULL = HULL[0]
- SHULL = HULL[2]
- //COLOR
- hullColor = switchColor ? HULL > HULL[2] ? #00ff00 : #ff0000 : #ff9800
- //PLOT
- ///< Frame
- Fi1 = plot(MHULL, title='MHULL', color=hullColor, linewidth=thicknesSwitch, transp=50)
- Fi2 = plot(visualSwitch ? SHULL : na, title='SHULL', color=hullColor, linewidth=thicknesSwitch, transp=50)
- alertcondition(ta.crossover(MHULL, SHULL), title='Hull trending up.', message='Hull trending up.')
- alertcondition(ta.crossover(SHULL, MHULL), title='Hull trending down.', message='Hull trending down.')
- ///< Ending Filler
- fill(Fi1, Fi2, title='Band Filler', color=hullColor, transp=transpSwitch)
- ///BARCOLOR
- barcolor(color=candleCol ? switchColor ? hullColor : na : na)
- //***********************************************************************************************************************
- //breakout finder
- //*****************
- prd21221 = input.int(defval=5, title='Period', minval=2)
- bo_len = input.int(defval=200, title='Max Breakout Length', minval=30, maxval=300)
- cwidthu = input.float(defval=3., title='Threshold Rate %', minval=1., maxval=10) / 100
- mintest = input.int(defval=2, title='Minimum Number of Tests', minval=1)
- bocolorup = input.color(defval=color.blue, title='Breakout Colors', inline='bocol')
- bocolordown = input.color(defval=color.red, title='', inline='bocol')
- lstyle = input.string(defval=line.style_solid, title='Line Style', options=[line.style_solid, line.style_dashed, line.style_dotted])
- //width
- lll = math.max(math.min(bar_index, 300), 1)
- float h_ = ta.highest(lll)
- float l_ = ta.lowest(lll)
- float chwidth = (h_ - l_) * cwidthu
- // check if PH/PL
- ph21221 = ta.pivothigh(prd21221, prd21221)
- pl21221 = ta.pivotlow(prd21221, prd21221)
- //keep Pivot Points and their locations in the arrays
- var phval = array.new_float(0)
- var phloc = array.new_int(0)
- var plval = array.new_float(0)
- var plloc = array.new_int(0)
- // keep PH/PL levels and locations
- if ph21221
- array.unshift(phval, ph21221)
- array.unshift(phloc, bar_index - prd21221)
- if array.size(phval) > 1 // cleanup old ones
- for x = array.size(phloc) - 1 to 1 by 1
- if bar_index - array.get(phloc, x) > bo_len
- array.pop(phloc)
- array.pop(phval)
- if pl21221
- array.unshift(plval, pl21221)
- array.unshift(plloc, bar_index - prd21221)
- if array.size(plval) > 1 // cleanup old ones
- for x = array.size(plloc) - 1 to 1 by 1
- if bar_index - array.get(plloc, x) > bo_len
- array.pop(plloc)
- array.pop(plval)
- // check bullish cup
- float bomax = na
- int bostart = bar_index
- num = 0
- hgst = ta.highest(prd21221)[1]
- if array.size(phval) >= mintest and close > open and close > hgst
- bomax := array.get(phval, 0)
- xx = 0
- for x = 0 to array.size(phval) - 1 by 1
- if array.get(phval, x) >= close
- break
- xx := x
- bomax := math.max(bomax, array.get(phval, x))
- bomax
- if xx >= mintest and open <= bomax
- for x = 0 to xx by 1
- if array.get(phval, x) <= bomax and array.get(phval, x) >= bomax - chwidth
- num += 1
- bostart := array.get(phloc, x)
- bostart
- if num < mintest or hgst >= bomax
- bomax := na
- bomax
- if not na(bomax) and num >= mintest
- line.new(x1=bar_index, y1=bomax, x2=bostart, y2=bomax, color=bocolorup, style=lstyle)
- line.new(x1=bar_index, y1=bomax - chwidth, x2=bostart, y2=bomax - chwidth, color=bocolorup, style=lstyle)
- line.new(x1=bostart, y1=bomax - chwidth, x2=bostart, y2=bomax, color=bocolorup, style=lstyle)
- line.new(x1=bar_index, y1=bomax - chwidth, x2=bar_index, y2=bomax, color=bocolorup, style=lstyle)
- plotshape(not na(bomax) and num >= mintest, location=location.belowbar, style=shape.triangleup, color=bocolorup, size=size.small)
- alertcondition(not na(bomax) and num >= mintest, title='Breakout', message='Breakout')
- // check bearish cup
- float bomin = na
- bostart := bar_index
- num1 = 0
- lwst = ta.lowest(prd21221)[1]
- if array.size(plval) >= mintest and close < open and close < lwst
- bomin := array.get(plval, 0)
- xx = 0
- for x = 0 to array.size(plval) - 1 by 1
- if array.get(plval, x) <= close
- break
- xx := x
- bomin := math.min(bomin, array.get(plval, x))
- bomin
- if xx >= mintest and open >= bomin
- for x = 0 to xx by 1
- if array.get(plval, x) >= bomin and array.get(plval, x) <= bomin + chwidth
- num1 += 1
- bostart := array.get(plloc, x)
- bostart
- if num1 < mintest or lwst <= bomin
- bomin := na
- bomin
- if not na(bomin) and num1 >= mintest
- line.new(x1=bar_index, y1=bomin, x2=bostart, y2=bomin, color=bocolordown, style=lstyle)
- line.new(x1=bar_index, y1=bomin + chwidth, x2=bostart, y2=bomin + chwidth, color=bocolordown, style=lstyle)
- line.new(x1=bostart, y1=bomin + chwidth, x2=bostart, y2=bomin, color=bocolordown, style=lstyle)
- line.new(x1=bar_index, y1=bomin + chwidth, x2=bar_index, y2=bomin, color=bocolordown, style=lstyle)
- plotshape(not na(bomin) and num1 >= mintest, location=location.abovebar, style=shape.triangledown, color=bocolordown, size=size.small)
- alertcondition(not na(bomin) and num1 >= mintest, title='Breakdown', message='Breakdown')
- alertcondition(not na(bomax) and num >= mintest or not na(bomin) and num1 >= mintest, title='Breakout or Breakdown', message='Breakout or Breakdown')
- //************************************************************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement