Divergences for many indicators pinescript
Forums › ProRealTime English forum › ProBuilder support › Divergences for many indicators pinescript
- This topic has 8 replies, 3 voices, and was last updated 2 months ago by colin2510.
Viewing 9 posts - 1 through 9 (of 9 total)
-
-
09/22/2024 at 9:25 AM #237916// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/// © LuxAlgo//@version=5indicator(“Order Blocks & Breaker Blocks [LuxAlgo]”, overlay = true, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500)//——————————————————————————//Settings//—————————————————————————–{length = input.int(10, ‘Swing Lookback’ , minval = 3)showBull = input.int(3, ‘Show Last Bullish OB’, minval = 0)showBear = input.int(3, ‘Show Last Bearish OB’, minval = 0)useBody = input(false, ‘Use Candle Body’)//StylebullCss = input(color.new(#2157f3, 80), ‘Bullish OB’ , inline = ‘bullcss’, group = ‘Style’)bullBreakCss = input(color.new(#ff1100, 80), ‘Bullish Break’, inline = ‘bullcss’, group = ‘Style’)bearCss = input(color.new(#ff5d00, 80), ‘Bearish OB’ , inline = ‘bearcss’, group = ‘Style’)bearBreakCss = input(color.new(#0cb51a, 80), ‘Bearish Break’, inline = ‘bearcss’, group = ‘Style’)showLabels = input(false, ‘Show Historical Polarity Changes’)//—————————————————————————–}//UDT’s//—————————————————————————–{type obfloat top = nafloat btm = naint loc = bar_indexbool breaker = falseint break_loc = natype swingfloat y = naint x = nabool crossed = false//—————————————————————————–}//Functions//—————————————————————————–{swings(len)=>var os = 0var swing top = swing.new(na, na)var swing btm = swing.new(na, na)upper = ta.highest(len)lower = ta.lowest(len)os:=high[len]>upper?0: low[len] < lower ? 1 : osif os == 0 and os[1] != 0top := swing.new(high[length], bar_index[length])if os == 1 and os[1] != 1btm := swing.new(low[length], bar_index[length])[top, btm]method notransp(color css) => color.rgb(color.r(css), color.g(css), color.b(css))method display(ob id, css, break_css)=>if id.breakerbox.new(id.loc, id.top, id.break_loc, id.btm, css.notransp(), bgcolor = css, xloc = xloc.bar_time)box.new(id.break_loc, id.top, time+1, id.btm, na, bgcolor = break_css, extend = extend.right, xloc = xloc.bar_time)line.new(id.loc, id.top, id.break_loc, id.top, xloc.bar_time, color = css.notransp())line.new(id.loc, id.btm, id.break_loc, id.btm, xloc.bar_time, color = css.notransp())line.new(id.break_loc, id.top, time+1, id.top, xloc.bar_time, extend.right, break_css.notransp(), line.style_dashed)line.new(id.break_loc, id.btm, time+1, id.btm, xloc.bar_time, extend.right, break_css.notransp(), line.style_dashed)elsebox.new(id.loc, id.top, time, id.btm, na, bgcolor = css, extend = extend.right, xloc = xloc.bar_time)line.new(id.loc, id.top, time, id.top, xloc.bar_time, extend.right, css.notransp())line.new(id.loc, id.btm, time, id.btm, xloc.bar_time, extend.right, css.notransp())//—————————————————————————–}//Detect Swings//—————————————————————————–{n = bar_index[top, btm] = swings(length)max = useBody ? math.max(close, open) : highmin = useBody ? math.min(close, open) : low//—————————————————————————–}//Bullish OB//—————————————————————————–{var bullish_ob = array.new<ob>(0)bull_break_conf = 0if close > top.y and not top.crossedtop.crossed := trueminima = max[1]maxima = min[1]loc = time[1]for i = 1 to (n – top.x)-1minima := math.min(min[i], minima)maxima := minima == min[i] ? max[i] : maximaloc := minima == min[i] ? time[i] : locbullish_ob.unshift(ob.new(maxima, minima, loc))if bullish_ob.size() > 0for i = bullish_ob.size()-1 to 0element = bullish_ob.get(i)ifnotelement.breakerif math.min(close, open) < element.btmelement.breaker := trueelement.break_loc := timeelseif close > element.topbullish_ob.remove(i)elseifi<showBullandtop.y<element.topandtop.y>element.btmbull_break_conf := 1//Set labelif bull_break_conf > bull_break_conf[1] and showLabelslabel.new(top.x, top.y, ‘▼’, color = na, textcolor = bearCss.notransp(), style = label.style_label_down, size = size.tiny)//—————————————————————————–}//Bearish OB//—————————————————————————–{var bearish_ob = array.new<ob>(0)bear_break_conf = 0if close < btm.y and not btm.crossedbtm.crossed := trueminima = min[1]maxima = max[1]loc = time[1]for i = 1 to (n – btm.x)-1maxima := math.max(max[i], maxima)minima := maxima == max[i] ? min[i] : minimaloc := maxima == max[i] ? time[i] : locbearish_ob.unshift(ob.new(maxima, minima, loc))if bearish_ob.size() > 0for i = bearish_ob.size()-1 to 0element = bearish_ob.get(i)ifnotelement.breakerif math.max(close, open) > element.topelement.breaker := trueelement.break_loc := timeelseif close < element.btmbearish_ob.remove(i)elseifi<showBearandbtm.y>element.btmandbtm.y<element.topbear_break_conf := 1//Set labelif bear_break_conf > bear_break_conf[1] and showLabelslabel.new(btm.x, btm.y, ‘▲’, color = na, textcolor = bullCss.notransp(), style = label.style_label_up, size = size.tiny)//—————————————————————————–}//Set Order Blocks//—————————————————————————–{for bx in box.allbx.delete()for l in line.alll.delete()if barstate.islast//Bullishif showBull > 0for i = 0 to math.min(showBull-1, bullish_ob.size())get_ob = bullish_ob.get(i)get_ob.display(bullCss, bullBreakCss)//Bearishif showBear > 0for i = 0 to math.min(showBear-1, bearish_ob.size())get_ob = bearish_ob.get(i)get_ob.display(bearCss, bearBreakCss)//—————————————————————————–}09/30/2024 at 12:34 PM #238290
Here you have the code:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167//---------------------------------------------------------////PRC_Order Blocks and Breaker Blocks//version = 0//30.09.2024//Iván González @ www.prorealcode.com//Sharing ProRealTime knowledge//---------------------------------------------------------////-----Inputs----------------------------------------------////---------------------------------------------------------//length=10 //Swing LookbackshowBull=5 //Show last bullish OBshowBear=5 //Show last bearish OBuseBody=0showLabels=1 //Show labelslookbackPeriod=300//---------------------------------------------------------////-----Detect Swings---------------------------------------////---------------------------------------------------------//n=barindexonce os=0upper=highest[length](high)lower=lowest[length](low)if high[length]>upper thenos=0elsif low[length]<lower thenos=1elseos=osendifif os=0 and os[1]<>0 thentopy=high[length]topx=barindex[length]topcrossed=0elsif os=1 and os[1]<>1 thenbtmy=low[length]btmx=barindex[length]btmcrossed=0endifif usebody thenmimax=max(close,open)mimin=min(close,open)elsemimax=max(close,open)mimin=min(close,open)endif//---------------------------------------------------------////-----Bullish OB------------------------------------------////---------------------------------------------------------//if close > topy and topcrossed=0 thentopcrossed=1minima=mimax[1]maxima=mimin[1]loc=barindex[1]for i=1 to (n-topx)-1 dominima=min(mimin[i],minima)if minima=mimin[i] thenmaxima=mimax[i]loc=barindex[i]elsemaxima=maximaloc=locendifnext$bullTop[m+1]=maxima$bullBtm[m+1]=minima$bullx[m+1]=loc$bullbreak[m+1]=0m=m+1endif//---------------------------------------------------------////-----Bearish OB------------------------------------------////---------------------------------------------------------//bearBreakConf=0if close < btmy and btmcrossed=0 thenbtmcrossed=1minima2=mimin[1]maxima2=mimax[1]loc2=barindex[1]for i=1 to (n-btmx)-1 domaxima2=max(mimax[i],maxima2)if maxima2=mimax[i] thenminima2=mimin[i]loc2=barindex[i]elseminima2=minima2loc2=loc2endifnext$bearTop[z+1]=maxima2$bearBtm[z+1]=minima2$bearx[z+1]=loc2$bearbreak[z+1]=0z=z+1endif//---------------------------------------------------------////-----Plot Order Blocks-----------------------------------////---------------------------------------------------------//if islastbarupdate thenfor i=m downto 1 dofor j=barindex downto 0 doif barindex[j]>$bullx[i] and close[j] < $bullBtm[i] then$bullright[i]=barindex[j]$bullbreak[i]=1breakelse$bullright[i]=barindexendifnextif i>m-showbull thenif $bullbreak[i]=0 thendrawrectangle($bullx[i],$bullBtm[i],$bullright[i]+20,$bullTop[i])coloured("blue")fillcolor("blue",50)elsif $bullbreak[i]=1 thendrawrectangle($bullx[i],$bullBtm[i],$bullright[i],$bullTop[i])coloured("blue")fillcolor("blue",50)if close>$bullTop[i]thendrawrectangle($bullright[i],$bullBtm[i],barindex+20,$bullTop[i])coloured("green",0)fillcolor("green",50)elsif close<$bullBtm[i]thendrawrectangle($bullright[i],$bullBtm[i],barindex+20,$bullTop[i])coloured("red",0)fillcolor("red",50)elsedrawrectangle($bullright[i],$bullBtm[i],barindex+20,$bullTop[i])coloured("yellow",0)fillcolor("yellow",50)endifendifendifnextfor i=z downto 1 dofor j=barindex downto 0 doif barindex[j]>$bearx[i] and close[j] > $bearTop[i] then$bearright[i]=barindex[j]$bearbreak[i]=1breakelse$bearright[i]=barindexendifnextif i>z-showbear thenif $bearbreak[i]=0 thendrawrectangle($bearx[i],$bearBtm[i],$bearright[i]+20,$bearTop[i])coloured("orange")fillcolor("orange",50)elsif $bearbreak[i]=1 thendrawrectangle($bearx[i],$bearBtm[i],$bearright[i],$bearTop[i])coloured("orange")fillcolor("orange",50)if close>$bearTop[i]thendrawrectangle($bearright[i],$bearBtm[i],barindex+20,$bearTop[i])coloured("green",0)fillcolor("green",50)elsif close<$bearBtm[i]thendrawrectangle($bearright[i],$bearBtm[i],barindex+20,$bearTop[i])coloured("red",0)fillcolor("red",50)elsedrawrectangle($bearright[i],$bearBtm[i],barindex+20,$bearTop[i])coloured("yellow",0)fillcolor("yellow",50)endifendifendifnextendifreturn09/30/2024 at 12:54 PM #23829109/30/2024 at 6:01 PM #238300It works like a charm to me.
Check that both the first and the last lines in your code are the same as those posted above, as it may happen that Copy & Paste adds odd characters and lines, at times.
09/30/2024 at 8:59 PM #23830510/01/2024 at 8:07 AM #238326Find the drawing lines of the rectangles and remove the
fillcolor("red")
part. This should work for you.2 users thanked author for this post.
10/01/2024 at 8:32 AM #23833410/01/2024 at 8:50 AM #23833710/01/2024 at 8:52 AM #238338// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © LonesomeTheBlue//@version=4study(“Divergence for Many Indicators v4 Colin”, overlay = true, max_bars_back = 1000, max_lines_count = 400, max_labels_count = 400)prd = input(defval = 5, title = “Pivot Period”, minval = 1, maxval = 50)source = input(defval = “Close”, title = “Source for Pivot Points”, options = [“Close”, “High/Low”])searchdiv = input(defval = “Regular”, title = “Divergence Type”, options = [“Regular”, “Hidden”, “Regular/Hidden”])showindis = input(defval = “Full”, title = “Show Indicator Names”, options = [“Full”, “First Letter”, “Don’t Show”])showlimit = input(1, title=”Minimum Number of Divergence”, minval = 1, maxval = 11)maxpp = input(defval = 10, title = “Maximum Pivot Points to Check”, minval = 1, maxval = 20)maxbars = input(defval = 100, title = “Maximum Bars to Check”, minval = 30, maxval = 200)shownum = input(defval = true, title = “Show Divergence Number”)showlast = input(defval = false, title = “Show Only Last Divergence”)dontconfirm = input(defval = false, title = “Don’t Wait for Confirmation”)showlines = input(defval = true, title = “Show Divergence Lines”)showpivot = input(defval = false, title = “Show Pivot Points”)calcmacd = input(defval = true, title = “MACD”)calcmacda = input(defval = true, title = “MACD Histogram”)calcrsi = input(defval = true, title = “RSI”)calcstoc = input(defval = true, title = “Stochastic”)calccci = input(defval = true, title = “CCI”)calcmom = input(defval = true, title = “Momentum”)calcobv = input(defval = true, title = “OBV”)calcvwmacd = input(true, title = “VWmacd”)calccmf = input(true, title = “Chaikin Money Flow”)calcmfi = input(true, title = “Money Flow Index”)calcext = input(false, title = “Check External Indicator”)externalindi = input(defval = close, title = “External Indicator”)pos_reg_div_col = input(defval = color.yellow, title = “Positive Regular Divergence”)neg_reg_div_col = input(defval = color.navy, title = “Negative Regular Divergence”)pos_hid_div_col = input(defval = color.lime, title = “Positive Hidden Divergence”)neg_hid_div_col = input(defval = color.red, title = “Negative Hidden Divergence”)pos_div_text_col = input(defval = color.black, title = “Positive Divergence Text Color”)neg_div_text_col = input(defval = color.white, title = “Negative Divergence Text Color”)reg_div_l_style_ = input(defval = “Solid”, title = “Regular Divergence Line Style”, options = [“Solid”, “Dashed”, “Dotted”])hid_div_l_style_ = input(defval = “Dashed”, title = “Hdden Divergence Line Style”, options = [“Solid”, “Dashed”, “Dotted”])reg_div_l_width = input(defval = 2, title = “Regular Divergence Line Width”, minval = 1, maxval = 5)hid_div_l_width = input(defval = 1, title = “Hidden Divergence Line Width”, minval = 1, maxval = 5)showmas = input(defval = false, title = “Show MAs 50 & 200”, inline = “ma12”)cma1col = input(defval = color.orange, title = “”, inline = “ma12”)cma2col = input(defval = color.blue, title = “”, inline = “ma12”)cma3col = input(defval = color.black, title = “”, inline = “ma12”)plot(showmas ? sma(close, 21) : na, color = showmas ? cma1col : na)plot(showmas ? sma(close, 50) : na, color = showmas ? cma2col: na)plot(showmas ? sma(close, 200) : na, color = showmas ? cma3col : na)// set line stylesvarreg_div_l_style=reg_div_l_style_==”Solid”?line.style_solid:reg_div_l_style_ == “Dashed” ? line.style_dashed :line.style_dottedvarhid_div_l_style=hid_div_l_style_==”Solid”?line.style_solid:hid_div_l_style_ == “Dashed” ? line.style_dashed :line.style_dotted// get indicatorsrsi = rsi(close, 14) // RSI[macd, signal, deltamacd] = macd(close, 12, 26, 9) // MACDmoment = mom(close, 10) // Momentumcci = cci(close, 10) // CCIObv = obv // OBVstk = sma(stoch(close, high, low, 14), 3) // StochmaFast = vwma(close, 12), maSlow = vwma(close, 26), vwmacd = maFast – maSlow // volume weighted macdCmfm = ((close-low) – (high-close)) / (high – low), Cmfv = Cmfm * volume, cmf = sma(Cmfv, 21) / sma(volume,21) // Chaikin money flowMfi = mfi(close, 14) // Moneyt Flow Index// keep indicators names and colors in arraysvar indicators_name = array.new_string(11)var div_colors = array.new_color(4)if barstate.isfirst// namesarray.set(indicators_name, 0, showindis == “Full” ? “MACD” : “M”)array.set(indicators_name, 1, showindis == “Full” ? “Hist” : “H”)array.set(indicators_name, 2, showindis == “Full” ? “RSI” : “E”)array.set(indicators_name, 3, showindis == “Full” ? “Stoch” : “S”)array.set(indicators_name, 4, showindis == “Full” ? “CCI” : “C”)array.set(indicators_name, 5, showindis == “Full” ? “MOM” : “M”)array.set(indicators_name, 6, showindis == “Full” ? “OBV” : “O”)array.set(indicators_name, 7, showindis == “Full” ? “VWMACD” : “V”)array.set(indicators_name, 8, showindis == “Full” ? “CMF” : “C”)array.set(indicators_name, 9, showindis == “Full” ? “MFI” : “M”)array.set(indicators_name,10, showindis == “Full” ? “Extrn” : “X”)//colorsarray.set(div_colors, 0, pos_reg_div_col)array.set(div_colors, 1, neg_reg_div_col)array.set(div_colors, 2, pos_hid_div_col)array.set(div_colors, 3, neg_hid_div_col)// Check if we get new Pivot High Or Pivot Lowfloat ph = pivothigh((source == “Close” ? close : high), prd, prd)float pl = pivotlow((source == “Close” ? close : low), prd, prd)plotshape(ph and showpivot, text = “H”, style = shape.labeldown, color = color.new(color.white, 100), textcolor = color.red, location = location.abovebar, offset = -prd)plotshape(pl and showpivot, text = “L”, style = shape.labelup, color = color.new(color.white, 100), textcolor = color.lime, location = location.belowbar, offset = -prd)// keep values and positions of Pivot Highs/Lows in the arraysvar int maxarraysize = 20var ph_positions = array.new_int(maxarraysize, 0)var pl_positions = array.new_int(maxarraysize, 0)var ph_vals = array.new_float(maxarraysize, 0.)var pl_vals = array.new_float(maxarraysize, 0.)// add PHs to the arrayif pharray.unshift(ph_positions, bar_index)array.unshift(ph_vals, ph)if array.size(ph_positions) > maxarraysizearray.pop(ph_positions)array.pop(ph_vals)// add PLs to the arrayif plarray.unshift(pl_positions, bar_index)array.unshift(pl_vals, pl)if array.size(pl_positions) > maxarraysizearray.pop(pl_positions)array.pop(pl_vals)// functions to check Regular Divergences and Hidden Divergences// function to check positive regular or negative hidden divergence// cond == 1 => positive_regular, cond == 2=> negative_hiddenpositive_regular_positive_hidden_divergence(src, cond)=>divlen = 0prsc = source == “Close” ? close : low// if indicators higher than last value and close price is higher than las closeif dontconfirm or src > src[1] or close > close[1]startpoint = dontconfirm ? 0 : 1 // don’t check last candle// we search last 15 PPsfor x = 0 to maxpp – 1len = bar_index – array.get(pl_positions, x) + prd// if we reach non valued array element or arrived 101. or previous bars then we don’t search moreif array.get(pl_positions, x) == 0 or len > maxbarsbreakiflen>5and((cond == 1 and src[startpoint] > src[len] and prsc[startpoint] < nz(array.get(pl_vals, x))) or(cond == 2 and src[startpoint] < src[len] and prsc[startpoint] > nz(array.get(pl_vals, x))))slope1 = (src[startpoint] – src[len]) / (len – startpoint)virtual_line1 = src[startpoint] – slope1slope2 = (close[startpoint] – close[len]) / (len – startpoint)virtual_line2 = close[startpoint] – slope2arrived = truefor y = 1 + startpoint to len – 1if src[y] < virtual_line1 or nz(close[y]) < virtual_line2arrived := falsebreakvirtual_line1 := virtual_line1 – slope1virtual_line2 := virtual_line2 – slope2if arriveddivlen := lenbreakdivlen// function to check negative regular or positive hidden divergence// cond == 1 => negative_regular, cond == 2=> positive_hiddennegative_regular_negative_hidden_divergence(src, cond)=>divlen = 0prsc = source == “Close” ? close : high// if indicators higher than last value and close price is higher than las closeif dontconfirm or src < src[1] or close < close[1]startpoint = dontconfirm ? 0 : 1 // don’t check last candle// we search last 15 PPsfor x = 0 to maxpp – 1len = bar_index – array.get(ph_positions, x) + prd// if we reach non valued array element or arrived 101. or previous bars then we don’t search moreif array.get(ph_positions, x) == 0 or len > maxbarsbreakiflen>5and((cond==1andsrc[startpoint]<src[len]andprsc[startpoint]>nz(array.get(ph_vals,x)))or(cond == 2 and src[startpoint] > src[len] and prsc[startpoint] < nz(array.get(ph_vals, x))))slope1 = (src[startpoint] – src[len]) / (len – startpoint)virtual_line1 = src[startpoint] – slope1slope2 = (close[startpoint] – nz(close[len])) / (len – startpoint)virtual_line2 = close[startpoint] – slope2arrived = truefor y = 1 + startpoint to len – 1if src[y] > virtual_line1 or nz(close[y]) > virtual_line2arrived := falsebreakvirtual_line1 := virtual_line1 – slope1virtual_line2 := virtual_line2 – slope2if arriveddivlen := lenbreakdivlen// calculate 4 types of divergence if enabled in the options and return divergences in an arraycalculate_divs(cond, indicator)=>divs = array.new_int(4, 0)array.set(divs, 0, cond and (searchdiv == “Regular” or searchdiv == “Regular/Hidden”) ? positive_regular_positive_hidden_divergence(indicator, 1) : 0)array.set(divs, 1, cond and (searchdiv == “Regular” or searchdiv == “Regular/Hidden”) ? negative_regular_negative_hidden_divergence(indicator, 1) : 0)array.set(divs, 2, cond and (searchdiv == “Hidden” or searchdiv == “Regular/Hidden”) ? positive_regular_positive_hidden_divergence(indicator, 2) : 0)array.set(divs, 3, cond and (searchdiv == “Hidden” or searchdiv == “Regular/Hidden”) ? negative_regular_negative_hidden_divergence(indicator, 2) : 0)divs// array to keep all divergencesvar all_divergences = array.new_int(44) // 11 indicators * 4 divergence = 44 elements// set related array elementsarray_set_divs(div_pointer, index)=>for x = 0 to 3array.set(all_divergences, index * 4 + x, array.get(div_pointer, x))// set divergences arrayarray_set_divs(calculate_divs(calcmacd, macd), 0)array_set_divs(calculate_divs(calcmacda, deltamacd), 1)array_set_divs(calculate_divs(calcrsi, rsi), 2)array_set_divs(calculate_divs(calcstoc, stk), 3)array_set_divs(calculate_divs(calccci, cci), 4)array_set_divs(calculate_divs(calcmom, moment), 5)array_set_divs(calculate_divs(calcobv, Obv), 6)array_set_divs(calculate_divs(calcvwmacd, vwmacd), 7)array_set_divs(calculate_divs(calccmf, cmf), 8)array_set_divs(calculate_divs(calcmfi, Mfi), 9)array_set_divs(calculate_divs(calcext, externalindi), 10)// check minimum number of divergence, if less than showlimit then delete all divergencetotal_div = 0for x = 0 to array.size(all_divergences) – 1total_div := total_div + round(sign(array.get(all_divergences, x)))if total_div < showlimitarray.fill(all_divergences, 0)// keep line in an arrayvar pos_div_lines = array.new_line(0)var neg_div_lines = array.new_line(0)var pos_div_labels = array.new_label(0)varneg_div_labels=array.new_label(0)// remove old lines and labels if showlast option is enableddelete_old_pos_div_lines()=>if array.size(pos_div_lines) > 0forj=0toarray.size(pos_div_lines)-1line.delete(array.get(pos_div_lines, j))array.clear(pos_div_lines)delete_old_neg_div_lines()=>if array.size(neg_div_lines) > 0forj=0toarray.size(neg_div_lines)-1line.delete(array.get(neg_div_lines, j))array.clear(neg_div_lines)delete_old_pos_div_labels()=>ifarray.size(pos_div_labels)>0forj=0toarray.size(pos_div_labels)-1label.delete(array.get(pos_div_labels, j))array.clear(pos_div_labels)delete_old_neg_div_labels()=>if array.size(neg_div_labels) > 0forj=0toarray.size(neg_div_labels)-1label.delete(array.get(neg_div_labels, j))array.clear(neg_div_labels)// delete last creted lines and labels until we met new PH/PVdelete_last_pos_div_lines_label(n)=>if n > 0 and array.size(pos_div_lines) >= nasz = array.size(pos_div_lines)for j = 1 to nline.delete(array.get(pos_div_lines, asz – j))array.pop(pos_div_lines)if array.size(pos_div_labels) > 0label.delete(array.get(pos_div_labels, array.size(pos_div_labels) – 1))array.pop(pos_div_labels)delete_last_neg_div_lines_label(n)=>if n > 0 and array.size(neg_div_lines) >= nasz = array.size(neg_div_lines)for j = 1 to nline.delete(array.get(neg_div_lines, asz – j))array.pop(neg_div_lines)if array.size(neg_div_labels) > 0label.delete(array.get(neg_div_labels, array.size(neg_div_labels) – 1))array.pop(neg_div_labels)// variables for Alertspos_reg_div_detected = falseneg_reg_div_detected = falsepos_hid_div_detected = falseneg_hid_div_detected = false// to remove lines/labels until we met new // PH/PLvar last_pos_div_lines = 0var last_neg_div_lines = 0varremove_last_pos_divs=falsevar remove_last_neg_divs = falseif plremove_last_pos_divs := falselast_pos_div_lines := 0if phremove_last_neg_divs := falselast_neg_div_lines := 0// draw divergences lines and labelsdivergence_text_top = “”divergence_text_bottom = “”distances = array.new_int(0)dnumdiv_top = 0dnumdiv_bottom = 0top_label_col = color.whitebottom_label_col = color.whiteold_pos_divs_can_be_removed = trueold_neg_divs_can_be_removed = truestartpoint = dontconfirm ? 0 : 1 // used for don’t confirm optionfor x = 0 to 10div_type = -1for y = 0 to 3if array.get(all_divergences, x * 4 + y) > 0 // any divergence?div_type := yif(y%2)==1dnumdiv_top := dnumdiv_top + 1top_label_col := array.get(div_colors, y)if (y % 2) == 0dnumdiv_bottom := dnumdiv_bottom + 1bottom_label_col := array.get(div_colors, y)if not array.includes(distances, array.get(all_divergences, x * 4 + y)) // line not exist ?array.push(distances, array.get(all_divergences, x * 4 + y))new_line=showlines?line.new(x1=bar_index-array.get(all_divergences,x*4+y),y1=(source==”Close”?close[array.get(all_divergences,x*4+y)]:(y%2)==0?low[array.get(all_divergences,x*4+y)]:high[array.get(all_divergences, x * 4 + y)]),x2 = bar_index – startpoint,y2=(source==”Close”?close[startpoint]:(y%2)==0?low[startpoint]:high[startpoint]),color = array.get(div_colors, y),style = y < 2 ? reg_div_l_style : hid_div_l_style,width = y < 2 ? reg_div_l_width : hid_div_l_width): naif (y % 2) == 0if old_pos_divs_can_be_removedold_pos_divs_can_be_removed := falseif not showlast and remove_last_pos_divsdelete_last_pos_div_lines_label(last_pos_div_lines)last_pos_div_lines := 0if showlastdelete_old_pos_div_lines()array.push(pos_div_lines, new_line)last_pos_div_lines := last_pos_div_lines + 1remove_last_pos_divs := trueif (y % 2) == 1if old_neg_divs_can_be_removedold_neg_divs_can_be_removed := falseif not showlast and remove_last_neg_divsdelete_last_neg_div_lines_label(last_neg_div_lines)last_neg_div_lines := 0if showlastdelete_old_neg_div_lines()array.push(neg_div_lines, new_line)last_neg_div_lines := last_neg_div_lines + 1remove_last_neg_divs := true// set variables for alertsif y == 0pos_reg_div_detected := trueif y == 1neg_reg_div_detected := trueif y == 2pos_hid_div_detected := trueif y == 3neg_hid_div_detected := true// get text for labelsif div_type >= 0divergence_text_top := divergence_text_top + ((div_type % 2) == 1 ? (showindis != “Don’t Show” ? array.get(indicators_name, x) + “\n” : “”) : “”)divergence_text_bottom := divergence_text_bottom + ((div_type % 2) == 0 ? (showindis != “Don’t Show” ? array.get(indicators_name, x) + “\n” : “”) : “”)// draw labelsif showindis != “Don’t Show” or shownumif shownum and dnumdiv_top > 0divergence_text_top := divergence_text_top + tostring(dnumdiv_top)if shownum and dnumdiv_bottom > 0divergence_text_bottom := divergence_text_bottom + tostring(dnumdiv_bottom)if divergence_text_top != “”if showlastdelete_old_neg_div_labels()array.push(neg_div_labels,label.new(x=bar_index,y=max(high,high[1]),text = divergence_text_top,color = top_label_col,textcolor = neg_div_text_col,style = label.style_label_down))if divergence_text_bottom != “”if showlastdelete_old_pos_div_labels()array.push(pos_div_labels,label.new(x=bar_index,y=min(low,low[1]),text = divergence_text_bottom,color=bottom_label_col,textcolor = pos_div_text_col,style = label.style_label_up))alertcondition(pos_reg_div_detected, title=’Positive Regular Divergence Detected’, message=’Positive Regular Divergence Detected’)alertcondition(neg_reg_div_detected, title=’Negative Regular Divergence Detected’, message=’Negative Regular Divergence Detected’)alertcondition(pos_hid_div_detected, title=’Positive Hidden Divergence Detected’, message=’Positive Hidden Divergence Detected’)alertcondition(neg_hid_div_detected, title=’Negative Hidden Divergence Detected’, message=’Negative Hidden Divergence Detected’)alertcondition(pos_reg_div_detected or pos_hid_div_detected, title=’Positive Divergence Detected’, message=’Positive Divergence Detected’)alertcondition(neg_reg_div_detected or neg_hid_div_detected, title=’Negative Divergence Detected’, message=’Negative Divergence Detected’) -
AuthorPosts
Viewing 9 posts - 1 through 9 (of 9 total)
Find exclusive trading pro-tools on
Similar topics: