Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- indicator("YoD - Price Action Pack (Version 2) (Swing)", "YoD - Price Action Pack (Version 2)(Swing)"
- , overlay = true)
- //-----------------------------------------------------------------------------{
- //Constants
- //-----------------------------------------------------------------------------{
- color TRANSP_CSS = #ffffff00
- //-----------------------------------------------------------------------------{
- //"Moving Average Band & Momentum Based Candles
- //----------------------------------------{
- len = input(title='SSL Length', defval=3000, group = "Moving Average Band & Momentum Based Candles")
- maType = input.string(title='SSL Type', defval='TMA',group = "Moving Average Band & Momentum Based Candles", options=['SMA','FRMA', 'EMA', 'DEMA', 'TEMA', 'LSMA', 'WMA', 'MF', 'VAMA', 'TMA', 'HMA', 'THMA', 'EHMA', 'JMA', 'Kijun v2', 'EDSMA', 'McGinley','ALMA','ALHLSMA'])
- blength = input.int(100, minval=1, title = "Basis Length" ,group = "Moving Average Band & Momentum Based Candles")
- bType = input.string(title='Basis Type', defval='MF',group = "Moving Average Band & Momentum Based Candles", options=['SMA','FRMA', 'EMA', 'DEMA', 'TEMA', 'LSMA', 'WMA', 'MF', 'VAMA', 'TMA', 'HMA', 'THMA', 'EHMA', 'JMA', 'Kijun v2', 'EDSMA', 'McGinley','ALMA','ALHLSMA'])
- src = close
- fibratio = input(defval=8, title='Fib Multiplier', group = "Moving Average Band & Momentum Based Candles")
- jurik_phase = input(1, "JMA Phase", group = "Moving Average Band & Momentum Based Candles")
- jurik_power = input(1, "JMA Power", group = "Moving Average Band & Momentum Based Candles")
- kidiv = input(1, "Kijun Mod Divider", group = "Moving Average Band & Momentum Based Candles")
- volatility_lookback = input(1000, "VAMA Lookback Voloatility", group = "Moving Average Band & Momentum Based Candles")
- ssfLength = input(20, "EDSMA Smoother Fileter Length", group = "Moving Average Band & Momentum Based Candles")
- ssfPoles = input(2, "EDSMA Smoother Poles Length", group = "Moving Average Band & Momentum Based Candles")
- offset = input.float(title="ALMA Offset", defval=0.85, group = "Moving Average Band & Momentum Based Candles")
- sigma = input.float(title="ALMA Sigma", defval=6, group = "Moving Average Band & Momentum Based Candles")
- //----------------------------------------}
- //Market Structure
- //----------------------------------------{
- show_Structure = input(true, 'Show Structure'
- , group = 'Market Structure')
- //Bull Structure
- show_bull = input.string('All', 'Bullish Structure'
- , options = ['All', 'BOS', 'CHoCH']
- , inline = 'bull'
- , group = 'Market Structure')
- swing_bull_css = input(#00c3ff, ''
- , inline = 'bull'
- , group = 'Market Structure')
- //Bear Structure
- show_bear = input.string('All', 'Bearish Structure'
- , options = ['All', 'BOS', 'CHoCH']
- , inline = 'bear'
- , group = 'Market Structure')
- swing_bear_css = input(#ff0062, ''
- , inline = 'bear'
- , group = 'Market Structure')
- swing_structure_size = input.string('Small', 'Label Size'
- , options = ['Tiny', 'Small', 'Normal']
- , group = 'Market Structure')
- length = input.int(100, 'Swing Trend Length'
- , minval = 10
- , group = 'Market Structure')
- show_hl_swings = false
- ilength = 50
- show_swings = input(true, 'Show HH/LH/HL/LL'
- , inline = 'swings'
- , group = 'Market Structure')
- //----------------------------------------}
- //Order Blocks
- //----------------------------------------{
- swinglength = input.int(10, title = 'Swing Length', group = 'Order Blocks', minval = 1, maxval = 100)
- box_width = input.float(15, title = 'OB Width', group = 'Order Blocks', minval = 1, maxval = 30, step = 0.5)
- supply_color = input.color(color.new(#ff0062,70), title = 'Bearish OB', group = 'Order Blocks', inline = '3')
- demand_color = input.color(color.new(#00c3ff,70), title = 'Bullish OB', group = 'Order Blocks', inline = '3')
- supply_outline_color = color.new(#ff0062,100)
- demand_outline_color = color.new(#ff0062,100)
- bos_label_color = color.rgb(255, 255, 255, 50)
- poi_label_color = color.rgb(255, 255, 255, 50)
- swing_type_color = color.black
- show_price_action_labels = false
- history_of_demand_to_keep = 5
- mode = 'Historical'
- style = 'Colored'
- show_trend = false
- show_internals = false
- show_ibull = 'All'
- swing_ibull_css = #ff0062
- show_ibear = 'All'
- swing_ibear_css = #000000
- ifilter_confluence = false
- internal_structure_size = 'Tiny'
- // ~~ Inputs {
- fibonacci = input.float(0.382,"Fibonacci Level ",options=[0.236,0.382,0.50,0.618,0.786], group="Fib Settings")
- custom = false
- customfib = 0.5
- bull = color.lime
- bear = color.red
- barcol = false
- structure = false
- sen = 5
- s_bull = color.rgb(203, 68, 140)
- s_bear = color.rgb(95, 75, 188)
- Channel = false
- mult = 3.5
- max_col = color.rgb(203, 68, 140)
- min_col = color.rgb(95, 75, 188)
- levels = input.bool(true,"Fibonacci Levels",inline="level", group="Fib Settings")
- lvl_lab = input.bool(true,"Labels",inline="level", group="Fib Settings")
- line_s = "Solid"
- width = 2
- info = false
- Level_0236 = input.color(#F44336,"0.236",inline="fibcol", group="Fib Settings")
- Level_0382 = input.color(#81C784,"0.382",inline="fibcol", group="Fib Settings")
- Level_050 = input.color(#ff0062,"0.50",inline="fibcol", group="Fib Settings")
- Level_0618 = input.color(#009688,"0.618",inline="fibcol", group="Fib Settings")
- Level_0786 = input.color(#64B5F6,"0.786",inline="fibcol", group="Fib Settings")
- Level_0 = input.color(#5f5f5f,"0",inline="fibcol", group="Fib Settings")
- line_style = switch line_s
- "Solid" => line.style_solid
- "Dotted"=> line.style_dotted
- "Dashed"=> line.style_dashed
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
- // ~~ Variables {
- b = bar_index
- var pos = 0
- var hi = high
- var lo = low
- var hloc = b
- var lloc = b
- var retrace = 0.0
- var t = string(na)
- fib = custom?customfib:fibonacci
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
- // ~~ Track high/low and calculate fibonacci level {
- if pos>=0
- if high<retrace
- pos := -1
- lo := low
- lloc := time
- retrace := lo+(hi-lo)*fib
- t := str.tostring(dayofmonth)+"/"+str.tostring(month)+"/"+str.tostring(year)+" "
- if structure
- label.new(math.round(math.avg(hloc,lloc)),retrace[1],"▼",xloc.bar_time,
- color=color(na),style=label.style_label_up,textcolor=bear)
- else if high>hi
- hi := high
- hloc := time
- retrace := hi-(hi-lo)*fib
- if structure and hi[1]==hi[sen]
- pos += 1
- line.new(hloc[1],hi[1],hloc,hi[1],xloc.bar_time,color=s_bear)
- if pos<=0
- if low>retrace
- pos := 1
- hi := high
- hloc := time
- retrace := hi-(hi-lo)*fib
- t := str.tostring(dayofmonth)+"/"+str.tostring(month)+"/"+str.tostring(year)+" "
- if structure
- label.new(math.round(math.avg(hloc,lloc)),retrace[1],"▲",xloc.bar_time,
- color=color(na),style=label.style_label_down,textcolor=bull)
- else if low<lo
- lo := low
- lloc := time
- retrace := lo+(hi-lo)*fib
- if structure and lo[1]==lo[sen]
- pos := pos-1
- line.new(lloc[1],lo[1],lloc,lo[1],xloc.bar_time,color=s_bull)
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
- // ~~ Levels {
- if levels or lvl_lab
- var lines = array.new<line>(7)
- var labels= array.new<label>(7)
- var lvls = array.from(0,0.236,0.382,0.50,0.618,0.786,1)
- var cols = array.from(Level_0,Level_0236,Level_0382,Level_050,Level_0618,Level_0786,Level_0)
- //Create fibonacci level lines
- if na(lines.get(0))
- for [i,c] in cols
- lines.pop().delete()
- labels.pop().delete()
- lines.unshift(line.new(na,na,na,na,xloc.bar_time,color=c,style=line_style,width=width))
- labels.unshift(label.new(na,na,str.tostring(lvls.get(i)*100,format.percent),
- color=color(na),style=label.style_label_left,textcolor=c))
- lines.push(line.new(na,na,na,na,xloc.bar_time,color=cols.get(0),style=line_style,width=width))
- //Update xy
- for [i,lvl] in lvls
- if levels
- lines.get(i).set_xy1(lvl==0?pos<0?hloc:lloc:pos>0?hloc:lloc,pos>0?lo+(hi-lo)*lvl:hi-(hi-lo)*lvl)
- lines.get(i).set_xy2(timenow,pos>0?lo+(hi-lo)*lvl:hi-(hi-lo)*lvl)
- if lvl_lab
- labels.get(i).set_xy(b,pos>0?lo+(hi-lo)*lvl:hi-(hi-lo)*lvl)
- if levels
- lines.get(7).set_xy1(pos<0?hloc:lloc,pos<0?hi:lo)
- lines.get(7).set_xy2(pos>0?hloc:lloc,pos<0?lo:hi)
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
- get2PoleSSF(src, length) =>
- PI = 2 * math.asin(1)
- arg = math.sqrt(2) * PI / length
- a1 = math.exp(-arg)
- b1 = 2 * a1 * math.cos(arg)
- c2 = b1
- c3 = -math.pow(a1, 2)
- c1 = 1 - c2 - c3
- ssf = 0.0
- ssf := c1 * src + c2 * nz(ssf[1]) + c3 * nz(ssf[2])
- ssf
- get3PoleSSF(src, length) =>
- PI = 2 * math.asin(1)
- arg = PI / length
- a1 = math.exp(-arg)
- b1 = 2 * a1 * math.cos(1.738 * arg)
- c1 = math.pow(a1, 2)
- coef2 = b1 + c1
- coef3 = -(c1 + b1 * c1)
- coef4 = math.pow(c1, 2)
- coef1 = 1 - coef2 - coef3 - coef4
- ssf = 0.0
- ssf := coef1 * src + coef2 * nz(ssf[1]) + coef3 * nz(ssf[2]) + coef4 * nz(ssf[3])
- ssf
- beta = 0.8
- feedback = false
- z = 0.5
- pow=10
- ma(type, src, len) =>
- float result = 0
- if type == 'TMA'
- result := ta.sma(ta.sma(src, math.ceil(len / 2)), math.floor(len / 2) + 1)
- result
- if type == 'MF'
- ts = 0.
- b = 0.
- c = 0.
- os = 0.
- //----
- alpha = 2 / (len + 1)
- a = feedback ? z * src + (1 - z) * nz(ts[1], src) : src
- //----
- b := a > alpha * a + (1 - alpha) * nz(b[1], a) ? a : alpha * a + (1 - alpha) * nz(b[1], a)
- c := a < alpha * a + (1 - alpha) * nz(c[1], a) ? a : alpha * a + (1 - alpha) * nz(c[1], a)
- os := a == b ? 1 : a == c ? 0 : os[1]
- //----
- upper = beta * b + (1 - beta) * c
- lower = beta * c + (1 - beta) * b
- ts := os * upper + (1 - os) * lower
- result := ts
- result
- if type == 'LSMA'
- result := ta.linreg(src, len, 1)
- result
- if type == 'SMA' // Simple
- result := ta.sma(src, len)
- result
- if type == 'EMA' // Exponential
- result := ta.ema(src, len)
- result
- if type == 'DEMA' // Double Exponential
- e = ta.ema(src, len)
- result := 2 * e - ta.ema(e, len)
- result
- if type == 'TEMA' // Triple Exponential
- e = ta.ema(src, len)
- result := 3 * (e - ta.ema(e, len)) + ta.ema(ta.ema(e, len), len)
- result
- if type == 'WMA' // Weighted
- result := ta.wma(src, len)
- result
- if type == 'VAMA' // Volatility Adjusted
- /// Copyright © 2019 to present, Joris Duyck (JD)
- mid = ta.ema(src, len)
- dev = (src - mid) * 1.25
- vol_up = ta.highest(dev, volatility_lookback)
- vol_down = ta.lowest(dev, volatility_lookback)
- result := mid + math.avg(vol_up, vol_down)
- result
- if type == 'ALHLSMA'
- A = ta.alma(src, len, 0.85, 6)
- B = ta.wma(2*ta.wma(src, len/2)-ta.wma(src, len), math.floor(math.sqrt(len)))
- C = ta.linreg(src, len, 0)
- alhlsma = (A + B + C) / 3
- result := alhlsma
- result
- if type == 'HMA' // Hull
- result := ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len)))
- result
- if type == 'JMA' // Jurik
- /// Copyright © 2018 Alex Orekhov (everget)
- /// Copyright © 2017 Jurik Research and Consulting.
- phaseRatio = jurik_phase < -100 ? 0.5 : jurik_phase > 100 ? 2.5 : jurik_phase / 100 + 1.5
- beta = 0.45 * (len - 1) / (0.45 * (len - 1) + 2)
- alpha = math.pow(beta, jurik_power)
- jma = 0.0
- e0 = 0.0
- e0 := (1 - alpha) * src + alpha * nz(e0[1])
- e1 = 0.0
- e1 := (src - e0) * (1 - beta) + beta * nz(e1[1])
- e2 = 0.0
- e2 := (e0 + phaseRatio * e1 - nz(jma[1])) * math.pow(1 - alpha, 2) + math.pow(alpha, 2) * nz(e2[1])
- jma := e2 + nz(jma[1])
- result := jma
- result
- if type == 'Kijun v2'
- kijun = math.avg(ta.lowest(len), ta.highest(len)) //, (open + close)/2)
- conversionLine = math.avg(ta.lowest(len / kidiv), ta.highest(len / kidiv))
- delta = (kijun + conversionLine) / 2
- result := delta
- result
- if type == 'McGinley'
- mg = 0.0
- mg := na(mg[1]) ? ta.ema(src, len) : mg[1] + (src - mg[1]) / (len * math.pow(src / mg[1], 4))
- result := mg
- result
- if type == 'FRMA'
- LMA = ta.rma(src, len)
- HMA = ta.rma(src, len)
- avg = ta.atr(len)
- lr3 = avg * 0.236
- hr3 = avg * 0.236
- topl = LMA + hr3
- bottl = HMA - lr3
- fma = (topl + bottl) / 2.00
- result := fma
- result
- if type == 'THMA'
- thma = ta.ema(2 * ta.ema(src, len / 2) - ta.ema(src, len), math.round(math.sqrt(len)))
- result := thma
- result
- if type == 'EHMA'
- ehma = ta.wma(ta.wma(src, len / 3) * 3 - ta.wma(src, len / 2) - ta.wma(src, len), len)
- result := ehma
- result
- if type == 'ALMA'
- alma = ta.alma(src, len, offset, sigma)
- result := alma
- result
- if type == 'EDSMA'
- zeros = src - nz(src[2])
- avgZeros = (zeros + zeros[1]) / 2
- // Ehlers Super Smoother Filter
- ssf = ssfPoles == 2 ? get2PoleSSF(avgZeros, ssfLength) : get3PoleSSF(avgZeros, ssfLength)
- // Rescale filter in terms of Standard Deviations
- stdev = ta.stdev(ssf, len)
- scaledFilter = stdev != 0 ? ssf / stdev : 0
- alpha = 5 * math.abs(scaledFilter) / len
- edsma = 0.0
- edsma := alpha * src + (1 - alpha) * nz(edsma[1])
- result := edsma
- result
- result
- ///Keltner Baseline Channel
- s2 = (ma(bType, close, blength) - ma(maType, close, len))
- c_color = s2 <= 0 ? #ff0062 : #00c3ff
- basis = ma(bType, close, blength)
- Keltma = ma(maType, close, len)
- avg = ta.atr(len)
- lr3 = avg * fibratio
- hr3 = avg * fibratio
- topl = Keltma + hr3
- bottl = Keltma - lr3
- fhma = (topl + bottl) / 2
- fill_color = color.rgb(255, 255, 255, 95)
- f3 = plot(Keltma, title='Median MA', color = c_color, linewidth = 2)
- t3 = plot(topl, title='Top MA', color = #00c3ff, linewidth = 2)
- b3 = plot(bottl, title='Bottom MA', color= #ff0062 , linewidth = 2)
- plot(basis, "Basis", color=c_color)
- plotcandle(open, high, low, close, color=c_color, wickcolor = c_color, bordercolor = c_color, title = "Momentum Candles")
- fill(t3, b3, color=fill_color, title="Background")
- // FUNCTION TO ADD NEW AND REMOVE LAST IN ARRAY
- f_array_add_pop(array, new_value_to_add) =>
- array.unshift(array, new_value_to_add)
- array.pop(array)
- // FUNCTION SWING H & L LABELS
- f_sh_sl_labels(array, swing_type) =>
- var string label_text = na
- if swing_type == 1
- if array.get(array, 0) >= array.get(array, 1)
- label_text := 'HH'
- else
- label_text := 'LH'
- label.new(bar_index - swinglength, array.get(array,0), text = label_text, style=label.style_label_down, textcolor = swing_type_color, color = color.new(swing_type_color, 100), size = size.tiny)
- else if swing_type == -1
- if array.get(array, 0) >= array.get(array, 1)
- label_text := 'HL'
- else
- label_text := 'LL'
- label.new(bar_index - swinglength, array.get(array,0), text = label_text, style=label.style_label_up, textcolor = swing_type_color, color = color.new(swing_type_color, 100), size = size.tiny)
- // FUNCTION MAKE SURE SUPPLY ISNT OVERLAPPING
- f_check_overlapping(new_poi, box_array, atr) =>
- atr_threshold = atr * 2
- okay_to_draw = true
- for i = 0 to array.size(box_array) - 1
- top = box.get_top(array.get(box_array, i))
- bottom = box.get_bottom(array.get(box_array, i))
- poi = (top + bottom) / 2
- upper_boundary = poi + atr_threshold
- lower_boundary = poi - atr_threshold
- if new_poi >= lower_boundary and new_poi <= upper_boundary
- okay_to_draw := false
- break
- else
- okay_to_draw := true
- okay_to_draw
- // FUNCTION TO DRAW SUPPLY OR DEMAND ZONE
- f_supply_demand(value_array, bn_array, box_array, label_array, box_type, atr) =>
- atr_buffer = atr * (box_width / 10)
- box_left = array.get(bn_array, 0)
- box_right = bar_index
- var float box_top = 0.00
- var float box_bottom = 0.00
- var float poi = 0.00
- if box_type == 1
- box_top := array.get(value_array, 0)
- box_bottom := box_top - atr_buffer
- poi := (box_top + box_bottom) / 2
- else if box_type == -1
- box_bottom := array.get(value_array, 0)
- box_top := box_bottom + atr_buffer
- poi := (box_top + box_bottom) / 2
- okay_to_draw = f_check_overlapping(poi, box_array, atr)
- // okay_to_draw = true
- //delete oldest box, and then create a new box and add it to the array
- if box_type == 1 and okay_to_draw
- box.delete( array.get(box_array, array.size(box_array) - 1) )
- f_array_add_pop(box_array, box.new( left = box_left, top = box_top, right = box_right, bottom = box_bottom, border_color = supply_outline_color,
- bgcolor = supply_color, extend = extend.right, text = 'Bearish OB', text_halign = text.align_center, text_valign = text.align_center, text_color = poi_label_color, text_size = size.small, xloc = xloc.bar_index))
- box.delete( array.get(label_array, array.size(label_array) - 1) )
- f_array_add_pop(label_array, box.new( left = box_left, top = poi, right = box_right, bottom = poi, border_color = color.new(poi_label_color,90),
- bgcolor = color.new(poi_label_color,90), extend = extend.right, text = '', text_halign = text.align_left, text_valign = text.align_center, text_color = poi_label_color, text_size = size.small, xloc = xloc.bar_index))
- else if box_type == -1 and okay_to_draw
- box.delete( array.get(box_array, array.size(box_array) - 1) )
- f_array_add_pop(box_array, box.new( left = box_left, top = box_top, right = box_right, bottom = box_bottom, border_color = demand_outline_color,
- bgcolor = demand_color, extend = extend.right, text = 'Bullish OB', text_halign = text.align_center, text_valign = text.align_center, text_color = poi_label_color, text_size = size.small, xloc = xloc.bar_index))
- box.delete( array.get(label_array, array.size(label_array) - 1) )
- f_array_add_pop(label_array, box.new( left = box_left, top = poi, right = box_right, bottom = poi, border_color = color.new(poi_label_color,90),
- bgcolor = color.new(poi_label_color,90), extend = extend.right, text = '', text_halign = text.align_left, text_valign = text.align_center, text_color = poi_label_color, text_size = size.small, xloc = xloc.bar_index))
- // FUNCTION TO CHANGE SUPPLY/DEMAND TO A BOS IF BROKEN
- f_sd_to_bos(box_array, bos_array, label_array, zone_type) =>
- if zone_type == 1
- for i = 0 to array.size(box_array) - 1
- level_to_break = box.get_top(array.get(box_array,i))
- // if ta.crossover(close, level_to_break)
- if close >= level_to_break
- copied_box = box.copy(array.get(box_array,i))
- f_array_add_pop(bos_array, copied_box)
- mid = (box.get_top(array.get(box_array,i)) + box.get_bottom(array.get(box_array,i))) / 2
- box.set_top(array.get(bos_array,0), mid)
- box.set_bottom(array.get(bos_array,0), mid)
- box.set_extend( array.get(bos_array,0), extend.none)
- box.set_right( array.get(bos_array,0), bar_index)
- box.set_text( array.get(bos_array,0), '' )
- box.set_text_color( array.get(bos_array,0), bos_label_color)
- box.set_text_size( array.get(bos_array,0), size.small)
- box.set_text_halign( array.get(bos_array,0), text.align_center)
- box.set_text_valign( array.get(bos_array,0), text.align_center)
- box.delete(array.get(box_array, i))
- box.delete(array.get(label_array, i))
- if zone_type == -1
- for i = 0 to array.size(box_array) - 1
- level_to_break = box.get_bottom(array.get(box_array,i))
- // if ta.crossunder(close, level_to_break)
- if close <= level_to_break
- copied_box = box.copy(array.get(box_array,i))
- f_array_add_pop(bos_array, copied_box)
- mid = (box.get_top(array.get(box_array,i)) + box.get_bottom(array.get(box_array,i))) / 2
- box.set_top(array.get(bos_array,0), mid)
- box.set_bottom(array.get(bos_array,0), mid)
- box.set_extend( array.get(bos_array,0), extend.none)
- box.set_right( array.get(bos_array,0), bar_index)
- box.set_text( array.get(bos_array,0), '' )
- box.set_text_color( array.get(bos_array,0), bos_label_color)
- box.set_text_size( array.get(bos_array,0), size.small)
- box.set_text_halign( array.get(bos_array,0), text.align_center)
- box.set_text_valign( array.get(bos_array,0), text.align_center)
- box.delete(array.get(box_array, i))
- box.delete(array.get(label_array, i))
- // FUNCTION MANAGE CURRENT BOXES BY CHANGING ENDPOINT
- f_extend_box_endpoint(box_array) =>
- for i = 0 to array.size(box_array) - 1
- box.set_right(array.get(box_array, i), bar_index + 100)
- //
- //CALCULATIONS
- //
- // CALCULATE ATR
- atr = ta.atr(50)
- // CALCULATE SWING HIGHS & SWING LOWS
- swing_high = ta.pivothigh(high, swinglength, swinglength)
- swing_low = ta.pivotlow(low, swinglength, swinglength)
- // ARRAYS FOR SWING H/L & BN
- var swing_high_values = array.new_float(5,0.00)
- var swing_low_values = array.new_float(5,0.00)
- var swing_high_bns = array.new_int(5,0)
- var swing_low_bns = array.new_int(5,0)
- // ARRAYS FOR SUPPLY / DEMAND
- var current_supply_box = array.new_box(history_of_demand_to_keep, na)
- var current_demand_box = array.new_box(history_of_demand_to_keep, na)
- // ARRAYS FOR SUPPLY / DEMAND POI LABELS
- var current_supply_poi = array.new_box(history_of_demand_to_keep, na)
- var current_demand_poi = array.new_box(history_of_demand_to_keep, na)
- // ARRAYS FOR BOS
- var supply_bos = array.new_box(5, na)
- var demand_bos = array.new_box(5, na)
- //
- //END CALCULATIONS
- //
- // NEW SWING HIGH
- if not na(swing_high)
- //MANAGE SWING HIGH VALUES
- f_array_add_pop(swing_high_values, swing_high)
- f_array_add_pop(swing_high_bns, bar_index[swinglength])
- if show_price_action_labels
- f_sh_sl_labels(swing_high_values, 1)
- f_supply_demand(swing_high_values, swing_high_bns, current_supply_box, current_supply_poi, 1, atr)
- // NEW SWING LOW
- else if not na(swing_low)
- //MANAGE SWING LOW VALUES
- f_array_add_pop(swing_low_values, swing_low)
- f_array_add_pop(swing_low_bns, bar_index[swinglength])
- if show_price_action_labels
- f_sh_sl_labels(swing_low_values, -1)
- f_supply_demand(swing_low_values, swing_low_bns, current_demand_box, current_demand_poi, -1, atr)
- f_sd_to_bos(current_supply_box, supply_bos, current_supply_poi, 1)
- f_sd_to_bos(current_demand_box, demand_bos, current_demand_poi, -1)
- f_extend_box_endpoint(current_supply_box)
- f_extend_box_endpoint(current_demand_box)
- //-----------------------------------------------------------------------------}
- //Functions
- //-----------------------------------------------------------------------------{
- n = bar_index
- cmean_range = ta.cum(high - low) / n
- //HL Output function
- hl() => [high, low]
- //Get ohlc values function
- get_ohlc()=> [close[1], open[1], high, low, high[2], low[2]]
- //Display Structure function
- display_Structure(x, y, txt, css, dashed, down, lbl_size)=>
- structure_line = line.new(x, y, n, y
- , color = css
- , style = dashed ? line.style_dashed : line.style_solid)
- structure_lbl = label.new(int(math.avg(x, n)), y, txt
- , color = TRANSP_CSS
- , textcolor = css
- , style = down ? label.style_label_down : label.style_label_up
- , size = lbl_size)
- if mode == 'Present'
- line.delete(structure_line[1])
- label.delete(structure_lbl[1])
- //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 trend = 0, var itrend = 0
- var top_y = 0., var top_x = 0
- var btm_y = 0., var btm_x = 0
- var itop_y = 0., var itop_x = 0
- var ibtm_y = 0., var ibtm_x = 0
- var trail_up = high, var trail_dn = low
- var trail_up_x = 0, var trail_dn_x = 0
- var top_cross = true, var btm_cross = true
- var itop_cross = true, var ibtm_cross = true
- var txt_top = '', var txt_btm = ''
- //Alerts
- bull_choch_alert = false
- bull_bos_alert = false
- bear_choch_alert = false
- bear_bos_alert = false
- bull_ichoch_alert = false
- bull_ibos_alert = false
- bear_ichoch_alert = false
- bear_ibos_alert = false
- bull_iob_break = false
- bear_iob_break = false
- bull_ob_break = false
- bear_ob_break = false
- eqh_alert = false
- eql_alert = false
- //Structure colors
- var bull_css = style == 'Monochrome' ? #b2b5be
- : swing_bull_css
- var bear_css = style == 'Monochrome' ? #b2b5be
- : swing_bear_css
- var ibull_css = style == 'Monochrome' ? #b2b5be
- : swing_ibull_css
- var ibear_css = style == 'Monochrome' ? #b2b5be
- : swing_ibear_css
- //Labels size
- var internal_structure_lbl_size = internal_structure_size == 'Tiny'
- ? size.tiny
- : internal_structure_size == 'Small'
- ? size.small
- : size.normal
- var swing_structure_lbl_size = swing_structure_size == 'Tiny'
- ? size.tiny
- : swing_structure_size == 'Small'
- ? size.small
- : size.normal
- //Swings
- [top, btm] = swings(length)
- [itop, ibtm] = swings(ilength)
- //-----------------------------------------------------------------------------}
- //Pivot High
- //-----------------------------------------------------------------------------{
- var line extend_top = na
- var label extend_top_lbl = label.new(na, na
- , color = TRANSP_CSS
- , textcolor = bear_css
- , style = label.style_label_down
- , size = size.tiny)
- if top
- top_cross := true
- txt_top := top > top_y ? 'HH' : 'LH'
- if show_swings
- top_lbl = label.new(n-length, top, txt_top
- , color = TRANSP_CSS
- , textcolor = bear_css
- , style = label.style_label_down
- , size = swing_structure_lbl_size)
- if mode == 'Present'
- label.delete(top_lbl[1])
- //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
- if itop
- itop_cross := true
- itop_y := itop
- itop_x := n - 5
- //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 show_hl_swings
- line.set_xy1(extend_top, trail_up_x, trail_up)
- line.set_xy2(extend_top, n + 20, trail_up)
- label.set_x(extend_top_lbl, n + 20)
- label.set_y(extend_top_lbl, trail_up)
- label.set_text(extend_top_lbl, trend < 0 ? 'Strong High' : 'Weak High')
- //-----------------------------------------------------------------------------}
- //Pivot Low
- //-----------------------------------------------------------------------------{
- var line extend_btm = na
- var label extend_btm_lbl = label.new(na, na
- , color = TRANSP_CSS
- , textcolor = bull_css
- , style = label.style_label_up
- , size = size.tiny)
- if btm
- btm_cross := true
- txt_btm := btm < btm_y ? 'LL' : 'HL'
- if show_swings
- btm_lbl = label.new(n - length, btm, txt_btm
- , color = TRANSP_CSS
- , textcolor = bull_css
- , style = label.style_label_up
- , size = swing_structure_lbl_size)
- if mode == 'Present'
- label.delete(btm_lbl[1])
- //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
- if ibtm
- ibtm_cross := true
- ibtm_y := ibtm
- ibtm_x := n - 5
- //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 show_hl_swings
- line.set_xy1(extend_btm, trail_dn_x, trail_dn)
- line.set_xy2(extend_btm, n + 20, trail_dn)
- label.set_x(extend_btm_lbl, n + 20)
- label.set_y(extend_btm_lbl, trail_dn)
- label.set_text(extend_btm_lbl, trend > 0 ? 'Strong Low' : 'Weak Low')
- //-----------------------------------------------------------------------------}
- //Order Blocks Arrays
- //-----------------------------------------------------------------------------{
- var iob_top = array.new_float(0)
- var iob_btm = array.new_float(0)
- var iob_left = array.new_int(0)
- var iob_type = array.new_int(0)
- var ob_top = array.new_float(0)
- var ob_btm = array.new_float(0)
- var ob_left = array.new_int(0)
- var ob_type = array.new_int(0)
- //-----------------------------------------------------------------------------}
- //Pivot High BOS/CHoCH
- //-----------------------------------------------------------------------------{
- //Filtering
- var bull_concordant = true
- if ifilter_confluence
- bull_concordant := high - math.max(close, open) > math.min(close, open - low)
- //Detect internal bullish Structure
- if ta.crossover(close, itop_y) and itop_cross and top_y != itop_y and bull_concordant
- bool choch = na
- if itrend < 0
- choch := true
- bull_ichoch_alert := true
- else
- bull_ibos_alert := true
- txt = choch ? 'CHoCH' : 'BOS'
- if show_internals
- if show_ibull == 'All' or (show_ibull == 'BOS' and not choch) or (show_ibull == 'CHoCH' and choch)
- display_Structure(itop_x, itop_y, txt, ibull_css, true, true, internal_structure_lbl_size)
- itop_cross := false
- itrend := 1
- //Detect bullish Structure
- if ta.crossover(close, top_y) and top_cross
- bool choch = na
- if trend < 0
- choch := true
- bull_choch_alert := true
- else
- bull_bos_alert := true
- txt = choch ? 'CHoCH' : 'BOS'
- if show_Structure
- if show_bull == 'All' or (show_bull == 'BOS' and not choch) or (show_bull == 'CHoCH' and choch)
- display_Structure(top_x, top_y, txt, bull_css, false, true, swing_structure_lbl_size)
- top_cross := false
- trend := 1
- //-----------------------------------------------------------------------------}
- //Pivot Low BOS/CHoCH
- //-----------------------------------------------------------------------------{
- var bear_concordant = true
- if ifilter_confluence
- bear_concordant := high - math.max(close, open) < math.min(close, open - low)
- //Detect internal bearish Structure
- if ta.crossunder(close, ibtm_y) and ibtm_cross and btm_y != ibtm_y and bear_concordant
- bool choch = false
- if itrend > 0
- choch := true
- bear_ichoch_alert := true
- else
- bear_ibos_alert := true
- txt = choch ? 'CHoCH' : 'BOS'
- if show_internals
- if show_ibear == 'All' or (show_ibear == 'BOS' and not choch) or (show_ibear == 'CHoCH' and choch)
- display_Structure(ibtm_x, ibtm_y, txt, ibear_css, true, false, internal_structure_lbl_size)
- ibtm_cross := false
- itrend := -1
- //Detect bearish Structure
- if ta.crossunder(close, btm_y) and btm_cross
- bool choch = na
- if trend > 0
- choch := true
- bear_choch_alert := true
- else
- bear_bos_alert := true
- txt = choch ? 'CHoCH' : 'BOS'
- if show_Structure
- if show_bear == 'All' or (show_bear == 'BOS' and not choch) or (show_bear == 'CHoCH' and choch)
- display_Structure(btm_x, btm_y, txt, bear_css, false, false, swing_structure_lbl_size)
- btm_cross := false
- trend := -1
- //-----------------------------------------------------------------------------}
- //Fair Value Gaps
- //-----------------------------------------------------------------------------{
- var bullish_fvg_max = array.new_box(0)
- var bullish_fvg_min = array.new_box(0)
- var bearish_fvg_max = array.new_box(0)
- var bearish_fvg_min = array.new_box(0)
- float bullish_fvg_avg = na
- float bearish_fvg_avg = na
- bullish_fvg_cnd = false
- bearish_fvg_cnd = false
- //Trend
- //-----------------------------------------------------------------------------{
- var color trend_css = na
- if show_trend
- if style == 'Colored'
- trend_css := itrend == 1 ? bull_css : bear_css
- else if style == 'Monochrome'
- trend_css := itrend == 1 ? #b2b5be : #5d606b
- plotcandle(open, high, low, close
- , color = trend_css
- , wickcolor = trend_css
- , bordercolor = trend_css
- , editable = false)
- filter(float src, int len) =>
- var float filter = na
- filter := ta.cum((src + (src[1] * 2) + (src[2] * 2) + src[3])/6)
- (filter - filter[len])/len
- rsi(src, len) =>
- rsi = ta.rsi(filter(src, 1), len)
- f = -math.pow(math.abs(math.abs(rsi - 50) - 50), 1 + math.pow(len / 14, 0.618) - 1) / math.pow(50, math.pow(len / 14, 0.618) - 1) + 50
- rsia = if rsi > 50
- f + 50
- else
- -f + 50
- rsia
- LL = ta.crossover(basis, topl) and s2 >= 0
- SS = ta.crossunder(basis, bottl) and s2 <= 0
- if (LL)
- trend := 1
- else if (SS)
- trend := -1
- var lastSignal = 0
- longCondition = trend == 1 and LL and lastSignal != 1
- shortCondition = trend == -1 and SS and lastSignal != -1
- if (longCondition)
- lastSignal := 1
- alert("Open Long")
- if (shortCondition)
- lastSignal := -1
- alert("Open Short")
- if close > topl and (bull_choch_alert or bull_bos_alert)
- alert("Bullish Choc/Bos in a long. Consider adding to your position")
- if close < bottl and (bull_choch_alert or bull_bos_alert)
- alert("Bullish Choc/Bos in a short. Consider closing your position")
- if close > topl and (bear_choch_alert or bear_bos_alert)
- alert("Bearish Choc/Bos in a long. Consider closing your position")
- if close < bottl and (bear_choch_alert or bear_bos_alert)
- alert("Bullish Choc/Bos in a short. Consider adding to your position")
- plotshape(shortCondition, location=location.abovebar, style=shape.labeldown, color= #ff0062, size=size.tiny, text='Sell', textcolor=color.new(#ffffff, 0))
- plotshape(longCondition, location=location.belowbar, style=shape.labelup, color= #00c3ff, size=size.tiny, text='Buy', textcolor=color.new(#000000, 0))
- plotcandle(open, high, low, close, color=c_color, wickcolor = c_color, bordercolor = c_color, title = "Momentum Candles")
- //alertcondition(longCondition, title='**Long**')
- //alertcondition(shortCondition, title='**Short**')
- //alertcondition(bull_bos_alert, 'Bullish BOS', 'Internal Bullish BOS formed')
- //alertcondition(bull_choch_alert, 'Bullish CHoCH', 'Internal Bullish CHoCH formed')
- //alertcondition(bear_bos_alert, 'Bearish BOS', 'Bearish BOS formed')
- //alertcondition(bear_choch_alert, 'Bearish CHoCH', 'Bearish CHoCH formed')
Add Comment
Please, Sign In to add comment