Guest User

Untitled

a guest
Aug 10th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.56 KB | None | 0 0
  1. //@version=3
  2. study(title="Support Resistance Fibonacci Levels",shorttitle="SupResFib",overlay=true)
  3.  
  4. use_monthly = input(true,"Use Monthly Basis?")
  5. months_in = input(1,"# Months for Monthly Basis (this is limited by max lookback at your resolution!)",minval=1)
  6. use_weekly = input(true,"Use Weekly Basis?")
  7. weeks_in = input(2,"# Weeks for Weekly Basis",minval=1)
  8. use_daily = input(true,"Use Daily Basis?")
  9. days_in = input(7,"# Days for Daily Basis",minval=1)
  10. use_hourly = input(false,"Use Hourly Basis? (only works on intraday charts)")
  11. hours_in = input(84,"# Hours for Hourly Basis",minval=1)
  12. use_minutes = input(false,"Use per-Minute Basis? (only works on intraday charts)")
  13. minutes_in = input(2520,"# Minutes for per-Minute Basis",minval=1)
  14. hold_intraday = input(true,"Keep Lines Straight All Day? (holds morning value until next day)")
  15. connect_lines = input(false,"Connect Sup/Res Crosses with a Line?")
  16. zigzag_depth = input(50,"Zig Zag Depth",minval=4)
  17. zigzag_filter = input(0.0,"Zig Zag Reversal Sensitivity, %",minval=-99,maxval=99)
  18.  
  19. zigzag(_price,_high,_low,_depth,_filter) =>
  20. _offset = max(floor(_depth/2),2)
  21. _deviation = highest(stdev(_price[_offset],50)/nz(_price[_offset],1),100)*(1-_filter/100)
  22. _window = na
  23. _window := nz(_window[1])+1
  24. _long_enough = _window>=_depth
  25. _top = _high[_offset-1]<nz(_high[_offset])
  26. _bottom = _low[_offset-1]>nz(_low[_offset])
  27. _zz_candidate = (_top or _bottom) and _long_enough
  28. _last_extreme = _high[_offset]
  29. _last_extreme := nz(_last_extreme[1])
  30. if _top and _bottom
  31. if abs(_last_extreme-_high[_offset])>abs(_last_extreme-_low[_offset])
  32. _bottom := false
  33. else
  34. _top := false
  35. _this_extreme = _top?_high[_offset]:(_bottom?_low[_offset]:na)
  36. _zz_found = (_zz_candidate and _long_enough and (abs(_this_extreme-_last_extreme)/nz(_price[_offset],1))>_deviation)
  37. or _high[_offset] >= highest(_high,_depth)
  38. or _low[_offset] <= lowest(_low,_depth)
  39. if _zz_found
  40. _last_extreme := _this_extreme
  41. _window := 0
  42. _zigzag = _zz_found?_this_extreme:na
  43.  
  44.  
  45. zz = zigzag(hl2,high,low,zigzag_depth,zigzag_filter)
  46. plot(zz,color=blue,linewidth=2,offset=-max(floor(zigzag_depth/2),2))
  47.  
  48. fib_source = input(title="Draw Fibonacci Levels For Which Basis Pair?",defval="Hourly",options=["None","Monthly","Weekly","Daily","Hourly","Minutes"])
  49. fib_invert = input(false,"Invert Fibonacci Levels? (i.e. draw from max to min)")
  50.  
  51.  
  52. daysinweek = input(5,"# Days in a Week of Trading (change to 7 for crypto)")
  53.  
  54. months = months_in%12
  55. year_months = floor(months_in/12)*12 //*12 added after commenting out below
  56. //it really bugs me that highest() and lowest() cannot accept series inputs...
  57. // monthly_bar = (month>(months+1))?(month-months_in):(month+12-months_in)
  58. // yearly_bar = year - year_months
  59. // monthly_high = highest(high,barssince(month==monthly_bar and year==yearly_bar))
  60. // monthly_low = lowest(low,barssince(month==monthly_bar and year==yearly_bar))
  61. monthly_bars = (ismonthly?months_in:((isintraday?1440:1)*(year_months*52*daysinweek+months*4*daysinweek)/(isweekly?daysinweek:1)))/interval
  62. monthly_high = highest(high,min(5000,monthly_bars))
  63. monthly_low = lowest(low,min(5000,monthly_bars))
  64. //plot(n)
  65. weeks = weeks_in%52
  66. year_weeks = floor(weeks_in/52)*daysinweek
  67. weekly_bars = (isweekly?weeks_in:((isintraday?1440:1)*(year_weeks*52*daysinweek+weeks*daysinweek)/(ismonthly?4*daysinweek:1)))/interval
  68. weekly_high = highest(high,min(5000,weekly_bars))
  69. weekly_low = lowest(low,min(5000,weekly_bars))
  70.  
  71. days = ismonthly?max(days_in,4*daysinweek):isweekly?max(days_in,daysinweek):days_in
  72. daily_bars = (isdaily?days:((isintraday?1440:1)*days/((ismonthly?4*daysinweek:1)*(isweekly?daysinweek:1))))/interval
  73. daily_high = highest(high,min(5000,daily_bars))
  74. daily_low = lowest(low,min(5000,daily_bars))
  75.  
  76. //# hours per day varies widely...restrict these next two to intraday.
  77. hours = isdwm?na:hours_in
  78. hourly_bars = max(hours*60/interval,1)
  79. hourly_high = highest(high,min(5000,isdwm?24:hourly_bars))
  80. hourly_low = lowest(low,min(5000,isdwm?24:hourly_bars))
  81.  
  82. minutes = isdwm?na:minutes_in
  83. minute_bars = max(minutes/interval,1)
  84. minute_high = highest(high,min(5000,isdwm?1440:minute_bars))
  85. minute_low = lowest(low,min(5000,isdwm?1440:minute_bars))
  86.  
  87. flat_monthly_high = high
  88. flat_monthly_low = low
  89. flat_weekly_high = high
  90. flat_weekly_low = low
  91. flat_daily_high = high
  92. flat_daily_low = low
  93.  
  94. flat_monthly_high := change(dayofmonth)?monthly_high:na(monthly_high)?na:nz(flat_monthly_high[1],na(monthly_high[1])?monthly_high:high)
  95. flat_monthly_low := change(dayofmonth)?monthly_low:na(monthly_low)?na:nz(flat_monthly_low[1],na(monthly_low[1])?monthly_low:low)
  96. flat_weekly_high := change(dayofmonth)?weekly_high:na(weekly_high)?na:nz(flat_weekly_high[1],na(weekly_high[1])?weekly_high:high)
  97. flat_weekly_low := change(dayofmonth)?weekly_low:na(weekly_low)?na:nz(flat_weekly_low[1],na(weekly_low[1])?weekly_low:low)
  98. flat_daily_high := change(dayofmonth)?daily_high:na(daily_high)?na:nz(flat_daily_high[1],na(daily_high[1])?daily_high:high)
  99. flat_daily_low := change(dayofmonth)?daily_low:na(daily_low)?na:nz(flat_daily_low[1],na(daily_low[1])?daily_low:low)
  100.  
  101. plot_monthly_high = hold_intraday?flat_monthly_high:monthly_high
  102. plot_monthly_low = hold_intraday?flat_monthly_low:monthly_low
  103. plot_weekly_high = hold_intraday?flat_weekly_high:weekly_high
  104. plot_weekly_low = hold_intraday?flat_weekly_low:weekly_low
  105. plot_daily_high = hold_intraday?flat_daily_high:daily_high
  106. plot_daily_low = hold_intraday?flat_daily_low:daily_low
  107.  
  108. fhigh = na
  109. flow = na
  110.  
  111. if fib_source!= "None"
  112. fhigh := fib_source=="Monthly"?plot_monthly_high:
  113. fib_source=="Weekly"?plot_weekly_high:
  114. fib_source=="Daily"?plot_daily_high:
  115. fib_source=="Hourly"?hourly_high:
  116. fib_source=="Minutes"?minute_high:
  117. na
  118. flow := fib_source=="Monthly"?plot_monthly_low:
  119. fib_source=="Weekly"?plot_weekly_low:
  120. fib_source=="Daily"?plot_daily_low:
  121. fib_source=="Hourly"?hourly_low:
  122. fib_source=="Minutes"?minute_low:
  123. na
  124.  
  125. calc_fib(_max,_min,_dir) =>
  126. _diff = _max-_min
  127. _236 = _diff*0.236*(_dir?-1:1)+(_dir?_max:_min)
  128. _382 = _diff*0.382*(_dir?-1:1)+(_dir?_max:_min)
  129. _500 = _diff*0.5*(_dir?-1:1)+(_dir?_max:_min)
  130. _618 = _diff*0.618*(_dir?-1:1)+(_dir?_max:_min)
  131. _786 = _diff*0.786*(_dir?-1:1)+(_dir?_max:_min)
  132. _1618 = _diff*1.618*(_dir?-1:1)+(_dir?_max:_min)
  133. [_236,_382,_500,_618,_786,_1618]
  134.  
  135. [F236,F382,F500,F618,F786,F1618] = calc_fib(fhigh,flow,fib_invert)
  136. p236 = plot(F236,color=red)
  137. p382 = plot(F382,color=yellow)
  138. p500 = plot(F500,color=lime)
  139. p618 = plot(F618,color=aqua)
  140. p786 = plot(F786,color=teal)
  141. p1618 = plot(F1618,color=blue)
  142.  
  143. color_monthly_high = use_monthly?gray:na
  144. color_monthly_low = use_monthly?gray:na
  145. pMh = plot(use_monthly?plot_monthly_high:high,style=cross,color=color_monthly_high,linewidth=4,transp=65,join=connect_lines) //change plotted values for better autoscaling
  146. pMl = plot(use_monthly?plot_monthly_low:low,style=cross,color=color_monthly_low,linewidth=4,transp=80,join=connect_lines)
  147.  
  148. color_weekly_high = use_weekly?red:na
  149. color_weekly_low = use_weekly?red:na
  150. pwh = plot(use_weekly?plot_weekly_high:high,style=circles,color=color_weekly_high,linewidth=3,transp=65,join=connect_lines)
  151. pwl = plot(use_weekly?plot_weekly_low:low,style=circles,color=color_weekly_low,linewidth=3,transp=80,join=connect_lines)
  152.  
  153. color_daily_high = use_daily?blue:na
  154. color_daily_low = use_daily?blue:na
  155. pdh = plot(use_daily?plot_daily_high:high,style=cross,color=color_daily_high,linewidth=3,transp=65,join=connect_lines)
  156. pdl = plot(use_daily?plot_daily_low:low,style=cross,color=color_daily_low,linewidth=3,transp=80,join=connect_lines)
  157.  
  158. color_hourly_high = (isintraday and use_hourly)?green:na
  159. color_hourly_low = (isintraday and use_hourly)?green:na
  160. phh = plot((isintraday and use_hourly)?hourly_high:high,style=circles,color=color_hourly_high,linewidth=2,transp=65,join=connect_lines)
  161. phl = plot((isintraday and use_hourly)?hourly_low:low,style=circles,color=color_hourly_low,linewidth=2,transp=80,join=connect_lines)
  162.  
  163. color_minute_high = (isintraday and use_minutes)?black:na
  164. color_minute_low = (isintraday and use_minutes)?black:na
  165. pmh = plot((isintraday and use_minutes)?minute_high:high,style=cross,color=color_minute_high,linewidth=2,transp=65,join=connect_lines)
  166. pml = plot((isintraday and use_minutes)?minute_low:low,style=cross,color=color_minute_low,linewidth=2,transp=80,join=connect_lines)
  167.  
  168. // pzh=plot(high,color=na)
  169. // pzl=plot(low,color=na)
  170. fphigh=fib_invert?flow:fhigh
  171. fplow=fib_invert?fhigh:flow
  172. phigh = plot(fphigh,color=na)
  173. plow = plot(fplow,color=na)
  174.  
  175. fill(plow,p236,red)
  176. fill(p236,p382,yellow)
  177. fill(p382,p500,lime)
  178. fill(p500,p618,aqua)
  179. fill(p618,p786,teal)
  180. fill(p786,phigh,gray)
  181. fill(phigh,p1618,blue)
Add Comment
Please, Sign In to add comment