Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Base source is cloned from built-in technicals - "Linear Regression Channel", v26
- // This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
- // ©Adulari
- // @version=5
- indicator("Dynamic Linear Regression Oscillator | Adulari", overlay=false,precision=2)
- // Inputs
- length = input.int(5,minval=1,title='Length',group='General Settings')
- smoothing = input.int(10,minval=2,title='Smoothing',group='General Settings')
- upperMultiplier = input.float(2.0,step=0.1, title="Upper Multiplier",group='General Settings')
- lowerMultiplier = input.float(2.0,step=0.1, title="Lower Multiplier",group='General Settings')
- maType = input.string('SMA',options=['SMA','EMA','RMA','WMA','VWMA','HMA'],title='MA Type',group='General Settings')
- smoothingType = input.string('SMA',options=['SMA','EMA','RMA','WMA','VWMA','HMA'],title='Smoothing Type',group='General Settings')
- signals = input.bool(false,title='Signals',tooltip='Show signals when value crosses below oversold line or crosses above overbought line.',group='General Settings')
- rescaleBarsBack = input.int(200,minval=20,title='Rescale Bars Back',group='Advanced Settings')
- // Functions
- rescale(float value, float oldMin, float oldMax, float newMin, float newMax) =>
- newMin + (newMax - newMin) * (value - oldMin) / math.max(oldMax - oldMin, 10e-10)
- ma(float source, simple int length, string maType) =>
- switch maType
- 'SMA' => ta.sma(source, length)
- 'EMA' => ta.ema(source, length)
- 'RMA' => ta.rma(source, length)
- 'WMA' => ta.wma(source, length)
- 'VWMA' => ta.vwma(source, length)
- 'HMA' => ta.hma(source, length)
- slope(source, length) =>
- max_bars_back(source, 5000)
- if barstate.isfirst or length <= 1
- [float(na), float(na), float(na)]
- else
- sumX = 0.0
- sumY = 0.0
- sumXSqr = 0.0
- sumXY = 0.0
- for i = 0 to length - 1 by 1
- val = source[i]
- per = i + 1.0
- sumX += per
- sumY += val
- sumXSqr += per * per
- sumXY += val * per
- slope = (length * sumXY - sumX * sumY) / (length * sumXSqr - sumX * sumX)
- average = sumY / length
- intercept = average - slope * sumX / length + slope
- [slope, average, intercept]
- var start_index = 1
- lengthInput = bar_index - start_index + 1
- inp = input(close)
- [s, a, i] = slope(inp, lengthInput)
- startPrice = i + s * (lengthInput - 1)
- endPrice = i
- deviation(source, length, slope, average, intercept) =>
- if barstate.isfirst or length <= 1
- [float(na), float(na), float(na), float(na)]
- else
- upDev = 0.0
- dnDev = 0.0
- stdDevAcc = 0.0
- dsxx = 0.0
- dsyy = 0.0
- dsxy = 0.0
- periods = length - 1
- daY = intercept + slope * periods / 2
- val = intercept
- for j = 0 to periods by 1
- price = high[j] - val
- if price > upDev
- upDev := price
- price := val - low[j]
- if price > dnDev
- dnDev := price
- price := source[j]
- dxt = price - average
- dyt = val - daY
- price -= val
- stdDevAcc += price * price
- dsxx += dxt * dxt
- dsyy += dyt * dyt
- dsxy += dxt * dyt
- val += slope
- stdDev = math.sqrt(stdDevAcc / (periods == 0 ? 1 : periods))
- pearsonR = dsxx == 0 or dsyy == 0 ? 0 : dsxy / math.sqrt(dsxx * dsyy)
- [stdDev, pearsonR, upDev, dnDev]
- [stdDev, pearsonR, upDev, dnDev] = deviation(inp, lengthInput, s, a, i)
- upperStartPrice = startPrice + upperMultiplier * stdDev
- upperEndPrice = endPrice + upperMultiplier * stdDev
- lowerStartPrice = startPrice - lowerMultiplier * stdDev
- lowerEndPrice = endPrice - lowerMultiplier * stdDev
- if (inp > upperEndPrice or inp < lowerEndPrice) and (not barstate.islast or barstate.isconfirmed)
- start_index := bar_index
- // Calculations
- value = ma(inp>startPrice ? high/startPrice : low/startPrice,length,maType)
- value := rescale(value,ta.lowest(value,rescaleBarsBack),ta.highest(value,rescaleBarsBack),0,100)
- signal = ma(value,smoothing,smoothingType)
- // Colors
- beColor = #675F76
- buColor = #a472ff
- // Plots
- pValue = plot(value,color=buColor,title='Value',linewidth=1)
- pSignal = plot(signal,color=beColor,title='Signal',linewidth=1)
- fill(pValue,pSignal,color=value>signal ? color.new(buColor,95) : color.new(beColor,95),title='Trend Fill')
- hline(80,title='Upper Line',color=beColor)
- hline(50,title='Middle Line',color=color.new(color.gray,50))
- hline(20,title='Lower Line',color=buColor)
- plotshape(signals ? ta.crossunder(value,20) ? 20 : na : na,'Bullish Signal',style=shape.circle,location=location.absolute,size=size.tiny,color=buColor)
- plotshape(signals ? ta.crossover(value,80) ? 80 : na : na,'Bearish Signal',style=shape.xcross,location=location.absolute,size=size.tiny,color=beColor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement