Linear Regression Forecast Indicator
CompletedHi I can you please add the Linear Regression Forecast indicator (moving regression line) to your library.
Its a great indicator
Here is the code in c# below.
Thanks
using cAlgo.API;
namespace cAlgo
{
/// <summary>
/// Linear Regression
/// </summary>
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class LinearRegression : Indicator
{
[Parameter()]
public DataSeries Source { get; set; }
[Parameter(DefaultValue = 14)]
public int Period { get; set; }
[Output("Main")]
public IndicatorDataSeries Result { get; set; }
public override void Calculate(int index)
{
double sumX = 0, sumY = 0, sumXY = 0, sumXPower = 0;
for (int i = 0; i < Period; i++)
{
sumXPower += i * i;
sumX += i;
sumY += Source[index - i];
sumXY += i * Source[index - i];
}
Result[index] = (sumXPower * sumY - sumX * sumXY) / (sumXPower * Period - sumX * sumX);
}
}
-
Aloha! Can you point to any articles describing what the indicator should look like? I found this, is this what you're after?
-
Aloha! I did some digging as well and arrived at a similar script to what you found. I also found the name used in some other platforms is "Linear Regression Curve", so that's what I named it.

I also added some standard deviation bands, which traders also use to help judge overbought/oversold.
You can turn these bands off, and play with the coloring of the linear regression line in the indicator's settings. 
You will also note you can chose the price used to calculate the band, for example the average of High/Low/Close which can lead to less noise if desired.
Let me know what you think!
Please sign in to leave a comment.


Comments
7 comments