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

Re: [amibroker] Re : volatility indicators to help with option trading



PureBytes Links

Trading Reference Links




I just got the code from John Ehlers latest book, 
for finding cycles and his Sine/LeadSine indicators.
 
The code works fine in TS, but can not get it to 
work in AB.
 
The first part of finding the period works fine, 
but finding the phase is a problem.
 
The problem starts with finding:
RealPart =  ....  and 
ImagPart=  ....
 
Appreciate any help in converting this to 
AB
 
TS code attached.
 
Thanks
 
Ara


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 the Yahoo! Terms of Service.







{SineWave Indicator}

inputs: 
	freq (40),
	prices((H+L)/2),
	alpha(0.07);



variables:
	j(0),
    pi(0),
	RTD(0),
	DTR(0),
	SWGen(0),
	Cycle(0),
	count(0),
	Price(0),
	Smooth(0),
	I1(0),
	Q1(0),
	I2(0),
	Q2(0),
	DeltaPhase(0),
	MedianDelta(0),
	MaxAmp(0),
	AmpFix(0),
	Re(0),
	Im(0),
	DC(0),
	alpha1(0),
	InstPeriod(0),
	DCPeriod(0),
	SmoothCycle(0),
	RealPart(0),
	ImagPart(0),
	DCPhase(0),
	SineWave(0),
	SWCount(0),
	SWcycle(0),
	SWGate(0),
	BurstLength(0),
	Jcount(0),
	LeadSine(0);


SWcount     = 0;
BurstLength = 2*freq;
pi=4*arctangent(1);
RTD=180/2*Pi;


{This section below generates a sine wave to test the code}
{ Sine wave Test}
SWcycle    =  sine( Cum(1)*(360*(pi/180 ))/freq );
for j=currentbar-500  to currentbar begin
Jcount = mod(J,burstLength);
	
	if SWcount=1 and Jcount = 0 then  SWcount = 0 else
	if SWcount=0 and Jcount = 0 then  SWcount = 1;

	if SWcount =0 then  SWGate = 1 else SWGate = 0;


SWGate = mod(SWcount,2) ;
SWGen  = SWCycle ;  {* SWGate;}
end;


{Select sinewave test pattern or real prices}
Price = SWGen;
{Price = Prices;}


Smooth = (Price + 2*Price[1] + 2*Price[2] + Price[3])/6;
Cycle  = (1-0.5*alpha)*(1-0.5*alpha)*(Smooth - 2*Smooth[1] 
	   + Smooth[2] + 2*(1-alpha)*Cycle[1] - (1-alpha)*(1-alpha)*Cycle[2]);  {Last ")" not in text}

if currentbar < 7 then Cycle = (Price - 2*Price[1] + Price[2])/4;
{Cycle = Price}
{Hilbert Transform}
Q1 = (0.0962*Cycle + 0.5769*Cycle[2] - 0.5769*Cycle[4] 
	 - 0.0962*Cycle[6]) * (0.5 + 0.08*InstPeriod[1]);
I1 = Cycle[3];


If Q1 <> 0 and Q1[1] <>0 then DeltaPhase = (I1/Q1- I1[1]/Q1[1]) / (1+ I1*I1[1]/(Q1*Q1[1]));

If DeltaPhase < 0.1 then DeltaPhase = 0.1;
If DeltaPhase > 1.1 then DeltaPhase = 1.1;
MedianDelta = Median(DeltaPhase,5);

If MedianDelta = 0 then DC=15 else DC = (6.28318/MedianDelta) + 0.5;

InstPeriod = 0.33*DC + 0.67*InstPeriod[1];
Value1 = 0.15*InstPeriod + 0.85*Value1[1];


{Compute Dominant Cycle Phase}
DCPeriod = IntPortion(Value1);
RealPart = 0;
ImagPart = 0;
For count=0 to DCPeriod-1 begin
	RealPart = RealPart + sine(360*count/DCPeriod) *Cycle[count];
	ImagPart = ImagPart + Cosine(360*count / DCPeriod) * (Cycle[count]);
end;

If absValue(ImagPart) > 0.001 then DCPhase = ArcTangent(RealPart/ImagPart);
If absValue(ImagPart) <= 0.001 then DCPhase = 90*Sign(RealPart);

   DCPhase = DCPhase + 90;
   if ImagPart < 0 then DCPhase = DCPhase + 180;
   if DCPhase > 345 then DCPhase = DCPhase - 360;


SineWave = Sine(DCPhase);
LeadSine = Sine(DCPhase+45);	


Plot1(Price,"Price");


{Plot2(SWCycle,"SWCycle");}
{Plot3(SWcount,"SWGate");}
{Plot2(I1,"IP");}
{Plot3(Q1,"IQ");}
Plot2(SineWave,"SW");
Plot3(LeadSine,"LS");
{Plot3(RealPart,"Re");}
{Plot4(ImagPart,"Im");}

{Plot4(DCPhase/200,"DP");}
{Plot4(DCPhase/100,"Phase");}
{Plot2(DCPeriod,"Period");}