adaptive cci codes
Posted: 27 Feb 2009
hello;
does any one has easy language code for adaptive cci as per John Ehlers book.?
does any one has easy language code for adaptive cci as per John Ehlers book.?
MultiCharts
https://www.multicharts.com/discussion/
https://www.multicharts.com/discussion/viewtopic.php?f=5&t=6109
Code: Select all
{Adaptive CCI
From the book 'Rocket Science for Traders' by John Ehlers
modified with new cycle measurement method from 'Cybernetic Analysis for Stocks and Futures'
mmillar, July 2004
Price - the current price - only used by the Cycle Period measurement, not by the CCI calculation
Length - used by the Cycle Period measurement - John Ehlers uses alpha but I have replaced it with the more intuitive Length, where alpha=2/(Length+1)
CycPart - allows you to change how much of the cycle period should be used in the CCI calculation - usually 1
}
Inputs: Price((H+L)/2), Length(19), CycPart(1);
Vars: oResult1(0), oResult2(0);
value1=_Oscillators(18, Price, Length, CycPart, 1, 0, 1, oResult1, oResult2);
Plot1(oResult1, "AdaptCCI");
Code: Select all
{A collection of oscillators by John Ehlers
by mmillar, July 2004
1-15 are taken from 'Cybernetic Analysis for Stocks and Futures'
16-18 are taken from 'Rocket Science for Traders' and updated using a new cycle period measurement method
This function calls the function '_CyclePeriod' to calculate the Dominant Cycle for use in adaptive strategies
Oscillator Types
1 - Cyber Cycle
2 - CG Oscillator
3 - Relative Vigor Index (RVI)
4 - Stochastic RSI
5 - Stochastic Cyber Cycle
6 - Stochastic CG
7 - Stochastic RVI
8 - Fisher Cyber Cycle
9 - Fisher CG
10 - Fisher RVI
11 - Adaptive Cyber Cycle
12 - Adaptive CG
13 - Adaptive RVI
14 - Sinewave Indicator
15 - Laguerre RSI
16 - Adaptive RSI
17 - Adaptive Stochastic
18 - Adaptive CCI
This function is called with
OscType - one of the above
Price - some indicators can use a price input e.g. (H+L)/2, Close etc, otherwise this can be set to 0
Length - the length or period that you wish to measure. In some instances John Ehlers uses 'alpha' as his
input but I have standardised all inputs using Length and then converted, where necessary, with
alpha=2/(Length+1)
Length2 - this is a catch all used when an additional input variable is needed (just because everything is done in one function)
RSILength, StocLength, WMALength - only used by Stochastic RSI (OscType=4)
oResult1, oResult2 - are the results returned by the function
}
Inputs: OscType(Numeric), Price(Numeric), Length(Numeric), Length2(Numeric), {general inputs}
RSILength(Numeric), StocLength(Numeric), WMALength(Numeric), {only used by OscType=4}
oResult1(NumericRef), oResult2(NumericRef); {results to return}
Vars: alpha(0),
count(0), Num(0), Denom(0),
Smooth(0), Cycle(0), {for OscType=1, 5, 8, 11}
CG(0), {for OscType=2, 6, 9, 12}
RVI(0), {for OscType=3, 7, 10, 13}
MaxVal(0), MinVal(0), {for OscType=5-10}
Period(0), {for OscType=11-18}
Cycle1(0), {for OscType=11-18 but only used in 14}
Smooth1(0), {for OscType=11-18 but only used in 11}
DCPeriod(0), RealPart(0), ImagPart(0), DCPhase(0), {for OscType=14}
L0(0), L1(0), L2(0), L3(0), {for OscType=15}
vRSI(0), CU(0), CD(0), {for OscType=15, 16}
vStoch(0), HH(0), LL(0), {for OscType=17}
vCCI(0), Avg(0), MD(0), MPrice(0); {for OscType=18}
If ( OscType=1 ) then Begin {Cyber Cycle}
alpha=2/(Length+1);
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];
If currentbar<7 then Cycle=(Price-2*Price[1]+Price[2])/4;
oResult1=Cycle;
oResult2=Cycle[1];
end
else If ( OscType=2 ) then Begin {CG Oscillator}
Num=0;
Denom=0;
For count=0 to Length-1 Begin
Num=Num+(1+count)*Price[count];
Denom=Denom+Price[count];
end;
If Denom<>0 then CG=-Num/Denom+(Length+1)/2;
oResult1=CG;
oResult2=CG[1];
end
else If ( OscType=3 ) then Begin {Relative Vigor Index}
Value1=((Close-Open)+2*(Close[1]-Open[1])+2*(Close[2]-Open[2])+(Close[3]-Open[3]))/6;
Value2=((High-Low)+2*(High[1]-Low[1])+2*(High[2]-Low[2])+(High[3]-Low[3]))/6;
Num=0;
Denom=0;
For count=0 to Length-1 Begin
Num=Num+Value1[count];
Denom=Denom+Value2[count];
end;
If Denom<>0 then RVI=Num/Denom;
oResult1=RVI;
oResult2=RVI[1];
end
else If ( OscType=4 ) then Begin {Stochastic RSI}
Value1=RSI(Close, RSILength)-Lowest(RSI(Close, RSILength), StocLength);
Value2=Highest(RSI(Close, RSILength), StocLength)-Lowest(RSI(Close, RSILength), StocLength);
If Value2<>0 then Value3=Value1/Value2;
Value4=2*(WAverage(Value3, WMALength)-0.5);
oResult1=Value4;
oResult2=Value4[1];
end
else If ( OscType=5 ) then Begin {Stochastic Cyber Cycle}
alpha=2/(Length+1);
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];
If currentbar<7 then Cycle=(Price-2*Price[1]+Price[2])/4;
MaxVal=Highest(Cycle, StocLength);
MinVal=Lowest(Cycle, StocLength);
If MaxVal<>MinVal then Value1=(Cycle-MinVal)/(MaxVal-MinVal);
Value2=(4*Value1+3*Value1[1]+2*Value1[2]+Value1[3])/10;
Value2=2*(Value2-0.5);
oResult1=Value2;
oResult2=0.96*(Value2[1]+0.02);
end
else If ( OscType=6 ) then Begin {Stochastic CG}
Num=0;
Denom=0;
For count=0 to Length-1 Begin
Num=Num+(1+count)*Price[count];
Denom=Denom+Price[count];
end;
If Denom<>0 then CG=-Num/Denom+(Length+1)/2;
MaxVal=Highest(CG, Length);
MinVal=Lowest(CG, Length);
If MaxVal<>MinVal then Value1=(CG-MinVal)/(MaxVal-MinVal);
Value2=(4*Value1+3*Value1[1]+2*Value1[2]+Value1[3])/10;
Value2=2*(Value2-0.5);
oResult1=Value2;
oResult2=0.96*(Value2[1]+0.02);
end
else If ( OscType=7 ) then Begin {Stochastic RVI}
Value1=((Close-Open)+2*(Close[1]-Open[1])+2*(Close[2]-Open[2])+(Close[3]-Open[3]))/6;
Value2=((High-Low)+2*(High[1]-Low[1])+2*(High[2]-Low[2])+(High[3]-Low[3]))/6;
Num=0;
Denom=0;
For count=0 to Length-1 Begin
Num=Num+Value1[count];
Denom=Denom+Value2[count];
end;
If Denom<>0 then RVI=Num/Denom;
MaxVal=Highest(RVI, Length);
MinVal=Lowest(RVI, Length);
If MaxVal<>MinVal then Value3=(RVI-MinVal)/(MaxVal-MinVal);
Value4=(4*Value3+3*Value3[1]+2*Value3[2]+Value3[3])/10;
Value4=2*(Value4-0.5);
oResult1=Value4;
oResult2=0.96*(Value4[1]+0.02);
end
else If ( OscType=8 ) then Begin {Fisher Cyber Cycle}
alpha=2/(Length+1);
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];
If currentbar<7 then Cycle=(Price-2*Price[1]+Price[2])/4;
MaxVal=Highest(Cycle, Length2);
MinVal=Lowest(Cycle, Length2);
If MaxVal<>MinVal then Value1=(Cycle-MinVal)/(MaxVal-MinVal);
Value2=(4*Value1+3*Value1[1]+2*Value1[2]+Value1[3])/10;
Value3=0.5*Log((1+1.98*(Value2-0.5))/(1-1.98*(Value2-0.5)));
oResult1=Value3;
oResult2=Value3[1];
end
else If ( OscType=9 ) then Begin {Fisher CG}
Num=0;
Denom=0;
For count=0 to length-1 Begin
Num=Num+(1+count)*(Price[count]);
Denom=Denom+(Price[count]);
end;
If Denom<>0 then CG=-Num/Denom+(Length+1)/2;
MaxVal=Highest(CG, Length);
MinVal=Lowest(CG, Length);
If MaxVal<>MinVal then Value1=(CG-MinVal)/(MaxVal-MinVal);
Value2=(4*Value1+3*Value1[1]+2*Value1[2]+Value1[3])/10;
Value3=0.5*Log((1+1.98*(Value2-0.5))/(1-1.98*(Value2-0.5)));
oResult1=Value3;
oResult2=Value3[1];
end
else If ( OscType=10 ) then Begin {Fisher RVI}
Value1=((Close-Open)+2*(Close[1]-Open[1])+2*(Close[2]-Open[2])+(Close[3]-Open[3]))/6;
Value2=((High-Low)+2*(High[1]-Low[1])+2*(High[2]-Low[2])+(High[3]-Low[3]))/6;
Num=0;
Denom=0;
For count=0 to Length-1 Begin
Num=Num+Value1[count];
Denom=Denom+Value2[count];
end;
If Denom<>0 then RVI=Num/Denom;
MaxVal=Highest(RVI, Length);
MinVal=Lowest(RVI, Length);
If MaxVal<>MinVal then Value3=(RVI-MinVal)/(MaxVal-MinVal);
Value4=(4*Value3+3*Value3[1]+2*Value3[2]+Value3[3])/10;
Value5=0.5*Log((1+1.98*(Value4-0.5))/(1-1.98*(Value4-0.5)));
oResult1=Value5;
oResult2=Value5[1];
end
else If ( OscType=11 ) then Begin {Adaptive Cyber Cycle}
alpha=2/(Length+1);
Period=_CyclePeriod(Price, alpha, Cycle1, Smooth1);
alpha=2/(Period+1);
Cycle=(1-0.5*alpha)*(1-0.5*alpha)*(Smooth1-2*Smooth1[1]+Smooth1[2])+2*(1-alpha)*Cycle[1]-(1-alpha)*(1-alpha)*Cycle[2];
If currentbar<7 then Cycle=(Price-2*Price[1]+Price[2])/4;
oResult1=Cycle;
oResult2=Cycle[1];
end
else If ( OscType=12 ) then Begin {Adaptive CG}
alpha=2/(Length+1);
Period=_CyclePeriod(Price, alpha, Cycle1, Smooth1);
Value1=IntPortion(Period/2); {use half the cycle period}
Num=0;
Denom=0;
For count=0 to Value1-1 Begin
Num=Num+(1+count)*(Price[count]);
Denom=Denom+(Price[count]);
end;
If Denom<>0 then CG=-Num/Denom+(Value1+1)/2;
oResult1=CG;
oResult2=CG[1];
end
else If ( OscType=13 ) then Begin {Adaptive RVI}
alpha=2/(Length+1);
Period=_CyclePeriod(Price, alpha, Cycle1, Smooth1);
Value3=IntPortion((4*Period+3*Period[1]+2*Period[3]+Period[4])/20);
Value1=((Close-Open)+2*(Close[1]-Open[1])+2*(Close[2]-Open[2])+(Close[3]-Open[3]))/6;
Value2=((High-Low)+2*(High[1]-Low[1])+2*(High[2]-Low[2])+(High[3]-Low[3]))/6;
Num=0;
Denom=0;
For count=0 to Value3-1 Begin
Num=Num+Value1[count];
Denom=Denom+Value2[count];
end;
If Denom<>0 then RVI=Num/Denom;
oResult1=RVI;
oResult2=RVI[1];
end
else If ( OscType=14 ) then Begin {Sinewave Indicator}
alpha=2/(Length+1);
Period=_CyclePeriod(Price, alpha, Cycle1, Smooth1);
DCPeriod=IntPortion(Period);
RealPart=0;
ImagPart=0;
For count=0 to DCPeriod-1 Begin
RealPart=RealPart+Sine(360*count/DCPeriod)*(Cycle1[count]);
ImagPart=ImagPart+Cosine(360*count/DCPeriod)*(Cycle1[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>315 then DCPhase=DCPhase-360;
oResult1=Sine(DCPhase);
oResult2=Sine(DCPhase+45);
end
else If ( OscType=15 ) then Begin {Laguerre RSI}
L0=(1-Length2)*Close+Length2*L0[1];
L1=-Length2*L0+L0[1]+Length2*L1[1];
L2=-Length2*L1+L1[1]+Length2*L2[1];
L3=-Length2*L2+L2[1]+Length2*L3[1];
CU=0;
CD=0;
If L0>=L1 then CU=L0-L1 else CD=L1-L0;
If L1>=L2 then CU=CU+L1-L2 else CD=CD+L2-L1;
If L2>=L3 then CU=CU+L2-L3 else CD=CD+L3-L2;
If CU+CD<>0 then vRSI=CU/(CU+CD);
oResult1=vRSI;
oResult2=0;
end
else If ( OscType=16 ) then Begin {Adaptive RSI}
alpha=2/(Length+1);
Period=_CyclePeriod(Price, alpha, Cycle1, Smooth1);
CU=0;
CD=0;
For count=0 to (Period*Length2)-1 Begin
If Close[count]-Close[count+1]>0 then CU=CU+(Close[count]-Close[count+1]);
If Close[count]-Close[count+1]<0 then CD=CD+(Close[count+1]-Close[count]);
end;
If CU+CD<>0 then vRSI=100*CU/(CU+CD);
oResult1=vRSI;
oResult2=0;
end
else If ( OscType=17 ) then Begin {Adaptive Stochastic}
alpha=2/(Length+1);
Period=_CyclePeriod(Price, alpha, Cycle1, Smooth1);
HH=High;
LL=Low;
For count=0 to IntPortion(Period*Length2)-1 Begin
If High[count]>HH then HH=High[count];
If Low[count]<LL then LL=Low[count];
end;
If HH-LL<>0 then vStoch=(Close-LL)/(HH-LL);
oResult1=vStoch;
oResult2=0;
end
else If ( OscType=18 ) then Begin {Adaptive CCI}
alpha=2/(Length+1);
Period=_CyclePeriod(Price, alpha, Cycle1, Smooth1);
Value1=IntPortion(Period*Length2);
MPrice=(High+Low+Close)/3;
Avg=0;
For count=0 to Value1-1 Begin
Avg=Avg+MPrice[count];
end;
If Value1<>0 then Avg=Avg/Value1;
MD=0;
For count=0 to Value1-1 Begin
MD=MD+AbsValue(MPrice[count]-Avg);
end;
If Value1<>0 then MD=MD/Value1;
If MD<>0 then vCCI=(MPrice-Avg)/(0.015*MD);
oResult1=vCCI;
oResult2=0;
end;
_Oscillators=1;
Code: Select all
{Cycle Period Measurement
From the book 'Cybernetic Analysis for Stocks and Futures' by John Ehlers
mmillar, July 2004
This function is called with
Price - the current price, such as (H+L)/2 or Close
alpha - the alpha (which is related to the measurement period/length)
oCycle - a return variable that is needed by some indicators
}
Inputs: Price(numeric), alpha(numeric), oCycle(numericref), oSmooth(numericref);
Vars: Smooth(0), Cycle(0), Q1(0), I1(0), DC(0),
DeltaPhase(0), MedianDelta(0), InstPeriod(0), Period(0);
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];
If currentbar<7 then Cycle=(Price-2*Price[1]+Price[2])/4;
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];
Period=0.15*InstPeriod+0.85*Period[1];
oCycle=Cycle;
oSmooth=Smooth;
_CyclePeriod=Period;