The following code will check the Fibonaccian behavior of a stock/index in the past. // The Fibonaccian behavior, by D. Tsokakis, Nov 2006 n=Param("n",6,3,20,1);// Calibrate the Zig percentage Var1=C; z=Zig(Var1,n); PeakCondition=PeakBars(Var1,n)==0; p1=ValueWhen(PeakCondition,Var1,1); p2=ValueWhen(PeakCondition,Var1,2); TroughCondition=TroughBars(Var1,n)==0; t1=ValueWhen(TroughCondition,Var1,1); t2=ValueWhen(TroughCondition,Var1,2); d=IIf(BarsSince(PeakCondition)<BarsSince(TroughCondition),(p1-t1)/(p2-t1),(p1-t1)/(p1-t2)); Plot(d,"d",colorWhite,1); // Create the main Fibonacci sequence d0=2/(sqrt(5)+1); n0=2; for(i=d0^(-n0);i>=d0^n0;i=i*d0) { fib=i; Plot(fib,"",colorBlue,1); } Plot(0.5,"",colorBlue,1); Plot(Var1,"Var1",1,8+styleLeftAxisScale); Plot(z,"",colorYellow,styleLeftAxisScale); // Calculation of the current Fibonacci ratio LVar1=LastValue(Var1); d1=IIf(BarsSince(PeakCondition)<BarsSince(TroughCondition),(p1-LVar1)/(p1-t1),(LVar1-t1)/(p1-t1)); Plot(d1,"The current Fib ratio is ",colorRed,styleNoLine); When the white line is near the blue Fib lines, then the main turning points are Fibonaccian. This behavior may be repeated in the future.
The folowing code will give the statistics for the Fibonacian levels. We use n0=2 and this value will check the levels 0.382, 0.5, 0.618, 1, 1.618, 2.618 The tolerance was 0.05 for any Fibonacci number. For example, any value between 1.537 and 1.698 was considered as 1.618. /*Statistics for Fibonaccian levels*/ n=Param("n",3,3,20,1);// Calibrate the Zig percentage Var1=C; z=Zig(Var1,n); PeakCondition=PeakBars(Var1,n)==0; p1=ValueWhen(PeakCondition,Var1,1); p2=ValueWhen(PeakCondition,Var1,2); TroughCondition=TroughBars(Var1,n)==0; t1=ValueWhen(TroughCondition,Var1,1); t2=ValueWhen(TroughCondition,Var1,2); d=IIf(BarsSince(PeakCondition)<BarsSince(TroughCondition),(p1-t1)/(p2-t1),(p1-t1)/(p1-t2)); Plot(d,"d",colorWhite,1); d0=2/(sqrt(5)+1); n0=2; for(i=d0^(-n0);i>=d0^n0;i=i*d0) { fib=i; Plot(fib,"",colorBlue,1); } Plot(0.5,"",colorBlue,1); Plot(Var1,"Var1",1,8+styleLeftAxisScale); Plot(z,"",colorYellow,styleLeftAxisScale); tp=Cum(Peakcondition); tt=Cum(Troughcondition); vd=d!=Ref(d,-1); tvd=Cum(Vd); ap=0.05;//calibrate the tolerance of Fib numbers Cond=Vd AND (abs(d-0.318)<ap*0.318 OR abs(d-0.5)<ap*0.5 OR abs(d-0.618)<ap*0.618 OR abs(d-1)<ap*1 OR abs(d-1.618)<ap*1.618 OR abs(d-2.618)<ap*2.618); tCond=Cum(Cond); F=100*tcond/tvd; Title=/*"tp = "+WriteVal(tp,1.0)+" , tt = "+WriteVal(tt,1.0)+"\n"+*/Name()+"\nn = "+WriteVal(n,1.0)+" % "+"\nTotal levels = "+WriteVal(tvd,1.0)+" , Fibonaccian levels = "+WriteVal(tCond,1.0)+"\nF = "+WriteVal(F,1.0) +" % "; Filter=1;//Explore for the n=1 last quotations AddColumn(F,"n="+WriteVal(n,1.0)+"%",1.0); The results for the last 10 years [from Jan 1996 till now] were very poor. For n=5%, the Fibonaccian levels percentage was ^DJI 15% ^GSPC 23% ^NDX 24% ^N225 23% For n=10% the respective results were ^DJI 12% ^GSPC 47% ^NDX 21% ^N225 29% For ^GSPC we have had 17 price levels over the last ten years. Eight levels were close to SOME Fibonaccian level. Since we have used six levels in this study, it is more than obvious that any use of Fibonaccian levels for trading decisions will not be reliable and may cause significant damage. PS : The codes do not appear properly, some pieces are missing. You may see http://www.amibroker.com/library/detail.php?id=767
The code is elementary and could be easily translated in any T/A software language. You only need Peak/Trough definition and the respected price when a Peak/Trough occurrs. I think these terms are available in any software...
It is interesting to see the Peak-To-Trough ratio for various Zig change values. The following Exploration/Commentary will select the most frequent range and give the long term statistics. //Peak-to-Trough Ratio Statistics, by D. Tsokakis function PTratio(n,Var,m,step) { z=Zig(Var,n); PeakCondition=PeakBars(Var,n)==0; p1=ValueWhen(PeakCondition,Var,1); p2=ValueWhen(PeakCondition,Var,2); TroughCondition=TroughBars(Var,n)==0; t1=ValueWhen(TroughCondition,Var,1); t2=ValueWhen(TroughCondition,Var,2); R=IIf(BarsSince(PeakCondition)<BarsSince(TroughCondition),(p1-t1)/(p2-t1),(p1-t1)/(p1-t2)); fs=Cum(R!=Ref(R,-1) AND R>=m*step AND R<(m+1)*step)*100/Cum(R!=Ref(R,-1)) ; return fs; } Var=C; nmax=11; STEP=0.50; j0=4/step;dd=0;ddd=0;m=0;jj=0;nn=0; for(n=2;n<nmax;n++) { for(j=0;j<j0;j++) { dd=LastValue(PTratio(n,Var,j,step)); ddd=Max(ddd,dd); if(dd==ddd) { jj=j; nn=n; } } } Title="n = "+WriteVal(nn,1.0)+" %\n"+WriteVal(jj*STEP,1.2)+" <= d < "+WriteVal((1+jj)*STEP,1.2)+" : "+WriteVal(ddd,1.0)+" % "; Filter=1;//n=1 last quotations AddColumn(NN,"Change",1.0); AddColumn(JJ*STEP,"<= R ",1.2); AddColumn((1+JJ)*STEP," R < ",1.2); AddColumn(DDD," % ",1.0); For example, the ^DJI retracements since 1996 are in the range 0.50 <= d < 1.00 for the 41% of the total cases, when the Zig change is 10%. You may see more narrow areas by calibrating the STEP=0.50; statement. The useful results will give a final emphasis that there is no Fibonaccian behavior in the Peak-To-Trough retracements. See also http://www.amibroker.com/library/detail.php?id=767