xmd79

Wavetrend in Dynamic Zones with Kumo Implied Volatility

Nov 23rd, 2022
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.93 KB | None | 0 0
  1. //@version=5
  2. // This program is free software: you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation, either version 3 of the License, or
  5. // any later version.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. //
  12. // The GNU General Public License can be found here
  13. // <http://www.gnu.org/licenses/>.
  14. //
  15. // -----------------------------------------------------------------------------
  16.  
  17.  
  18. indicator(title='Wavetrend in Dynamic Zones with Kumo Implied Volatility', shorttitle='Wavetrend', precision=0)
  19.  
  20. n1 = input(9, 'Wavetrend Channel Length')
  21. n2 = input(12, 'Wavetrend Average Length')
  22.  
  23. ap = input(hlc3)
  24. esa = ta.ema(ap, n1)
  25. d = ta.ema(math.abs(ap - esa), n1)
  26. ci = (ap - esa) / (0.015 * d)
  27. tci = ta.ema(ci, n2)
  28.  
  29. tsi1 = tci
  30. tsi2 = ta.sma(tsi1, 4)
  31.  
  32. long = n1
  33. short = n2
  34. signal = n2
  35.  
  36. price = ap
  37. src2 = ap
  38. src = ap
  39. input_src = tsi1
  40.  
  41. //input_src = input(close, title='Price Source')
  42. input_lookbackbars = input.int(70, title='Lookback Bars', minval=1)
  43. input_startbuyprobability = input(0.12, title='Start Buy Probability')
  44. input_startsellprobability = input(0.12, title='Start Sell Probability')
  45.  
  46.  
  47. dzsell(src, initvalue, lookbackbars) =>
  48. left = -10000.0
  49. right = 10000.0
  50. eps = 0.001
  51. yval = (left + right) / 2.0
  52. delta = yval - left
  53. maxsteps = 0
  54. for i = 0 to 99999 by 1
  55. if not(delta > 0.005 and maxsteps < 50)
  56. break
  57. maxsteps += 1
  58. count = 0
  59. for k = 0 to lookbackbars - 1 by 1
  60. if src[k] > yval
  61. count += 1
  62. count
  63. prob = count / lookbackbars
  64. if prob > initvalue + eps
  65. left := yval
  66. yval := (yval + right) / 2.0
  67. yval
  68. if prob < initvalue - eps
  69. right := yval
  70. yval := (yval + left) / 2.0
  71. yval
  72. if prob < initvalue + eps and prob > initvalue - eps
  73. left := yval
  74. yval := (yval + right) / 2.0
  75. yval
  76. delta := yval - left
  77. delta
  78. [yval, delta, maxsteps]
  79.  
  80. dzbuy(src, initvalue, lookbackbars) =>
  81. left = -10000.0
  82. right = 10000.0
  83. eps = 0.001
  84. yval = (left + right) / 2.0
  85. delta = yval - left
  86. maxsteps = 0
  87. for i = 0 to 99999 by 1
  88. if not(delta > 0.005 and maxsteps < 50)
  89. break
  90. maxsteps += 1
  91. count = 0
  92. for k = 0 to lookbackbars - 1 by 1
  93. if src[k] < yval
  94. count += 1
  95. count
  96. prob = count / lookbackbars
  97. if prob > initvalue + eps
  98. right := yval
  99. yval := (yval + left) / 2.0
  100. yval
  101. if prob < initvalue - eps
  102. left := yval
  103. yval := (yval + right) / 2.0
  104. yval
  105. if prob < initvalue + eps and prob > initvalue - eps
  106. right := yval
  107. yval := (yval + left) / 2.0
  108. yval
  109. delta := yval - left
  110. delta
  111. [yval, delta, maxsteps]
  112.  
  113. [sell_zone, sell_delta, sell_maxsteps] = dzsell(input_src, input_startsellprobability, input_lookbackbars)
  114. [buy_zone, buy_delta, buy_maxsteps] = dzbuy(input_src, input_startbuyprobability, input_lookbackbars)
  115.  
  116. pivR = input(title='WT Pivot Lookback Right', defval=1)
  117. pivL = input(title='WT Pivot Lookback Left', defval=20)
  118. trshold = input(25, 'Pivot Threshold')
  119.  
  120.  
  121. // Compute VPT ocillator
  122. obvOsc = tsi1
  123.  
  124. obvColor = obvOsc > 0 ? color.new(color.aqua, 60) : color.new(color.orange, 60)
  125.  
  126. // Divergence
  127.  
  128. up = ta.rma(math.max(ta.change(src), 0), short)
  129. down = ta.rma(-math.min(ta.change(src), 0), short)
  130. lbR = input(title='Divergence Pivot Lookback Right', defval=1)
  131. lbL = input(title='Divergence Pivot Lookback Left', defval=5)
  132. rangeUpper = input(title='Max of Lookback Range', defval=100)
  133. rangeLower = input(title='Min of Lookback Range', defval=2)
  134. plotBull = input(title='Plot Bullish', defval=true)
  135. plotHiddenBull = input(title='Plot Hidden Bullish', defval=false)
  136. plotBear = input(title='Plot Bearish', defval=true)
  137. plotHiddenBear = input(title='Plot Hidden Bearish', defval=false)
  138. bearColor = color.new(color.red, 0)
  139. bullColor = color.new(color.green, 0)
  140. hiddenBullColor = color.new(color.aqua, 0)
  141. hiddenBearColor = color.new(color.orange, 0)
  142. textColor = color.new(color.white, 0)
  143. noneColor = color.new(color.white, 100)
  144. osc = tsi1
  145. plFound = na(ta.pivotlow(osc, lbL, lbR)) ? false : true
  146. phFound = na(ta.pivothigh(osc, lbL, lbR)) ? false : true
  147. _inRange(cond) =>
  148. bars = ta.barssince(cond == true)
  149. rangeLower <= bars and bars <= rangeUpper
  150.  
  151. //------------------------------------------------------------------------------
  152. // Regular Bullish
  153. // Osc: Higher Low
  154.  
  155. oscHL = osc[lbR] > ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
  156.  
  157. // Price: Lower Low
  158.  
  159. priceLL = low[lbR] < ta.valuewhen(plFound, low[lbR], 1)
  160. bullCond = plotBull and priceLL and oscHL and plFound
  161.  
  162. plot(plFound ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=1, color=bullCond ? bullColor : noneColor, transp=0)
  163.  
  164. plotshape(bullCond ? osc[lbR] : na, offset=-lbR, title='Regular Bullish Label', style=shape.labelup, location=location.absolute, size=size.tiny, color=bullColor, textcolor=textColor, transp=0)
  165.  
  166. //------------------------------------------------------------------------------
  167. // Hidden Bullish
  168. // Osc: Lower Low
  169.  
  170. oscLL = osc[lbR] < ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
  171.  
  172. // Price: Higher Low
  173.  
  174. priceHL = low[lbR] > ta.valuewhen(plFound, low[lbR], 1)
  175. hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound
  176.  
  177. plot(plFound ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=1, color=hiddenBullCond ? hiddenBullColor : noneColor, transp=0)
  178.  
  179. plotshape(hiddenBullCond ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish Label', style=shape.labelup, location=location.absolute, size=size.tiny, color=hiddenBullColor, textcolor=textColor, transp=0)
  180.  
  181. //------------------------------------------------------------------------------
  182. // Regular Bearish
  183. // Osc: Lower High
  184.  
  185. oscLH = osc[lbR] < ta.valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
  186.  
  187. // Price: Higher High
  188.  
  189. priceHH = high[lbR] > ta.valuewhen(phFound, high[lbR], 1)
  190.  
  191. bearCond = plotBear and priceHH and oscLH and phFound
  192.  
  193. plot(phFound ? osc[lbR] : na, offset=-lbR, title='Regular Bearish', linewidth=1, color=bearCond ? bearColor : noneColor, transp=0)
  194.  
  195. plotshape(bearCond ? osc[lbR] : na, offset=-lbR, title='Regular Bearish Label', style=shape.labeldown, location=location.absolute, size=size.tiny, color=bearColor, textcolor=textColor, transp=0)
  196.  
  197. //------------------------------------------------------------------------------
  198. // Hidden Bearish
  199. // Osc: Higher High
  200.  
  201. oscHH = osc[lbR] > ta.valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
  202.  
  203. // Price: Lower High
  204.  
  205. priceLH = high[lbR] < ta.valuewhen(phFound, high[lbR], 1)
  206.  
  207. hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound
  208.  
  209. plot(phFound ? osc[lbR] : na, offset=-lbR, title='Hidden Bearish', linewidth=1, color=hiddenBearCond ? hiddenBearColor : noneColor, transp=0)
  210.  
  211. plotshape(hiddenBearCond ? osc[lbR] : na, offset=-lbR, title='Hidden Bearish Label', style=shape.labeldown, location=location.absolute, color=hiddenBearColor, size=size.tiny, textcolor=textColor, transp=0)
  212.  
  213. //Ichimoku
  214. Tenkansen = (ta.highest(high, 9) + ta.lowest(low, 9)) / 2
  215. Kijunsen = (ta.highest(high, 26) + ta.lowest(low, 26)) / 2
  216. FutureSpanA = (Tenkansen + Kijunsen) / 2
  217. FutureSpanB = (ta.highest(high, 52) + ta.lowest(low, 52)) / 2
  218.  
  219. //Implied volatility based on Kumo thickness
  220. Kumodepth = math.abs(FutureSpanA - FutureSpanB)
  221.  
  222. //Normalize implied volatility
  223. Indicator = Kumodepth
  224. AverageIndicator = ta.sma(Indicator, 200)
  225. StandardDeviation = ta.stdev(Indicator, 200)
  226. UpperBand = AverageIndicator + 2 * StandardDeviation
  227. LowerBand = AverageIndicator - 2 * StandardDeviation
  228. NormalizedIndicator = (Indicator - LowerBand) / (UpperBand - LowerBand) * 100
  229.  
  230.  
  231. // Bollinger Bands Percentage
  232.  
  233. lengthBB = input.int(20, minval=1, title="Bollinger Bands Lengh")
  234. srcBB = input(close, title="Bollinger Bands Source")
  235. multBB = input.float(2.0, minval=0.001, maxval=50, title="Bollinger Bands StdDev")
  236. basisBB = ta.sma(srcBB, lengthBB)
  237. devBB = multBB * ta.stdev(srcBB, lengthBB)
  238. upperBB = basisBB + devBB
  239. lowerBB = basisBB - devBB
  240. bbrBB = (srcBB - lowerBB)/(upperBB - lowerBB)
  241.  
  242. Perc1=bbrBB
  243.  
  244.  
  245. //BB Width %
  246.  
  247. i_priceSrc = input.source ( close, 'BB Price Source', inline='1', group='BBWP Properties')
  248. i_bbwpLen = input.int ( 13, 'BB Length', minval=1, inline='2', group='BBWP Properties')
  249. i_bbwpLkbk = input.int ( 252, '     Lookback', minval=1, inline='2', group='BBWP Properties')
  250. i_ma1On = input.bool ( true, '', inline='3', group='BBWP Properties')
  251. i_ma1Type = input.string ( 'SMA', 'BBWP MA Type', options=['SMA', 'EMA', 'VWMA'], inline='3', group='BBWP Properties')
  252. i_ma1Len = input.int ( 5, 'BB Length', minval=1, inline='3', group='BBWP Properties')
  253.  
  254. bbwp = ta.percentrank(ta.bbw(i_priceSrc, i_bbwpLen, 1), i_bbwpLkbk)
  255. c_bbwp = bbwp >= 50 ? color.from_gradient(bbwp, 50, 100, #0AFF00, #FF2900): color.from_gradient(bbwp, 0, 49, #0000FF, #0AFF00)
  256. bbwpMA1 = i_ma1On ? i_ma1Type == 'VWMA' ? ta.vwma ( bbwp, i_ma1Len ) : i_ma1Type == 'EMA' ? ta.ema ( bbwp, i_ma1Len ) : ta.sma ( bbwp, i_ma1Len ) : na
  257.  
  258. //p_bbwp = plot ( bbwp, 'BBWP', c_bbwp, 2, plot.style_line, editable=false )
  259. //p_ma1 = plot ( bbwpMA1, 'BBWP MA', #FFFFFF, 1, plot.style_line, 0 )
  260.  
  261.  
  262. // Plots
  263.  
  264. labelwhere = input.int(20, title="Where to Draw the Label 1")
  265. labelwhere2 = input.int(50, title="Where to Draw the Label 2")
  266.  
  267. perccolor = bbrBB < 0.5 and bbrBB > 0 ? color.new(color.aqua,50) : bbrBB < 0 ? color.new(color.green,50) : bbrBB > 1 ? color.new(color.red,50) : color.new(color.orange,50)
  268. pertext = bbrBB > 1 ? "Bollinger Bands Overbought" : bbrBB < 0 ? "Bollinger Bands Oversold" : "BB"
  269.  
  270. if barstate.islast
  271. var lbl = label.new(na, na)
  272.  
  273. label.set_xy(lbl, bar_index + labelwhere, input_src)
  274. label.set_textalign(lbl, text.align_right)
  275. label.set_text(lbl, str.tostring(Perc1, format.mintick)+ " " + (pertext))
  276. label.set_textcolor(lbl, perccolor)
  277. label.set_style(lbl, label.style_none)
  278.  
  279.  
  280.  
  281.  
  282.  
  283. if barstate.islast
  284. var lbl2 = label.new(na, na)
  285.  
  286. label.set_xy(lbl2, bar_index + labelwhere2, 55)
  287. label.set_textalign(lbl2, text.align_right)
  288. label.set_text(lbl2, str.tostring(bbwp, format.mintick)+ " " + "%" + " " + "BB Width")
  289. label.set_textcolor(lbl2, color.new(color.aqua, 50))
  290. label.set_style(lbl2, label.style_none)
  291.  
  292.  
  293.  
  294. crossoverCond1 = ta.crossunder(tsi1, tsi2)
  295. crossoverCond2 = ta.crossover(tsi1, tsi2)
  296.  
  297. upper_band = plot(sell_zone, title='Sell Zone', color=color.new(color.white, 100))
  298. lower_band = plot(buy_zone, title='Buy Zone', color=color.new(color.white, 100))
  299. zero1 = plot(100, title='Absolute Max', color=color.new(color.white, 100))
  300. zero2 = plot(-100, title='Absolute Min', color=color.new(color.white, 100))
  301. fill(lower_band, zero2, color=color.new(color.aqua, 90))
  302. fill(upper_band, zero1, color=color.new(color.orange, 90))
  303. plot(input_src, linewidth=1, style=plot.style_columns, color=NormalizedIndicator >= 105 ? color.new(#8d0000, 70) : NormalizedIndicator >= 90 ? color.new(color.red,70) : NormalizedIndicator >= 70 ? color.new(color.orange,70) : NormalizedIndicator >= 50 ? color.new(color.yellow,70) : NormalizedIndicator >= 20 ? color.new(color.aqua, 70) : NormalizedIndicator >= 5 ? color.new(color.silver,70) : color.new(color.white,70), title='Kumo Implied Historical Volatility')
  304. filltype = plot(input_src, linewidth=2, title='WT Line', style=plot.style_line, color=color.new(color.aqua, 0))
  305. plot(tsi2, linewidth=2, title='WT Signal Line', style=plot.style_line, color=color.new(color.orange, 0))
  306. zeromarker = plot(0, linewidth=1, title='Zero', style=plot.style_line, color=color.new(color.white, 100))
  307. hline(0, 'Zero', color.new(color.white,80), hline.style_dashed, 1)
  308. hline(80, 'Overbought', color.new(color.orange,80), hline.style_dashed, 1)
  309. hline(-80, 'Oversold', color.new(color.aqua,80), hline.style_dashed, 1)
  310.  
  311.  
  312.  
  313. PHCond = tsi1 > trshold ? ta.pivothigh(tsi1, pivL, pivR) : na
  314. PLCond = tsi1 < -(trshold) ? ta.pivotlow(tsi1, pivL, pivR) : na
  315.  
  316. vwph = ta.valuewhen(PHCond, PHCond, 0)
  317. vwpl = ta.valuewhen(PLCond, PLCond, 0)
  318.  
  319. counterph = 0
  320. counterph := ta.change(vwph < vwph[1]) ? nz(counterph[1]) + 1 : 0
  321.  
  322. counterpl = 0
  323. counterpl := ta.change(vwpl > vwpl[1]) ? nz(counterpl[1]) + 1 : 0
  324.  
  325. sellmaybe = counterph == 1 ? color.new(color.orange,00) : color.new(color.white,00)
  326.  
  327. buymaybe = counterpl == 1 ? color.new(color.aqua,00) : color.new(color.white,00)
  328.  
  329. plotshape(PHCond, title='WT Pivot High', location=location.absolute, style=shape.labeldown, size=size.auto, color=sellmaybe)
  330. plotshape(PLCond, title='WT Pivot Low', location=location.absolute, style=shape.labelup, size=size.auto, color=buymaybe)
  331.  
  332. alertcondition(PHCond, title='Pivot High', message='Pivot High {{ticker}}')
  333. alertcondition(PLCond, title='Pivot Low', message='Pivot Low {{ticker}}')
  334. alertcondition(crossoverCond1, title='WT Crossed Up', message='WT Cross Up {{ticker}}')
  335. alertcondition(crossoverCond2, title='WT Crossed Down', message='WT Cross Down {{ticker}}')
  336. alertcondition(bullCond, title='Bull Diversion', message='WT Regular Bull Div {{ticker}}')
  337. alertcondition(bearCond, title='Bear Diversion', message='WT Regular Bear Div {{ticker}}')
  338. alertcondition(hiddenBullCond, title='Hidden Bull Diversion', message='WT Hidden Bull Div {{ticker}}')
  339. alertcondition(hiddenBearCond, title='Hidden Bear Diversion', message='WT Hidden Bear Div {{ticker}}')
Advertisement
Add Comment
Please, Sign In to add comment