xmd79

Pivot Points by kansal

Oct 10th, 2021
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.42 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0
  2. // © Mkansal
  3.  
  4. //@version=5
  5. indicator('Pivot Points by kansal ', overlay=true)
  6. prd = input.int(defval=2, title='Pivot Point Period', minval=1, maxval=50)
  7. Factor = input.float(defval=3, title='ATR Factor', minval=1, step=0.1)
  8. Pd = input.int(defval=10, title='ATR Period', minval=1)
  9. showpivot = input(defval=false, title='Show Pivot Points')
  10. showlabel = input(defval=true, title='Show Buy/Sell Labels')
  11. showcl = input(defval=false, title='Show PP Center Line')
  12. showsr = input(defval=false, title='Show Support/Resistance')
  13.  
  14. float ph = na
  15. float pl = na
  16. ph := ta.pivothigh(prd, prd)
  17. pl := ta.pivotlow(prd, prd)
  18.  
  19. plotshape(ph and showpivot, text='H', style=shape.labeldown, color=na, textcolor=color.new(color.red, 0), location=location.abovebar, offset=-prd, transp=0)
  20. plotshape(pl and showpivot, text='L', style=shape.labeldown, color=na, textcolor=color.new(color.lime, 0), location=location.belowbar, offset=-prd, transp=0)
  21.  
  22. float center = na
  23. center := center[1]
  24. float lastpp = ph ? ph : pl ? pl : na
  25. if lastpp
  26. if na(center)
  27. center := lastpp
  28. center
  29. else
  30. center := (center * 2 + lastpp) / 3
  31. center
  32.  
  33. Up = center - Factor * ta.atr(Pd)
  34. Dn = center + Factor * ta.atr(Pd)
  35.  
  36. float TUp = na
  37. float TDown = na
  38. Trend = 0
  39. TUp := close[1] > TUp[1] ? math.max(Up, TUp[1]) : Up
  40. TDown := close[1] < TDown[1] ? math.min(Dn, TDown[1]) : Dn
  41. Trend := close > TDown[1] ? 1 : close < TUp[1] ? -1 : nz(Trend[1], 1)
  42. Trailingsl = Trend == 1 ? TUp : TDown
  43.  
  44. linecolor = Trend == 1 and nz(Trend[1]) == 1 ? color.lime : Trend == -1 and nz(Trend[1]) == -1 ? color.red : na
  45. plot(Trailingsl, color=linecolor, linewidth=2, title='PP SuperTrend')
  46.  
  47. plot(showcl ? center : na, color=showcl ? center < hl2 ? color.blue : color.red : na, transp=0)
  48.  
  49. bsignal = Trend == 1 and Trend[1] == -1
  50. ssignal = Trend == -1 and Trend[1] == 1
  51. plotshape(bsignal and showlabel ? Trailingsl : na, title='Buy', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.lime, 0), textcolor=color.new(color.black, 0))
  52. plotshape(ssignal and showlabel ? Trailingsl : na, title='Sell', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))
  53.  
  54. float resistance = na
  55. float support = na
  56. support := pl ? pl : support[1]
  57. resistance := ph ? ph : resistance[1]
  58.  
  59. plot(showsr and support ? support : na, color=showsr and support ? color.lime : na, style=plot.style_circles, offset=-prd)
  60. plot(showsr and resistance ? resistance : na, color=showsr and resistance ? color.red : na, style=plot.style_circles, offset=-prd)
  61.  
  62. alertcondition(Trend == 1 and Trend[1] == -1, title='Buy Signal', message='Buy Signal')
  63. alertcondition(Trend == -1 and Trend[1] == 1, title='Sell Signal', message='Sell Signal')
  64. alertcondition(ta.change(Trend), title='Trend Changed', message='Trend Changed')
  65.  
  66.  
  67. gr = 'LENGTH LEFT / RIGHT'
  68. leftLenH = input.int(title='Pivot High', defval=10, minval=1, inline='Pivot High', group=gr)
  69. rightLenH = input.int(title='/', defval=10, minval=1, inline='Pivot High', group=gr)
  70. colorH = input.color(title='', defval=#E3EFFD, inline='Pivot High', group=gr)
  71.  
  72. leftLenL = input.int(title='Pivot Low', defval=10, minval=1, inline='Pivot Low', group=gr)
  73. rightLenL = input.int(title='/', defval=10, minval=1, inline='Pivot Low', group=gr)
  74. colorL = input.color(title='', defval=#E3EFFD, inline='Pivot Low', group=gr)
  75.  
  76. phu = ta.pivothigh(leftLenH, rightLenH)
  77. pll = ta.pivotlow(leftLenL, rightLenL)
  78.  
  79. drawLabel(_offset, _pivot, _style, _color) =>
  80. if not na(_pivot)
  81. label.new(bar_index[_offset], _pivot, str.tostring(_pivot, format.mintick), style=_style, color=_color, textcolor=#131722)
  82.  
  83. drawLabel(rightLenH, phu, label.style_label_down, colorH)
  84. drawLabel(rightLenL, pll, label.style_label_up, colorL)
  85.  
  86. depthTooltip = 'The minimum number of bars that will be taken into account when calculating the indicator.'
  87. depth = input.int(title='Depth', defval=10, minval=1, inline='Pivots', tooltip=depthTooltip)
  88. reverse = input(false, 'Reverse')
  89. var extendLeft = input.bool(false, 'Extend Left    |    Extend Right', inline='Extend Lines')
  90. var extendRight = input.bool(true, '', inline='Extend Lines')
  91. var extending = extend.none
  92. if extendLeft and extendRight
  93. extending := extend.both
  94. extending
  95. if extendLeft and not extendRight
  96. extending := extend.left
  97. extending
  98. if not extendLeft and extendRight
  99. extending := extend.right
  100. extending
  101. prices = input(true, 'Show Prices')
  102. levels = input.bool(true, 'Show Levels', inline='Levels')
  103. levelsFormat = input.string('Values', '', options=['Values', 'Percent'], inline='Levels')
  104. labelsPosition = input.string('Left', 'Labels Position', options=['Left', 'Right'])
  105.  
  106. var float upperThreshold = 0.236
  107. var float lowerThreshold = 1.0
  108.  
  109. var line lineLastHL = na
  110. var line lineLastLH = na
  111. var line lineLast = na
  112.  
  113. var float iLastH = 0
  114. var float iLastL = 0
  115. var float pLastH = 0
  116. var float pLastL = 0
  117.  
  118. var float[] iPivotsH = array.new_float()
  119. var float[] iPivotsL = array.new_float()
  120. var float[] pPivotsH = array.new_float()
  121. var float[] pPivotsL = array.new_float()
  122.  
  123. var isHighLast = false
  124. var float startPrice = na
  125. var float endPrice = na
  126. var float diff = na
  127.  
  128. pivots(src, length, isHigh) =>
  129. l2 = length * 2
  130. c = nz(src[length])
  131. ok = true
  132. for i = 0 to l2 by 1
  133. if isHigh and src[i] > c
  134. ok := false
  135. ok
  136.  
  137. if not isHigh and src[i] < c
  138. ok := false
  139. ok
  140. if ok
  141. [bar_index[length], c]
  142. else
  143. [int(na), float(na)]
  144. [iH, pH] = pivots(high, depth / 2, true)
  145. [iL, pL] = pivots(low, depth / 2, false)
  146.  
  147. countPivotsH = array.size(iPivotsH)
  148. countPivotsL = array.size(iPivotsL)
  149.  
  150. if countPivotsH > 0 and countPivotsL > 0
  151. iLastH := array.get(iPivotsH, countPivotsH - 1)
  152. iLastL := array.get(iPivotsL, countPivotsL - 1)
  153. isHighLast := iLastH > iLastL
  154. iLastH := 1
  155. if isHighLast
  156. if not na(iH)
  157. pLastH := array.get(pPivotsH, countPivotsH - 1)
  158. if pH > pLastH
  159. array.set(iPivotsH, countPivotsH - 1, iH)
  160. array.set(pPivotsH, countPivotsH - 1, pH)
  161. iH := na
  162. iH
  163. else
  164. if not na(iL)
  165. pLastL := array.get(pPivotsL, countPivotsL - 1)
  166. if pL < pLastL
  167. array.set(iPivotsL, countPivotsL - 1, iL)
  168. array.set(pPivotsL, countPivotsL - 1, pL)
  169. iL := na
  170. iL
  171.  
  172. if not na(iH)
  173. array.push(iPivotsH, iH)
  174. array.push(pPivotsH, pH)
  175.  
  176. if not na(iL)
  177. array.push(iPivotsL, iL)
  178. array.push(pPivotsL, pL)
  179.  
  180. pPivotsHCopy = array.copy(pPivotsH)
  181. pPivotsLCopy = array.copy(pPivotsL)
  182. iPivotsHCopy = array.copy(iPivotsH)
  183. iPivotsLCopy = array.copy(iPivotsL)
  184.  
  185. if barstate.islast
  186. for j = bar_index to 0 by 1
  187. if array.size(iPivotsHCopy) == 0 or array.size(iPivotsLCopy) == 0
  188. break
  189.  
  190. iLastH := array.pop(iPivotsHCopy)
  191. iLastL := array.pop(iPivotsLCopy)
  192.  
  193. pLastH := array.pop(pPivotsHCopy)
  194. pLastL := array.pop(pPivotsLCopy)
  195.  
  196. iPrevPivot = 0.0
  197. pPrevPivot = 0.0
  198.  
  199. isHighLast := iLastH > iLastL
  200.  
  201. if isHighLast
  202. for i = array.size(iPivotsHCopy) - 1 to 0 by 1
  203. if i < 0
  204. break
  205. else if array.get(iPivotsHCopy, i) < iLastL
  206. break
  207. if array.get(pPivotsHCopy, i) > pLastH
  208. iLastH := array.pop(iPivotsHCopy)
  209. pLastH := array.pop(pPivotsHCopy)
  210. pLastH
  211. else
  212. array.remove(iPivotsHCopy, i)
  213. array.remove(pPivotsHCopy, i)
  214. else
  215. for i = array.size(iPivotsLCopy) - 1 to 0 by 1
  216. if i < 0
  217. break
  218. else if array.get(iPivotsLCopy, i) < iLastH
  219. break
  220. if array.get(pPivotsLCopy, i) < pLastL
  221. iLastL := array.pop(iPivotsLCopy)
  222. pLastL := array.pop(pPivotsLCopy)
  223. pLastL
  224. else
  225. array.remove(iPivotsLCopy, i)
  226. array.remove(pPivotsLCopy, i)
  227.  
  228. if array.size(iPivotsHCopy) == 0 or array.size(iPivotsLCopy) == 0
  229. break
  230.  
  231. isHighLast := iLastH > iLastL
  232.  
  233. if isHighLast
  234. iPrevPivot := array.get(iPivotsHCopy, array.size(iPivotsHCopy) - 1)
  235. pPrevPivot := array.get(pPivotsHCopy, array.size(iPivotsHCopy) - 1)
  236. pPrevPivot
  237. else
  238. iPrevPivot := array.get(iPivotsLCopy, array.size(iPivotsLCopy) - 1)
  239. pPrevPivot := array.get(pPivotsLCopy, array.size(iPivotsLCopy) - 1)
  240. pPrevPivot
  241.  
  242. if isHighLast
  243. startPrice := pPrevPivot
  244. endPrice := pLastL
  245. diff := math.abs(startPrice - endPrice)
  246. if pLastH > endPrice + diff * lowerThreshold or pLastH < endPrice + diff * upperThreshold
  247. array.push(iPivotsLCopy, iLastL)
  248. array.push(pPivotsLCopy, pLastL)
  249. continue
  250. line.delete(lineLastHL)
  251. line.delete(lineLastLH)
  252. line.delete(lineLast)
  253. lineLastHL := line.new(int(iPrevPivot), pPrevPivot, int(iLastL), pLastL, color=color.red, width=1, style=line.style_dashed)
  254. lineLastLH := line.new(int(iLastL), pLastL, int(iLastH), pLastH, color=color.green, width=1, style=line.style_dashed)
  255. lineLast := lineLastLH
  256. lineLast
  257. else
  258. startPrice := pPrevPivot
  259. endPrice := pLastH
  260. diff := math.abs(startPrice - endPrice)
  261. if pLastL < endPrice - diff * lowerThreshold or pLastL > endPrice - diff * upperThreshold
  262. array.push(iPivotsHCopy, iLastH)
  263. array.push(pPivotsHCopy, pLastH)
  264. continue
  265. line.delete(lineLastHL)
  266. line.delete(lineLastLH)
  267. line.delete(lineLast)
  268. lineLastLH := line.new(int(iPrevPivot), pPrevPivot, int(iLastH), pLastH, color=color.red, width=1, style=line.style_dashed)
  269. lineLastHL := line.new(int(iLastH), pLastH, int(iLastL), pLastL, color=color.green, width=1, style=line.style_dashed)
  270. lineLast := lineLastHL
  271. lineLast
  272. break
  273.  
  274. _draw_line(price, col) =>
  275. var id = line.new(0, price, 0, price, color=col, width=1, extend=extending)
  276. if not na(lineLast)
  277. line.set_xy1(id, line.get_x1(lineLast), price)
  278. line.set_xy2(id, line.get_x2(lineLast), price)
  279.  
  280. _draw_label(price, txt, txtColor) =>
  281. if not na(price)
  282. x = labelsPosition == 'Left' ? line.get_x1(lineLast) : not extendRight ? line.get_x2(lineLast) : bar_index
  283. labelStyle = labelsPosition == 'Left' ? label.style_label_right : label.style_label_left
  284. align = labelsPosition == 'Left' ? text.align_right : text.align_left
  285. labelsAlignStrLeft = txt + '\n ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ \n'
  286. labelsAlignStrRight = ' ' + txt + '\n ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ \n'
  287. labelsAlignStr = labelsPosition == 'Left' ? labelsAlignStrLeft : labelsAlignStrRight
  288. var id = label.new(x=x, y=price, text=labelsAlignStr, textcolor=txtColor, style=labelStyle, textalign=align, color=#00000000)
  289. label.set_xy(id, x, price)
  290. label.set_text(id, labelsAlignStr)
  291. label.set_textcolor(id, txtColor)
  292.  
  293. _wrap(txt) =>
  294. '(' + str.tostring(txt, '#.##') + ')'
  295.  
  296. _label_txt(level, price) =>
  297. if not na(price)
  298. l = levelsFormat == 'Values' ? str.tostring(level) : str.tostring(level * 100) + '%'
  299. (levels ? l : '') + (prices ? _wrap(price) : '')
  300.  
  301. _crossing_level(sr, r) =>
  302. r > sr and r < sr[1] or r < sr and r > sr[1]
  303.  
  304. diff := (isHighLast ? -1 : 1) * math.abs(startPrice - endPrice)
  305. offset = isHighLast ? line.get_y1(lineLastLH) - line.get_y2(lineLastLH) : line.get_y1(lineLastHL) - line.get_y2(lineLastHL)
  306. offset := (isHighLast ? -1 : 1) * math.abs(offset)
  307.  
  308. processLevel(show, value, colorL) =>
  309. float m = value
  310. r = endPrice - offset + (reverse ? -1 : 1) * diff * m
  311. if show
  312. _draw_line(r, colorL)
  313. _draw_label(r, _label_txt(m, r), colorL)
  314. if _crossing_level(close, r)
  315. alert('Autofib: ' + syminfo.ticker + ' crossing level ' + str.tostring(value))
  316.  
  317. show_0 = input.bool(true, '', inline='Level0')
  318. value_0 = input.int(0, '', inline='Level0')
  319. color_0 = input.color(#787b86, '', inline='Level0')
  320. processLevel(show_0, value_0, color_0)
  321.  
  322. show_0_236 = input.bool(true, '', inline='Level0')
  323. value_0_236 = input.float(0.236, '', inline='Level0')
  324. color_0_236 = input.color(#f44336, '', inline='Level0')
  325. processLevel(show_0_236, value_0_236, color_0_236)
  326.  
  327. show_0_382 = input.bool(true, '', inline='Level1')
  328. value_0_382 = input.float(0.382, '', inline='Level1')
  329. color_0_382 = input.color(#81c784, '', inline='Level1')
  330. processLevel(show_0_382, value_0_382, color_0_382)
  331.  
  332. show_0_5 = input.bool(true, '', inline='Level1')
  333. value_0_5 = input.float(0.5, '', inline='Level1')
  334. color_0_5 = input.color(#4caf50, '', inline='Level1')
  335. processLevel(show_0_5, value_0_5, color_0_5)
  336.  
  337. show_0_618 = input.bool(true, '', inline='Level2')
  338. value_0_618 = input.float(0.618, '', inline='Level2')
  339. color_0_618 = input.color(#009688, '', inline='Level2')
  340. processLevel(show_0_618, value_0_618, color_0_618)
  341.  
  342. show_0_65 = input.bool(false, '', inline='Level2')
  343. value_0_65 = input.float(0.65, '', inline='Level2')
  344. color_0_65 = input.color(#009688, '', inline='Level2')
  345. processLevel(show_0_65, value_0_65, color_0_65)
  346.  
  347. show_0_786 = input.bool(true, '', inline='Level3')
  348. value_0_786 = input.float(0.786, '', inline='Level3')
  349. color_0_786 = input.color(#64b5f6, '', inline='Level3')
  350. processLevel(show_0_786, value_0_786, color_0_786)
  351.  
  352. show_1 = input.bool(true, '', inline='Level3')
  353. value_1 = input.int(1, '', inline='Level3')
  354. color_1 = input.color(#787b86, '', inline='Level3')
  355. processLevel(show_1, value_1, color_1)
  356.  
  357. show_1_272 = input.bool(false, '', inline='Level4')
  358. value_1_272 = input.float(1.272, '', inline='Level4')
  359. color_1_272 = input.color(#81c784, '', inline='Level4')
  360. processLevel(show_1_272, value_1_272, color_1_272)
  361.  
  362. show_1_414 = input.bool(false, '', inline='Level4')
  363. value_1_414 = input.float(1.414, '', inline='Level4')
  364. color_1_414 = input.color(#f44336, '', inline='Level4')
  365. processLevel(show_1_414, value_1_414, color_1_414)
  366.  
  367. show_1_618 = input.bool(true, '', inline='Level5')
  368. value_1_618 = input.float(1.618, '', inline='Level5')
  369. color_1_618 = input.color(#2196f3, '', inline='Level5')
  370. processLevel(show_1_618, value_1_618, color_1_618)
  371.  
  372. show_1_65 = input.bool(false, '', inline='Level5')
  373. value_1_65 = input.float(1.65, '', inline='Level5')
  374. color_1_65 = input.color(#2196f3, '', inline='Level5')
  375. processLevel(show_1_65, value_1_65, color_1_65)
  376.  
  377. show_2_618 = input.bool(true, '', inline='Level6')
  378. value_2_618 = input.float(2.618, '', inline='Level6')
  379. color_2_618 = input.color(#f44336, '', inline='Level6')
  380. processLevel(show_2_618, value_2_618, color_2_618)
  381.  
  382. show_2_65 = input.bool(false, '', inline='Level6')
  383. value_2_65 = input.float(2.65, '', inline='Level6')
  384. color_2_65 = input.color(#f44336, '', inline='Level6')
  385. processLevel(show_2_65, value_2_65, color_2_65)
  386.  
  387. show_3_618 = input.bool(true, '', inline='Level7')
  388. value_3_618 = input.float(3.618, '', inline='Level7')
  389. color_3_618 = input.color(#9c27b0, '', inline='Level7')
  390. processLevel(show_3_618, value_3_618, color_3_618)
  391.  
  392. show_3_65 = input.bool(false, '', inline='Level7')
  393. value_3_65 = input.float(3.65, '', inline='Level7')
  394. color_3_65 = input.color(#9c27b0, '', inline='Level7')
  395. processLevel(show_3_65, value_3_65, color_3_65)
  396.  
  397. show_4_236 = input.bool(true, '', inline='Level8')
  398. value_4_236 = input.float(4.236, '', inline='Level8')
  399. color_4_236 = input.color(#e91e63, '', inline='Level8')
  400. processLevel(show_4_236, value_4_236, color_4_236)
  401.  
  402. show_4_618 = input.bool(false, '', inline='Level8')
  403. value_4_618 = input.float(4.618, '', inline='Level8')
  404. color_4_618 = input.color(#81c784, '', inline='Level8')
  405. processLevel(show_4_618, value_4_618, color_4_618)
  406.  
  407. show_neg_0_236 = input.bool(false, '', inline='Level9')
  408. value_neg_0_236 = input.float(-0.236, '', inline='Level9')
  409. color_neg_0_236 = input.color(#f44336, '', inline='Level9')
  410. processLevel(show_neg_0_236, value_neg_0_236, color_neg_0_236)
  411.  
  412. show_neg_0_382 = input.bool(false, '', inline='Level9')
  413. value_neg_0_382 = input.float(-0.382, '', inline='Level9')
  414. color_neg_0_382 = input.color(#81c784, '', inline='Level9')
  415. processLevel(show_neg_0_382, value_neg_0_382, color_neg_0_382)
  416.  
  417. show_neg_0_618 = input.bool(false, '', inline='Level10')
  418. value_neg_0_618 = input.float(-0.618, '', inline='Level10')
  419. color_neg_0_618 = input.color(#009688, '', inline='Level10')
  420. processLevel(show_neg_0_618, value_neg_0_618, color_neg_0_618)
  421.  
  422. show_neg_0_65 = input.bool(false, '', inline='Level10')
  423. value_neg_0_65 = input.float(-0.65, '', inline='Level10')
  424. color_neg_0_65 = input.color(#009688, '', inline='Level10')
  425. processLevel(show_neg_0_65, value_neg_0_65, color_neg_0_65)
  426.  
  427.  
Advertisement
Add Comment
Please, Sign In to add comment