What I want to calculate now is the linear regression among the last four High points. I know I have the build-in function Lineareq(), but I just don't know how to combine them together. Anyone here can kindly give me a hand?
I have added line for lows in addition and have put all into one loop (includes extensions).
// calculate the linear fit
lsft = aa[0][0];
cond1 = bi>= xvalue[0] AND bi <= xvalue[n];
cond2 = bi > xvalue[n] AND bi < BarCount;
bi_adj = IIf(cond1 OR cond2, bi-avgx, Null);
for ( s = 1; s <= order; s++ )
lsft += aa[s][0] * bi_adj^s;
/// @link https://forum.amibroker.com/t/linear-regression-among-swing-high/22681/2
/// by AmiBroker.com, empottasch and fxshrat@gmail.com
order = 1; // linear
Hnbar = 30;
Lnbar = 30;
n = 3;
function calculateCoefficients( xVal, yVal, bars, av ) {
local j, aa, xx, yy, xxt;
xx = Matrix( bars + 1, order + 1, 1 );
for( j = 1; j <= order; j++ )
xx = MxSetBlock(xx, 0, bars, j, j, (xVal - av)^j);
yy = Matrix( bars + 1, 1, 0 );
yy = MxSetBlock(yy, 0, bars, 0, 0, yVal);
xxt = MxTranspose( xx );
aa = MxSolve( xxt @ xx, xxt ) @ yy;
return aa;
}
bi = BarIndex();
p1 = H == HHV(H, Hnbar) AND Ref(HHV(H, Hnbar), Hnbar) < H;
p2 = L == LLV(L, Lnbar) AND Ref(LLV(L, Lnbar), Lnbar) > L;
arr1 = H; arr2 = L;
SetChartOptions( 0, chartShowDates );
SetChartBkColor( ColorRGB( 0, 0, 0 ) );
Plot( Close, "Price", colorDefault, styleCandle, Null, Null, 0, 0, 1 );
PlotShapes( shapeSmallCircle*P1, ColorRGB( 250, 0, 0 ), 0, H, 10 );
PlotShapes( shapeSmallCircle*P2, ColorRGB( 250, 0, 0 ), 0, L, 10 );
if ( BarCount > n ) {
style2 = styleLine | styleNoRescale | styleDots | styleNoLine;
for ( k = 1; k <= 2; k++ ) { // Peaks and throughs
xvalue = yvalue = Null;
for ( i = 0; i <= n; i++ ) {
xvalue[i] = LastValue(ValueWhen(VarGet("P"+k), bi, n+1-i));
yvalue[i] = LastValue(ValueWhen(VarGet("P"+k), VarGet("arr"+k), n+1-i));
}
// average index of range examined. Used to make the process of fitting more accurate (numerically)
avgx = (xvalue[n] + xvalue[0]) / 2;
aa = calculateCoefficients(xvalue, yvalue, n, avgx);
// calculate the linear fit
lsft = aa[0][0];
cond1 = bi>= xvalue[0] AND bi <= xvalue[n];
cond2 = bi > xvalue[n] AND bi < BarCount;
bi_adj = IIf(cond1 OR cond2, bi-avgx, Null);
for ( s = 1; s <= order; s++ )
lsft += aa[s][0] * bi_adj^s;
color = IIf(k==1, colorAqua, colorViolet);
Plot(IIf(cond1, lsft, Null), "", color, styleLine, Null, Null, 0, 0, 4 );
Plot(IIf(cond2, lsft, Null), "", color, style2, Null, Null, 0, 0, 1 );
}
}
Thanks Ed and FX's reply. Now this version is calculating the regression line of the "last" four pivots. What if I would like to obtain the value of "lsft" of each bar? what I am trying to do is:
First, declare the variables by using similar loops:
for ( k = 1; k <= 2; k++ )
{
for ( i = 0; i <= n; i++ )
{
//xvalue[i] = LastValue(ValueWhen(VarGet("P"+k), bi, n+1-i));
//yvalue[i] = LastValue(ValueWhen(VarGet("P"+k), VarGet("arr"+k), n+1-i));
VarSet("P"+k+"_"+n+"th_bi", (ValueWhen(VarGet("P"+k), bi, n+1-i)));
VarSet("P"+k+"_"+n+"th_arr"+k, ValueWhen(VarGet("P"+k), VarGet("arr"+k), n+1-i));
}
}
Then, I call these two variables in every bar by using [j]:
for( j = n; j < BarCount; j++ )
{
if ( BarCount > n )
{
for ( k = 1; k <= 2; k++ )
{ // Peaks and throughs
for ( i = 0; i <= n; i++ )
{
//xvalue[i] = LastValue(ValueWhen(VarGet("P"+k), bi, n+1-i));
//yvalue[i] = LastValue(ValueWhen(VarGet("P"+k), VarGet("arr"+k), n+1-i));
xvalue[i] = VarGet("P"+k+"_"+n+"th_bi"+"["+j+"]");
yvalue[i] = VarGet("P"+k+"_"+n+"th_arr"+k+"["+j+"]");
}
// average index of range examined. Used to make the process of fitting more accurate (numerically)
avgx[j] = (xvalue[n] + xvalue[0]) / 2;
}
}
}
Then I am not sure the next step. The function "calculateCoefficients(xvalue, yvalue, n, avgx)" is dealing with input in the format of array with 4 element( in this case). It really confuses me. May I have some hints from you Gents?
oh, i understood you wanted to only use price at the pivot points. If you want to use all data you just use the regular Amibroker functions. Lots of examples out there. I also made a couple of AFL. Just google for "Amibroker LinRegIntercept LinRegSlope LinearReg" you will find a bunch of code.
Thanks for your quick reply!
You didn't misunderstand my question - I do really want to only use the price at the pivot points, and so your solution is perfectly solving my problem. I want to obtain the regression line for, say in this example, for last 4 pivot points for every single bar. I studied your "using fractals for trendlines" and I believe the concept is very similar, but just replacing the trendlines by the regression line.
So I believe I can only calculate the regression line by using for-loop for each bar. But I have not yet got a solution to call your function "calculateCoefficients" in the for-loop. In the for-loop, I can calculate each element by using array[i]. But your function's input, i.e xvalue and yvalue, are forth dimension array. So any idea I can combine them together? Or do I miss anything? should I go for completely different diection?
Thanks FX. You are right, they are 1 dimension array, i wanted to say they store 4 elements.
I go through the reply from Empottasch and you again, and sorry that I didn't study them details enough. I think the file provided by Empottasch in the library is already doing what I am looking for.
Programming is a really special talent and I hope I can always translate my idea into code like all of you.
I have been playing with the idea of having one byte of peaks (or throughs) and then finding of the 8 peaks, the combination of peaks that are as much as possible on the same line. So that would need to loop through the 255 combinations of peaks and choose the best fit and omit some peaks that are lower than the resistance line. Thanks for posting these wonderfull codes, it will really help if I have time to test that idea.
and since we now have the wonderful new python plugin by Kuba and Tomasz we can now avoid all the math and let the calculations be done by Python. I am playing around with it to get familiar with the plugin and Python.
If you did not install the plugin yet the instructions are posted here:
best go through the entire thread and make sure you install Python through the link given by Tomasz in 1 of the posts (version 3.8).
then if every thing is setup, you installed Python, installed the Amipy plugin, installed numpy then the only additional thing to run my code is to do type in the CMD window: pip install scipy
the python code I stored in a file called PolyfitForAB.py and you need to make sure you store it in a location that corresponds to the link you use in the AFL file.