/*[[ Name := MESA Author := Aborigen Separate Window := No First Color := Aqua First Draw Type := Line First Symbol := 217 Use Second Data := Yes Second Color := Chocolate Second Draw Type := Line Second Symbol := 218 ]]*/ Vars: jI(0), jQ(0), DeltaPhase(0), alpha(0),Shift(0),ttime(0); Array: Price[4](0),Smooth[7](0),Detrender[7](0), Q1[7](0),I1[7](0), I2[2](0), Q2[2](0), Re[2](0), Im[2](0), SmoothPeriod[2](0), Period_[2](0), Phase[2](0), MAMA[2](0), FAMA[2](0); Inputs: FastLimit(0.5), SlowLimit(0.05); if ttime=time then exit; ttime=time; SetLoopCount(0); // loop from first bar to current bar (with shift=0) For shift = 500 Downto 0 Begin Price[1]=((H[shift]+L[shift])/2); Price[2]=((H[shift+1]+L[shift+1])/2); Price[3]=((H[shift+2]+L[shift+2])/2); Price[4]=((H[shift+3]+L[shift+3])/2); Smooth[1] = (4*Price[1] + 3*Price[2] + 2*Price[3] + Price[4]) / 10; Detrender[1] = (0.0962*Smooth[1] + 0.5769*Smooth[3] - 0.5769*Smooth[5] - 0.0962*Smooth[7])*(0.075*Period_[2] + 0.54); // {Compute InPhase and Quadrature components} Q1[1] = (0.0962*Detrender[1] + 0.5769*Detrender[3] - 0.5769*Detrender[5] - 0.0962*Detrender[7])*(0.075*Period_[2] + 0.54); I1[1] = Detrender[4]; // {Advance the phase of I1 and Q1 by 90 degrees} jI = (0.0962*I1[1] + 0.5769*I1[3] - 0.5769*I1[5] - 0.0962*I1[7])*(0.075*Period_[2] + 0.54); jQ = (0.0962*Q1[1] + 0.5769*Q1[3] - 0.5769*Q1[5] - 0.0962*Q1[7])*(0.075*Period_[2] + 0.54); // {Phasor addition for 3 bar averaging)} I2[1] = I1[1] - jQ; Q2[1] = Q1[1] + jI; // {Smooth the I and Q components before applying the discriminator} I2[1] = 0.2*I2[1] + 0.8*I2[2]; Q2[1] = 0.2*Q2[1] + 0.8*Q2[2]; // {Homodyne Discriminator} Re[1] = I2[1]*I2[2] + Q2[1]*Q2[2]; Im[1] = I2[1]*Q2[2] - Q2[1]*I2[2]; Re[1] = 0.2*Re[1] + 0.8*Re[2]; Im[1] = 0.2*Im[1] + 0.8*Im[2]; If Im[1] <> 0 and Re[1] <> 0 then Period_ [1]= 360/ArcTan(Im[1]/Re[1]); If Period_[1] > 1.5*Period_[2] then Period_[1] = 1.5*Period_[2]; If Period_[1] < 0.67*Period_[2] then Period_[1] = 0.67*Period_[2]; If Period_[1] < 6 then Period_[1] = 6; If Period_[1] > 50 then Period_[1] = 50; Period_[1] = 0.2*Period_[1] + 0.8*Period_[2]; SmoothPeriod[1] =0.33*Period_[1] + 0.67*SmoothPeriod[2]; If I1[1] <> 0 then Phase[1] = (ArcTan(Q1[1] / I1[1])); DeltaPhase = Phase[2] - Phase[1]; If DeltaPhase < 1 then DeltaPhase = 1; alpha = FastLimit / DeltaPhase; If alpha < SlowLimit then alpha = SlowLimit; MAMA[1] = alpha*Price[1] + (1 - alpha)*MAMA[2]; FAMA[1] = 0.5*alpha*MAMA[1] + (1 - 0.5*alpha)*FAMA[2]; If shift<450 then Begin SetIndexValue(shift,MAMA[1]); SetIndexValue2(shift,FAMA[1]); end; Smooth[7]=Smooth[5];Smooth[6]=Smooth[5];Smooth[5]=Smooth[4];Smooth[4]=Smooth[3];Smooth[3]=Smooth[2];Smooth[2]=Smooth[1]; Detrender[7]=Detrender[6];Detrender[6]=Detrender[5];Detrender[5]=Detrender[4];Detrender[4]=Detrender[3];Detrender[3]=Detrender[2];Detrender[2]=Detrender[1]; Q1[7]=Q1[6];Q1[6]=Q1[5];Q1[5]=Q1[4];Q1[4]=Q1[3];Q1[3]=Q1[2];Q1[2]=Q1[1]; I1[7]=I1[6];I1[6]=I1[5];I1[5]=I1[4];I1[4]=I1[3];I1[3]=I1[2];I1[2]=I1[1]; Q2[2]=Q2[1]; I2[2]=I2[1]; Re[2]=Re[1]; Im[2]=Im[1]; SmoothPeriod[2]=SmoothPeriod[1]; Phase[2]=Phase[1]; Period_[2]=Period_[1]; MAMA[2]=MAMA[1]; FAMA[2]=FAMA[1]; End;