/*[[ Name := RSTD_Instataneous Trendline Author := Luis Damiani et al Notes := Instantaneous Trend line Separate Window := no First Color := DodgerBlue First Draw Type := Line First Symbol := 217 Use Second Data := yes Second Color := red Second Draw Type := Line Second Symbol := 218 ]]*/ input: maxbars(300),Cycle_adj(1.1),MA_0zema_1WMA(0),alpha(0.1); Variable : shift(0), prevbars(0), first(True), loopbegin(0),comm(0), i(0), Detrender(0), I1(0), Q1(0), jI(0), jQ(0), CurI2(0), CurQ2(0), I2p(0), Q2p(0), I2(0), Q2(0), CurRe(0), CurIm(0), Re(0), Im(0),pos(0), CurPer(0), Per(0), SmoothPeriod(0); var: DCPeriod(0),cnt(0),Trendline(0),wma(0),chksum(0); Arrays : I1p[7](0), Q1p[7](0), Detp[7](0),ITrend[7](0); SetLoopCount(0); // check conditions are ok. if Bars < 6 or prevbars = bars then exit; // check for additional bars loading or total reloading If Bars > prevbars Or comm!=Symbol Then first = True; prevbars = bars; comm=symbol; If first Then Begin // loopbegin prevent counting of counted bars exclude current loopbegin = maxbars-6; first = False; SmoothPeriod = Per; End Else Begin loopbegin = Bars-prevbars; End; // loop from first bar to the current bar For shift=loopbegin Downto 0 Begin if shift > 0 then begin ITrend[1]=ITrend[0]; for i=6 DownTo 2 begin I1p[i] = I1p[i-1]; Q1p[i] = Q1p[i-1]; Detp[i] = Detp[i-1]; ITrend[i]=ITrend[i-1]; end; end; I2p = I2; Q2p = Q2; //HILBERT TRANSFORM BEGIN // Smothing and calculating detrender Detrender = (0.0962*iMAEx(4, MODE_LWMA, 0, PRICE_MEDIAN, shift)+ 0.5769*iMAEx(4, MODE_LWMA, 0, PRICE_MEDIAN, shift+2) - 0.5769*iMAEx(4, MODE_LWMA, 0, PRICE_MEDIAN, shift+4)- 0.0962*iMAEx(4, MODE_LWMA, 0, PRICE_MEDIAN, shift+6)) * (0.075*Per + 0.54); // Compute InPhase and Quadrature components I1 = Detp[3]; Q1 = (0.0962*Detrender + 0.5769*Detp[2] - 0.5769*Detp[4] - 0.0962*Detp[6]) * (0.075*Per + 0.54); if shift > 0 then begin I1p[1] = I1; Q1p[1] = Q1; Detp[1] = Detrender; end; //HILBERT TRANSFORM END // Advance the phase of I1 and Q1 by 90 degrees jI = (0.0962*I1 + 0.5769*I1p[2] - 0.5769*I1p[4] - 0.0962*I1p[6])*(0.075*Per + 0.54); jQ = (0.0962*Q1 + 0.5769*Q1p[2] - 0.5769*Q1p[4] - 0.0962*Q1p[6])*(0.075*Per + 0.54); // Phasor addition for 3 bar averaging CurI2 = I1 - jQ; CurQ2 = Q1 + jI; // Smooth the I and Q components before applying the discriminator I2 = 0.2*CurI2 + 0.8*I2; Q2 = 0.2*CurQ2 + 0.8*Q2; // Homodyne Discriminator CurRe = I2*I2p + Q2*Q2p; CurIm = I2*Q2p - Q2*I2p; Re = 0.2*CurRe + 0.8*Re; Im = 0.2*CurIm + 0.8*Im; if (Im != 0 and Re != 0) then CurPer = 6.2832 / ArcTan(Im/Re); if (CurPer > 1.5*Per) then CurPer = 1.5*Per; if (CurPer < 0.67*Per) then CurPer = 0.67*Per; if CurPer < 6 then CurPer = 6.0; if CurPer > 50 then CurPer = 50.0; Per = 0.2*CurPer + 0.8*Per; SmoothPeriod = 0.33*Per + 0.67*SmoothPeriod; //Compute Trendline as simple average over the measured dominant cycle Period DCPeriod = Floor(Cycle_adj*SmoothPeriod+0.5); ITrend[0]=0; for cnt= 0 to DCPeriod-1 Begin ITrend[0]= ITrend[0]+(h[cnt+shift]+l[cnt+shift])/2; end; if DCPeriod >0 then ITrend[0]=Itrend[0]/DCPeriod; Trendline=(4*ITrend[0]+3*ITrend[1]+2*ITrend[2]+ITrend[3])/10; If bars-shift<12 then Trendline=(h[shift]+l[shift])/2; if( MA_0zema_1WMA=0) then wma=alpha*((h[shift]+l[shift])/2 + 0.5*((h[shift]+l[shift])/2-(h[shift+3]+l[shift+3])/2)) + (1-alpha)*wma Else wma=iMAEx(4, MODE_LWMA, 0, PRICE_MEDIAN, shift); //Comment((h[0]+l[0])/2); // if showlag=1 then pos=shift+9 else pos=shift; pos=shift; SetIndexValue(pos, Trendline); SetIndexValue2(pos, wma); //loopbegin--; End;