Hi All,
I recently came across a code in Tradingview its in Pine script. I am not able to convert it into the AFL code to be used in Amibroker. Can any one pls help me convert this code to AFL.
src = hl2
len=input(13)
w = input(defval=4, title="width", minval=1, maxval=7, step=1)
lrc = linreg(src, len, 0)
lrcColor=change(lrc) >= 0 ? lime:red
plot(lrc, color=lrcColor, linewidth=3)
plot(w>=4?lrc:na, offset=4, color=lrcColor)
plot(w>=3?lrc:na, offset=3, color=lrcColor)
plot(w>=2?lrc:na, offset=2, color=lrcColor)
plot(w>=1?lrc:na, offset=1, color=lrcColor)
plot(w>=4?lrc:na, offset=-4, color=lrcColor)
plot(w>=3?lrc:na, offset=-3, color=lrcColor)
plot(w>=2?lrc:na, offset=-2, color=lrcColor)
plot(w>=1?lrc:na, offset=-1, color=lrcColor)
linreg() function is plotted in
Here are the essential code fragments:
First linreg.calcSlope() function is called.
void calcSlope()
{
double sumX = 0.0;
double sumY = 0.0;
double sumXSqr = 0.0;
double sumXY = 0.0;
final int length = res.end - res.begin + 1;
for (int i=0; i<length; ++i)
{
final double val = source.at(res.begin + i);
final double per = i + 1;
sumX += per;
sumY += val;
sumXSqr += per * per;
sumXY += val * per;
}
res.slope = ((length * sumXY) - (sumX * sumY)) / ((length * sumXSqr) - (sumX * sumX));
res.average = sumY / length;
res.intercept = res.average - (res.slope * sumX) / length + res.slope;
}
Then, it's result (res) is used to calculate linreg() value:
return linreg.res.intercept + linreg.res.slope * (length - 1 - offset);
I recently came across a code in Tradingview its in Pine script. I am not able to convert it into the AFL code to be used in Amibroker. Can any one pls help me convert this code to AFL.
src = hl2
len=input(13)
w = input(defval=4, title="width", minval=1, maxval=7, step=1)
lrc = linreg(src, len, 0)
lrcColor=change(lrc) >= 0 ? lime:red
plot(lrc, color=lrcColor, linewidth=3)
plot(w>=4?lrc:na, offset=4, color=lrcColor)
plot(w>=3?lrc:na, offset=3, color=lrcColor)
plot(w>=2?lrc:na, offset=2, color=lrcColor)
plot(w>=1?lrc:na, offset=1, color=lrcColor)
plot(w>=4?lrc:na, offset=-4, color=lrcColor)
plot(w>=3?lrc:na, offset=-3, color=lrcColor)
plot(w>=2?lrc:na, offset=-2, color=lrcColor)
plot(w>=1?lrc:na, offset=-1, color=lrcColor)
linreg() function is plotted in
Here are the essential code fragments:
First linreg.calcSlope() function is called.
void calcSlope()
{
double sumX = 0.0;
double sumY = 0.0;
double sumXSqr = 0.0;
double sumXY = 0.0;
final int length = res.end - res.begin + 1;
for (int i=0; i<length; ++i)
{
final double val = source.at(res.begin + i);
final double per = i + 1;
sumX += per;
sumY += val;
sumXSqr += per * per;
sumXY += val * per;
}
res.slope = ((length * sumXY) - (sumX * sumY)) / ((length * sumXSqr) - (sumX * sumX));
res.average = sumY / length;
res.intercept = res.average - (res.slope * sumX) / length + res.slope;
}
Then, it's result (res) is used to calculate linreg() value:
return linreg.res.intercept + linreg.res.slope * (length - 1 - offset);