Video Blog Post

Trading Insights through Code: Linear Regression in Pine Script

Display Pine Script Code



    //@version=4
    study("Invite Straton Automations Linear Regression", overlay=true)


    source      = input(close)
    length      = input(100, minval=1)
    offset      = input(0, minval=0)
    dev         = input(0.5, "Deviation")
    smoothing   = input(1, minval=1)
    mtf_val     = input("", "Resolution", input.resolution)
    line_thick  = input(1, "S&R Thickness", minval=1, maxval=2)
    signals     = input("All", "Signals Display", options=["Recent", "All"])
    p           = input("Red", "Up Color", options=["Red", "Lime", "Orange", "Teal", "Yellow", "White", "Black"])
    q           = input("Red", "Down Color", options=["Red", "Lime", "Orange", "Teal", "Yellow", "White", "Black"])
    goto        = input(0, "End At Bar Index")

    cc(x) => x=="Red"?color.red:x=="Lime"?color.lime:x=="Orange"?color.orange:x=="Teal"?
    color.teal:x=="Yellow"?color.yellow:x=="Black"?color.black:color.white
    data(x) => sma(security(syminfo.tickerid, mtf_val!="" ? mtf_val : timeframe.period, x), smoothing)

    linreg = data(linreg(source, length, offset))
    linreg_p = data(linreg(source, length, offset+1))
    plot(linreg, "Regression Line", cc(linreg>linreg[1]?p:q), editable=false)

    x = bar_index
    slope = linreg - linreg_p
    intercept = linreg - x*slope
    deviationSum = 0.0
    for i=0 to length-1
    deviationSum:= deviationSum + pow(source[i]-(slope*(x-i)+intercept), 2)
    deviation = sqrt(deviationSum/(length))
    x1 = x-length
    x2 = x
    y1 = slope*(x-length)+intercept
    y2 = linreg

    updating = goto <= 0 or x < goto

    if updating
    line b = line.new(x1, y1, x2, y2, xloc.bar_index, extend.right, color.aqua, width=line_thick)
    line.delete(b[1])
    line dp = line.new(x1, deviation*dev + y1, x2, deviation*dev + y2, xloc.bar_index, extend.right, cc(q), width=line_thick)
    line.delete(dp[1])
    line dm = line.new(x1, -deviation*dev + y1, x2, -deviation*dev + y2, xloc.bar_index, extend.right, cc(p), width=line_thick)
    line.delete(dm[1])

    dm_current = -deviation*dev + y2
    dp_current = deviation*dev + y2
    buy = crossunder(close, dm_current)
    sell = crossover(close, dp_current)
    alertcondition(buy, "Buy Lin Reg", "Crossing On the Lower Regression Channel")
    alertcondition(sell, "Sell Lin Reg", "Crossing On the Higher Regression Channel")

    //plotshape(buy, "BUY", shape.labelup, location.belowbar, color.lime, text='BUY', textcolor=color.black, show_last=signals=="All"?99999999:length)
    //plotshape(sell, "SELL", shape.labeldown, location.abovebar, color.red, text='SELL', textcolor=color.white, show_last=signals=="All"?99999999:length)

    plot(x, "Bar Index", color.aqua, line_thick, plot.style_cross, display=display.none)




    //////////////////////////////////////////////////////////////////////////////////////////////////////




    source1      = input(close)
    length1      = input(100, minval=1)
    offset1      = input(0, minval=0)
    dev1         = input(1.5, "Deviation")
    smoothing1   = input(1, minval=1)
    mtf_val1     = input("", "Resolution", input.resolution)
    line_thick1  = input(1, "S&R Thickness", minval=1, maxval=2)
    signals1     = input("Recent", "Signals Display", options=["Recent", "All"])
    p1           = input("Yellow", "Up Color", options=["Red", "Lime", "Orange", "Teal", "Yellow", "White", "Black"])
    q1           = input("Yellow", "Down Color", options=["Red", "Lime", "Orange", "Teal", "Yellow", "White", "Black"])
    goto1        = input(0, "End At Bar Index")

    cc1(x0) => x0=="Red"?color.red:x0=="Lime"?color.lime:x0=="Orange"?color.orange:x0=="Teal"?
    color.teal:x0=="Yellow"?color.yellow:x0=="Black"?color.black:color.white
    data1(x0) => sma(security(syminfo.tickerid, mtf_val1!="" ? mtf_val1 : timeframe.period, x0), smoothing1)

    linreg1 = data1(linreg(source1, length1, offset1))
    linreg_p1 = data1(linreg(source1, length1, offset1+1))
    plot(linreg1, "Regression Line", cc1(linreg1>linreg1[1]?p1:q1), editable=false)

    x0 = bar_index
    slope1 = linreg1 - linreg_p1
    intercept1 = linreg1 - x0*slope1
    deviationSum1 = 0.0
    for i=0 to length1-1
    deviationSum1:= deviationSum1 + pow(source1[i]-(slope1*(x0-i)+intercept1), 2)
    deviation1 = sqrt(deviationSum1/(length1))
    x3 = x0-length1
    x4 = x0
    y3 = slope1*(x-length)+intercept1
    y4 = linreg

    updating1 = goto1 <= 0 or x0 < goto1

    if updating1
    line b1 = line.new(x3, y3, x4, y4, xloc.bar_index, extend.right, color.aqua, width=line_thick1)
    line.delete(b1[1])
    line dp1 = line.new(x3, deviation1*dev1 + y3, x4, deviation1*dev1 + y4, xloc.bar_index, extend.right, cc1(q1), width=line_thick1)
    line.delete(dp1[1])
    line dm1 = line.new(x3, -deviation1*dev1 + y3, x4, -deviation1*dev1 + y4, xloc.bar_index, extend.right, cc1(p1), width=line_thick1)
    line.delete(dm1[1])

    dm_current1 = -deviation1*dev1 + y4
    dp_current1 = deviation1*dev1 + y4
    buy1 = crossunder(close, dm_current1)
    sell1 = crossover(close, dp_current1)
    alertcondition(buy1, "Buy Lin Reg", "Crossing On the Lower Regression Channel")
    alertcondition(sell1, "Sell Lin Reg", "Crossing On the Higher Regression Channel")

    //plotshape(buy, "BUY", shape.labelup, location.belowbar, color.lime, text='BUY', textcolor=color.black, show_last=signals=="All"?99999999:length)
    //plotshape(sell, "SELL", shape.labeldown, location.abovebar, color.red, text='SELL', textcolor=color.white, show_last=signals=="All"?99999999:length)

    plot(x0, "Bar Index", color.aqua, line_thick1, plot.style_cross, display=display.none)





    //////////////////////////////////////////////////////////////////////////////////////////////////////




    source2      = input(close)
    length2      = input(100, minval=1)
    offset2      = input(0, minval=0)
    dev2         = input(2.5, "Deviation")
    smoothing2   = input(1, minval=1)
    mtf_val2     = input("", "Resolution", input.resolution)
    line_thick2  = input(1, "S&R Thickness", minval=1, maxval=2)
    signals2     = input("Recent", "Signals Display", options=["Recent", "All"])
    p2           = input("Lime", "Up Color", options=["Red", "Lime", "Orange", "Teal", "Yellow", "White", "Black"])
    q2           = input("Red", "Down Color", options=["Red", "Lime", "Orange", "Teal", "Yellow", "White", "Black"])
    goto2        = input(0, "End At Bar Index")

    cc2(x00) => x00=="Red"?color.red:x00=="Lime"?color.lime:x00=="Orange"?color.orange:x00=="Teal"?
    color.teal:x00=="Yellow"?color.yellow:x00=="Black"?color.black:color.white
    data2(x00) => sma(security(syminfo.tickerid, mtf_val2!="" ? mtf_val2 : timeframe.period, x00), smoothing2)

    linreg2 = data2(linreg(source2, length2, offset2))
    linreg_p2 = data2(linreg(source2, length2, offset2+1))
    plot(linreg2, "Regression Line", cc2(linreg2>linreg2[1]?p2:q2), editable=false)

    x00 = bar_index
    slope2 = linreg2 - linreg_p2
    intercept2 = linreg2 - x00*slope2
    deviationSum2 = 0.0
    for i=0 to length2-1
    deviationSum2:= deviationSum2 + pow(source2[i]-(slope2*(x00-i)+intercept2), 2)
    deviation2 = sqrt(deviationSum2/(length2))
    x6 = x00-length2
    x8 = x00
    y6 = slope2*(x00-length2)+intercept2
    y8 = linreg2

    updating2 = goto2 <= 0 or x00 < goto2

    if updating2
    line b2 = line.new(x6, y6, x8, y8, xloc.bar_index, extend.right, color.blue, width=line_thick2)
    line.delete(b2[1])
    line dp2 = line.new(x6, deviation2*dev2 + y6, x8, deviation2*dev2 + y8, xloc.bar_index, extend.right, cc2(q2), width=line_thick2)
    line.delete(dp2[1])
    line dm2 = line.new(x6, -deviation2*dev2 + y6, x8, -deviation2*dev2 + y8, xloc.bar_index, extend.right, cc2(p2), width=line_thick2)
    line.delete(dm2[1])

    dm_current2 = -deviation2*dev2 + y8
    dp_current2 = deviation2*dev2 + y8
    buy2 = crossover(close, dm_current2)
    sell2 = crossunder(close, dp_current2)

    alertcondition(buy2, "Buy Lin Reg", "Crossing On the Lower Regression Channel")
    alertcondition(sell2, "Sell Lin Reg", "Crossing On the Higher Regression Channel")

    plotshape(buy2, "BUY", shape.labelup, location.belowbar, color.green, text='OS', textcolor=color.white, show_last=signals=="All"?99999999:length)
    plotshape(sell2, "SELL", shape.labeldown, location.abovebar, color.red, text='OB', textcolor=color.white, show_last=signals=="All"?99999999:length)

    plot(x00, "Bar Index", color.aqua, line_thick2, plot.style_cross, display=display.none)