Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. //@version=4
  2. study(shorttitle="BBB", title="Better Bollinger Bands", overlay=true)
  3.  
  4. //Inputs and constants.
  5. length = input(20, minval=2)
  6. mult = input(2.0, minval=0.001,maxval=50,step = 0.1)
  7. src = input(hlc3, title="Source")
  8. timewindow = input(title="Time window", options = ["Simple","Sawtooth" , "Exponential"], defval="Simple")
  9. use_linear_regression = input(false, title="Use linear regression?")
  10. ignore_volume = input(false, title="Ignore Volume?")
  11. centerscheme = input(title="Centerline color scheme?", options = ["Centerline change","Momentum","RSI hysteresis","Hybrid"], defval="Hybrid")
  12. bandscheme = input(title="Band color scheme?", options = ["Market type", "Volatility Change","Centerline","Plain"], defval="Centerline")
  13.  
  14. multiple = 8 // input(8,minval=1,title= "Length multiple for market determination")
  15. rsi_length = length
  16. hysteresislength = 7
  17. t = 1.2
  18. r = 1.1
  19. hybrid_parameter = 1.0
  20. hybrid_trend = 4.5
  21.  
  22. // Basic function definitions.
  23. corrvol = ignore_volume ? 1 : nz(volume) + 1 // Corrected volume
  24. oscillator_coloring(series) => series > 0 ? color.green : color.red
  25.  
  26. volume_sma(series,period) => sma(series*corrvol,period)/sma(corrvol,period)
  27. volume_ema(series,period) => ema(series*corrvol,period)/ema(corrvol,period)
  28. volume_wma(series,period) => wma(series*corrvol,period)/wma(corrvol,period)
  29.  
  30. besselcorrection_sma(period) => (period + 1)/period
  31. besselcorrection_ema(period) => (period + 1)/(2*(period-1)) // Honestly the only part of Bessels correction that I *actually* care about is that constant factor of 1/2 in the exponential case.
  32. besselcorrection_wma(period) => 3*period*(period+1)/(3*period*period+period-1)
  33. // Excercise for the reader: consider how Bessel corrections are affeted by volume, and the difference between frequency & reliability weights!
  34.  
  35. //volumex_variance(series,period) => (volume_ema(series*series,period) - volume_ema(series,period)*volume_ema(series,period))*besselcorrection_ema(period)
  36. //volumex_stdev(series,period) => sqrt(volumex_variance(series,period))
  37.  
  38. mean(series,len) => timewindow == "Exponential" ? volume_ema(series,len) : timewindow == "Sawtooth" ? volume_wma(series,len) : volume_sma(series,len)
  39. besselcorrection(len) => timewindow == "Exponential" ? besselcorrection_ema(len) : timewindow == "Sawtooth" ? besselcorrection_wma(len) : besselcorrection_sma(len)
  40. Cov(x,y,len) => (mean(x*y,len) - mean(x,len)*mean(y,len))*besselcorrection(len)
  41. Var(series,len) => (mean(series*series,len) - mean(series,len)*mean(series,len))*besselcorrection(len)
  42. sdev(series,len) => sqrt(Var(series,len))
  43.  
  44. n = bar_index
  45. linreg_slope(series,len) => Cov(n,series,len)/Var(n,len)
  46.  
  47. linear_regression(series,len) =>
  48. m = linreg_slope(series,len)
  49. mean(series,len) + m*(n - mean(n,len))
  50.  
  51. lsq_variance(series,len) =>
  52. m = linreg_slope(series,len)
  53. Var(series,len) + m*m*Var(n,len) - 2*m*Cov(series,n,len)
  54.  
  55. lsq_stdev(series,len) => sqrt(lsq_variance(series,len))
  56.  
  57. rsinorm(series,length) =>
  58. rsi = rsi(series,length)
  59. (rsi - ema(rsi,4*length))/15
  60.  
  61. // BB Calculation
  62. lsrc = log(src)
  63. basis = use_linear_regression ? linear_regression(lsrc,length) : mean(lsrc,length)
  64. dev = mult *(use_linear_regression ? lsq_stdev(lsrc,length) : sdev(lsrc,length))
  65. upper = basis + dev
  66. lower = basis - dev
  67.  
  68. //oscillators
  69.  
  70. shorttrend = linreg_slope(lsrc,length/2)*length/4 //volume_ema(lsrc,25*length/100) - volume_ema(lsrc,50*length/100)
  71. mediumtrend = volume_ema(lsrc,50*length/100) - volume_ema(lsrc,length)
  72. longtrend = nz(ema(lsrc,length) - ema(lsrc,multiple*length))
  73. nocleartrend = nz(abs(longtrend)) <= 2*nz(sma(dev,multiple*length),1)
  74.  
  75.  
  76. rn = rsinorm(src,rsi_length)
  77. overbought = false
  78. overbought := (overbought[1] or crossover(rn,t)) and not crossunder(rn,r)
  79. oversold = false
  80. oversold := (oversold[1] or crossunder(rn,-t)) and not crossover(rn,-r)
  81. rsihit = oversold or overbought
  82.  
  83. rsi_hysteresis = ema(rsihit ? -rn : 0,hysteresislength) + sma(rsihit ? -rn : 0,hysteresislength)
  84.  
  85. hybrid_oscillator = mediumtrend + hybrid_parameter*rsi_hysteresis*nz(ema(abs(mediumtrend),5*length) + hybrid_trend*sma(rsi_hysteresis,2*hysteresislength)*shorttrend)
  86.  
  87. centerline_delta = basis - ema(basis,2)
  88. volatility_delta = dev - ema(dev,2)
  89.  
  90.  
  91. //centerline plot
  92. momentumcolor = oscillator_coloring(mediumtrend)
  93. rsicolor = abs(rsi_hysteresis) > 0.1 ? oscillator_coloring(rsi_hysteresis) : color.purple
  94. hybridcolor = oscillator_coloring(hybrid_oscillator)
  95. deltacolor = oscillator_coloring(centerline_delta)
  96. colorcenter = centerscheme == "Centerline change" ? deltacolor : centerscheme == "Momentum" ? momentumcolor : centerscheme == "RSI hysteresis" ? rsicolor : hybridcolor
  97. plot(exp(basis), title="Centerline", color= colorcenter)
  98. // band plot
  99. markettypecolor = nocleartrend ? color.blue : oscillator_coloring(longtrend)
  100. volatilitycolor = volatility_delta > 0 ? color.yellow : color.maroon
  101. bandcolor = bandscheme == "Centerline" ? colorcenter : bandscheme == "Volatility Change" ? volatilitycolor : bandscheme == "Market type" ? markettypecolor : color.teal
  102.  
  103. p1 = plot(exp(upper), title="Upper band", color= bandcolor)
  104. p2 = plot(exp(lower), title="Lower band", color= bandcolor)
  105. fill(p1, p2, title="Band fill", color= bandcolor,transp=92)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement