Advertisement
Guest User

dassd

a guest
Jun 16th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.97 KB | None | 0 0
  1. //@version=4
  2. study(
  3. title="[RS]ZigZag Percent Reversal - LVL2",
  4. shorttitle="ZZ2",
  5. overlay=true
  6. )
  7.  
  8. // |--------------------------------------------------------------------------||
  9. // | ZigZag: ||
  10. // |--------------------------------------------------------------------------||
  11. // |{
  12. string percent_description = "Percent of last pivot price for zigzag reversal:"
  13. string zzrealpivot_description = "Show real zigzag pivots:"
  14.  
  15.  
  16. percent = input(defval=0.25, title=percent_description, minval=0.0, maxval=99.0) / 100
  17. bool show_real_pivots_zz = input(defval=true, title=zzrealpivot_description, type=input.bool)
  18. // ||-------------------------------------------------------------------------||
  19. // || zigzag function:
  20. // ||-------------------------------------------------------------------------||
  21. // |{
  22. f_zz(_percent)=>
  23.  
  24. // direction after last pivot
  25. var bool _is_direction_up = (na)
  26. // track highest price since last lower pivot
  27. var float _htrack = (na)
  28. // track lowest price since last higher pivot
  29. var float _ltrack = (na)
  30. // zigzag variable for ploting
  31. var float _pivot = (na)
  32. // range needed for reaching reversal threshold
  33. var float _reverse_range = 0.0
  34. // real pivot time
  35. var int _real_pivot_time = na
  36. // reverse line
  37. var float _reverse_line = 0.0
  38. if bar_index >= 1
  39. _reverse_range := nz(_pivot[1]) * _percent
  40. _is_direction_up := nz(_is_direction_up[1], true)
  41.  
  42. if _is_direction_up
  43. _htrack := max(max(high, high[1]), nz(_htrack[1], high))
  44. if change(_htrack) > 0 //or na(_htrack[1])
  45. _real_pivot_time := time
  46. _ltrack := (na)
  47. _reverse_line := (_htrack[1] - _reverse_range)
  48. if close < _reverse_line
  49. _pivot := _htrack
  50. _is_direction_up := false
  51.  
  52. if not _is_direction_up
  53. _ltrack := min(min(low, low[1]), nz(_ltrack[1], low))
  54. if change(_ltrack) < 0 //or na(_ltrack[1])
  55. _real_pivot_time := time
  56. _htrack := (na)
  57. _reverse_line := (_ltrack[1] + _reverse_range)
  58. if close > _reverse_line
  59. _pivot := _ltrack
  60. _is_direction_up := true
  61.  
  62. [_pivot, _is_direction_up, _reverse_line, _real_pivot_time]
  63.  
  64. // || |}---------------------------------------------------------------------<•
  65.  
  66. // ||-------------------------------------------------------------------------||
  67. // || zigzag data:
  68. // ||-------------------------------------------------------------------------||
  69. // |{
  70. [pivot, is_up, reverse, _rl_time] = f_zz(percent)
  71. zz_color = is_up ? color.orange : color.teal
  72. zz = change(pivot) != 0 ? pivot : na
  73. tt = show_real_pivots_zz ? _rl_time : time
  74. //
  75. //plot(zz, title="Z", color=zz_color, linewidth=1, transp=80)
  76. //plot(reverse, title="R", color=color.white, style=plot.style_circles, linewidth=4, transp=40, offset=1, show_last=1)
  77.  
  78. // ||-------------------------------------------------------------------------||
  79. // || Pseudo Array Generic code: ID: (__x_)
  80. // |{-------------------------------------------------------------------------<•
  81. // || variable initialization:
  82. var int __x_A00 = na
  83. var int __x_A01 = na
  84. var int __x_A02 = na
  85. var int __x_A03 = na
  86. var int __x_A04 = na
  87. var int __x_A05 = na
  88. var int __x_A06 = na
  89. var int __x_A07 = na
  90. var int __x_A08 = na
  91. var int __x_A09 = na
  92. var int __x_A10 = na
  93.  
  94. var int __x_ALEN = 10
  95. string __x_AMODE = na
  96. int __x_ASET = na
  97. // |{-------------------------------------------------------------------------<•
  98. // || Utility Functions: (ID:__x_)
  99. 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
  100. f__x_ASET(_value, _index, _length, _mode)=>
  101. int _return = na
  102. if _index > _length
  103. _return := na
  104. else
  105. if _index == 0
  106. _return := _value
  107. else
  108. if _mode == "push" or _mode == "PUSH" or _mode == "p" or _mode == "P"
  109. _return := f__x_AGET(_index-1)
  110. if _mode == "replace" or _mode == "REPLACE" or _mode == "r" or _mode == "R"
  111. _return := f__x_AGET(_index)
  112. _return
  113. // || |}---------------------------------------------------------------------<•
  114.  
  115. // || |}---------------------------------------------------------------------<•
  116. // ||-------------------------------------------------------------------------||
  117. // || Pseudo Array Generic code: ID: (__y_)
  118. // |{-------------------------------------------------------------------------<•
  119. // || variable initialization:
  120. var float __y_A00 = na
  121. var float __y_A01 = na
  122. var float __y_A02 = na
  123. var float __y_A03 = na
  124. var float __y_A04 = na
  125. var float __y_A05 = na
  126. var float __y_A06 = na
  127. var float __y_A07 = na
  128. var float __y_A08 = na
  129. var float __y_A09 = na
  130. var float __y_A10 = na
  131.  
  132. var int __y_ALEN = 10
  133. string __y_AMODE = na
  134. float __y_ASET = na
  135. // |{-------------------------------------------------------------------------<•
  136. // || Utility Functions: (ID:__y_)
  137. 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
  138. f__y_ASET(_value, _index, _length, _mode)=>
  139. float _return = na
  140. if _index > _length
  141. _return := na
  142. else
  143. if _index == 0
  144. _return := _value
  145. else
  146. if _mode == "push" or _mode == "PUSH" or _mode == "p" or _mode == "P"
  147. _return := f__y_AGET(_index-1)
  148. if _mode == "replace" or _mode == "REPLACE" or _mode == "r" or _mode == "R"
  149. _return := f__y_AGET(_index)
  150. _return
  151. // || |}---------------------------------------------------------------------<•
  152.  
  153. // || |}---------------------------------------------------------------------<•
  154.  
  155. // | Get Extremes:
  156. // float a_y = valuewhen(zz, zz, show_previous + 0)
  157. // int a_x = round(valuewhen(zz, tt, show_previous + 0))
  158. // float b_y = valuewhen(zz, zz, show_previous + 1)
  159. // int b_x = round(valuewhen(zz, tt, show_previous + 1))
  160.  
  161. f_y(_i)=>valuewhen(zz, zz, _i)
  162. f_x(_i)=>valuewhen(zz, tt, _i)
  163.  
  164. if zz
  165. if na(f__y_AGET(1))
  166. __x_AMODE := "push"
  167. __y_AMODE := "push"
  168. __x_ASET := tt
  169. __y_ASET := zz
  170. else
  171. //higher high
  172. if f_y(0) > f_y(1) and f_y(0) > f_y(2)
  173. if f__y_AGET(0) < f__y_AGET(1)
  174. __x_AMODE := "push"
  175. __y_AMODE := "push"
  176. __x_ASET := tt
  177. __y_ASET := zz
  178. else
  179. __x_AMODE := "replace"
  180. __y_AMODE := "replace"
  181. __x_ASET := tt
  182. __y_ASET := zz
  183. //higher low
  184. if f_y(0) < f_y(1) and f_y(0) < f_y(2)
  185. if f__y_AGET(0) > f__y_AGET(1)
  186. __x_AMODE := "push"
  187. __y_AMODE := "push"
  188. __x_ASET := tt
  189. __y_ASET := zz
  190. else
  191. __x_AMODE := "replace"
  192. __y_AMODE := "replace"
  193. __x_ASET := tt
  194. __y_ASET := zz
  195.  
  196. // |{-------------------------------------------------------------------------<•
  197. // || update array: (ID´: _x)
  198. // || • this must/should be right after event.
  199. // || • if theres more than one event per bar the code bellow must be
  200. // || repeated to update the array. its possible to change f___x_ASET()
  201. // || so that you can update multiple positions if they dont interact
  202. if not na(__x_ASET)
  203. __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)
  204. __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)
  205. __x_A00 := f__x_ASET(__x_ASET, 00, __x_ALEN, __x_AMODE)
  206. // || |}---------------------------------------------------------------------<•
  207. // |{-------------------------------------------------------------------------<•
  208. // || update array: (ID´: _x)
  209. // || • this must/should be right after event.
  210. // || • if theres more than one event per bar the code bellow must be
  211. // || repeated to update the array. its possible to change f___y_ASET()
  212. // || so that you can update multiple positions if they dont interact
  213. if not na(__y_ASET)
  214. __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)
  215. __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)
  216. __y_A00 := f__y_ASET(__y_ASET, 00, __y_ALEN, __y_AMODE)
  217. // || |}---------------------------------------------------------------------<•
  218.  
  219.  
  220. // plot(zz, color=color.aqua, linewidth=4)
  221. // plot(f__y_AGET(0), color=color.red, linewidth=4)
  222. // plot(f__y_AGET(1), color=color.blue, linewidth=4)
  223.  
  224. // f_draw_infopanel(_x, _y, _line, _text)=>
  225. // _rep_text = ""
  226. // for _l = 0 to _line
  227. // _rep_text := _rep_text + "\n"
  228. // _rep_text := _rep_text + _text
  229. // var label _la = na
  230. // label.delete(_la)
  231. // _la := label.new(
  232. // x=_x, y=_y,
  233. // text=_rep_text, xloc=xloc.bar_time, yloc=yloc.price,
  234. // color=color.black, style=label.style_labelup, textcolor=color.silver, size=size.large
  235. // )
  236.  
  237. // lapos_x = timenow + round(change(time)*100)
  238. // lapos_y = highest(20)
  239. // f_draw_infopanel(lapos_x, lapos_y, 00, "00 - " + tostring(f__y_AGET(00), "#.##") + " - " + tostring(f__x_AGET(00), "#.##"))
  240. // f_draw_infopanel(lapos_x, lapos_y, 01, "01 - " + tostring(f__y_AGET(01), "#.##") + " - " + tostring(f__x_AGET(01), "#.##"))
  241. // f_draw_infopanel(lapos_x, lapos_y, 02, "02 - " + tostring(f__y_AGET(02), "#.##") + " - " + tostring(f__x_AGET(02), "#.##"))
  242. // f_draw_infopanel(lapos_x, lapos_y, 03, "03 - " + tostring(f__y_AGET(03), "#.##") + " - " + tostring(f__x_AGET(03), "#.##"))
  243. // f_draw_infopanel(lapos_x, lapos_y, 04, "04 - " + tostring(f__y_AGET(04), "#.##") + " - " + tostring(f__x_AGET(04), "#.##"))
  244. // f_draw_infopanel(lapos_x, lapos_y, 05, "05 - " + tostring(f__y_AGET(05), "#.##") + " - " + tostring(f__x_AGET(05), "#.##"))
  245. // f_draw_infopanel(lapos_x, lapos_y, 06, "06 - " + tostring(f__y_AGET(06), "#.##") + " - " + tostring(f__x_AGET(06), "#.##"))
  246. // f_draw_infopanel(lapos_x, lapos_y, 07, "07 - " + tostring(f__y_AGET(07), "#.##") + " - " + tostring(f__x_AGET(07), "#.##"))
  247. // f_draw_infopanel(lapos_x, lapos_y, 08, "08 - " + tostring(f__y_AGET(08), "#.##") + " - " + tostring(f__x_AGET(08), "#.##"))
  248. // f_draw_infopanel(lapos_x, lapos_y, 09, "09 - " + tostring(f__y_AGET(09), "#.##") + " - " + tostring(f__x_AGET(09), "#.##"))
  249. // f_draw_infopanel(lapos_x, lapos_y, 10, "10 - " + tostring(f__y_AGET(10), "#.##") + " - " + tostring(f__x_AGET(10), "#.##"))
  250.  
  251.  
  252. // float price_difference = abs(extreme_d_price - extreme_c_price)
  253. // int time_difference = extreme_d_time - extreme_c_time
  254. // || |}---------------------------------------------------------------------<•
  255.  
  256. // ||-------------------------------------------------------------------------||
  257. // || Draw the zigzag (abcdefg) lines:
  258. // ||-------------------------------------------------------------------------||
  259. // |{
  260. color col1 = #6d8b0d90//color.teal
  261. color col2 = #bb704590//color.orange
  262. line_width = 5
  263. var line _li = na
  264. _li_color = f__y_AGET(0) > f__y_AGET(1) ? col1 : col2
  265. line.delete(_li)
  266. _li := line.new(
  267. x1=f__x_AGET(0), y1=f__y_AGET(0),
  268. x2=f__x_AGET(1), y2=f__y_AGET(1),
  269. xloc=xloc.bar_time, extend=extend.none, color=_li_color,
  270. style=line.style_solid, width=line_width
  271. )
  272.  
  273. var line _li1 = na
  274. _li_color1 = f__y_AGET(1) > f__y_AGET(2) ? col1 : col2
  275. line.delete(_li1)
  276. _li1 := line.new(
  277. x1=f__x_AGET(1), y1=f__y_AGET(1),
  278. x2=f__x_AGET(2), y2=f__y_AGET(2),
  279. xloc=xloc.bar_time, extend=extend.none, color=_li_color1,
  280. style=line.style_solid, width=line_width
  281. )
  282.  
  283. var line _li2 = na
  284. _li_color2 = f__y_AGET(2) > f__y_AGET(3) ? col1 : col2
  285. line.delete(_li2)
  286. _li2 := line.new(
  287. x1=f__x_AGET(2), y1=f__y_AGET(2),
  288. x2=f__x_AGET(3), y2=f__y_AGET(3),
  289. xloc=xloc.bar_time, extend=extend.none, color=_li_color2,
  290. style=line.style_solid, width=line_width
  291. )
  292.  
  293. var line _li3 = na
  294. _li_color3 = f__y_AGET(3) > f__y_AGET(4) ? col1 : col2
  295. line.delete(_li3)
  296. _li3 := line.new(
  297. x1=f__x_AGET(3), y1=f__y_AGET(3),
  298. x2=f__x_AGET(4), y2=f__y_AGET(4),
  299. xloc=xloc.bar_time, extend=extend.none, color=_li_color3,
  300. style=line.style_solid, width=line_width
  301. )
  302.  
  303. var line _li4 = na
  304. _li_color4 = f__y_AGET(4) > f__y_AGET(5) ? col1 : col2
  305. line.delete(_li4)
  306. _li4 := line.new(
  307. x1=f__x_AGET(4), y1=f__y_AGET(4),
  308. x2=f__x_AGET(5), y2=f__y_AGET(5),
  309. xloc=xloc.bar_time, extend=extend.none, color=_li_color4,
  310. style=line.style_solid, width=line_width
  311. )
  312.  
  313. var line _li5 = na
  314. _li_color5 = f__y_AGET(5) > f__y_AGET(6) ? col1 : col2
  315. line.delete(_li5)
  316. _li5 := line.new(
  317. x1=f__x_AGET(5), y1=f__y_AGET(5),
  318. x2=f__x_AGET(6), y2=f__y_AGET(6),
  319. xloc=xloc.bar_time, extend=extend.none, color=_li_color5,
  320. style=line.style_solid, width=line_width
  321. )
  322.  
  323. // ||-------------------------------------------------------------------------||
  324. // || Draw zigzag:
  325. // ||-------------------------------------------------------------------------||
  326. // |{
  327. // f_draw_zigzag_lines(_x1, _y1, _x2, _y2)=>
  328. // var line _li = na
  329. // _li_color = _y1 > _y2 ? color.teal : color.orange
  330. // line.delete(_li)
  331. // _li := line.new(
  332. // x1=f__x_AGET(0), y1=f__y_AGET(0),
  333. // x2=f__x_AGET(1), y2=f__y_AGET(1),
  334. // xloc=xloc.bar_time, extend=extend.none, color=_li_color,
  335. // style=line.style_solid, width=2
  336. // )
  337.  
  338.  
  339. // if show_zigzag_lines
  340. // for _i = 0 to 9
  341. //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))
  342. // || |}---------------------------------------------------------------------<•
  343.  
  344. // || |}---------------------------------------------------------------------<•
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement