Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- study(
- title="[RS]ZigZag Percent Reversal - LVL2",
- shorttitle="ZZ2",
- overlay=true
- )
- // |--------------------------------------------------------------------------||
- // | ZigZag: ||
- // |--------------------------------------------------------------------------||
- // |{
- string percent_description = "Percent of last pivot price for zigzag reversal:"
- string zzrealpivot_description = "Show real zigzag pivots:"
- percent = input(defval=0.25, title=percent_description, minval=0.0, maxval=99.0) / 100
- bool show_real_pivots_zz = input(defval=true, title=zzrealpivot_description, type=input.bool)
- // ||-------------------------------------------------------------------------||
- // || zigzag function:
- // ||-------------------------------------------------------------------------||
- // |{
- f_zz(_percent)=>
- // direction after last pivot
- var bool _is_direction_up = (na)
- // track highest price since last lower pivot
- var float _htrack = (na)
- // track lowest price since last higher pivot
- var float _ltrack = (na)
- // zigzag variable for ploting
- var float _pivot = (na)
- // range needed for reaching reversal threshold
- var float _reverse_range = 0.0
- // real pivot time
- var int _real_pivot_time = na
- // reverse line
- var float _reverse_line = 0.0
- if bar_index >= 1
- _reverse_range := nz(_pivot[1]) * _percent
- _is_direction_up := nz(_is_direction_up[1], true)
- if _is_direction_up
- _htrack := max(max(high, high[1]), nz(_htrack[1], high))
- if change(_htrack) > 0 //or na(_htrack[1])
- _real_pivot_time := time
- _ltrack := (na)
- _reverse_line := (_htrack[1] - _reverse_range)
- if close < _reverse_line
- _pivot := _htrack
- _is_direction_up := false
- if not _is_direction_up
- _ltrack := min(min(low, low[1]), nz(_ltrack[1], low))
- if change(_ltrack) < 0 //or na(_ltrack[1])
- _real_pivot_time := time
- _htrack := (na)
- _reverse_line := (_ltrack[1] + _reverse_range)
- if close > _reverse_line
- _pivot := _ltrack
- _is_direction_up := true
- [_pivot, _is_direction_up, _reverse_line, _real_pivot_time]
- // || |}---------------------------------------------------------------------<•
- // ||-------------------------------------------------------------------------||
- // || zigzag data:
- // ||-------------------------------------------------------------------------||
- // |{
- [pivot, is_up, reverse, _rl_time] = f_zz(percent)
- zz_color = is_up ? color.orange : color.teal
- zz = change(pivot) != 0 ? pivot : na
- tt = show_real_pivots_zz ? _rl_time : time
- //
- //plot(zz, title="Z", color=zz_color, linewidth=1, transp=80)
- //plot(reverse, title="R", color=color.white, style=plot.style_circles, linewidth=4, transp=40, offset=1, show_last=1)
- // ||-------------------------------------------------------------------------||
- // || Pseudo Array Generic code: ID: (__x_)
- // |{-------------------------------------------------------------------------<•
- // || variable initialization:
- var int __x_A00 = na
- var int __x_A01 = na
- var int __x_A02 = na
- var int __x_A03 = na
- var int __x_A04 = na
- var int __x_A05 = na
- var int __x_A06 = na
- var int __x_A07 = na
- var int __x_A08 = na
- var int __x_A09 = na
- var int __x_A10 = na
- var int __x_ALEN = 10
- string __x_AMODE = na
- int __x_ASET = na
- // |{-------------------------------------------------------------------------<•
- // || Utility Functions: (ID:__x_)
- f__x_AGET(_i)=>_i==0?__x_A00:_i==1?__x_A01:_i==2?__x_A02:_i==3?__x_A03:_i==4?__x_A04:_i==5?__x_A05:_i==6?__x_A06:_i==7?__x_A07:_i==8?__x_A08:_i==9?__x_A09:_i==10?__x_A10:na
- f__x_ASET(_value, _index, _length, _mode)=>
- int _return = na
- if _index > _length
- _return := na
- else
- if _index == 0
- _return := _value
- else
- if _mode == "push" or _mode == "PUSH" or _mode == "p" or _mode == "P"
- _return := f__x_AGET(_index-1)
- if _mode == "replace" or _mode == "REPLACE" or _mode == "r" or _mode == "R"
- _return := f__x_AGET(_index)
- _return
- // || |}---------------------------------------------------------------------<•
- // || |}---------------------------------------------------------------------<•
- // ||-------------------------------------------------------------------------||
- // || Pseudo Array Generic code: ID: (__y_)
- // |{-------------------------------------------------------------------------<•
- // || variable initialization:
- var float __y_A00 = na
- var float __y_A01 = na
- var float __y_A02 = na
- var float __y_A03 = na
- var float __y_A04 = na
- var float __y_A05 = na
- var float __y_A06 = na
- var float __y_A07 = na
- var float __y_A08 = na
- var float __y_A09 = na
- var float __y_A10 = na
- var int __y_ALEN = 10
- string __y_AMODE = na
- float __y_ASET = na
- // |{-------------------------------------------------------------------------<•
- // || Utility Functions: (ID:__y_)
- f__y_AGET(_i)=>_i==0?__y_A00:_i==1?__y_A01:_i==2?__y_A02:_i==3?__y_A03:_i==4?__y_A04:_i==5?__y_A05:_i==6?__y_A06:_i==7?__y_A07:_i==8?__y_A08:_i==9?__y_A09:_i==10?__y_A10:na
- f__y_ASET(_value, _index, _length, _mode)=>
- float _return = na
- if _index > _length
- _return := na
- else
- if _index == 0
- _return := _value
- else
- if _mode == "push" or _mode == "PUSH" or _mode == "p" or _mode == "P"
- _return := f__y_AGET(_index-1)
- if _mode == "replace" or _mode == "REPLACE" or _mode == "r" or _mode == "R"
- _return := f__y_AGET(_index)
- _return
- // || |}---------------------------------------------------------------------<•
- // || |}---------------------------------------------------------------------<•
- // | Get Extremes:
- // float a_y = valuewhen(zz, zz, show_previous + 0)
- // int a_x = round(valuewhen(zz, tt, show_previous + 0))
- // float b_y = valuewhen(zz, zz, show_previous + 1)
- // int b_x = round(valuewhen(zz, tt, show_previous + 1))
- f_y(_i)=>valuewhen(zz, zz, _i)
- f_x(_i)=>valuewhen(zz, tt, _i)
- if zz
- if na(f__y_AGET(1))
- __x_AMODE := "push"
- __y_AMODE := "push"
- __x_ASET := tt
- __y_ASET := zz
- else
- //higher high
- if f_y(0) > f_y(1) and f_y(0) > f_y(2)
- if f__y_AGET(0) < f__y_AGET(1)
- __x_AMODE := "push"
- __y_AMODE := "push"
- __x_ASET := tt
- __y_ASET := zz
- else
- __x_AMODE := "replace"
- __y_AMODE := "replace"
- __x_ASET := tt
- __y_ASET := zz
- //higher low
- if f_y(0) < f_y(1) and f_y(0) < f_y(2)
- if f__y_AGET(0) > f__y_AGET(1)
- __x_AMODE := "push"
- __y_AMODE := "push"
- __x_ASET := tt
- __y_ASET := zz
- else
- __x_AMODE := "replace"
- __y_AMODE := "replace"
- __x_ASET := tt
- __y_ASET := zz
- // |{-------------------------------------------------------------------------<•
- // || update array: (ID´: _x)
- // || • this must/should be right after event.
- // || • if theres more than one event per bar the code bellow must be
- // || repeated to update the array. its possible to change f___x_ASET()
- // || so that you can update multiple positions if they dont interact
- if not na(__x_ASET)
- __x_A10 := f__x_ASET(__x_ASET, 10, __x_ALEN, __x_AMODE), __x_A09 := f__x_ASET(__x_ASET, 09, __x_ALEN, __x_AMODE), __x_A08 := f__x_ASET(__x_ASET, 08, __x_ALEN, __x_AMODE), __x_A07 := f__x_ASET(__x_ASET, 07, __x_ALEN, __x_AMODE), __x_A06 := f__x_ASET(__x_ASET, 06, __x_ALEN, __x_AMODE)
- __x_A05 := f__x_ASET(__x_ASET, 05, __x_ALEN, __x_AMODE), __x_A04 := f__x_ASET(__x_ASET, 04, __x_ALEN, __x_AMODE), __x_A03 := f__x_ASET(__x_ASET, 03, __x_ALEN, __x_AMODE), __x_A02 := f__x_ASET(__x_ASET, 02, __x_ALEN, __x_AMODE), __x_A01 := f__x_ASET(__x_ASET, 01, __x_ALEN, __x_AMODE)
- __x_A00 := f__x_ASET(__x_ASET, 00, __x_ALEN, __x_AMODE)
- // || |}---------------------------------------------------------------------<•
- // |{-------------------------------------------------------------------------<•
- // || update array: (ID´: _x)
- // || • this must/should be right after event.
- // || • if theres more than one event per bar the code bellow must be
- // || repeated to update the array. its possible to change f___y_ASET()
- // || so that you can update multiple positions if they dont interact
- if not na(__y_ASET)
- __y_A10 := f__y_ASET(__y_ASET, 10, __y_ALEN, __y_AMODE), __y_A09 := f__y_ASET(__y_ASET, 09, __y_ALEN, __y_AMODE), __y_A08 := f__y_ASET(__y_ASET, 08, __y_ALEN, __y_AMODE), __y_A07 := f__y_ASET(__y_ASET, 07, __y_ALEN, __y_AMODE), __y_A06 := f__y_ASET(__y_ASET, 06, __y_ALEN, __y_AMODE)
- __y_A05 := f__y_ASET(__y_ASET, 05, __y_ALEN, __y_AMODE), __y_A04 := f__y_ASET(__y_ASET, 04, __y_ALEN, __y_AMODE), __y_A03 := f__y_ASET(__y_ASET, 03, __y_ALEN, __y_AMODE), __y_A02 := f__y_ASET(__y_ASET, 02, __y_ALEN, __y_AMODE), __y_A01 := f__y_ASET(__y_ASET, 01, __y_ALEN, __y_AMODE)
- __y_A00 := f__y_ASET(__y_ASET, 00, __y_ALEN, __y_AMODE)
- // || |}---------------------------------------------------------------------<•
- // plot(zz, color=color.aqua, linewidth=4)
- // plot(f__y_AGET(0), color=color.red, linewidth=4)
- // plot(f__y_AGET(1), color=color.blue, linewidth=4)
- // f_draw_infopanel(_x, _y, _line, _text)=>
- // _rep_text = ""
- // for _l = 0 to _line
- // _rep_text := _rep_text + "\n"
- // _rep_text := _rep_text + _text
- // var label _la = na
- // label.delete(_la)
- // _la := label.new(
- // x=_x, y=_y,
- // text=_rep_text, xloc=xloc.bar_time, yloc=yloc.price,
- // color=color.black, style=label.style_labelup, textcolor=color.silver, size=size.large
- // )
- // lapos_x = timenow + round(change(time)*100)
- // lapos_y = highest(20)
- // f_draw_infopanel(lapos_x, lapos_y, 00, "00 - " + tostring(f__y_AGET(00), "#.##") + " - " + tostring(f__x_AGET(00), "#.##"))
- // f_draw_infopanel(lapos_x, lapos_y, 01, "01 - " + tostring(f__y_AGET(01), "#.##") + " - " + tostring(f__x_AGET(01), "#.##"))
- // f_draw_infopanel(lapos_x, lapos_y, 02, "02 - " + tostring(f__y_AGET(02), "#.##") + " - " + tostring(f__x_AGET(02), "#.##"))
- // f_draw_infopanel(lapos_x, lapos_y, 03, "03 - " + tostring(f__y_AGET(03), "#.##") + " - " + tostring(f__x_AGET(03), "#.##"))
- // f_draw_infopanel(lapos_x, lapos_y, 04, "04 - " + tostring(f__y_AGET(04), "#.##") + " - " + tostring(f__x_AGET(04), "#.##"))
- // f_draw_infopanel(lapos_x, lapos_y, 05, "05 - " + tostring(f__y_AGET(05), "#.##") + " - " + tostring(f__x_AGET(05), "#.##"))
- // f_draw_infopanel(lapos_x, lapos_y, 06, "06 - " + tostring(f__y_AGET(06), "#.##") + " - " + tostring(f__x_AGET(06), "#.##"))
- // f_draw_infopanel(lapos_x, lapos_y, 07, "07 - " + tostring(f__y_AGET(07), "#.##") + " - " + tostring(f__x_AGET(07), "#.##"))
- // f_draw_infopanel(lapos_x, lapos_y, 08, "08 - " + tostring(f__y_AGET(08), "#.##") + " - " + tostring(f__x_AGET(08), "#.##"))
- // f_draw_infopanel(lapos_x, lapos_y, 09, "09 - " + tostring(f__y_AGET(09), "#.##") + " - " + tostring(f__x_AGET(09), "#.##"))
- // f_draw_infopanel(lapos_x, lapos_y, 10, "10 - " + tostring(f__y_AGET(10), "#.##") + " - " + tostring(f__x_AGET(10), "#.##"))
- // float price_difference = abs(extreme_d_price - extreme_c_price)
- // int time_difference = extreme_d_time - extreme_c_time
- // || |}---------------------------------------------------------------------<•
- // ||-------------------------------------------------------------------------||
- // || Draw the zigzag (abcdefg) lines:
- // ||-------------------------------------------------------------------------||
- // |{
- color col1 = #6d8b0d90//color.teal
- color col2 = #bb704590//color.orange
- line_width = 5
- var line _li = na
- _li_color = f__y_AGET(0) > f__y_AGET(1) ? col1 : col2
- line.delete(_li)
- _li := line.new(
- x1=f__x_AGET(0), y1=f__y_AGET(0),
- x2=f__x_AGET(1), y2=f__y_AGET(1),
- xloc=xloc.bar_time, extend=extend.none, color=_li_color,
- style=line.style_solid, width=line_width
- )
- var line _li1 = na
- _li_color1 = f__y_AGET(1) > f__y_AGET(2) ? col1 : col2
- line.delete(_li1)
- _li1 := line.new(
- x1=f__x_AGET(1), y1=f__y_AGET(1),
- x2=f__x_AGET(2), y2=f__y_AGET(2),
- xloc=xloc.bar_time, extend=extend.none, color=_li_color1,
- style=line.style_solid, width=line_width
- )
- var line _li2 = na
- _li_color2 = f__y_AGET(2) > f__y_AGET(3) ? col1 : col2
- line.delete(_li2)
- _li2 := line.new(
- x1=f__x_AGET(2), y1=f__y_AGET(2),
- x2=f__x_AGET(3), y2=f__y_AGET(3),
- xloc=xloc.bar_time, extend=extend.none, color=_li_color2,
- style=line.style_solid, width=line_width
- )
- var line _li3 = na
- _li_color3 = f__y_AGET(3) > f__y_AGET(4) ? col1 : col2
- line.delete(_li3)
- _li3 := line.new(
- x1=f__x_AGET(3), y1=f__y_AGET(3),
- x2=f__x_AGET(4), y2=f__y_AGET(4),
- xloc=xloc.bar_time, extend=extend.none, color=_li_color3,
- style=line.style_solid, width=line_width
- )
- var line _li4 = na
- _li_color4 = f__y_AGET(4) > f__y_AGET(5) ? col1 : col2
- line.delete(_li4)
- _li4 := line.new(
- x1=f__x_AGET(4), y1=f__y_AGET(4),
- x2=f__x_AGET(5), y2=f__y_AGET(5),
- xloc=xloc.bar_time, extend=extend.none, color=_li_color4,
- style=line.style_solid, width=line_width
- )
- var line _li5 = na
- _li_color5 = f__y_AGET(5) > f__y_AGET(6) ? col1 : col2
- line.delete(_li5)
- _li5 := line.new(
- x1=f__x_AGET(5), y1=f__y_AGET(5),
- x2=f__x_AGET(6), y2=f__y_AGET(6),
- xloc=xloc.bar_time, extend=extend.none, color=_li_color5,
- style=line.style_solid, width=line_width
- )
- // ||-------------------------------------------------------------------------||
- // || Draw zigzag:
- // ||-------------------------------------------------------------------------||
- // |{
- // f_draw_zigzag_lines(_x1, _y1, _x2, _y2)=>
- // var line _li = na
- // _li_color = _y1 > _y2 ? color.teal : color.orange
- // line.delete(_li)
- // _li := line.new(
- // x1=f__x_AGET(0), y1=f__y_AGET(0),
- // x2=f__x_AGET(1), y2=f__y_AGET(1),
- // xloc=xloc.bar_time, extend=extend.none, color=_li_color,
- // style=line.style_solid, width=2
- // )
- // if show_zigzag_lines
- // for _i = 0 to 9
- //f_draw_zigzag_lines(f__x_AGET(0), 0.0, 0, 0.0) //round(f__x_AGET(_i+0)), f__y_AGET(_i+0), round(f__x_AGET(_i+1)), f__y_AGET(_i+1))
- // || |}---------------------------------------------------------------------<•
- // || |}---------------------------------------------------------------------<•
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement