Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
- //# * ══════════════════════════════════════════════════════════════════════════════════════════════
- //# *
- //# * Study : Linear Regression Channel / Curve / Slope
- //# * - Volume / Volatility AddOns
- //# * Author : © dgtrd
- //# *
- //# * Revision History
- //# * Release : Apr 19, 2021
- //# * Update : Apr 22, 2021 : Added option to plot second Linear Regression Channel
- //# *
- //# * ══════════════════════════════════════════════════════════════════════════════════════════════
- // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
- study("Linear Regression Channel / Curve / Slope by DGT", "LinReg ʙʏ DGT ☼☾", true)
- // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
- //
- // Linear Regression Channel / Curve / Slope
- // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
- // ---------------------------------------------------------------------------------------------- //
- // -Inputs -------------------------------------------------------------------------------------- //
- tooltip_linreg = "Linear Regression Channel" +
- "\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"
- group_linReg = "Linear Regression Channel Settings"
- i_linreg = input(true , "LinReg1 ⇨ Count" , inline = "LinReg1" , group = group_linReg, tooltip = tooltip_linreg)
- i_linregLength = input(200 , "", minval = 2, maxval = 300, inline = "LinReg1" , group = group_linReg)
- i_linregSrc = input(close, " Source" , inline = "LinReg1" , group = group_linReg)
- i_linreg2 = input(true , "LinReg2 ⇨ Count" , inline = "LinReg2" , group = group_linReg)
- i_linregLength2 = input(50 , "", minval = 2, maxval = 300, inline = "LinReg2" , group = group_linReg)
- i_linregSrc2 = input(close, " Source" , inline = "LinReg2" , group = group_linReg)
- i_extendLines = input(false, "Extend Lines " , inline = "Lines" , group = group_linReg)
- i_channelLevels = input(true , "Level Labels " , inline = "Lines" , group = group_linReg)
- i_trendColor = input(false, "Use Trend Colors" , inline = "Lines" , group = group_linReg)
- // ---------------------------------------------------------------------------------------------- //
- // -Calculations -------------------------------------------------------------------------------- //
- // ---------------------------------------------------------------------------------------------- //
- // Linear Regression Channel / Curve (Bands) / Slope
- f_calcSlope(_source, _length) =>
- max_bars_back(_source, 300)
- if not barstate.islast
- [float(na), float(na)]
- else
- sumX = 0.0
- sumY = 0.0
- sumXSqr = 0.0
- sumXY = 0.0
- for i = 0 to _length - 1
- val = _source[i]
- per = i + 1.0
- sumX := sumX + per
- sumY := sumY + val
- sumXSqr := sumXSqr + per * per
- sumXY := sumXY + val * per
- slope = (_length * sumXY - sumX * sumY) / (_length * sumXSqr - sumX * sumX)
- intercept = sumY / _length - slope * sumX / _length + slope
- [slope, intercept]
- f_calcDev(_source, _length, _slope, _intercept) =>
- stdDevAcc = 0.0
- periods = _length - 1
- val = _intercept
- for i = 0 to periods
- price = _source[i]
- price := price - val
- stdDevAcc := stdDevAcc + price * price
- val := val + _slope
- stdDev = sqrt(stdDevAcc / periods)
- [slope, intercept] = f_calcSlope(i_linregSrc, i_linregLength)
- stdDev = f_calcDev(i_linregSrc, i_linregLength, slope, intercept)
- startPrice = intercept + slope * (i_linregLength - 1)
- endPrice = intercept
- [slope2, intercept2] = f_calcSlope(i_linregSrc2, i_linregLength2)
- stdDev2 = f_calcDev(i_linregSrc2, i_linregLength2, slope2, intercept2)
- startPrice2 = intercept2 + slope2 * (i_linregLength2 - 1)
- endPrice2 = intercept2
- // ---------------------------------------------------------------------------------------------- //
- // -Plotting ------------------------------------------------------------------------------------ //
- f_crossingLevel(_curret, _level) =>
- (_level > _curret and _level < _curret[1]) or (_level < _curret and _level > _curret[1])
- f_drawLabel(_index, _price, _txt, _txtColor, _style, _align, _lrc, _tooltip) =>
- var id = label.new(_index, _price, _txt, textcolor=_txtColor, style=_style, textalign=_align, color=#00000000, tooltip=_tooltip, size=size.small)
- label.set_xy (id, _index, _price )
- label.set_textcolor(id, _txtColor)
- label.set_text (id, i_extendLines ? _txt + '\n\n' : _txt)
- label.set_tooltip (id, _tooltip)
- f_drawLine(_iStart, _pStart, _iEnd, _pEnd, _color, _width, _style, _extend) =>
- style = _style == "Solid" ? line.style_solid : _style == "Dotted" ? line.style_dotted : line.style_dashed
- var linReg = line.new(_iStart, _pStart, _iEnd, _pEnd, xloc.bar_index, _extend ? extend.right : extend.none, _color, style, _width)
- line.set_xy1 (linReg, _iStart, _pStart)
- line.set_xy2 (linReg, _iEnd , _pEnd )
- line.set_color(linReg, _color)
- processLinRegLevel(_show, _level, _colorL, _widthL, _styleL, _linReg, _which) =>
- if _show and _linReg
- pStart = _which ? startPrice : startPrice2
- pEnd = _which ? endPrice : endPrice2
- dev = _which ? stdDev : stdDev2
- length = _which ? i_linregLength : i_linregLength2
- lrColor = i_trendColor ? pStart > pEnd ? color.red : color.green : _colorL
- f_drawLine(bar_index - length + 1, pStart + _level * dev, bar_index, pEnd + _level * dev, lrColor, _widthL, _styleL, i_extendLines)
- f_drawLine(bar_index - length + 1, pStart - _level * dev, bar_index, pEnd - _level * dev, lrColor, _widthL, _styleL, i_extendLines)
- lbl = _which ? " 1ST" : " 2ND"
- if i_channelLevels
- 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) )
- 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) )
- if f_crossingLevel(close, pEnd + _level * dev)
- alert("LinRegChannel: " + syminfo.ticker + " crossing" + lbl + " Linear Regression Standart Deviation Upper Line " + tostring(_level) + " : " + tostring(pEnd + _level * dev))
- if f_crossingLevel(close, pEnd - _level * dev)
- alert("LinRegChannel: " + syminfo.ticker + " crossing" + lbl + " Linear Regression Standart Deviation Lower Line " + tostring(_level) + " : " + tostring(pEnd - _level * dev))
- i_lrcolor_m = input(#f44336, "Linear Regression Line 1 " , inline = "lrLine", group = group_linReg)
- i_lrstyle_m = input("Solid", "", options = ["Dashed", "Dotted", "Solid"], inline = "lrLine", group = group_linReg)
- i_lrwidth_m = input(3 , "", minval = 1 , inline = "lrLine", group = group_linReg)
- if i_linreg
- 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)
- if i_channelLevels
- 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) )
- if f_crossingLevel(close, endPrice)
- alert("LinRegChannel: " + syminfo.ticker + " crossing 1ST Linear Regression Base Line : " + tostring(endPrice))
- i_lrshow_1 = input(true , "StdDev" , inline = "lrLevel_1", group = group_linReg)
- i_lrvalue_1 = input(1. , "", minval = 0, step = .1 , inline = "lrLevel_1", group = group_linReg)
- i_lrcolor_1 = input(#2196f3 , "" , inline = "lrLevel_1", group = group_linReg)
- i_lrstyle_1 = input("Dashed", "", options = ["Dashed", "Dotted", "Solid"] , inline = "lrLevel_1", group = group_linReg)
- i_lrwidth_1 = input(1 , "", minval = 1 , inline = "lrLevel_1", group = group_linReg)
- processLinRegLevel(i_lrshow_1, i_lrvalue_1, i_lrcolor_1, i_lrwidth_1, i_lrstyle_1, i_linreg , true )
- i_lrshow_2 = input(true , "StdDev" , inline = "lrLevel_2", group = group_linReg)
- i_lrvalue_2 = input(2. , "", minval = 1, step = .1 , inline = "lrLevel_2", group = group_linReg)
- i_lrcolor_2 = input(#2196f3 , "" , inline = "lrLevel_2", group = group_linReg)
- i_lrstyle_2 = input("Solid" , "", options = ["Dashed", "Dotted", "Solid"] , inline = "lrLevel_2", group = group_linReg)
- i_lrwidth_2 = input(2 , "", minval = 1 , inline = "lrLevel_2", group = group_linReg)
- processLinRegLevel(i_lrshow_2, i_lrvalue_2, i_lrcolor_2, i_lrwidth_2, i_lrstyle_2, i_linreg , true )
- i_lrshow_3 = input(true , "StdDev" , inline = "lrLevel_3", group = group_linReg)
- i_lrvalue_3 = input(3. , "", minval = 2, step = .1 , inline = "lrLevel_3", group = group_linReg)
- i_lrcolor_3 = input(#2196f3 , "" , inline = "lrLevel_3", group = group_linReg)
- i_lrstyle_3 = input("Dotted", "", options = ["Dashed", "Dotted", "Solid"] , inline = "lrLevel_3", group = group_linReg)
- i_lrwidth_3 = input(2 , "", minval = 1 , inline = "lrLevel_3", group = group_linReg)
- processLinRegLevel(i_lrshow_3, i_lrvalue_3, i_lrcolor_3, i_lrwidth_3, i_lrstyle_3, i_linreg , true )
- i_2lrcolor_m = input(#ff9800, "Linear Regression Line 2 " , inline = "2lrLine", group = group_linReg)
- i_2lrstyle_m = input("Solid", "", options = ["Dashed", "Dotted", "Solid"], inline = "2lrLine", group = group_linReg)
- i_2lrwidth_m = input(3 , "", minval = 1 , inline = "2lrLine", group = group_linReg)
- if i_linreg2
- 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)
- if i_channelLevels
- 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) )
- if f_crossingLevel(close, endPrice2)
- alert("LinRegChannel: " + syminfo.ticker + " crossing 2ND Linear Regression Base Line : " + tostring(endPrice2))
- i_2lrshow_1 = input(true , "StdDev" , inline = "2lrLevel_1", group = group_linReg)
- i_2lrvalue_1 = input(1. , "", minval = 0, step = .1 , inline = "2lrLevel_1", group = group_linReg)
- i_2lrcolor_1 = input(#00bcd4 , "" , inline = "2lrLevel_1", group = group_linReg)
- i_2lrstyle_1 = input("Dashed", "", options = ["Dashed", "Dotted", "Solid"] , inline = "2lrLevel_1", group = group_linReg)
- i_2lrwidth_1 = input(1 , "", minval = 1 , inline = "2lrLevel_1", group = group_linReg)
- processLinRegLevel(i_2lrshow_1, i_2lrvalue_1, i_2lrcolor_1, i_2lrwidth_1, i_2lrstyle_1, i_linreg2, false)
- i_2lrshow_2 = input(true , "StdDev" , inline = "2lrLevel_2", group = group_linReg)
- i_2lrvalue_2 = input(2. , "", minval = 1, step = .1 , inline = "2lrLevel_2", group = group_linReg)
- i_2lrcolor_2 = input(#00bcd4 , "" , inline = "2lrLevel_2", group = group_linReg)
- i_2lrstyle_2 = input("Solid" , "", options = ["Dashed", "Dotted", "Solid"] , inline = "2lrLevel_2", group = group_linReg)
- i_2lrwidth_2 = input(2 , "", minval = 1 , inline = "2lrLevel_2", group = group_linReg)
- processLinRegLevel(i_2lrshow_2, i_2lrvalue_2, i_2lrcolor_2, i_2lrwidth_2, i_2lrstyle_2, i_linreg2, false)
- i_2lrshow_3 = input(true , "StdDev" , inline = "2lrLevel_3", group = group_linReg)
- i_2lrvalue_3 = input(3. , "", minval = 2, step = .1 , inline = "2lrLevel_3", group = group_linReg)
- i_2lrcolor_3 = input(#00bcd4 , "" , inline = "2lrLevel_3", group = group_linReg)
- i_2lrstyle_3 = input("Dotted", "", options = ["Dashed", "Dotted", "Solid"] , inline = "2lrLevel_3", group = group_linReg)
- i_2lrwidth_3 = input(2 , "", minval = 1 , inline = "2lrLevel_3", group = group_linReg)
- processLinRegLevel(i_2lrshow_3, i_2lrvalue_3, i_2lrcolor_3, i_2lrwidth_3, i_2lrstyle_3, i_linreg2, false)
- group_linRegC = "Linear Regression Curve Settings"
- i_showCurve = input(true , "Linear Regression Curve | Length" , inline = "Curve" , group = group_linRegC)
- i_linregCurveL = input(50 , "" , inline = "Curve" , group = group_linRegC)
- i_lrcolorCurve = input(#872323 , "" , inline = "Curve" , group = group_linRegC)
- i_showCurveBand = input(false , "Linear Regression Curve Bands " , inline = "Bands" , group = group_linRegC)
- i_lrcolorBand1 = input(#00897b , "1" , inline = "Bands" , group = group_linRegC)
- i_lrcolorBand2 = input(#00897b , "2" , inline = "Bands" , group = group_linRegC)
- i_lrcolorBand3 = input(#00897b , "3" , inline = "Bands" , group = group_linRegC)
- i_showCurveBTyp = input("StdDev", "Bands", options = [ "StdDev", "ATR"] , inline = "Bands2" , group = group_linRegC)
- i_linregCurveBL = input(21 , " StdDev" , inline = "Bands2" , group = group_linRegC)
- i_linregCurveAL = input(13 , "ATR" , inline = "Bands2" , group = group_linRegC)
- group_linRegS = "Linear Regression Slope Settings"
- i_showSlope = input(false , "Linear Regression Slope | Length" , inline = "Slope" , group = group_linRegS)
- i_linregSlopeL = input(5 , "" , inline = "Slope" , group = group_linRegS)
- i_lrcolorSlopeU = input(#06bad1 , "" , inline = "Slope" , group = group_linRegS)
- i_lrcolorSlopeD = input(#f57f17 , "" , inline = "Slope" , group = group_linRegS)
- dev = i_showCurveBTyp == "StdDev" ? stdev(i_linregSrc, i_linregCurveBL) : atr(i_linregCurveAL)
- lrc = linreg(i_linregSrc, i_linregCurveL, 0)
- plot(i_showCurve ? lrc : na, "LRC" , i_lrcolorCurve, 3, show_last = 3 * i_linregLength)
- plot(i_showCurve and i_showCurveBand ? lrc + 1 * dev : na, "LRC Band StdDev 1", i_lrcolorBand1, 1, show_last = 3 * i_linregLength)
- plot(i_showCurve and i_showCurveBand ? lrc - 1 * dev : na, "LRC Band StdDev 1", i_lrcolorBand1, 1, show_last = 3 * i_linregLength)
- plot(i_showCurve and i_showCurveBand ? lrc + 2 * dev : na, "LRC Band StdDev 2", i_lrcolorBand2, 1, show_last = 3 * i_linregLength)
- plot(i_showCurve and i_showCurveBand ? lrc - 2 * dev : na, "LRC Band StdDev 2", i_lrcolorBand2, 1, show_last = 3 * i_linregLength)
- plot(i_showCurve and i_showCurveBand ? lrc + 3 * dev : na, "LRC Band StdDev 3", i_lrcolorBand3, 1, show_last = 3 * i_linregLength)
- plot(i_showCurve and i_showCurveBand ? lrc - 3 * dev : na, "LRC Band StdDev 3", i_lrcolorBand3, 1, show_last = 3 * i_linregLength)
- 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)
- // Linear Regression Channel / Curve (Bands) / Slope
- // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
- // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
- //
- // Volume / Volatility AddOns
- // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
- // ---------------------------------------------------------------------------------------------- //
- // -Inputs -------------------------------------------------------------------------------------- //
- group_vol_vol = "Volume / Volatility AddOns"
- tooltip_volume_spike_sign_of_exhaustion = "Moments where\n" +
- "huge volume detected : current volume is grater than the product of the theshold value and volume moving average"
- tooltip_high_volatility = "Moments where\n" +
- "price range of the current bar is grater than the product of the theshold value and average true range value of defined period"
- tooltip_volume_weighted_colored_bars = "Volume Weighted Colored Bars\n" +
- "Colors bars based on the bar's volume relative to volume moving average"
- // ---------------------------------------------------------------------------------------------- //
- // High Volatility ------------------------------------------------------------------------------ //
- i_hATRLb = input(true , "⚡" , inline = "ATR" , group = group_vol_vol, tooltip = tooltip_high_volatility)
- i_weightedATR = atr(input(13 , "ATR : Length" , inline = "ATR" , group = group_vol_vol)) *
- input(2.718 , " Multiplier", minval = .1, step = .1 , inline = "ATR" , group = group_vol_vol)
- // ---------------------------------------------------------------------------------------------- //
- // Volume Moving Average : Base ----------------------------------------------------------------- //
- i_vSMA = sma(nz(volume), input(89, "Volume Moving Average Length", group = group_vol_vol))
- // ---------------------------------------------------------------------------------------------- //
- // Volume Spike - Sign of Exhaustion ------------------------------------------------------------ //
- i_vSpikeLb = input(true , "🚦" , inline = "SRS1", group = group_vol_vol, tooltip = tooltip_volume_spike_sign_of_exhaustion)
- i_vSpikeThresh = input(4.669, "Volume Spike Theshold ", minval = .1, step = .1, inline = "SRS1", group = group_vol_vol)
- // ---------------------------------------------------------------------------------------------- //
- // Volume Weighted Colored Bars ----------------------------------------------------------------- //
- i_vwcb = input(true , "" , inline = "VWC", group = group_vol_vol, tooltip = tooltip_volume_weighted_colored_bars)
- i_vwcbHighThresh = input(1.618, "VWCB Thesholds : High ", minval = 1., step = .1, inline = "VWC", group = group_vol_vol)
- i_vwcbLowThresh = input(0.618, " Low" , minval = .1, step = .1, inline = "VWC", group = group_vol_vol)
- // ---------------------------------------------------------------------------------------------- //
- // -Calculations and Plotting ------------------------------------------------------------------- //
- nzVolume = nz(volume)
- bullCandle = close > open
- bearCandle = close < open
- range = abs(high - low)
- // ---------------------------------------------------------------------------------------------- //
- // Volume Spike - Sign of Exhaustion ------------------------------------------------------------ //
- exhaustVol = nzVolume > i_vSpikeThresh * i_vSMA
- plotchar(i_vSpikeLb and nzVolume ? exhaustVol : na, "Exhaustion Bar", "🚦", location.abovebar, size=size.tiny, show_last = 3 * i_linregLength)
- 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}}")
- // ---------------------------------------------------------------------------------------------- //
- // High Volatility ------------------------------------------------------------------------------ //
- highVolatility = range > i_weightedATR
- plotchar(i_hATRLb ? highVolatility : na, "High Volatile Bar", "⚡", location.belowbar, size=size.tiny, show_last = 3 * i_linregLength)
- alertcondition(crossover(range, i_weightedATR), "High Volatility", "high volatility detected\n{{exchange}}:{{ticker}}->\nOpen = {{open}}, Current = {{close}},\nTime = {{time}}")
- // ---------------------------------------------------------------------------------------------- //
- // Volume Weighted Colored Bars by Kıvanç Özbilgiç ---------------------------------------------- //
- vwcbCol = nzVolume > i_vSMA * i_vwcbHighThresh ?
- bullCandle ? #006400 : #910000 :
- nzVolume < i_vSMA * i_vwcbLowThresh ?
- bearCandle ? #FF9800 : #7FFFD4 :
- na
- barcolor(i_vwcb and nzVolume ? vwcbCol : na, title = "Volume Weighted Colored Bars", show_last = 3 * i_linregLength)
- // Voloume / Volatility AddOns
- // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
Advertisement
Add Comment
Please, Sign In to add comment