Sie sind auf Seite 1von 2

Linear Regression Formula

Scraped Calculation With Alerts

Here is the Linear Regression Script For traders Who love rich features

++ Multi time frame -> Source Regression from a different Chart
++ Customized Colors -> This includes the pine lines
++ Smoothing -> Allow Filtered Regression; Note: Using 1 Defaults to the original
line. The default is 1
++ Alerts On Channel/Range Crossing

++ Use this for BreakOuts and Reversals
++ This Script is not to be used Independently

Please note, this script is the likes of Bollinger bands and poses a risk of
falling in a trend range.
Signals may Keep running on the same direction while the market is reversing.

If you have any feature requests, comment below or dm me. I will answer when i can.
Feel free to utilize this on your chart and share your ideas

For developers who want to use this on their chart, Please use this script
The original formula for calculation is posted there

// This source code is subject to the terms of the Mozilla Public License 2.0 at
// � LucemAnb
// User Version

study("Linear Regression ++", "Lin Reg ++", overlay=true)

source = input(close)
length = input(100, minval=1)
offset = input(0, minval=0)
dev = input(2.0, "Deviation")
smoothing = input(1, minval=1)
mtf_val = input("", "Resolution", input.resolution)
line_thick = input(4, "Line Thickness", input.integer, minval=1, maxval=4)
p = input("Lime", "Up Color", options=["Red", "Lime", "Orange", "Teal",
"Yellow", "White", "Black"])
q = input("Red", "Down Color", options=["Red", "Lime", "Orange", "Teal",
"Yellow", "White", "Black"])

cc(x) => x=="Red"?"Lime"?color.lime:x=="Orange"?"Teal"?
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))

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
line b =, y1, x2, y2, xloc.bar_index, extend.right, color.aqua,
line dp =, deviation*dev + y1, x2, deviation*dev + y2, xloc.bar_index,
extend.right, cc(q), width=line_thick)
line dm =, -deviation*dev + y1, x2, -deviation*dev + y2,
xloc.bar_index, extend.right, cc(p), width=line_thick)

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',, show_last=length)
plotshape(sell, "SELL", shape.labeldown, location.abovebar,, text='SELL',
textcolor=color.white, show_last=length)