Dear Experts,
How are you today.
I have searched and found out as below code.
Please help me creat filter for change color of candle. My purpose is to filter stocks that change color on the chart. I don't know how to use the filter when there is a function returning a value. I am very grateful if someone helps me to study.
Thanks a lots.
function ALFilter(price, length, medianlong) {
result=price;
L0 = price;
L1 = price;
L2 = price;
L3 = price;
coef=0.5;
Diff=0;
HH=0.1;
LL=0;
alpha=0.5;
nonzero = 10^-20;
for(i = 1+length; i < BarCount; i++) {
Diff[i] = abs(price[i] - result[i-1]);
HH[i] = Diff[i];
LL[i] = Diff[i];
for(j = 0; j < (length-1); j++) {
if (Diff[i-j] > HH[i]) HH[i] = Diff[i-j];
if (Diff[i-j] < LL[i]) LL[i] = Diff[i-j];
}
if ( (i > length) AND (HH[i] - LL[i] != 0) ) {
// SLOW :
//coef = Median(((Diff - LL) / (HH - LL)), 5);
// FAST :
//----- Speed up the median calculation by placing it inline : Thanks to its author "Not Too Swift"
coeftemp=(Diff - LL) / ((HH - LL)+nonzero);
mlen = medianlong;
for(k = mlen - 1; k >= 0; k--) temparray[k] = coeftemp[i + k - (mlen - 1)];
temp=0;
for(k = mlen - 1; k > 0; k--) {
for (j = mlen - 1; j > 0; j--) {
if (temparray[j-1] > temparray[j]) {
temp = temparray[j-1];
temparray[j-1] = temparray[j];
temparray[j] = temp;
}
}
}
coef[i] = temparray[(mlen/2)-0.5];
//----- End median calculation
} // end main IF
alpha=coef[i];
L0[i] = alpha*price[i] + (1 - alpha)*L0[i-1];
L1[i] = -(1 - alpha)*L0[i] + L0[i-1] + (1 - alpha)*L1[i-1];
L2[i] = -(1 - alpha)*L1[i] + L1[i-1] + (1 - alpha)*L2[i-1];
L3[i] = -(1 - alpha)*L2[i] + L2[i-1] + (1 - alpha)*L3[i-1];
result[i] = (L0[i] + 2*L1[i] + 2*L2[i] + L3[i]) / 6;
} // end main FOR
return result;
}
SetBarsRequired(2000,2000);
P = ParamField("Price field",-1);
periods = Param( "Periods", 20, 1, 40, 1 );
periodsmedian = Param( "Periods Median", 5, 1, 40, 1 );
Plot( ALFilter(P,periods,periodsmedian), "\n"+"Adaptive Laguerre Filter", ParamColor( "Adaptive Laguerre Filter", colorCycle ), styleBar ); //ParamStyle("Style")