xmd79

BUY-SELL ZONES

Nov 20th, 2021
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.65 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2.  
  3.  
  4.  
  5.  
  6. // WARNING THIS ARE A MIX OF INDICATORS AND I DO NOT OWN ANY OF IT.
  7. // THANKS TO: HEAVY91,LONSOMETHEBLUE,JULIANZACO
  8.  
  9.  
  10.  
  11.  
  12. //@version=4
  13. study("BUY-SELL ZONES", "BUY-SELL ZONES", overlay = true, max_bars_back = 501)
  14.  
  15. var bool is_first_candle= true
  16. var diff = 0.0
  17. float maxV = 0.0
  18. int maxV_index = 0
  19. int last_time = 0
  20. margin=0.0
  21. ys=0.0
  22. int bar_offset=0
  23. var is_first_inRange=false
  24.  
  25.  
  26.  
  27. period_time = input(defval = 10,
  28. title = "Number of days (Min: 3, Max: 500)",
  29. type = input.integer,
  30. minval = 3,
  31. maxval = 500)
  32.  
  33. resolution = input(defval = 500,
  34. title = "calculation resolution (Min: 400, Max: 700)",
  35. type = input.integer,
  36. minval = 400,
  37. maxval = 700)
  38.  
  39. show_from = input(defval = 3,
  40. title = "show support/resistance since (Min: 2, Max: 45)",
  41. type = input.integer,
  42. minval = 2,
  43. maxval = 45)
  44.  
  45.  
  46. show_volume_bars = input(defval = true,
  47. title = "Show volume bars (If selected, maximum days are 17)",
  48. type = input.bool
  49. )
  50.  
  51. show_vpoc_line = input(defval = true,
  52. title = "Show VPOC line",
  53. type = input.bool
  54. )
  55.  
  56. show_support_line = input(defval = true,
  57. title = "Show support line",
  58. type = input.bool
  59. )
  60.  
  61. show_res_line = input(defval = true,
  62. title = "Show resistance line",
  63. type = input.bool
  64. )
  65.  
  66. show_H_line = input(defval = true,
  67. title = "Show high line",
  68. type = input.bool
  69. )
  70.  
  71. show_L_line = input(defval = true,
  72. title = "Show low line",
  73. type = input.bool
  74. )
  75.  
  76. show_labels = input(defval = true,
  77. title = "Show from days label",
  78. type = input.bool
  79. )
  80.  
  81. vpoc_color = input(defval = color.new(color.yellow, 10),
  82. title = "VPOC color",
  83. type = input.color
  84. )
  85.  
  86. high_low_color = input(defval = color.new(color.blue, 50),
  87. title = "High-low lines color",
  88. type = input.color
  89. )
  90.  
  91. volume_bars_color = input(defval = color.new(color.green, 70),
  92. title = "Volume bars color",
  93. type = input.color
  94. )
  95.  
  96. sup_color = input(defval = color.new(color.red, 10),
  97. title = "Support line color",
  98. type = input.color
  99. )
  100.  
  101. res_color = input(defval = color.new(color.green, 10),
  102. title = "Resistance line color",
  103. type = input.color
  104. )
  105.  
  106.  
  107. range_prices = array.new_float((resolution + 1), 0.0)
  108. partial_vol = array.new_float(resolution, 0.0)
  109. total_vol = array.new_float(resolution, 0.0)
  110. var all_ys = array.new_float(show_from, 0.0)
  111. var ys_time = array.new_float(show_from, 0.0)
  112. var is_support = array.new_bool(show_from, true)
  113.  
  114. t = time("1440", session.regular)
  115. var int startTime =time
  116. int ncandles =0
  117.  
  118. if barstate.isfirst
  119. startTime := timenow - period_time*86400000
  120.  
  121.  
  122. bar_offset:= barssince(is_first_inRange)
  123. is_first = na(t[1]) and not na(t) or t[1] < t
  124. is_first_inRange:=is_first and t>startTime
  125. high_price=0.0
  126. low_price=0.0
  127. price_level=0.0
  128.  
  129. if is_first_inRange or barstate.islast
  130.  
  131. last_time:= t[1]
  132. ncandles := bar_offset
  133. current_low_price=0.0
  134. for i = 1 to (ncandles)
  135. current_low_price:=low[i]
  136. if current_low_price < low_price or low_price==0.0
  137. low_price:=current_low_price
  138.  
  139. current_high_price=0.0
  140. for i = 1 to (ncandles)
  141. current_high_price:=high[i]
  142. if current_high_price >high_price or high_price==0.0
  143. high_price:=current_high_price
  144. diff := (high_price-low_price)/resolution
  145.  
  146. for j = 0 to (resolution)
  147. array.set(range_prices, (j), (low_price + (diff * (j+1))))
  148.  
  149. for i = 0 to (ncandles)
  150. int w_candle = 0
  151. array.fill(partial_vol, 0.0)
  152.  
  153. for j=0 to (resolution-1)
  154. float current_price = array.get(range_prices, j)
  155. if high[i] > current_price and low[i] < current_price
  156. float j_partial_vol = array.get(partial_vol, j)
  157. float sum_vol = j_partial_vol + nz(volume[i])
  158. array.set(partial_vol, j, sum_vol)
  159. w_candle := w_candle + 1
  160.  
  161. for j = 0 to (resolution - 1)
  162. float j_total_vol = array.get(total_vol, j)
  163. float j_partial_vol = array.get(partial_vol, j)
  164. float sum_partial_total=0.0
  165. if w_candle > 0
  166. sum_partial_total := j_total_vol + j_partial_vol/w_candle
  167. else
  168. sum_partial_total := j_total_vol
  169. array.set(total_vol, j, sum_partial_total)
  170. maxV := array.max(total_vol)
  171. maxV_index := array.indexof(total_vol, maxV)
  172.  
  173. if is_first_inRange
  174. price_level:=diff*maxV_index+low_price
  175. if high_price - price_level > price_level - low_price
  176. margin:=high_price - price_level
  177. ys:=price_level - margin
  178. else
  179. margin:=price_level - low_price
  180. ys:=price_level + margin
  181. if ys > high_price
  182. array.shift(is_support)
  183. array.push(is_support, false)
  184. else
  185. array.shift(is_support)
  186. array.push(is_support, true)
  187. array.shift(all_ys)
  188. array.push(all_ys, ys)
  189. array.shift(ys_time)
  190. array.push(ys_time, t)
  191.  
  192. if barstate.islast
  193. for i=0 to (show_from - 1)
  194. ys := array.get(all_ys, i)
  195. time_back = array.get(ys_time, i)
  196. if array.get(is_support, i)
  197. if show_support_line
  198. line.new(x1=int(time_back), y1=ys, x2=time + 86400000, y2=ys, color=sup_color ,xloc= xloc.bar_time, extend = extend.none, width=2, style=line.style_dashed)
  199. else
  200. if show_res_line
  201. line.new(x1=int(time_back), y1=ys, x2=time + 86400000, y2=ys, color=res_color ,xloc= xloc.bar_time, extend = extend.none, width=2, style=line.style_dashed)
  202.  
  203. if show_labels
  204. label.new(int(time + 86400000), ys, text ="From days: " + tostring(show_from - i), xloc=xloc.bar_time)
  205.  
  206. dif_time=0.0
  207.  
  208.  
  209. if is_first_inRange or barstate.islast
  210.  
  211. last_time:= int(t[1])
  212. dif_time:= (time-last_time)
  213. if dif_time/86400000 > 1.5
  214. dif_time:=(dif_time)/9
  215. else
  216. dif_time:=(dif_time)/3
  217. diff_t=0
  218. x1=0.0
  219. norm_vol=0.0
  220. proportion=resolution/40
  221. for i = 0 to (resolution - 1)
  222. y=array.get(range_prices,i)
  223. current_vol= array.get(total_vol,i)
  224. norm_vol :=current_vol/maxV
  225. x1 :=dif_time*norm_vol
  226. diff_t := last_time + int(x1)
  227. if (i % int(proportion) == 0 or int(proportion) == 0) and show_volume_bars
  228. line.new(x1=t[1], y1=y, x2=diff_t, y2=y, color=volume_bars_color ,xloc= xloc.bar_time, extend = extend.none, width=2)
  229.  
  230. if show_L_line
  231. line.new(x1=t[1], y1=low_price, x2=time, y2=low_price,xloc= xloc.bar_time, color=high_low_color)
  232. if show_vpoc_line
  233. line.new(x1=t[1], y1=diff*maxV_index+low_price, x2=time, y2=diff*maxV_index+low_price, color=vpoc_color, width=2,xloc= xloc.bar_time)
  234. if show_H_line
  235. line.new(x1=t[1], y1=high_price, x2=time, y2=high_price,xloc= xloc.bar_time, color=high_low_color)
  236.  
  237.  
  238.  
  239. prd = input(defval = 10, title="Pivot Period", minval = 4, maxval = 30, group = "Settings", tooltip="Used while calculating Pivot Points, checks left&right bars")
  240. ppsrc = input(defval = 'High/Low', title="Source", options = ['High/Low', 'Close/Open'], group = "Settings ", tooltip="Source for Pivot Points")
  241. ChannelW = input(defval = 5, title = "Maximum Channel Width %", minval = 1, maxval = 8, group = "Settings ", tooltip="Calculated using Highest/Lowest levels in 300 bars")
  242. minstrength = input(defval = 1, title = "Minimum Strength", minval = 1, group = "Settings ", tooltip = "Channel must contain at least 2 Pivot Points")
  243. maxnumsr = input(defval = 6, title = "Maximum Number of S/R", minval = 1, maxval = 10, group = "Settings ", tooltip = "Maximum number of Support/Resistance Channels to Show") - 1
  244. loopback = input(defval = 290, title = "Loopback Period", minval = 100, maxval = 400, group = "Settings ", tooltip="While calculating S/R levels it checks Pivots in Loopback Period")
  245. res_col = input(defval = color.new(color.red, 75), title = "Resistance Color", group = "Colors")
  246. sup_col = input(defval = color.new(color.lime, 75), title = "Support Color", group = "Colors ")
  247. inch_col = input(defval = color.new(color.gray, 75), title = "Color When Price in Channel", group = "Colors")
  248. showpp = input(defval = false, title = "Show Pivot Points", group = "Extras ⏶⏷")
  249. showsrbroken = input(defval = false, title = "Show Broken Support/Resistance", group = "Extras ⏶⏷")
  250. showthema1en = input(defval = false, title = "MA 1", inline = "ma1")
  251. showthema1len = input(defval = 50, title = "", inline = "ma1")
  252. showthema1type = input(defval = "SMA", title = "", options = ["SMA", "EMA"], inline = "ma1")
  253. showthema2en = input(defval = false, title = "MA 2", inline = "ma2")
  254. showthema2len = input(defval = 200, title = "", inline = "ma2")
  255. showthema2type = input(defval = "SMA", title = "", options = ["SMA", "EMA"], inline = "ma2")
  256.  
  257. ma1 = showthema1en ? (showthema1type == "SMA" ? sma(close, showthema1len) : ema(close, showthema1len)) : na
  258. ma2 = showthema2en ? (showthema2type == "SMA" ? sma(close, showthema2len) : ema(close, showthema2len)) : na
  259.  
  260. plot(ma1, color = not na(ma1) ? color.blue : na)
  261. plot(ma2, color = not na(ma2) ? color.red : na)
  262.  
  263. // get Pivot High/low
  264. float src1 = ppsrc == 'High/Low' ? high : max(close, open)
  265. float src2 = ppsrc == 'High/Low' ? low: min(close, open)
  266. float ph = pivothigh(src1, prd, prd)
  267. float pl = pivotlow(src2, prd, prd)
  268.  
  269. // draw Pivot points
  270. plotshape(ph and showpp, text = "H", style = shape.labeldown, color = na, textcolor = color.red, location = location.abovebar, offset = -prd)
  271. plotshape(pl and showpp, text = "L", style = shape.labelup, color = na, textcolor = color.lime, location = location.belowbar, offset = -prd)
  272.  
  273. //calculate maximum S/R channel width
  274. prdhighest = highest(300)
  275. prdlowest = lowest(300)
  276. cwidth = (prdhighest - prdlowest) * ChannelW / 100
  277.  
  278. // get/keep Pivot levels
  279. var pivotvals= array.new_float(0)
  280. var pivotlocs= array.new_float(0)
  281. if ph or pl
  282. array.unshift(pivotvals, ph ? ph : pl)
  283. array.unshift(pivotlocs, bar_index)
  284. for x = array.size(pivotvals) - 1 to 0
  285. if bar_index - array.get(pivotlocs, x) > loopback // remove old pivot points
  286. array.pop(pivotvals)
  287. array.pop(pivotlocs)
  288. continue
  289. break
  290.  
  291. //find/create SR channel of a pivot point
  292. get_sr_vals(ind)=>
  293. float lo = array.get(pivotvals, ind)
  294. float hi = lo
  295. int numpp = 0
  296. for y = 0 to array.size(pivotvals) - 1
  297. float cpp = array.get(pivotvals, y)
  298. float wdth = cpp <= hi ? hi - cpp : cpp - lo
  299. if wdth <= cwidth // fits the max channel width?
  300. if cpp <= hi
  301. lo := min(lo, cpp)
  302. else
  303. hi := max(hi, cpp)
  304.  
  305. numpp := numpp + 20 // each pivot point added as 20
  306. [hi, lo, numpp]
  307.  
  308. // keep old SR channels and calculate/sort new channels if we met new pivot point
  309. var suportresistance = array.new_float(20, 0) // min/max levels
  310. changeit(x, y)=>
  311. tmp = array.get(suportresistance, y * 2)
  312. array.set(suportresistance, y * 2, array.get(suportresistance, x * 2))
  313. array.set(suportresistance, x * 2, tmp)
  314. tmp := array.get(suportresistance, y * 2 + 1)
  315. array.set(suportresistance, y * 2 + 1, array.get(suportresistance, x * 2 + 1))
  316. array.set(suportresistance, x * 2 + 1, tmp)
  317.  
  318. if ph or pl
  319. supres = array.new_float(0) // number of pivot, strength, min/max levels
  320. stren = array.new_float(10, 0)
  321. // get levels and strengs
  322. for x = 0 to array.size(pivotvals) - 1
  323. [hi, lo, strength] = get_sr_vals(x)
  324. array.push(supres, strength)
  325. array.push(supres, hi)
  326. array.push(supres, lo)
  327.  
  328. // add each HL to strengh
  329. for x = 0 to array.size(pivotvals) - 1
  330. h = array.get(supres, x * 3 + 1)
  331. l = array.get(supres, x * 3 + 2)
  332. s = 0
  333. for y = 0 to loopback
  334. if (high[y] <= h and high[y] >= l) or
  335. (low[y] <= h and low[y] >= l)
  336. s := s + 1
  337. array.set(supres, x * 3, array.get(supres, x * 3) + s)
  338.  
  339. //reset SR levels
  340. array.fill(suportresistance, 0)
  341. // get strongest SRs
  342. src = 0
  343. for x = 0 to array.size(pivotvals) - 1
  344. stv = -1. // value
  345. stl = -1 // location
  346. for y = 0 to array.size(pivotvals) - 1
  347. if array.get(supres, y * 3) > stv and array.get(supres, y * 3) >= minstrength * 20
  348. stv := array.get(supres, y * 3)
  349. stl := y
  350. if stl >= 0
  351. //get sr level
  352. hh = array.get(supres, stl * 3 + 1)
  353. ll = array.get(supres, stl * 3 + 2)
  354. array.set(suportresistance, src * 2, hh)
  355. array.set(suportresistance, src * 2 + 1, ll)
  356. array.set(stren, src, array.get(supres, stl * 3))
  357.  
  358. // make included pivot points' strength zero
  359. for y = 0 to array.size(pivotvals) - 1
  360. if (array.get(supres, y * 3 + 1) <= hh and array.get(supres, y * 3 + 1) >= ll) or
  361. (array.get(supres, y * 3 + 2) <= hh and array.get(supres, y * 3 + 2) >= ll)
  362. array.set(supres, y * 3, -1)
  363.  
  364. src += 1
  365. if src >= 10
  366. break
  367.  
  368. for x = 0 to 8
  369. for y = x + 1 to 9
  370. if array.get(stren, y) > array.get(stren, x)
  371. tmp = array.get(stren, y)
  372. array.set(stren, y, array.get(stren, x))
  373. changeit(x, y)
  374.  
  375.  
  376. get_level(ind)=>
  377. float ret = na
  378. if ind < array.size(suportresistance)
  379. if array.get(suportresistance, ind) != 0
  380. ret := array.get(suportresistance, ind)
  381. ret
  382.  
  383. get_color(ind)=>
  384. color ret = na
  385. if ind < array.size(suportresistance)
  386. if array.get(suportresistance, ind) != 0
  387. ret := array.get(suportresistance, ind) > close and array.get(suportresistance, ind + 1) > close ? res_col :
  388. array.get(suportresistance, ind) < close and array.get(suportresistance, ind + 1) < close ? sup_col :
  389. inch_col
  390. ret
  391.  
  392. var srchannels = array.new_box(10)
  393. for x = 0 to min(9, maxnumsr)
  394. box.delete(array.get(srchannels, x))
  395. srcol = get_color(x * 2)
  396. if not na(srcol)
  397. array.set(srchannels, x,
  398. box.new(left = bar_index, top = get_level(x * 2), right = bar_index + 1, bottom = get_level(x * 2 + 1),
  399. border_color = srcol,
  400. border_width = 1,
  401. extend = extend.both,
  402. bgcolor = srcol))
  403.  
  404. resistancebroken = false
  405. supportbroken = false
  406.  
  407.  
  408. lengthc = input(20, "Upper Length", inline = "length")
  409. length2 = input(20, "Lower Length", inline = "length")
  410. astartc = input(2,"Draw Upper Line From Pivot #", inline = "pivot")
  411. aend = input(0,"To Pivot #", inline = "pivot")
  412. bstart = input(3,"Draw Lower Line From Pivot #", inline = "pivot2")
  413. bend = input(0,"To Pivot #", inline = "pivot2")
  414. csrc = input(false,"Use Custom Source?", inline = "custom")
  415. src = input(close,"Select:", inline = "custom")
  416. color = input("Red/Green", "Line Color:", options=["Red/Green", "White", "Yellow", "Blue"], inline = "custom2")
  417. lwidth = input("1", "Line Width:", options=["1", "2", "3", "4"], inline = "custom2")
  418.  
  419. //Conditions
  420. up = pivothigh(iff(csrc,src,high),lengthc,lengthc)
  421. dn = pivotlow(iff(csrc,src,low),length2,length2)
  422.  
  423. n = bar_index
  424. a1 = valuewhen(not na(up),n,astartc)
  425. b1 = valuewhen(not na(dn),n,bstart)
  426. a2 = valuewhen(not na(up),n,aend)
  427. b2 = valuewhen(not na(dn),n,bend)
  428.  
  429. //Colors
  430. color1 = color.red
  431. color2 = color.green
  432. if color == "White"
  433. color1 := color.white
  434. color2 := color.white
  435. if color == "Yellow"
  436. color1 := color.yellow
  437. color2 := color.yellow
  438. if color == "Blue"
  439. color1 := color.blue
  440. color2 := color.blue
  441. if color == "Red/Green"
  442. color1 := color.red
  443. color2 := color.green
  444.  
  445. //Width
  446. width1 = 1
  447. if lwidth == "1"
  448. width1 := 1
  449. if lwidth == "2"
  450. width1 := 2
  451. if lwidth == "3"
  452. width1 := 3
  453. if lwidth == "4"
  454. width1 := 4
  455.  
  456. //Plots
  457. line upper = line.new(n[n - a1 + lengthc],up[n - a1],n[n - a2 + lengthc],up[n - a2],
  458. extend=extend.right,color=color1,width=width1)
  459. line lower = line.new(n[n - b1 + length2],dn[n - b1],n[n - b2 + length2],dn[n - b2],
  460. extend=extend.right,color=color2,width=width1)
  461. line.delete(upper[1])
  462. line.delete(lower[1])
  463.  
  464. // check if it's not in a channel
  465. not_in_a_channel = true
  466. for x = 0 to min(9, maxnumsr)
  467. if close <= array.get(suportresistance, x * 2) and close >= array.get(suportresistance, x * 2 + 1)
  468. not_in_a_channel := false
  469.  
  470. // if price is not in a channel then check broken ones
  471. if not_in_a_channel
  472. for x = 0 to min(9, maxnumsr)
  473. if close[1] <= array.get(suportresistance, x * 2) and close > array.get(suportresistance, x * 2)
  474. resistancebroken := true
  475. if close[1] >= array.get(suportresistance, x * 2 + 1) and close < array.get(suportresistance, x * 2 + 1)
  476. supportbroken := true
  477.  
  478. alertcondition(resistancebroken, title = "Resistance Broken", message = "Resistance Broken")
  479. alertcondition(supportbroken, title = "Support Broken", message = "Support Broken")
  480. plotshape(showsrbroken and resistancebroken, style = shape.triangleup, location = location.belowbar, color = color.new(color.lime, 0), size = size.tiny)
  481. plotshape(showsrbroken and supportbroken, style = shape.triangledown, location = location.abovebar, color = color.new(color.red, 0), size = size.tiny)
  482.  
Advertisement
Add Comment
Please, Sign In to add comment