#### ebtrader

##### New member

Good morning all! Thanks for including me in this group.

Here is one of my favorite studies. It uses the Least Squares Method. I would love your thoughts.

There is nothing out there that can catch the turns as well as this baby can!

Here is one of my favorite studies. It uses the Least Squares Method. I would love your thoughts.

There is nothing out there that can catch the turns as well as this baby can!

Code:

```
#LeastSquares created by tradescripter
#Dec. 24, 2010 Merry Christmas
#It uses the Least-Squares Method to forecast a new price
#For an explanation of Least-Squares method see:
#http://en.wikiversity.org/wiki/Least-Squares_Method
#Hint PriceColorOn: Affect the color of the Price bars according to the signals of this indicator\n<b>Default is Yes
#Hint ArrowsOn: Plot Arrows at signal changes \n<b>Default is Yes
#Hint PredictionLineOn: Plot the indicator \n<b>Default is Yes
#Hint ShowTodayOnly: Plot only the most recent arrows in order not to clutter the Price history\nMakes it easier to see prior prices \n<b>Default is no
#Hint ShowExtraDays: Further adjustment to Show Only Today \n<b>Default is 0
#Hint space: Allows you to move the arrows, and avoid stepping on other arrows when multiple studies are shown \n<b>Default is 0.333
#Hint length: Touch this only if you are an expert! Just kidding. \n<b>Default is 9
#Hint price: For the experimenter \n<b>Default is HL2
#FuturePredictor
input price = close;
input length = 9;
input PredictionLineOn = Yes;
# Input BubblesOn = No;
input ShowTodayOnly = no;
input ShowExtraDays = 0;
input space = 0.333;
def Today = if !ShowTodayOnly then 1 else if GetDay() + ShowExtraDays >= GetLastDay() then 1 else 0;
def AvgPrice = Average(price, length);
def SumTime = fold i = 1 to length + 1 with x = 0 do x + i;
def AvgTime = SumTime / length;
#sx is the sum of all the deviations from time for the last x bars
def sx = fold j = 1 to length + 1 with y = 0 do y + ((j - AvgTime) * (GetValue(price, length - j, length + 1) - AvgPrice));
#sy is the sum of all the deviations from price for the last x bars
def sy = fold k = 1 to length + 1 with z = 0 do z + (Power(k - AvgTime, 2));
#m is the slope of the line, b is the slope intercept of the line in the equation y = mx + b
def m = sx / sy;
def b = AvgPrice - m * AvgTime;
def Prediction = (m * (length + 1)) + b;
plot FuturePrediction = if !PredictionLineOn then Double.NaN else Prediction;
FuturePrediction.AssignValueColor(if FuturePrediction > FuturePrediction[1] then Color.GREEN else Color.RED);
FuturePrediction.SetLineWeight(3);
FuturePrediction.HideBubble();
```

#### Attachments

Last edited: