[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: addtocomp && filling holes



PureBytes Links

Trading Reference Links

Hi Folks,

Below is some code that I developed awhile ago to create Hurst-like 
dominancy envelopes. Its roughly identical to the Wealth-Lab code 
some of you have mentioned (I wrote that too) and the AFL script is 
just about the same thing.

The extrapolation from the lagged SMA is accomplished two different 
ways. One is a method Hurst suggests in his book "The Profit Magic 
of Stock Transaction Timing" using a parabolic fit of three points 
along a centered SMA. 

The other is as Clyde Lee suggested in the Swing Machine Yahoo board 
by taking the shape of the CMA from one cycle period ago and 
starting it where the 1/2 period lagged data ends.

Both are shown on this code.

I've found the Hilbert period algorythm offered at the Amibroker 
third party page is somewhat unstable so I set the periods by 
counting between significant lows and using the parameter setting 
functions.

Be warned. You cannt backtest this code. If you do you will get 
unbelievably good results because you have shifted known data back 
in time to a point where you cannot possibly have known the data for 
sure to trade by.

Enjoy. 

-ace

//------------------------------------------------------
// --------------------  HURST DE  ---------------------
// --- by Ace
//------------------------------------------------------
// Hurst DE based on Centered SMA's with a forward projected
// curve based on A parabolic fit 
//------------------------------------------------------
// SET THE S/T and I/T PERIODS
periodicity=Interval();
p1=Param("1) S/T DE Period",20,3,373,1);
p2=Param("2) I/T DE Period",50,3,373,1);
p3=Param("3) L/T DE Period",144,3,373,1);

//------------------------------------------------------
// Moving Averages and Lags
//------------------------------------------------------
p0=p1/2;
IIf(p1/2!=round(p1/2),p0=p0-1,p0=p0);
Ave0=MA(Avg,p0);
Ave1=MA(Avg,p1);
Ave2=MA(Avg,p2);
Ave3=MA(Avg,p3);

Lag0=(p0+1)/2;
Lag1=(p1+1)/2;
Lag2=(p2+1)/2;
Lag3=(p3+1)/2;
//------------------------------------------------------
// Center the MA's
//------------------------------------------------------
cma0=Ref(Ave0,Lag0);
cma1=Ref(Ave1,Lag1);
cma2=Ref(Ave2,Lag2);
cma3=Ref(Ave3,Lag3);
//------------------------------------------------------
// Percentage Based Widths
//------------------------------------------------------
k=3;
dp1=LastValue(Ref(StDev( (Close-cma1)/cma1, k*p1 ),(p1+1)/2-1));
dp2=LastValue(Ref(StDev( (Close-cma2)/cma2, k*p2 ),(p2+1)/2-1));
dp3=LastValue(Ref(StDev( (Close-cma3)/cma3, k*p3 ),(p3+1)/2-1));
//------------------------------------------------------
// Arithmetic Widths
//------------------------------------------------------
/*
Hilo=IIf(H-cma1>=cma1-L,H-cma1,L-cma1);
//dp1=LastValue(Ref(StDev( (Close-cma1), k*p1 ),-Lag1));
dp1=LastValue(Ref(StDev( Hilo, k*p1 ),-Lag1));
Hilo=IIf(H-cma2>=cma2-L,H-cma2,L-cma2);
//dp2=LastValue(Ref(StDev( Close-cma2, k*p2 ),-Lag2));
dp2=LastValue(Ref(StDev( Hilo, k*p2 ),-Lag2));
Hilo=IIf(H-cma3>=cma3-L,H-cma3,L-cma3);
//dp3=LastValue(Ref(StDev( Close-cma3, k*p3 ),-Lag3));
dp3=LastValue(Ref(StDev( Hilo, k*p3 ),-Lag3));
*/
//------------------------------------------------------
// Calculate a Parabolic Extension for the S/T DE
//------------------------------------------------------
tn=round(p1/6);
fillforward=BarIndex() >= LastValue( BarIndex() )-Lag1;
t=BarIndex()-Ref(BarIndex(),Lag1)+Lag1;
// Get Constants for the Parabolic fit equation
Sm=LastValue(Ref(Ave1,-2*tn));
// This coordinate is actually t=0
So=LastValue(Ref(Ave1,-tn));
Sp=LastValue(Ave1);
// Calculate coeff's for the parabola
Ak=So; 
Bk=(Sp-Sm)/2/tn; 
Ck=(Sp+Sm-2*So)/2/tn^2; 
parabfit1=Ak+Bk*t+Ck*t^2;
dy1=LastValue(Ave1)-Ak;
cma1=IIf(fillforward,parabfit1+dy1,cma1);
//------------------------------------------------------
// Calculate a Parabolic Extension for the I/T DE
//------------------------------------------------------
tn=round(p2/6);
fillforward=BarIndex() >= LastValue( BarIndex() )-Lag2;
t=BarIndex()-Ref(BarIndex(),Lag2)+Lag2;
// Get Constants for the Parabolic fit equation
Sm=LastValue(Ref(Ave2,-2*tn));
// This coordinate is actually t=0
So=LastValue(Ref(Ave2,-tn));
Sp=LastValue(Ave2);
// Calculate coeff's for the parabola
Ak=So; 
Bk=(Sp-Sm)/2/tn; 
Ck=(Sp+Sm-2*So)/2/tn^2; 
parabfit2=Ak+Bk*t+Ck*t^2;
dy2=LastValue(Ave2)-Ak;
cma2=IIf(fillforward,parabfit2+dy2,cma2);
//------------------------------------------------------
// Calculate a Parabolic Extension for the L/T DE
//------------------------------------------------------
tn=round(p3/6);
fillforward=BarIndex() >= LastValue( BarIndex() )-Lag3;
t=BarIndex()-Ref(BarIndex(),Lag3)+Lag3;
// Get Constants for the Parabolic fit equation
Sm=LastValue(Ref(Ave3,-2*tn));
// This coordinate is actually t=0
So=LastValue(Ref(Ave3,-tn));
Sp=LastValue(Ave3);
// Calculate coeff's for the parabola
Ak=So; 
Bk=(Sp-Sm)/2/tn; 
Ck=(Sp+Sm-2*So)/2/tn^2; 
parabfit3=Ak+Bk*t+Ck*t^2;
dy3=LastValue(Ave3)-Ak;
cma3=IIf(fillforward,parabfit3+dy3,cma3);
//------------------------------------------------------
// Fast MA with parabolic fit - p1/2 period
//------------------------------------------------------
tn=round(p0/6);
fillforward=BarIndex() >= LastValue( BarIndex() )-Lag0;
t=BarIndex()-Ref(BarIndex(),Lag0)+Lag0;
// Get Constants for the Parabolic fit equation
Sm=LastValue(Ref(Ave0,-2*tn));
// This coordinate is actually t=0
So=LastValue(Ref(Ave0,-tn));
Sp=LastValue(Ave0);
// Calculate coeff's for the parabola
Ak=So; 
Bk=(Sp-Sm)/2/tn; 
Ck=(Sp+Sm-2*So)/2/tn^2; 
parabfit0=Ak+Bk*t+Ck*t^2;
dy0=LastValue(Ave0)-Ak;
cma0=IIf(fillforward,parabfit0+dy0,cma0);
//------------------------------------------------------
Plot( Close, "OHLC", colorBlack, styleCandle);
//------------------------------------------------------
// Plot Arithmetic Bands
//------------------------------------------------------
/*
//Plot(cma1,"I/T-CTR",colorRed,styleLine);    
Plot(cma1+2*dp1,"S/T-UB",colorRed,styleLine);
Plot(cma1-2*dp1,"S/T-LB",colorRed,styleLine);
//Plot(cma2,"I/T-CTR",colorBlue,styleLine);
Plot(cma2+2*dp2,"I/T-UB",colorBlue,styleLine);
Plot(cma2-2*dp2,"I/T-LB",colorBlue,styleLine);
//Plot(cma3,"L/T-CTR",colorGreen,styleLine);
Plot(cma3+2*dp3,"L/T-UB",colorGreen,styleLine);
Plot(cma3-2*dp3,"L/T-LB",colorGreen,styleLine);
Plot(cma0,"Zline",colorBrown,styleLine);
*/
//------------------------------------------------------
// Write the selected bar lengths to the window
//------------------------------------------------------
"HURST DOMINANCY ENVELOPES";
"Current Periods are set to: ";
"S/T Period: "+WriteVal(p1,3.0);
"I/T Period: "+WriteVal(p2,3.0);
"L/T Period: "+WriteVal(p3,3.0);
" ";
"Select a range on the screen to calculate a period.";
" ";
rangelength = EndValue( BarIndex() ) - BeginValue( BarIndex() );
"Selected Range is "+
WriteVal( rangelength, format = 3.0 )+
" bars";
//------------------------------------------------------
// Use the below plotting for % Width Bands 
//------------------------------------------------------
m=2.2;
Plot(cma1*(1+m*dp1),WriteVal(p1,3.0)+"p"+" UB",colorRed,styleLine);
Plot(cma1*(1-m*dp1),"LB",colorRed,styleLine);
Plot(cma2*(1+m*dp2),WriteVal(p2,3.0)+"p"+" UB",colorBlue,styleLine);
Plot(cma2*(1-m*dp2),"LB",colorBlue,styleLine);
Plot(cma3*(1+m*dp3),WriteVal(p3,3.0)+"p"+" UB",colorGreen,styleLine);
Plot(cma3*(1-m*dp3),"LB",colorGreen,styleLine);
ub=cma1*(1+m*dp1);
Lb=cma1*(1-m*dp1);
inpercentage1=Sum(IIf(H<ub AND H>lb AND L<ub AND 
L>lb,1,0),3*p1)/3/p1*100;
"";
ub=cma2*(1+2*dp2);
Lb=cma2*(1-2*dp2);
inpercentage2=Sum(IIf(H<ub AND H>lb AND L<ub AND 
L>lb,1,0),3*p2)/3/p2*100;
ub=cma3*(1+2*dp3);
Lb=cma3*(1-2*dp3);
inpercentage3=Sum(IIf(H<ub AND H>lb AND L<ub AND 
L>lb,1,0),3*p3)/3/p3*100;
"";
WriteVal(inpercentage1,3.0)+"% prices within 1st DE over 3 Periods";
WriteVal(inpercentage2,3.0)+"% prices within 2nd DE over 3 Periods";
WriteVal(inpercentage3,3.0)+"% prices within 3rd DE over 3 Periods";
PlotShapes(IIf(Cross(BarIndex(),BarCount-1-
(p1+1)/2),shapeSmallUpTriangle,0), colorRed, 0, cma1*(1-2*dp1));
PlotShapes(IIf(Cross(BarIndex(),BarCount-1-
(p2+1)/2),shapeSmallUpTriangle,0), colorBlue, 0, cma2*(1-2*dp2));
PlotShapes(IIf(Cross(BarIndex(),BarCount-1-
(p3+1)/2),shapeSmallUpTriangle,0), colorGreen, 0, cma3*(1-2*dp3));
//------------------------------------------------------
// Clyde Lee said that Hurst recommended going back one 
// full cycle to and copy that centerline shape to the 
// shifted CMA to project forward for the envelopes.
//------------------------------------------------------
dy1=LastValue(Ref(cma1,-3*p1/2))-LastValue(Ref(cma1,-p1/2));
RefCMA1=IIf(BarIndex()>=BarCount-p1/2-1,Ref(cma1,-p1),Null)-dy1;
Plot(RefCMA1*(1+m*dp1),"RefUB1",colorOrange);
Plot(RefCMA1*(1-m*dp1),"RefLB1",colorOrange);


dy2=LastValue(Ref(cma2,-3*p2/2))-LastValue(Ref(cma2,-p2/2));
Refcma2=IIf(BarIndex()>=BarCount-p2/2-1,Ref(cma2,-p2),Null)-dy2;
Plot(Refcma2*(1+m*dp2),"RefUB1",colorLightBlue);
Plot(Refcma2*(1-m*dp2),"RefLB1",colorLightBlue);

dy3=LastValue(Ref(cma3,-3*p3/2))-LastValue(Ref(cma3,-p3/2));
Refcma3=IIf(BarIndex()>=BarCount-p3/2-1,Ref(cma3,-p3),Null)-dy3;
Plot(Refcma3*(1+m*dp3),"RefUB1",colorSeaGreen);
Plot(Refcma3*(1-m*dp3),"RefLB1",colorSeaGreen);




------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for your HP, Epson, Canon or Lexmark
Printer at MyInks.com. Free s/h on orders $50 or more to the US & Canada.
http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/GHeqlB/TM
---------------------------------------------------------------------~->

Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx 
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/amibroker/

<*> To unsubscribe from this group, send an email to:
     amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/