inputs: Series(c);

vars:
        smooth(0),
        detrender(0),
        I1(0),Q1(0),
        jI(0),jQ(0),
        I2(0),Q2(0),
        Re(0),Im(0),
        period(0),smoothPeriod(0),
        smoothPrice(0),
        DCPeriod(0),RealPart(0),ImagPart(0),
        count(0),DCPhase(0);
        
if currentbar>5 then begin
        smooth = (4*Series+3*Series[1]+2*Series[2]+Series[3])/10;
        detrender = (0.0962*smooth+0.5769*smooth[2]-0.5769*smooth[4]-0.0962*smooth[6])*(0.075*period[1]+0.54);
        
        Q1=(0.0962*detrender+0.5769*detrender[2]-0.5769*detrender[4]-0.0962*detrender[6])*(0.075*period[1]+0.54);
        I1=detrender[3];

        jI = (0.0962*I1+0.5769*I1[2]-0.5769*I1[4]-0.0962*I1[6])*(0.075*period[1]+0.54);
        jQ = (0.0962*Q1+0.5769*Q1[2]-0.5769*Q1[4]-0.0962*Q1[6])*(0.075*period[1]+0.54);
        
        I2=I1-jQ;
        Q2=Q1+jI;
        
        I2=0.2*I2+0.8*I2[1];
        Q2=0.2*Q2+0.8*Q2[1];
        
        Re=I2*I2[1] + Q2*Q2[1];
        Im=I2*Q2[1] - Q2*I2[1];
        Re=0.2*Re+0.8*Re[1];
        Im=0.2*Im+0.8*Im[1];
        if IM<>0 and RE<>0 then period=360/arctangent(Im/Re);
        
        if period>1.5*period[1] then period=1.5*period[1];
        if period<0.67*period[1] then period=0.67*period[1];
        
        if period<6 then period=6;
        if period>50 then period=50;
        
        period=0.2*period+0.8*period[1];
        smoothPeriod = 0.33*period+0.67*smoothPeriod[1];
        
        smoothPrice = smooth;
        DCPeriod = ceiling(smoothperiod+0.5);
        RealPart=0;
        ImagPart=0;
        for count=0 to DCPeriod-1 begin
                RealPart = RealPart+cosine(360*count/dcperiod)*smoothPrice[count];
                ImagPart = ImagPart+Sine(360*count/dcperiod)*smoothPrice[count];        
        end;
        
        if absvalue(realpart)>0.001 then DCPhase = arctangent(imagpart/realpart);
        if absvalue(realpart)<=0.001 then DCPhase = 90*sign(imagpart);
        
        DCPhase=DCPhase+90;
        
        DCPhase=DCPhase+360/smoothPeriod;
        
        if ImagPart<0 then DCPhase=DCPhase+180;
        if DCPhase>315 then DCPhase=DCPhase-360;
        
        plot1(Sine(dcphase),"sine");
        plot2(sine(dcphase+45),"leadSine");
                                
end;
