zigzag trading


SUBMITTED BY: kuddel80

DATE: April 21, 2020, 3:24 p.m.

FORMAT: Text only

SIZE: 2.8 kB

HITS: 613

  1. // Ping me for donations, Collaborations or Support
  2. study("ZigZag++", "ZigZag++ Lucem Anb", true, format.price)
  3. Depth = input(12, "Depth", input.integer, minval=1, step=1)
  4. Deviation = input(5, "Deviation", input.integer, minval=1, step=1)
  5. mtf_val = input("", "Resolution", input.resolution)
  6. line_thick = input(4, "Line Thickness", input.integer, minval=1, maxval=4)
  7. p = input("Lime", "Up Color", options=["Red", "Lime", "Orange", "Teal", "Yellow", "White", "Black"])
  8. q = input("Red", "Down Color", options=["Red", "Lime", "Orange", "Teal", "Yellow", "White", "Black"])
  9. display_a = input(true, "Display Background Direction Changes?")
  10. repaint = input(true, "Repaint Levels")
  11. var lastlow = 0.0
  12. var lasthigh = 0.0
  13. data(x) => security(syminfo.tickerid, mtf_val!="" ? mtf_val : timeframe.period, x)
  14. cc(x) => x=="Red"?color.red:x=="Lime"?color.lime:x=="Orange"?color.orange:x=="Teal"?
  15. color.teal:x=="Yellow"?color.yellow:x=="Black"?color.black:color.white
  16. col(c) => c>0 ? (cc(p)) : c<0 ? (cc(q)) : na
  17. getLow(x, y, z, a) =>
  18. ll = y
  19. v = data(x)
  20. m = v==ll or data(z) - v > a*syminfo.mintick
  21. if v!=ll
  22. ll := v
  23. if m
  24. v := 0.0
  25. [v,ll]
  26. getHigh(x, y, z, a) =>
  27. lh = y
  28. v = data(x)
  29. m = v==lh or v - data(z) > a*syminfo.mintick
  30. if v!=lh
  31. lh := v
  32. if m
  33. v := 0.0
  34. [v,lh]
  35. [v,e] = getLow(lowest(Depth), lastlow, low, Deviation)
  36. lastlow := e
  37. zBB = v != 0.0
  38. [v1,e1] = getHigh(highest(Depth), lasthigh, high, Deviation)
  39. lasthigh := e1
  40. zSS = v1 != 0.0
  41. d = 0, zigzagHigh = 0, zigzagLow = 0
  42. d := zBB ? 1 : zSS ? -1 : nz(d[1], 0)
  43. virtualLow = zigzagLow[1] + 1
  44. if not zBB or (zBB and d == d[1] and low > low[virtualLow])
  45. zigzagLow := nz(zigzagLow[1]) + 1
  46. virtualHigh = zigzagHigh[1] + 1
  47. if not zSS or (zSS and d == d[1] and high < high[virtualHigh])
  48. zigzagHigh := nz(zigzagHigh[1]) + 1
  49. direction_changed = d!=d[1]
  50. x1 = bar_index-zigzagLow
  51. x2 = bar_index-zigzagHigh
  52. y1 = low[zigzagLow]
  53. y2 = high[zigzagHigh]
  54. if not repaint and d!=d[1]
  55. line zigzag = line.new(x1[1], y1[1], x2[1], y2[1], color=col(d[1]*-1), style=line.style_solid, width=line_thick)
  56. if repaint
  57. line zigzag = line.new(x1, y1, x2, y2, color=col(d*-1), style=line.style_solid, width=line_thick)
  58. if (d == d[1])
  59. line.delete(zigzag[1])
  60. bgcolor(display_a ? col(-d) : na, editable=false, title="Direction Changes")
  61. alertcondition(direction_changed, "Direction Changed", 'Zigzag on {{ticker}} direction changed at {{time}}')
  62. alertcondition(direction_changed and d<0, "Bullish Direction", 'Zigzag on {{ticker}} bullish direction at {{time}}')
  63. alertcondition(direction_changed and d>0, "Bearish Direction", 'Zigzag on {{ticker}} bearish direction at {{time}}')

comments powered by Disqus