Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // Β© Greg_007
- //@version=5
- indicator(title="Fibonacci compression" , shorttitle="Fib compression", overlay=true)
- var length = input.int(300, title="How many bars to look back?")
- min_length = 50
- bool searchBull = input.bool(true, title="Search for bullish structures, uncheck for bearish")
- float fib1 = input.float(defval=0.786, title="Minumum fib level for 2nd leg", options=[0.786,0.618])
- float fib2 = input.float(0.618, title="Minumum fib level for 3rd leg")
- float fib3 = input.float(0.618, title="Minumum fib level for 4th leg")
- float fib4 = input.float(defval=0.5, title="Minumum fib level for 5th leg", options=[0.5,0.382,0.236])
- float high1 = 0.0
- float high2 = 0.0
- float high3 = 0.0
- float low1 = 0.0
- float low2 = 0.0
- float low3 = 0.0
- arr_all_highs = array.new_float(length, 0) //will contain the highs
- arr_all_lows = array.new_float(length, 0) //will contain the lows
- arr_cumul_vol = array.new_float(length, 0) //will contain the cumulative volume
- //temporary arrays
- b = array.new_float()
- c = array.new_float()
- d = array.new_float()
- e = array.new_float()
- f = array.new_float()
- int positionH1 = 0
- int positionH2 = 0
- int positionH3 = 0
- int positionL1 = 0
- int positionL2 = 0
- int positionL3 = 0
- var cumVol = 0.0
- float cumVolH1 = 0.0
- float cumVolH2 = 0.0
- float cumVolH3 = 0.0
- float cumVolL1 = 0.0
- float cumVolL2 = 0.0
- float cumVolL3 = 0.0
- string volDivergence = ""
- label l_H1 = na
- label l_H2 = na
- label l_H3 = na
- label l_L1 = na
- label l_L2 = na
- label l_L3 = na
- label l_info = na
- //FUNCTIONS
- check_volume(volH1, volH2, volH3, volL1, volL2, volL3) =>
- txt = ""
- if volH3 > volH2 and volH2 > volH1
- txt := "Strong Volume uptrend\nin favour of a long"
- else
- if volH2 > volH1
- txt := "Volume in favour\nof a long"
- else
- if volL3 < volL2 and volL2 < volL1
- txt := "Strong Volume downtrend\nin favour of a short"
- else
- if volL2 < volL1
- txt := "Volume in favour\nof a short"
- drawPivotLines(H, posH, L, posL) =>
- pivotLine = line.new(bar_index - length + posH + 1, H, bar_index - length + posL + 1, L, color = color.purple)
- line.delete(pivotLine[1])
- calcDrawTPlevels(A, B, C) =>
- TP1 = math.round_to_mintick(A + ((B - C) * 0.236))
- TP2 = math.round_to_mintick(A + ((B - C) * 0.618))
- TP3 = math.round_to_mintick(A + ((B - C) * 1))
- TP1_line = line.new(bar_index,TP1, bar_index + 100, TP1, color=#00aeff, width=2, style=line.style_dashed)
- TP2_line = line.new(bar_index,TP2, bar_index + 100, TP2, color=#00ff15, width=2, style=line.style_dashed)
- TP3_line = line.new(bar_index,TP3, bar_index + 100, TP3, color=#00ff15, width=2, style=line.style_dashed)
- line.delete(TP1_line[1])
- line.delete(TP2_line[1])
- line.delete(TP3_line[1])
- TP1Label = label.new(bar_index + 101 , TP1, text = "TP1 - "+ str.tostring(TP1), color=#00aeff, style=label.style_label_left)
- label.delete(TP1Label[1])
- TP2Label = label.new(bar_index + 101 , TP2, text = "TP2 - "+ str.tostring(TP2), color=#00ff15, style=label.style_label_left)
- label.delete(TP2Label[1])
- TP3Label = label.new(bar_index + 101 , TP3, text = "TP3 - "+ str.tostring(TP3), color=#00ff15, style=label.style_label_left)
- label.delete(TP3Label[1])
- [TP1, TP2, TP3]
- // END FUNCTIONS
- if close > open
- cumVol += nz(volume)
- else
- cumVol -= nz(volume)
- if barstate.islast
- i = length - 1
- while i > -1
- array.set(arr_all_highs, length - 1 - i, high[i])
- array.set(arr_all_lows,length - 1 - i, low[i])
- array.set(arr_cumul_vol,length - 1 - i, cumVol[i])
- i -= 1
- if searchBull
- //Search High 1
- high1 := array.max(arr_all_highs)
- positionH1 := array.indexof(arr_all_highs, high1)
- cumVolH1 := array.get(arr_cumul_vol, positionH1)
- if positionH1 < length - min_length //Don't search if the high is too close to the current candle
- //Search Low 1
- b := array.slice(arr_all_lows, positionH1 + 1, array.size(arr_all_lows))
- len = array.size(b)
- low1 := array.min(b)
- positionL1 := length - array.size(b) + array.indexof(b, low1)
- cumVolL1 := array.get(arr_cumul_vol, positionL1)
- //Search High 2
- if positionL1 + 1 == length //avoid slicing index error when we already reached the current candle
- c := array.slice(arr_all_highs, positionL1, array.size(arr_all_highs))
- else
- c := array.slice(arr_all_highs, positionL1 + 1, array.size(arr_all_highs))
- high2 := array.max(c)
- positionH2 := length - array.size(c) + array.indexof(c, high2)
- cumVolH2 := array.get(arr_cumul_vol, positionH2)
- if high2 >= low1 + ((high1 - low1) * fib1)
- l_H1 := label.new(bar_index - length + positionH1 + 1,high1,"High", style = label.style_label_down, textcolor=color.yellow)
- l_L1 := label.new(bar_index - length + positionL1 + 1,low1,"Low", style = label.style_label_up, textcolor=color.yellow)
- l_H2 := label.new(bar_index - length + positionH2 + 1,high2,"Lower high", style = label.style_label_down, textcolor=color.yellow)
- drawPivotLines(high1, positionH1, low1, positionL1)
- drawPivotLines(high2, positionH2, low1, positionL1)
- //Search Low 2
- if positionH2 + 1 == length
- d := array.slice(arr_all_lows, positionH2, array.size(arr_all_lows))
- else
- d := array.slice(arr_all_lows, positionH2 + 1, array.size(arr_all_lows))
- low2 := array.min(d)
- positionL2 := length - array.size(d) + array.indexof(d, low2)
- cumVolL2 := array.get(arr_cumul_vol, positionL2)
- if low2 <= high2 - ((high2 - low1) * fib2)
- l_L2 := label.new(bar_index - length + positionL2 + 1,low2,"Higher low", style = label.style_label_up, textcolor=color.yellow)
- drawPivotLines(high2, positionH2, low2, positionL2)
- [TP1, TP2, TP3] = calcDrawTPlevels(high2, high1, low1)
- //Search High 3
- if positionL2 + 1 == length //avoid slicing index error when we already reached the current candle
- e := array.slice(arr_all_highs, positionL2, array.size(arr_all_highs))
- else
- e := array.slice(arr_all_highs, positionL2 + 1, array.size(arr_all_highs))
- high3 := array.max(e)
- positionH3 := length - array.size(e) + array.indexof(e, high3)
- cumVolH3 := array.get(arr_cumul_vol, positionH3)
- if high3 >= low2 + ((high2 - low2) * fib3)
- l_H3 := label.new(bar_index - length + positionH3 + 1,high3,"2nd lower high", style = label.style_label_down, textcolor=color.yellow)
- drawPivotLines(high3, positionH3, low2, positionL2)
- //Search Low 3
- if positionH3 + 1 == length //avoid slicing index error when we already reached the current candle
- f := array.slice(arr_all_lows, positionH3, array.size(arr_all_lows))
- else
- f := array.slice(arr_all_lows, positionH3 + 1, array.size(arr_all_lows))
- low3 := array.min(f)
- positionL3 := length - array.size(f) + array.indexof(f, low3)
- cumVolL3 := array.get(arr_cumul_vol, positionL3)
- if low3 <= high3 - ((high3 - low2) * fib4)
- l_L3 := label.new(bar_index - length + positionL3 + 1,low3,"2nd higher low", style = label.style_label_up, textcolor=color.yellow)
- drawPivotLines(high3, positionH3, low3, positionL3)
- stoploss = line.new(bar_index - length + positionL3 + 1,low3, bar_index + 100, low3, color=#ff0000, width=2)
- line.delete(stoploss[1])
- SLLabel = label.new(bar_index + 101 , low3, text = "Stop/Loss "+ str.tostring(low3), color=#ff0000, style=label.style_label_left)
- label.delete(SLLabel[1])
- lineL3to0618 = line.new(bar_index - length + positionL3 + 1,low3, bar_index + 50,TP2, color = color.green, style=line.style_arrow_right)
- line.delete(lineL3to0618[1])
- volDivergence := check_volume(cumVolH1, cumVolH2, cumVolH3, cumVolL1, cumVolL2, cumVolL3)
- l_info := label.new(bar_index + 10 ,low, "info: " + volDivergence, style = label.style_label_left, textcolor=color.yellow)
- label.delete(l_info[1])
- else
- //Search Low 1
- low1 := array.min(arr_all_lows)
- positionL1 := array.indexof(arr_all_lows, low1)
- cumVolL1 := array.get(arr_cumul_vol, positionL1)
- if positionL1 < length - min_length //Don't search if the low is too close to the current candle
- //Search High 1
- b := array.slice(arr_all_highs, positionL1 + 1, array.size(arr_all_highs))
- high1 := array.max(b)
- positionH1 := length - array.size(b) + array.indexof(b, high1)
- cumVolH1 := array.get(arr_cumul_vol, positionH1)
- //Search Low 2
- if positionH1 + 1 == length //avoid slicing index error when we already reached the current candle
- c := array.slice(arr_all_lows, positionH1, array.size(arr_all_lows))
- else
- c := array.slice(arr_all_lows, positionH1 + 1, array.size(arr_all_lows))
- low2 := array.min(c)
- positionL2 := length - array.size(c) + array.indexof(c, low2)
- cumVolL2 := array.get(arr_cumul_vol, positionL2)
- if low2 <= high1 + ((low1 - high1) * fib1)
- l_L1 := label.new(bar_index - length + positionL1 + 1,low1,"Low", style = label.style_label_up, textcolor=color.yellow)
- l_H1 := label.new(bar_index - length + positionH1 + 1,high1,"High", style = label.style_label_down, textcolor=color.yellow)
- l_L2 := label.new(bar_index - length + positionL2 + 1,low2,"Higher low", style = label.style_label_up, textcolor=color.yellow)
- drawPivotLines(high1, positionH1, low1, positionL1)
- drawPivotLines(high1, positionH1, low2, positionL2)
- //Search High 2
- if positionL2 + 1 == length //avoid slicing index error when we already reached the current candle
- d := array.slice(arr_all_highs, positionL2, array.size(arr_all_highs))
- else
- d := array.slice(arr_all_highs, positionL2 + 1, array.size(arr_all_highs))
- high2 := array.max(d)
- positionH2 := length - array.size(d) + array.indexof(d, high2)
- cumVolH2 := array.get(arr_cumul_vol, positionH2)
- if high2 >= low2 - ((low2 - high1) * fib2)
- l_H2 := label.new(bar_index - length + positionH2 + 1,high2,"Lower high", style = label.style_label_down, textcolor=color.yellow)
- drawPivotLines(high2, positionH2, low2, positionL2)
- [TP1, TP2, TP3] = calcDrawTPlevels(low2, low1, high1)
- //Search Low 3
- if positionH2 + 1 == length //avoid slicing index error when we already reached the current candle
- e := array.slice(arr_all_lows, positionH2, array.size(arr_all_lows))
- else
- e := array.slice(arr_all_lows, positionH2 + 1, array.size(arr_all_lows))
- low3 := array.min(e)
- positionL3 := length - array.size(e) + array.indexof(e, low3)
- cumVolL3 := array.get(arr_cumul_vol, positionL3)
- if low3 <= high2 + ((low2 - high2) * fib3)
- l_L3 := label.new(bar_index - length + positionL3 + 1,low3,"2nd higher low", style = label.style_label_up, textcolor=color.yellow)
- drawPivotLines(high2, positionH2, low3, positionL3)
- //Search High 3
- if positionL3 + 1 == length //avoid slicing index error when we already reached the current candle
- f := array.slice(arr_all_highs, positionL3, array.size(arr_all_highs))
- else
- f := array.slice(arr_all_highs, positionL3 + 1, array.size(arr_all_highs))
- high3 := array.max(f)
- positionH3 := length - array.size(f) + array.indexof(f, high3)
- cumVolH3 := array.get(arr_cumul_vol, positionH3)
- if high3 >= low3 - ((low3 - high2) * fib4)
- l_H3 := label.new(bar_index - length + positionH3 + 1,high3,"2nd lower high" + str.tostring(high3), style = label.style_label_down, textcolor=color.yellow)
- drawPivotLines(high3, positionH3, low3, positionL3)
- stoploss = line.new(bar_index - length + positionH3 + 1,high3, bar_index + 100, high3, color=#ff0000, width=2)
- line.delete(stoploss[1])
- SLLabel = label.new(bar_index + 101 , high3, text = "Stop/Loss "+ str.tostring(high3), color=#ff0000, style=label.style_label_left)
- label.delete(SLLabel[1])
- lineH3to0618 = line.new(bar_index - length + positionH3 + 1,high3, bar_index + 50,TP2, color = color.green, style=line.style_arrow_right)
- line.delete(lineH3to0618[1])
- volDivergence := check_volume(cumVolH1, cumVolH2, cumVolH3, cumVolL1, cumVolL2, cumVolL3)
- l_info := label.new(bar_index + 10 ,high,"info: " + volDivergence, style = label.style_label_left, textcolor=color.yellow)
- label.delete(l_info[1])
- //end of this part
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // Β© Greg_007
- //@version=5
- //indicator("Swing Failure Pattern", overlay=true)
- lookback = input.int(100, "Bars to look back")
- ignore = input.int(10, "Ignore last ... bars")
- char = input.string('π', "Character to show (only 1 is allowed)")
- char_color = input.color(color.blue, "Character Color")
- last_high = ta.highest(lookback)
- last_low = ta.lowest(lookback)
- ignored_high = ta.highest(ignore)
- ignored_low = ta.lowest(ignore)
- plotchar(high > last_high[1] and close < last_high[1] and last_high[1] != ignored_high[1] ? close : na, char=char, color = char_color, location=location.abovebar, size=size.small)
- plotchar(low < last_low[1] and close > last_low[1] and last_low[1] != ignored_low[1] ? close : na, char=char, color = char_color, location=location.belowbar, size=size.small)
- //end of thispart
- // SFP Inquisitor
- // v0.2a
- //
- // coded by Bogdan Vaida
- // Code for Swing High, Swing Low and Swing Failure Pattern.
- // Note that we're still in the alpha version, bugs may appear.
- // Note that the number you set in your Swing History variable
- // will also be the minimum delay you see until the apples appear.
- // This is because we're checking the forward "history" too.
- // The SFP will only check for these conditions:
- // - high above Swing History high and close below it
- // - low below Swing History high and close above it
- // In some cases you may see an apple before the SFP that "doesn't fit"
- // with the SFP conditions. That's because that apple was drawn later and
- // the SFP actually appeared because of the previous apple.
- // 20 candles later.
- // Legend:
- // π - swing high
- // π - swing low
- // π§Ί - candle where the last swing was driven from
- // π - swing failure pattern
- // ππ - hungry scenario: swing low but also a SFP compared to the last swing
- // TODO:
- // - show potential swing highs/lows (where current bar is < Swing History
- // - fix banana happening when a potential swing low exists but didn't have
- // enough forward candles
- //@version=5
- //indicator(title='πππ Swing Failure Pattern Inquisitor', shorttitle='SFP-I', overlay=true)
- swingHistory = input.int(20, title='Swing history:', minval=1)
- plotSwings = input(true, title='Plot swings:')
- plotFirstSFPOnly = input(true, title='Plot only first SFP candle:')
- var lastSwingHigh = float(na)
- var lastSwingLow = float(na)
- // Swing Lows and Swing Highs code
- isSwingHigh = false
- isSwingLow = false
- swingHigh = high[swingHistory]
- swingLow = low[swingHistory]
- range_1 = swingHistory * 2
- for i = 0 to range_1 by 1
- isSwingHigh := true
- if i < swingHistory
- if high[i] > swingHigh
- isSwingHigh := false
- break
- if i > swingHistory
- if high[i] >= swingHigh
- isSwingHigh := false
- break
- for i = 0 to range_1 by 1
- isSwingLow := true
- if i < swingHistory
- if low[i] < swingLow
- isSwingLow := false
- break
- if i > swingHistory
- if low[i] <= swingLow
- isSwingLow := false
- break
- // Swing Failure Pattern
- isSwingHighFailure = false
- isSwingLowFailure = false
- lastSwingHigh := isSwingHigh ? swingHigh : lastSwingHigh
- lastSwingLow := isSwingLow ? swingLow : lastSwingLow
- if lastSwingHigh < high and lastSwingHigh > close and ta.barssince(lastSwingHigh) <= swingHistory
- isSwingHighFailure := true
- if plotFirstSFPOnly
- lastSwingHigh := na
- lastSwingHigh
- if lastSwingLow > low and lastSwingLow < close and ta.barssince(lastSwingLow) <= swingHistory
- isSwingLowFailure := true
- if plotFirstSFPOnly
- lastSwingLow := na
- lastSwingLow
- // Debugging
- // barssince(high > swingHigh)
- plotchar(isSwingHighFailure, 'Swing High Failure', '', location.top)
- plotchar(isSwingLowFailure, 'Swing Low Failure', '', location.top)
- plotchar(lastSwingHigh, 'Last Swing High', '', location.top)
- plotchar(lastSwingLow, 'Last Swing Low', '', location.top)
- // Plotting
- plotchar(series=plotSwings ? isSwingHigh : na, char='π', location=location.abovebar, size=size.tiny, offset=-swingHistory)
- plotchar(series=plotSwings ? isSwingLow : na, char='π', location=location.belowbar, size=size.tiny, offset=-swingHistory)
- plotchar(isSwingHighFailure, char='π', location=location.abovebar, size=size.tiny)
- plotchar(isSwingLowFailure, char='π', location=location.belowbar, size=size.tiny)
- // plotchar(swingHigh, char="π§Ί", location=location.abovebar, size=size.tiny) // location where the swing highs were drawn from
- // plotchar(swingLow, char="π§Ί", location=location.belowbar, size=size.tiny) // location where the swing lows were drawn from
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement