This is the Ehlers’ adaptive Stochastic code. The modified John Ehlers version of the Stochastic adapt the classical Stochastic period dynamically adapted to the market cycle. It tends to give more better indication as the standard Stochastic indicator itself.
//c = 0.5
pri=Customclose
CycPart=c
If Barindex > 5 Then
Smooth=(4*pri+3*pri[1]+2*pri[2]+pri[3])/10
Detrender=(0.0962*Smooth+0.5769*Smooth[2]-0.5769*Smooth[4]-0.0962*Smooth[6])*(0.075*Period[1]+0.54)
REM Compute InPhase & Quadrature Components
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]
REM Advance The Phase Of I1 & Q1 By 90 Degrees
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)
REM Phasor Addition
I2=I1-jQ
Q2=Q1+jI
REM Smooth The I & Q Components
I2=0.2*I2+0.8*I2[1]
Q2=0.2*Q2+0.8*Q2[1]
REM Homodyne Discriminator
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/ATAN(Im/Re)
Endif
If Period > 1.5*Period[1] Then
Period=1.5*Period[1]
Endif
If Period < 0.67*Period[1] Then
Period=0.67*Period[1]
Endif
If Period < 6 Then
Period=6
Endif
If Period > 50 Then
Period=50
Endif
Period=0.2*Period+0.8*Period[1]
SmoothPeriod=0.33*Period+0.67*SmoothPeriod[1]
HH=High
LL=Low
For count=0 To Round(CycPart*SmoothPeriod)-1
If High[count] > HH Then
HH=High[count]
Endif
If Low[count] < LL Then
LL=Low[count]
Endif
Next
If HH-LL <> 0 Then
ASto=(Close-LL)/(HH-LL)*100
Endif
Endif
Return ASto as "Adaptive Stochastic", 50, 80, 20