Convert mlq4 to .js
CompletedHi guys I was wondering if anyone could convert this code to .js.
It is in mlq4 used in mt4
It is Moving Linear Regression Line Indicator. Great indicator.
Here is the code below.
Cheers JS.
#property indicator_chart_window
#property indicator_color1 Blue
extern int LRPeriod=13;
double ind_buffer[];
int init()
{
IndicatorBuffers(1);
SetIndexStyle(0,DRAW_LINE,0,1);
SetIndexBuffer(0,ind_buffer);
return(0);
}
int start()
{
int limit, i;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
for(i=limit; i>=0; i--) {
ind_buffer[i]=linreg(LRPeriod,i);
}
return(0);
}
//+------------------------------------------------------------------+
double linreg(int p,int i)
{
double SumY=0;
double Sum1=0;
double Slope=0;
double c;
for (int x=0; x<=p-1;x++) {
c=Close[x+i];
SumY+=c;
Sum1+=x*c; }
double SumBars=p*(p-1)*0.5;
double SumSqrBars=(p-1)*p*(2*p-1)/6;
double Sum2=SumBars*SumY;
double Num1=p*Sum1-Sum2;
double Num2=SumBars*SumBars-p*SumSqrBars;
if(Num2!=0) Slope=Num1/Num2;
else Slope=0;
double Intercept=(SumY-Slope*SumBars)/p;
double linregval=Intercept+Slope*(p-1);
return(linregval);
}
-
This is the c# code for this indicator
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! Just letting you know that I coded this up and it's available! It's called the "Tiki Linear Regression Curve" in the code explorer.
See this post for more details: https://tradovate.zendesk.com/hc/en-us/community/posts/115009396428-Linear-Regression-Forecast-Indicator
Please sign in to leave a comment.
Comments
3 comments