xmd79

Linear Regression Channel / Curve / Slope by DGT

Apr 22nd, 2021
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.05 KB | None | 0 0
  1. //@version=4
  2. // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
  3. //# * ══════════════════════════════════════════════════════════════════════════════════════════════
  4. //# *
  5. //# * Study : Linear Regression Channel / Curve / Slope
  6. //# * - Volume / Volatility AddOns
  7. //# * Author : © dgtrd
  8. //# *
  9. //# * Revision History
  10. //# * Release : Apr 19, 2021
  11. //# * Update : Apr 22, 2021 : Added option to plot second Linear Regression Channel
  12. //# *
  13. //# * ══════════════════════════════════════════════════════════════════════════════════════════════
  14. // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
  15.  
  16. study("Linear Regression Channel / Curve / Slope by DGT", "LinReg ʙʏ DGT ☼☾", true)
  17.  
  18. // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
  19. //
  20. // Linear Regression Channel / Curve / Slope
  21. // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
  22.  
  23. // ---------------------------------------------------------------------------------------------- //
  24. // -Inputs -------------------------------------------------------------------------------------- //
  25.  
  26. tooltip_linreg = "Linear Regression Channel" +
  27. "\nBase Line : a line that best fits the prices specified over a user-defined time period (Count). It is calculated using the least squares method"
  28. group_linReg = "Linear Regression Channel Settings"
  29.  
  30. i_linreg = input(true , "LinReg1 ⇨ Count" , inline = "LinReg1" , group = group_linReg, tooltip = tooltip_linreg)
  31. i_linregLength = input(200 , "", minval = 2, maxval = 300, inline = "LinReg1" , group = group_linReg)
  32. i_linregSrc = input(close, "       Source" , inline = "LinReg1" , group = group_linReg)
  33. i_linreg2 = input(true , "LinReg2 ⇨ Count" , inline = "LinReg2" , group = group_linReg)
  34. i_linregLength2 = input(50 , "", minval = 2, maxval = 300, inline = "LinReg2" , group = group_linReg)
  35. i_linregSrc2 = input(close, "       Source" , inline = "LinReg2" , group = group_linReg)
  36. i_extendLines = input(false, "Extend Lines       " , inline = "Lines" , group = group_linReg)
  37. i_channelLevels = input(true , "Level Labels        " , inline = "Lines" , group = group_linReg)
  38. i_trendColor = input(false, "Use Trend Colors" , inline = "Lines" , group = group_linReg)
  39.  
  40. // ---------------------------------------------------------------------------------------------- //
  41. // -Calculations -------------------------------------------------------------------------------- //
  42.  
  43. // ---------------------------------------------------------------------------------------------- //
  44. // Linear Regression Channel / Curve (Bands) / Slope
  45.  
  46. f_calcSlope(_source, _length) =>
  47. max_bars_back(_source, 300)
  48.  
  49. if not barstate.islast
  50. [float(na), float(na)]
  51. else
  52. sumX = 0.0
  53. sumY = 0.0
  54. sumXSqr = 0.0
  55. sumXY = 0.0
  56.  
  57. for i = 0 to _length - 1
  58. val = _source[i]
  59. per = i + 1.0
  60. sumX := sumX + per
  61. sumY := sumY + val
  62. sumXSqr := sumXSqr + per * per
  63. sumXY := sumXY + val * per
  64.  
  65. slope = (_length * sumXY - sumX * sumY) / (_length * sumXSqr - sumX * sumX)
  66. intercept = sumY / _length - slope * sumX / _length + slope
  67.  
  68. [slope, intercept]
  69.  
  70. f_calcDev(_source, _length, _slope, _intercept) =>
  71. stdDevAcc = 0.0
  72. periods = _length - 1
  73. val = _intercept
  74.  
  75. for i = 0 to periods
  76. price = _source[i]
  77. price := price - val
  78. stdDevAcc := stdDevAcc + price * price
  79. val := val + _slope
  80.  
  81. stdDev = sqrt(stdDevAcc / periods)
  82.  
  83. [slope, intercept] = f_calcSlope(i_linregSrc, i_linregLength)
  84. stdDev = f_calcDev(i_linregSrc, i_linregLength, slope, intercept)
  85. startPrice = intercept + slope * (i_linregLength - 1)
  86. endPrice = intercept
  87.  
  88. [slope2, intercept2] = f_calcSlope(i_linregSrc2, i_linregLength2)
  89. stdDev2 = f_calcDev(i_linregSrc2, i_linregLength2, slope2, intercept2)
  90. startPrice2 = intercept2 + slope2 * (i_linregLength2 - 1)
  91. endPrice2 = intercept2
  92.  
  93. // ---------------------------------------------------------------------------------------------- //
  94. // -Plotting ------------------------------------------------------------------------------------ //
  95.  
  96. f_crossingLevel(_curret, _level) =>
  97. (_level > _curret and _level < _curret[1]) or (_level < _curret and _level > _curret[1])
  98.  
  99. f_drawLabel(_index, _price, _txt, _txtColor, _style, _align, _lrc, _tooltip) =>
  100. var id = label.new(_index, _price, _txt, textcolor=_txtColor, style=_style, textalign=_align, color=#00000000, tooltip=_tooltip, size=size.small)
  101. label.set_xy (id, _index, _price )
  102. label.set_textcolor(id, _txtColor)
  103. label.set_text (id, i_extendLines ? _txt + '\n\n' : _txt)
  104. label.set_tooltip (id, _tooltip)
  105.  
  106. f_drawLine(_iStart, _pStart, _iEnd, _pEnd, _color, _width, _style, _extend) =>
  107. style = _style == "Solid" ? line.style_solid : _style == "Dotted" ? line.style_dotted : line.style_dashed
  108.  
  109. var linReg = line.new(_iStart, _pStart, _iEnd, _pEnd, xloc.bar_index, _extend ? extend.right : extend.none, _color, style, _width)
  110. line.set_xy1 (linReg, _iStart, _pStart)
  111. line.set_xy2 (linReg, _iEnd , _pEnd )
  112. line.set_color(linReg, _color)
  113.  
  114. processLinRegLevel(_show, _level, _colorL, _widthL, _styleL, _linReg, _which) =>
  115. if _show and _linReg
  116. pStart = _which ? startPrice : startPrice2
  117. pEnd = _which ? endPrice : endPrice2
  118. dev = _which ? stdDev : stdDev2
  119. length = _which ? i_linregLength : i_linregLength2
  120. lrColor = i_trendColor ? pStart > pEnd ? color.red : color.green : _colorL
  121.  
  122. f_drawLine(bar_index - length + 1, pStart + _level * dev, bar_index, pEnd + _level * dev, lrColor, _widthL, _styleL, i_extendLines)
  123. f_drawLine(bar_index - length + 1, pStart - _level * dev, bar_index, pEnd - _level * dev, lrColor, _widthL, _styleL, i_extendLines)
  124.  
  125. lbl = _which ? " 1ST" : " 2ND"
  126.  
  127. if i_channelLevels
  128.  
  129. f_drawLabel(bar_index, pEnd + _level * dev, "SD " + tostring(_level) + " R" + lbl, lrColor, label.style_label_left, text.align_left, true, tostring(round((pEnd + _level * dev) / syminfo.mintick) * syminfo.mintick) )
  130. f_drawLabel(bar_index, pEnd - _level * dev, "SD " + tostring(_level) + " S" + lbl, lrColor, label.style_label_left, text.align_left, true, tostring(round((pEnd - _level * dev) / syminfo.mintick) * syminfo.mintick) )
  131.  
  132. if f_crossingLevel(close, pEnd + _level * dev)
  133. alert("LinRegChannel: " + syminfo.ticker + " crossing" + lbl + " Linear Regression Standart Deviation Upper Line " + tostring(_level) + " : " + tostring(pEnd + _level * dev))
  134.  
  135. if f_crossingLevel(close, pEnd - _level * dev)
  136. alert("LinRegChannel: " + syminfo.ticker + " crossing" + lbl + " Linear Regression Standart Deviation Lower Line " + tostring(_level) + " : " + tostring(pEnd - _level * dev))
  137.  
  138. i_lrcolor_m = input(#f44336, "Linear Regression Line 1      " , inline = "lrLine", group = group_linReg)
  139. i_lrstyle_m = input("Solid", "", options = ["Dashed", "Dotted", "Solid"], inline = "lrLine", group = group_linReg)
  140. i_lrwidth_m = input(3 , "", minval = 1 , inline = "lrLine", group = group_linReg)
  141.  
  142. if i_linreg
  143. f_drawLine(bar_index - i_linregLength + 1, startPrice, bar_index, endPrice, i_trendColor ? startPrice > endPrice ? color.red : color.green : i_lrcolor_m, i_lrwidth_m, i_lrstyle_m, i_extendLines)
  144.  
  145. if i_channelLevels
  146. f_drawLabel(bar_index, endPrice, "LRL 1ST", i_trendColor ? startPrice > endPrice ? color.red : color.green : i_lrcolor_m, label.style_label_left, text.align_left, true, tostring(round(endPrice / syminfo.mintick) * syminfo.mintick) )
  147.  
  148. if f_crossingLevel(close, endPrice)
  149. alert("LinRegChannel: " + syminfo.ticker + " crossing 1ST Linear Regression Base Line : " + tostring(endPrice))
  150.  
  151. i_lrshow_1 = input(true , "StdDev" , inline = "lrLevel_1", group = group_linReg)
  152. i_lrvalue_1 = input(1. , "", minval = 0, step = .1 , inline = "lrLevel_1", group = group_linReg)
  153. i_lrcolor_1 = input(#2196f3 , "" , inline = "lrLevel_1", group = group_linReg)
  154. i_lrstyle_1 = input("Dashed", "", options = ["Dashed", "Dotted", "Solid"] , inline = "lrLevel_1", group = group_linReg)
  155. i_lrwidth_1 = input(1 , "", minval = 1 , inline = "lrLevel_1", group = group_linReg)
  156. processLinRegLevel(i_lrshow_1, i_lrvalue_1, i_lrcolor_1, i_lrwidth_1, i_lrstyle_1, i_linreg , true )
  157.  
  158. i_lrshow_2 = input(true , "StdDev" , inline = "lrLevel_2", group = group_linReg)
  159. i_lrvalue_2 = input(2. , "", minval = 1, step = .1 , inline = "lrLevel_2", group = group_linReg)
  160. i_lrcolor_2 = input(#2196f3 , "" , inline = "lrLevel_2", group = group_linReg)
  161. i_lrstyle_2 = input("Solid" , "", options = ["Dashed", "Dotted", "Solid"] , inline = "lrLevel_2", group = group_linReg)
  162. i_lrwidth_2 = input(2 , "", minval = 1 , inline = "lrLevel_2", group = group_linReg)
  163. processLinRegLevel(i_lrshow_2, i_lrvalue_2, i_lrcolor_2, i_lrwidth_2, i_lrstyle_2, i_linreg , true )
  164.  
  165. i_lrshow_3 = input(true , "StdDev" , inline = "lrLevel_3", group = group_linReg)
  166. i_lrvalue_3 = input(3. , "", minval = 2, step = .1 , inline = "lrLevel_3", group = group_linReg)
  167. i_lrcolor_3 = input(#2196f3 , "" , inline = "lrLevel_3", group = group_linReg)
  168. i_lrstyle_3 = input("Dotted", "", options = ["Dashed", "Dotted", "Solid"] , inline = "lrLevel_3", group = group_linReg)
  169. i_lrwidth_3 = input(2 , "", minval = 1 , inline = "lrLevel_3", group = group_linReg)
  170. processLinRegLevel(i_lrshow_3, i_lrvalue_3, i_lrcolor_3, i_lrwidth_3, i_lrstyle_3, i_linreg , true )
  171.  
  172. i_2lrcolor_m = input(#ff9800, "Linear Regression Line 2      " , inline = "2lrLine", group = group_linReg)
  173. i_2lrstyle_m = input("Solid", "", options = ["Dashed", "Dotted", "Solid"], inline = "2lrLine", group = group_linReg)
  174. i_2lrwidth_m = input(3 , "", minval = 1 , inline = "2lrLine", group = group_linReg)
  175.  
  176. if i_linreg2
  177. f_drawLine(bar_index - i_linregLength2 + 1, startPrice2, bar_index, endPrice2, i_trendColor ? startPrice2 > endPrice2 ? color.red : color.green : i_2lrcolor_m, i_2lrwidth_m, i_2lrstyle_m, i_extendLines)
  178.  
  179. if i_channelLevels
  180. f_drawLabel(bar_index, endPrice2, "LRL 2ND", i_trendColor ? startPrice2 > endPrice2 ? color.red : color.green : i_2lrcolor_m, label.style_label_left, text.align_left, true, tostring(round(endPrice2 / syminfo.mintick) * syminfo.mintick) )
  181.  
  182. if f_crossingLevel(close, endPrice2)
  183. alert("LinRegChannel: " + syminfo.ticker + " crossing 2ND Linear Regression Base Line : " + tostring(endPrice2))
  184.  
  185. i_2lrshow_1 = input(true , "StdDev" , inline = "2lrLevel_1", group = group_linReg)
  186. i_2lrvalue_1 = input(1. , "", minval = 0, step = .1 , inline = "2lrLevel_1", group = group_linReg)
  187. i_2lrcolor_1 = input(#00bcd4 , "" , inline = "2lrLevel_1", group = group_linReg)
  188. i_2lrstyle_1 = input("Dashed", "", options = ["Dashed", "Dotted", "Solid"] , inline = "2lrLevel_1", group = group_linReg)
  189. i_2lrwidth_1 = input(1 , "", minval = 1 , inline = "2lrLevel_1", group = group_linReg)
  190. processLinRegLevel(i_2lrshow_1, i_2lrvalue_1, i_2lrcolor_1, i_2lrwidth_1, i_2lrstyle_1, i_linreg2, false)
  191.  
  192. i_2lrshow_2 = input(true , "StdDev" , inline = "2lrLevel_2", group = group_linReg)
  193. i_2lrvalue_2 = input(2. , "", minval = 1, step = .1 , inline = "2lrLevel_2", group = group_linReg)
  194. i_2lrcolor_2 = input(#00bcd4 , "" , inline = "2lrLevel_2", group = group_linReg)
  195. i_2lrstyle_2 = input("Solid" , "", options = ["Dashed", "Dotted", "Solid"] , inline = "2lrLevel_2", group = group_linReg)
  196. i_2lrwidth_2 = input(2 , "", minval = 1 , inline = "2lrLevel_2", group = group_linReg)
  197. processLinRegLevel(i_2lrshow_2, i_2lrvalue_2, i_2lrcolor_2, i_2lrwidth_2, i_2lrstyle_2, i_linreg2, false)
  198.  
  199. i_2lrshow_3 = input(true , "StdDev" , inline = "2lrLevel_3", group = group_linReg)
  200. i_2lrvalue_3 = input(3. , "", minval = 2, step = .1 , inline = "2lrLevel_3", group = group_linReg)
  201. i_2lrcolor_3 = input(#00bcd4 , "" , inline = "2lrLevel_3", group = group_linReg)
  202. i_2lrstyle_3 = input("Dotted", "", options = ["Dashed", "Dotted", "Solid"] , inline = "2lrLevel_3", group = group_linReg)
  203. i_2lrwidth_3 = input(2 , "", minval = 1 , inline = "2lrLevel_3", group = group_linReg)
  204. processLinRegLevel(i_2lrshow_3, i_2lrvalue_3, i_2lrcolor_3, i_2lrwidth_3, i_2lrstyle_3, i_linreg2, false)
  205.  
  206. group_linRegC = "Linear Regression Curve Settings"
  207.  
  208. i_showCurve = input(true , "Linear Regression Curve      |     Length" , inline = "Curve" , group = group_linRegC)
  209. i_linregCurveL = input(50 , "" , inline = "Curve" , group = group_linRegC)
  210. i_lrcolorCurve = input(#872323 , "" , inline = "Curve" , group = group_linRegC)
  211.  
  212. i_showCurveBand = input(false , "Linear Regression Curve Bands           " , inline = "Bands" , group = group_linRegC)
  213. i_lrcolorBand1 = input(#00897b , "1" , inline = "Bands" , group = group_linRegC)
  214. i_lrcolorBand2 = input(#00897b , "2" , inline = "Bands" , group = group_linRegC)
  215. i_lrcolorBand3 = input(#00897b , "3" , inline = "Bands" , group = group_linRegC)
  216. i_showCurveBTyp = input("StdDev", "Bands", options = [ "StdDev", "ATR"] , inline = "Bands2" , group = group_linRegC)
  217. i_linregCurveBL = input(21 , "   StdDev" , inline = "Bands2" , group = group_linRegC)
  218. i_linregCurveAL = input(13 , "ATR" , inline = "Bands2" , group = group_linRegC)
  219.  
  220. group_linRegS = "Linear Regression Slope Settings"
  221.  
  222. i_showSlope = input(false , "Linear Regression Slope  |  Length" , inline = "Slope" , group = group_linRegS)
  223. i_linregSlopeL = input(5 , "" , inline = "Slope" , group = group_linRegS)
  224. i_lrcolorSlopeU = input(#06bad1 , "" , inline = "Slope" , group = group_linRegS)
  225. i_lrcolorSlopeD = input(#f57f17 , "" , inline = "Slope" , group = group_linRegS)
  226.  
  227. dev = i_showCurveBTyp == "StdDev" ? stdev(i_linregSrc, i_linregCurveBL) : atr(i_linregCurveAL)
  228. lrc = linreg(i_linregSrc, i_linregCurveL, 0)
  229.  
  230. plot(i_showCurve ? lrc : na, "LRC" , i_lrcolorCurve, 3, show_last = 3 * i_linregLength)
  231. plot(i_showCurve and i_showCurveBand ? lrc + 1 * dev : na, "LRC Band StdDev 1", i_lrcolorBand1, 1, show_last = 3 * i_linregLength)
  232. plot(i_showCurve and i_showCurveBand ? lrc - 1 * dev : na, "LRC Band StdDev 1", i_lrcolorBand1, 1, show_last = 3 * i_linregLength)
  233. plot(i_showCurve and i_showCurveBand ? lrc + 2 * dev : na, "LRC Band StdDev 2", i_lrcolorBand2, 1, show_last = 3 * i_linregLength)
  234. plot(i_showCurve and i_showCurveBand ? lrc - 2 * dev : na, "LRC Band StdDev 2", i_lrcolorBand2, 1, show_last = 3 * i_linregLength)
  235. plot(i_showCurve and i_showCurveBand ? lrc + 3 * dev : na, "LRC Band StdDev 3", i_lrcolorBand3, 1, show_last = 3 * i_linregLength)
  236. plot(i_showCurve and i_showCurveBand ? lrc - 3 * dev : na, "LRC Band StdDev 3", i_lrcolorBand3, 1, show_last = 3 * i_linregLength)
  237.  
  238. plotarrow(i_showSlope ? correlation(close, bar_index, i_linregSlopeL) * stdev(close, i_linregSlopeL) / stdev(bar_index, i_linregSlopeL) : na, "Linear Regression Slope", i_lrcolorSlopeU, i_lrcolorSlopeD, 80, show_last = 3 * i_linregLength)
  239.  
  240. // Linear Regression Channel / Curve (Bands) / Slope
  241. // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
  242.  
  243. // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
  244. //
  245. // Volume / Volatility AddOns
  246. // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
  247.  
  248. // ---------------------------------------------------------------------------------------------- //
  249. // -Inputs -------------------------------------------------------------------------------------- //
  250.  
  251. group_vol_vol = "Volume / Volatility AddOns"
  252. tooltip_volume_spike_sign_of_exhaustion = "Moments where\n" +
  253. "huge volume detected : current volume is grater than the product of the theshold value and volume moving average"
  254.  
  255. tooltip_high_volatility = "Moments where\n" +
  256. "price range of the current bar is grater than the product of the theshold value and average true range value of defined period"
  257.  
  258. tooltip_volume_weighted_colored_bars = "Volume Weighted Colored Bars\n" +
  259. "Colors bars based on the bar's volume relative to volume moving average"
  260.  
  261. // ---------------------------------------------------------------------------------------------- //
  262. // High Volatility ------------------------------------------------------------------------------ //
  263.  
  264. i_hATRLb = input(true , "⚡" , inline = "ATR" , group = group_vol_vol, tooltip = tooltip_high_volatility)
  265. i_weightedATR = atr(input(13 , "ATR : Length" , inline = "ATR" , group = group_vol_vol)) *
  266. input(2.718 , "    Multiplier", minval = .1, step = .1 , inline = "ATR" , group = group_vol_vol)
  267.  
  268. // ---------------------------------------------------------------------------------------------- //
  269. // Volume Moving Average : Base ----------------------------------------------------------------- //
  270.  
  271. i_vSMA = sma(nz(volume), input(89, "Volume Moving Average Length", group = group_vol_vol))
  272.  
  273. // ---------------------------------------------------------------------------------------------- //
  274. // Volume Spike - Sign of Exhaustion ------------------------------------------------------------ //
  275.  
  276. i_vSpikeLb = input(true , "🚦" , inline = "SRS1", group = group_vol_vol, tooltip = tooltip_volume_spike_sign_of_exhaustion)
  277. i_vSpikeThresh = input(4.669, "Volume Spike Theshold                    ", minval = .1, step = .1, inline = "SRS1", group = group_vol_vol)
  278.  
  279. // ---------------------------------------------------------------------------------------------- //
  280. // Volume Weighted Colored Bars ----------------------------------------------------------------- //
  281.  
  282. i_vwcb = input(true , "" , inline = "VWC", group = group_vol_vol, tooltip = tooltip_volume_weighted_colored_bars)
  283. i_vwcbHighThresh = input(1.618, "VWCB Thesholds : High ", minval = 1., step = .1, inline = "VWC", group = group_vol_vol)
  284. i_vwcbLowThresh = input(0.618, "  Low" , minval = .1, step = .1, inline = "VWC", group = group_vol_vol)
  285.  
  286. // ---------------------------------------------------------------------------------------------- //
  287. // -Calculations and Plotting ------------------------------------------------------------------- //
  288.  
  289. nzVolume = nz(volume)
  290. bullCandle = close > open
  291. bearCandle = close < open
  292. range = abs(high - low)
  293.  
  294. // ---------------------------------------------------------------------------------------------- //
  295. // Volume Spike - Sign of Exhaustion ------------------------------------------------------------ //
  296.  
  297. exhaustVol = nzVolume > i_vSpikeThresh * i_vSMA
  298. plotchar(i_vSpikeLb and nzVolume ? exhaustVol : na, "Exhaustion Bar", "🚦", location.abovebar, size=size.tiny, show_last = 3 * i_linregLength)
  299. alertcondition(crossover(nzVolume, i_vSMA * i_vSpikeThresh), "Volume Spikes", "sign of exhaustion, huge volume increase detected\n{{exchange}}:{{ticker}}->\nOpen = {{open}}, Current = {{close}},\nTime = {{time}}")
  300.  
  301. // ---------------------------------------------------------------------------------------------- //
  302. // High Volatility ------------------------------------------------------------------------------ //
  303.  
  304. highVolatility = range > i_weightedATR
  305. plotchar(i_hATRLb ? highVolatility : na, "High Volatile Bar", "⚡", location.belowbar, size=size.tiny, show_last = 3 * i_linregLength)
  306. alertcondition(crossover(range, i_weightedATR), "High Volatility", "high volatility detected\n{{exchange}}:{{ticker}}->\nOpen = {{open}}, Current = {{close}},\nTime = {{time}}")
  307.  
  308. // ---------------------------------------------------------------------------------------------- //
  309. // Volume Weighted Colored Bars by Kıvanç Özbilgiç ---------------------------------------------- //
  310.  
  311. vwcbCol = nzVolume > i_vSMA * i_vwcbHighThresh ?
  312. bullCandle ? #006400 : #910000 :
  313. nzVolume < i_vSMA * i_vwcbLowThresh ?
  314. bearCandle ? #FF9800 : #7FFFD4 :
  315. na
  316.  
  317. barcolor(i_vwcb and nzVolume ? vwcbCol : na, title = "Volume Weighted Colored Bars", show_last = 3 * i_linregLength)
  318.  
  319. // Voloume / Volatility AddOns
  320. // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
Advertisement
Add Comment
Please, Sign In to add comment