Just a modification of my old auto-line script, lot of errors fixed and a correction in the standard deviation. Also changed the tittle because "auto-line" was really confusing.
Check out the indicators we are making at luxalgo: www.tradingview.com/u/LuxAlgo/