coure.2011
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: October 8th, 2023

Regression Channel is drawn too widely

I m trying to create an indicator to draw regression channel based on starting and ending candle index. But its drawing the channel too much wider vertically. I want to make it wider as same as of tradingview's regression channel tool. See the screenshot where The gray lines are from my custom indicator. I am not sure what I m doing wrong.

Image

Here is the code

Code: Select all

//@version=4
study("Linear Regression Channel PlayGround",shorttitle="LRC", overlay=true)
//==== REGRESSION SLOPE
src = input(close, title="Source")
start_candle = input(218, title="Start Candle", minval=0) // the index of the start candle from the last
distance_candles = input(122, title="Distance", minval=0) // the index of the start candle from the last
end_candle = start_candle + distance_candles // input(0 + 89, title="End Candle", minval=0) // the index of the end candle from the last
dev = input(2.0, title="Deviation",step=0.1)
ext=input(false,title="Extend Lines")
ln=ext?extend.right:extend.none
//-- MEAN
calcSlope(src, start_candle, end_candle) =>
    if not barstate.islast
        [float(na), float(na), float(na)]
    else
        len = end_candle - start_candle + 1 // the length of the channel
        sumX = 0.0
        sumY = 0.0
        sumXSqr = 0.0
        sumXY = 0.0
        for i = start_candle to end_candle // loop over the candles in the range
            val = src[i]
            per = i + 1.0 - start_candle // adjust the period to start from 1
            sumX := sumX + per
            sumY := sumY + val
            sumXSqr := sumXSqr + per * per
            sumXY := sumXY + val * per
        slope = (len * sumXY - sumX * sumY) / (len * sumXSqr - sumX * sumX)
        average = sumY / len
        intercept = average - slope * sumX / len + slope
        [slope, average, intercept]
[s, a, i] = calcSlope(src, start_candle, end_candle)
 
//deviation code by jwammo12
lrc = linreg(src, end_candle - start_candle + 1, start_candle)
lrc1 = linreg(src,end_candle - start_candle + 1,start_candle + 1)
lrSlope = (lrc-lrc1)
lrIntercept = lrc - (bar_index - start_candle)*lrSlope
deviationSum = 0.0
for z=start_candle to end_candle
    deviationSum:= deviationSum + pow(src[z]-(lrSlope*(bar_index-z)+lrIntercept), 2)
deviation = sqrt(deviationSum/(end_candle - start_candle + 1))

up = deviation * dev //+lrc
dn = deviation * dev

startPrice = (i + s * (end_candle - start_candle))
endPrice = i

 
///////////////////
var line baseLine = na
if na(baseLine)
    baseLine := line.new(bar_index - end_candle, startPrice, bar_index - start_candle, endPrice, width=2, color=color.silver, style=line.style_solid, extend=ln)
else
    line.set_xy1(baseLine, bar_index - end_candle, startPrice)
    line.set_xy2(baseLine, bar_index - start_candle, endPrice)
    na // 
 
 
//////////High Channel//////////////
var line baseLine1 = na
if na(baseLine1)
    baseLine1 := line.new(bar_index - end_candle, startPrice + up, bar_index - start_candle, endPrice + up, width=2, color=color.silver, style=line.style_solid, extend=ln)
else
    line.set_xy1(baseLine1, bar_index - end_candle, startPrice + up)
    line.set_xy2(baseLine1, bar_index - start_candle, endPrice + up)
    na // 
 
////////////////Low Channel///////////////////
var line baseLine2 = na
if na(baseLine2)
    baseLine2 := line.new(bar_index - end_candle, startPrice-dn, bar_index - start_candle, endPrice-dn, width=2, color=color.silver, style=line.style_solid, extend=ln)
else
    line.set_xy1(baseLine2, bar_index - end_candle, startPrice- dn)
    line.set_xy2(baseLine2, bar_index - start_candle, endPrice- dn)
    na // 

Return to “Pine Script Q&A”