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/
- // © Klytonug
- //@version=5
- indicator("Pivot Low & High reversals + Alerts", overlay=true, max_labels_count=500, max_lines_count=500, max_bars_back=500)
- pivot_length = input(70,'Pivot Length')
- Apply_Regz = input.bool(true,'Regular Pivots',inline='inline1')
- Apply_Missed = input.bool(true,'Missed Pivots',inline='inline2')
- PHColor = input.color(#ef5350,'High',inline='inline1')
- PLColor = input.color(#26a69a,'Low',inline='inline1')
- //---------------------------------------------------------------
- // Setup alert condition
- //---------------------------------------------------------------
- allow_alerts = input.bool(true, "Allow alerts")
- var alert_triggered_reg_high = false
- var alert_triggered_reg_low = false
- var alert_triggered_missed_low = false
- var alert_triggered_missed_high = false
- var current_signal = "none"
- text_color = input.color(color.white,'Text Label Color')
- //------------------------------------------------------------------------------
- var line structure_line = na
- var line inv_level = na
- var max = 0.,var min = 0.
- var max_xline1 = 0,var min_xline1 = 0
- var use_max = 0.,var use_max_xline1 = 0
- var use_min = 0.,var use_min_xline1 = 0
- var os = 0,var py1 = 0.,var px1 = 0
- //------------------------------------------------------------------------------
- n = bar_index
- pivot_high = ta.pivothigh(pivot_length,pivot_length)
- pivot_low = ta.pivotlow(pivot_length,pivot_length)
- max := math.max(high[pivot_length],max)
- min := math.min(low[pivot_length],min)
- use_max := math.max(high[pivot_length],use_max)
- use_min := math.min(low[pivot_length],use_min)
- if max > max[1]
- max_xline1 := n-pivot_length
- use_min := low[pivot_length]
- if min < min[1]
- min_xline1 := n-pivot_length
- use_max := high[pivot_length]
- if use_min < use_min[1]
- use_min_xline1 := n-pivot_length
- if use_max > use_max[1]
- use_max_xline1 := n-pivot_length
- //------------------------------------------------------------------------------
- line.set_x2(inv_level[1],n)
- //---------------------------------------------------------------------------
- // Calculate Possible Reversal + Sell Highs
- //---------------------------------------------------------------------------
- if pivot_low
- if Apply_Missed
- if os[1] == 0
- label.new(max_xline1,max,'Sell',textcolor=text_color,color=PHColor,style=label.style_label_down,size=size.normal,
- tooltip=str.tostring(max,'#.####'))
- structure_line := line.new(px1,py1,max_xline1,max,color=PLColor,style=line.style_solid)
- px1 := max_xline1,py1 := max
- line.set_x2(inv_level[1],px1)
- inv_level := line.new(px1,py1,px1,py1,color=color.new(PHColor,50),width=2)
- // Set the alert condition for missed pivot
- alert_triggered_missed_low := true
- if current_signal == "Buy" and allow_alerts
- alert("Sell")
- current_signal := "Sell"
- else if pivot_low > min
- label.new(use_max_xline1,use_max,'Sell',textcolor=text_color,color=PHColor,style=label.style_label_down,size=size.normal,
- tooltip=str.tostring(max,'#.####'))
- label.new(min_xline1,min,'Sell',textcolor=text_color,color=PLColor,style=label.style_label_up,size=size.normal,
- tooltip=str.tostring(min,'#.####'))
- structure_line := line.new(px1,py1,min_xline1,min,color=PHColor,style=line.style_solid)
- px1 := min_xline1,py1 := min
- line.set_x2(inv_level[1],px1)
- inv_level := line.new(px1,py1,px1,py1,color=color.new(PLColor,50),width=2)
- structure_line := line.new(px1,py1,use_max_xline1,use_max,color=PLColor,style=line.style_solid)
- px1 := use_max_xline1,py1 := use_max
- line.set_x2(inv_level,px1)
- inv_level := line.new(px1,py1,px1,py1,color=color.new(PHColor,50),width=2)
- // Set the alert condition for regular pivot low
- alert_triggered_reg_low := true
- if current_signal == "Buy" and allow_alerts
- alert("Sell")
- current_signal := "Sell"
- if Apply_Regz
- label.new(n-pivot_length,pivot_low,'Buy',textcolor=text_color,color=PLColor,style=label.style_label_up,size=size.normal,
- tooltip=str.tostring(pivot_low,'#.####'))
- structure_line := line.new(px1,py1,n-pivot_length,pivot_low,color=PHColor,style=pivot_low > min or os[1] == 0 ? line.style_solid : line.style_solid)
- // Set the alert condition for regular pivot high
- alert_triggered_reg_high := true
- if current_signal == "Sell" and allow_alerts
- alert("Buy")
- current_signal := "Buy"
- py1 := pivot_low,px1 := n-pivot_length,os := 0,max := pivot_low,min := pivot_low
- //------------------------------------------------------------------------------
- // Calculate Possible Reversal + Buy lows
- //------------------------------------------------------------------------------
- if pivot_high
- if Apply_Missed
- if os[1] == 1
- label.new(min_xline1,min,'Buy',textcolor=text_color,color=PLColor,style=label.style_label_up,size=size.normal,
- tooltip=str.tostring(min,'#.####'))
- structure_line := line.new(px1,py1,min_xline1,min,color=PHColor,style=line.style_solid)
- px1 := min_xline1,py1 := min
- line.set_x2(inv_level[1],px1)
- inv_level := line.new(px1,py1,px1,py1,color=color.new(PLColor,50),width=2)
- // Set the alert condition for missed pivot
- alert_triggered_missed_low := true
- if current_signal == "Sell" and allow_alerts
- alert("Buy")
- current_signal := "Buy"
- else if pivot_high < max
- label.new(max_xline1,max,'Buy',textcolor=text_color,color=PHColor,style=label.style_label_down,size=size.normal,
- tooltip=str.tostring(max,'#.####'))
- label.new(use_min_xline1,use_min,'Buy',textcolor=text_color,color=PLColor,style=label.style_label_up,size=size.normal,
- tooltip=str.tostring(min,'#.####'))
- structure_line := line.new(px1,py1,max_xline1,max,color=PLColor,style=line.style_solid)
- px1 := max_xline1,py1 := max
- line.set_x2(inv_level[1],px1)
- inv_level := line.new(px1,py1,px1,py1,color=color.new(PHColor,50),width=2)
- structure_line := line.new(px1,py1,use_min_xline1,use_min,color=PHColor,style=line.style_solid)
- px1 := use_min_xline1,py1 := use_min
- line.set_x2(inv_level,px1)
- inv_level := line.new(px1,py1,px1,py1,color=color.new(PLColor,50),width=2)
- // Set the alert condition for regular pivot low
- alert_triggered_reg_high := true
- if current_signal == "Sell" and allow_alerts
- alert("Buy")
- current_signal := "Buy"
- if Apply_Regz
- label.new(n-pivot_length,pivot_high,'Sell',textcolor=text_color,color=PHColor,style=label.style_label_down,size=size.normal,
- tooltip=str.tostring(pivot_high,'#.####'))
- structure_line := line.new(px1,py1,n-pivot_length,pivot_high,color=PLColor,style=pivot_high < max or os[1] == 1 ? line.style_solid : line.style_solid)
- // Set the alert condition for regular pivot high
- alert_triggered_reg_high := true
- if current_signal == "Buy" and allow_alerts
- alert("Sell")
- current_signal := "Sell"
- py1 := pivot_high,px1 := n-pivot_length,os := 1,max := pivot_high,min := pivot_high
- //------------------------------------------------------------------------------
- var label lblz = na
- if barstate.islast
- x = 0,y = 0.
- price_levels = array.new_float(0)
- price_levels_x = array.new_int(0)
- for i = 0 to n-px1-1
- array.push(price_levels,os==1?low[i]:high[i])
- array.push(price_levels_x,n-i)
- label.delete(lblz[1])
- if os == 1
- y := array.min(price_levels)
- x := array.get(price_levels_x,array.indexof(price_levels,y))
- if Apply_Missed
- lblz := label.new(x,y,'Buy',textcolor=text_color,color=PLColor,style=label.style_label_up,size=size.normal,
- tooltip=str.tostring(y,'#.####'))
- else
- y := array.max(price_levels)
- x := array.get(price_levels_x,array.indexof(price_levels,y))
- if Apply_Missed
- lblz := label.new(x,y,'Sell',textcolor=text_color,color=PHColor,style=label.style_label_down,size=size.normal,
- tooltip=str.tostring(y,'#.####'))
- if Apply_Missed
- line.delete(line.new(px1,py1,x,y,color=os == 1 ? PHColor : PLColor,style=line.style_solid)[1])
- line.delete(line.new(x,y,n,y,color = color.new(os == 1 ? PHColor : PLColor,50),width=2)[1])
- // Alert conditions
- alertcondition(alert_triggered_reg_high, title="Regular Pivot High", message="Regular pivot high alert triggered!")
- alertcondition(alert_triggered_reg_low, title="Regular Pivot Low", message="Regular pivot low alert triggered!")
- alertcondition(alert_triggered_missed_high, title="Missed High Pivot", message="Missed pivot alert triggered!")
- alertcondition(alert_triggered_missed_low, title="Missed Low Pivot", message="Missed pivot alert triggered!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement