The following AFL code will plot the best-fit parabolic from the last Peak[or Trough] till now. The boundary parabolas will also be plotted, in order to ensure the loop range. Plot(C,"C",1,64); perc=3;//sensitivity calibration x=BarIndex();xx=LastValue(x); t1=LastValue(ValueWhen(PeakBars(H,perc)==0,x)); H1=LastValue(ValueWhen(PeakBars(H,perc)==0,H)); t11=LastValue(ValueWhen(TroughBars(L,perc)==0,x)); H11=LastValue(ValueWhen(TroughBars(L,perc)==0,L)); g=t1>t11; shape=IIf(g,shapeDownArrow*(x==t1),shapeUpArrow*(x==t11)); Color=IIf(g,colorRed,colorBrightGreen); PlotShapes(shape,color); t=IIf(g,x-t1,x-t11); diff1=IIf(g,H1*(xx-t1),H11*(xx-t11)); Lma=LastValue(MA(C,50)); f1=0;f2=IIf(Lma<100,1,0)+3*int(log10(Lma)); fa=0;fb=0;step=f2/100; for(f=f1;f<f2;f=f+step) { parabolic=IIf(g,H1-f*t^2,H11+f*t^2); S1=LastValue(Sum(abs(parabolic-H),xx-t1)); S11=LastValue(Sum(abs(parabolic-L),xx-t11)); diff=IIf(g,S1,S11); if(diff<diff1) { diff1=diff;fa=f; } } for(f=Max(fa-step,0);f<fa+step;f=f+0.01*step) { parabolic=IIf(g,H1-f*t^2,H11+f*t^2); S1=LastValue(Sum(abs(parabolic-H),xx-t1)); S11=LastValue(Sum(abs(parabolic-L),xx-t11)); diff=IIf(g,S1,S11); if(diff<diff1) { diff1=diff;fb=f; } } p=IIf(g,H1-fb*t^2,H11+fb*t^2); p0=IIf(g,H1-f2*t^2,H11+f2*t^2); p0=IIf(p0>LLV(L,200) AND p0<HHV(H,200),p0,Null); p1=IIf(g,H1,H11); Plot(IIf(x>=Max(t1,t11),p,-1e10),"",color,8); Plot(IIf(x>=Max(t1,t11),p0,-1e10),"",color+1,1); Plot(IIf(x>=Max(t1,t11),p1,-1e10),"",color+1,1); Title=Name()+", "+WriteIf(t1>t11,"f_desc","f_asc")+"="+WriteVal(fb,1.4);//+"[f2="+WriteVal(f2)+",step="+WriteVal(step); GraphXSpace=3;

We see ^NDX, ^N225 and ^VXO for Aug27, 2004 and two bars later. For ^NDX and ^N225 the parabolic trendline is still ascending but it is moving slightly to the right, as the trend seems to cease. In ^VXO a new trough is already recognized as a trough and the red descending parabolic is substituted by the green ascending one. It think it is more useful than the parabolic SAR

This copper example is characteristic : 26/8 : The descending parabolic has already crossed the price chart. 27/8 : The cross has moved one bar later 30/8 : The last two candles are after the cross 31/8 : The point T is recognized as a trough, we have a confirmation of the change of the trend. As you see, the parabolic crossed the price two bars before the recognition of the trough.

The day T2 confirmed the change of the trend for December Crude Oil. Since the end of the T2 session was +4.6%, it was too late, perhaps, to Cover any Short or Buy a new Long. The best-fit parabolic had already crossed the price chart by the end of the previous day T1, as you may see in the attached gif

will be plotted if we replace all the LastValue statements with the respective SelectedValue. Plot(C,"C",1,64); perc=3;//sensitivity calibration x=BarIndex();xx=SelectedValue(x); t1=SelectedValue(ValueWhen(PeakBars(H,perc)==0,x)); H1=SelectedValue(ValueWhen(PeakBars(H,perc)==0,H)); t11=SelectedValue(ValueWhen(TroughBars(L,perc)==0,x)); H11=SelectedValue(ValueWhen(TroughBars(L,perc)==0,L)); g=t1>t11; shape=IIf(g,shapeDownArrow*(x==t1),shapeUpArrow*(x==t11)); Color=IIf(g,colorRed,colorBrightGreen); PlotShapes(shape,color); t=IIf(g,x-t1,x-t11); diff1=IIf(g,H1*(xx-t1),H11*(xx-t11)); Lma=SelectedValue(MA(C,50)); f1=0;f2=IIf(Lma<100,1,0)+3*int(log10(Lma)); fa=0;fb=0;step=f2/100; for(f=f1;f<f2;f=f+step) { parabolic=IIf(g,H1-f*t^2,H11+f*t^2); S1=SelectedValue(Sum(abs(parabolic-H),xx-t1)); S11=SelectedValue(Sum(abs(parabolic-L),xx-t11)); diff=IIf(g,S1,S11); if(diff<diff1) { diff1=diff;fa=f; } } for(f=Max(fa-step,0);f<fa+step;f=f+0.01*step) { parabolic=IIf(g,H1-f*t^2,H11+f*t^2); S1=SelectedValue(Sum(abs(parabolic-H),xx-t1)); S11=SelectedValue(Sum(abs(parabolic-L),xx-t11)); diff=IIf(g,S1,S11); if(diff<diff1) { diff1=diff;fb=f; } } p=IIf(g,H1-fb*t^2,H11+fb*t^2); Plot(IIf(x>=Max(t1,t11),p,-1e10),"",color,1); Title=Name()+", "+WriteIf(t1>t11,"f_desc","f_asc")+"="+WriteVal(fb,1.4);//+"[f2="+WriteVal(f2)+",step="+WriteVal(step); GraphXSpace=3;

Since the whole candle X is on the right side of the parabolic, we have to Sell at next bar Open[upper graph]. The trend may continue sometimes [lower graph]. In this case, the best-fit parabolic is out of the market until the next Peak.

Scoffing fools. This algorithm has the benefits that it starts working on the first bar of data, unlike a moving average, and functions as a high order trend line, analogous to a linear regression. An infinite impulse response filter estimator is a much easier way to do this, however. Two variables in addition to the data. Two statements in the main body of code. Some people do not appreciate complexity. Others appreciate it too much. Tsokakis' detractors are as Aristotelians before the revelations of Newton.