• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Jul 10th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. //@version=4
2. study(title="Squeeze Box [DW]", overlay=true, linktoseries=true)
3. //by Donovan Wall
4.
5. //This is an experimental study designed using data from Bollinger Bands to determine price squeeze ranges and active levels of support and resistance.
6. //First, a set of Bollinger Bands using a Coefficient of Variation weighted moving average as the basis is calculated.
7. //Then, the relative percentage of current bandwidth to maximum bandwidth over the specified sampling period determines the relative squeeze.
8. //The box is outlined by drawing the current highest and lowest source value over the sampling period whenever a squeeze is active.
9. //I've included the COVWMA in the visualization for additional confirmation of price activity.
10.
11. //Custom Bar color scheme is included.
12.
13. //---------------------------------------------------------------------------------------------------------------------------------------------------------------
15. //Added alternate moving average types. The available moving averages now in this script are:
16. //  -Exponential Moving Average
17. //  -Simple Moving Average
18. //  -Smoothed Moving Average
19. //  -Weighted Moving Average
20. //  -Volume Weighted Moving Average
21. //  -Least Squares Moving Average
22. //  -Arnaud Legoux Moving Average
23. //  -Hull Moving Average
24. //  -Coefficient of Variation Weighted Moving Average
25. //  -Fractal Adaptive Moving Average
26. //  -Kaufman's Adaptive Moving Average
27.
28. //Refined the color scheme for both bar colors and the moving averages.
29.
30. //Please note: Each moving average type generates different box values, so play around with them and find the one that works best for you.
31.
32. //-----------------------------------------------------------------------------------------------------------------------------------------------------------------
33. //Inputs
34. //-----------------------------------------------------------------------------------------------------------------------------------------------------------------
35.
36. //Source
37. src = input(defval=hlc3, title="Source")
38.
39. //Periods
40. per = input(defval=21, minval=1, title="Sampling Period")
41.
42. //MA Type
43. matype = input(defval="COVWMA", title="MA Type (Available Inputs: 'EMA', 'SMA', 'SMMA', 'WMA', 'VWMA', 'LSMA', 'ALMA', 'HULLMA', 'COVWMA', 'FRAMA', 'KAMA')")
44.
45. //Standard Deviations
46. ndev = input(defval=2, minval=1, title="Number of Deviations for Squeeze Calculation")
47.
48. //Relative Squeeze Threshold
49. sr = input(defval=50, minval=0, maxval=100, step=0.01, title="Relative Squeeze % Threshold")
50.
51. //LSMA Offset
52. loff = input(defval=0, minval=0, title="Offset (if LSMA)")
53.
54. //ALMA Offset and Sigma
55. aoff  = input(defval=0.85, step=0.01, minval=0, title="Offset (if ALMA)")
56. sigma = input(defval=6,               minval=0, title="Sigma (if ALMA)")
57.
58. //FRAMA Coefficient
59. w = input(defval=-4.6, title="Coefficient (if FRAMA)")
60.
61. //KAMA Smoothing Constant
62. fast = input(defval=0.666,  step=0.001,  title="Smoothing Constant Fast End (if KAMA)")
63. slow = input(defval=0.0645, step=0.0001, title="Smoothing Constant Slow End (if KAMA)")
64.
65. //-----------------------------------------------------------------------------------------------------------------------------------------------------------------
66. //Definitions
67. //-----------------------------------------------------------------------------------------------------------------------------------------------------------------
68.
69. //EMA
70. ema = ema(src, per)
71.
72. //SMA
73. sma = sma(src, per)
74.
75. //SMMA
76. smma = 0.0
77. smma := na(smma[1]) ? sma(src, per) : (smma[1]*(per - 1) + src)/per
78.
79. //WMA
80. wma = wma(src, per)
81.
82. //VWMA
83. vmp  = src*volume
84. vwma = sum(vmp, per)/sum(volume, per)
85.
86. //LSMA
87. lsma  = linreg(src, per, loff)
88.
89. //ALMA
90. alma = alma(src, per, aoff, sigma)
91.
92. //Hull MA
93. hullma = wma(2*wma(src, per/2)-wma(src, per), round(sqrt(per)))
94.
95. //COVWMA
96. covwma(a, b) =>
97.     cov    = stdev(a, b)/sma(a, b)
98.     cw     = a*cov
99.     covwma = sum(cw, b)/sum(cov, b)
100. cma = covwma(src, per)
101.
102. //FRAMA
103. frama(a, b) =>
104.     n3    = (highest(high, b) - lowest(low, b))/b
105.     hd2   = highest(high, b/2)
106.     ld2   = lowest(low, b/2)
107.     n2    = (hd2 - ld2)/(b/2)
108.     n1    = (hd2[b/2] - ld2[b/2])/(b/2)
109.     dim   = (n1 > 0) and (n2 > 0) and (n3 > 0) ? (log(n1 + n2) - log(n3))/log(2) : 0
110.     alpha = exp(w*(dim - 1))
111.     sc    = (alpha < 0.01 ? 0.01 : (alpha > 1 ? 1 : alpha))
112.     frama = 0.0
113.     frama := cum(1)<=2*b ? a : (a*sc) + nz(frama[1])*(1 - sc)
114. fma = frama(src, per)
115.
116. //KAMA
117. kama(a, b)=>
118.     dist   = abs(a[0] - a[1])
119.     signal = abs(a - a[b])
120.     noise  = sum(dist, b)
121.     effr   = noise!=0 ? signal/noise : 1
122.     sc     = pow(effr*(fast - slow) + slow,2)
123.     kama   = 0.0
124.     kama   := nz(kama[1], a) + sc*(a - nz(kama[1], a))
125. kma = kama(src, per)
126.
127. //MA Type
128. ma = (matype=="EMA") ? ema : (matype=="SMA") ? sma : (matype=="SMMA") ? smma : (matype=="WMA") ? wma : (matype=="VWMA") ? vwma : (matype=="LSMA") ? lsma : (matype=="ALMA") ? alma : (matype=="HULLMA") ? hullma : (matype=="COVWMA") ? cma : (matype=="FRAMA") ? fma : (matype=="KAMA") ? kma : ema
129.
130. //Bollinger Bands
131. bu = ma + stdev(src, per)*ndev
132. bd = ma - stdev(src, per)*ndev
133.
134. //Bandwidth
135. bw = (bu - bd)
136.
137. //Squeeze Percentage
138. buh  = highest(bu, per)
139. bdl  = lowest(bd, per)
140. brng = buh - bdl
141. sqp  = 100*bw/brng
142.
143. //Squeeze Box
144. sqz  = (sqp < sr) ? 1 : na
145. highest = highest(src, per)
146. lowest = lowest(src, per)
147. boxh = sqz ? highest : na
148. boxl = sqz ? lowest : na
149. bh = valuewhen(sqz, boxh, 1)
150. bl = valuewhen(sqz, boxl, 1)
151.
152. //Colors
153. boxcolor = src > bh ? color.lime : src < bl ? color.red : color.orange
154. bhcolor  = sqz ? boxcolor : na
155. blcolor  = sqz ? boxcolor : na
156. barcolor = (src > bh) and (src > src[1]) ? color.lime : (src > bh) and (src < src[1]) ? color.green : (src < bl) and (src < src[1]) ? color.red : (src < bl) and (src > src[1]) ? color.maroon : color.orange
157. macolor = (ma > ma[1]) ? color.lime : (ma < ma[1]) ? color.red : color.orange
158.
159. //-----------------------------------------------------------------------------------------------------------------------------------------------------------------
160. //Plots
161. //-----------------------------------------------------------------------------------------------------------------------------------------------------------------
162.
163. //Squeeze Box
164. bhplot = plot(bh, color=boxcolor, title="Box High Level (Color 0 = Box is an Active Support Zone, Color 1 = Box is an Active Resistance Zone, Color 2 = Price is Within Box)")
165. blplot = plot(bl, color=boxcolor, title="Box Low Level (Color 0 = Box is an Active Support Zone, Color 1 = Box is an Active Resistance Zone, Color 2 = Price is Within Box)")
166.
167. //COVWMA
168. maplot = plot(ma, color=macolor, linewidth=2, title="MA (Color 0 = Uptrend, Color 1 = Downtrend, Color 2 = Neutral)")
169.
170. //Fill
171. fill(bhplot, blplot, color=boxcolor, transp=70, title="Box Fill (Same Color Scheme Calculation as Box High and Low)")
172.
173. //Bar Color
174. barcolor(barcolor, title="Bar Color (Color 0 = Uptrend, Color 1 = Pullback From Uptrend, Color 2 = Downtrend, Color 3 = Pullback From Downtrend, Color 4 = Within Box)")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top