Ehlers Adaptive Cyber Cycle

Kant-en-klare, geteste indicatoren en handelssystemen
Plaats reactie
Eric
Berichten: 2936
Lid geworden op: za sep 10, 2005 2:41 am
Locatie: Den Haag

Ehlers Adaptive Cyber Cycle

Bericht door Eric » vr apr 24, 2009 10:36 pm

In zijn boek "Cybernetic Analysis for Stock and Futures" beschrijft Ehlers op pag. 124 e.v. de Adaptive Cyber Cycle indicator.

Code: Selecteer alles

{- Filename: Ehlers Adaptive Cycle -}

function MedianSeries(Series: TSeries; Length: integer): TSeries;
{$IFDEF SCRIPTVERSION-3}
var
  Count, i, j: integer;
  Changed: boolean;
  tmp: real;
  TmpSeries: TSeries;
begin
  Count := GetArrayLength(Series);
  Result := CreateSeries(Count);
  TmpSeries := CreateSeries(Length);

  for i:=Length-1 to Count-1 do
  begin
    for j:=0 to Length-1 do TmpSeries[j] := Series[i-j];
    repeat
      Changed := false;
      for j:=1 to Length-1 do
      begin
        if TmpSeries[j-1] < TmpSeries[j] then
        begin
          tmp := TmpSeries[j];
          TmpSeries[j] := TmpSeries[j-1];
          TmpSeries[j-1] := tmp;
          Changed := true;
        end;
      end;
    until not Changed;
    if Length mod 2 = 0 then
      Result[i] := (TmpSeries[Length div 2] + TmpSeries[Length div 2 - 1])/2
    else
      Result[i] := TmpSeries[Length div 2]
  end;
{$ELSE}
begin
  Result := Median(Series, Length);
{$ENDIF}
end;

function MedianX(S: TSeries; Period, Index: Integer): real;
var
  sTemp: TSeries;
  i: integer;
begin
  sTemp := CreateSeries(Period);
  for i:=0 to Period-1 do sTemp[i] := S[Index-i];
  sTemp := Median(sTemp, Period);
  Result := sTemp[Period-1];
end;

var
  HL, Smooth, Cycle, AdaptCycle, Q1, I1, DP: TSeries;
  DC, IP, DeltaPhase, MedianDelta, Period, Alpha, Alpha1, C1, C2: real;
  i: integer;
begin
  Alpha := CreateParameterReal('Alpha', 0, 1, 0.07, true);

  with Indicator Do
  begin
    RequiredBars := 300;
  end;

  HL := DivideSeriesBy((AddSeries(High, Low)), 2);
  Smooth := FillSeries(CreateSeries(BarCount), 0);
  Cycle := FillSeries(CreateSeries(BarCount), 0);
  AdaptCycle := FillSeries(CreateSeries(BarCount), 0);
  DP := FillSeries(CreateSeries(Barcount), 0);
  Q1 := FillSeries(CreateSeries(BarCount), 0);
  I1 := FillSeries(CreateSeries(Barcount), 0);
  C1 := 0.0962;
  C2 := 0.5769;

  for i := 6 to BarCount-1 do
  begin
    Smooth[i] := (HL[i]+HL[i-1]*2+HL[i-2]*2+HL[i-3])/6;
    Cycle[i] := (sqr(1-Alpha/2)) * (Smooth[i] - 2*Smooth[i-1] + Smooth[i-2])
                  + 2*(1-Alpha)*Cycle[i-1] - sqr(1-Alpha)*Cycle[i-2];
    Q1[i] := (Cycle[i]*C1 + Cycle[i-2]*C2 - Cycle[i-4]*C2 - Cycle[i-6]*C1)
               * (0.5+IP*0.08);
    I1[i] := Cycle[i-3];
    if Q1[i]*Q1[i-1] <> 0 then
      DeltaPhase := (I1[i]/Q1[i] - I1[i-1]/Q1[i-1]) / (1 + I1[i]*I1[i-1]/(Q1[i]*Q1[i-1]));

    If DeltaPhase < 0.1 then DeltaPhase := 0.1 ;
    If DeltaPhase > 1.1 then DeltaPhase := 1.1;
    DP[i] := DeltaPhase;
    MedianDelta := MedianX(DP, 5, i);
    if MedianDelta=0 then
      DC := 15
    else
      DC := 6.2831852/MedianDelta + 0.5;
      
    IP := 0.33*DC + 0.67*IP;
    Period := 0.15*IP + 0.85*Period;
    
    Alpha1 := 2 / (Period + 1);
    AdaptCycle[i] := (sqr(1-Alpha1/2)) * (Smooth[i] - 2*Smooth[i-1] + Smooth[i-2])
                  + 2*(1-Alpha1)*AdaptCycle[i-1] - sqr(1-Alpha1)*AdaptCycle[i-2];
  end;

  with CreateLine(AdaptCycle) do
  begin
    Name := 'Adaptive Cycle';
    Color := clLime;
  end;
  with CreateLine(ShiftSeries(AdaptCycle, 1)) do
  begin
    Name := 'Trigger';
    Color := clRed;
  end;
end.
---
Eric

Plaats reactie