Jump to content

Welcome to the new Traders Laboratory! Please bear with us as we finish the migration over the next few days. If you find any issues, want to leave feedback, get in touch with us, or offer suggestions please post to the Support forum here.

  • Welcome Guests

    Welcome. You are currently viewing the forum as a guest which does not give you access to all the great features at Traders Laboratory such as interacting with members, access to all forums, downloading attachments, and eligibility to win free giveaways. Registration is fast, simple and absolutely free. Create a FREE Traders Laboratory account here.

Recommended Posts

{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"); 

 

_Oscillators function:

{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; 

 

_cycleperiod function

{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; 

Adaptive_CCI.gif.8b1f12826c065036210c034a328f2b00.gif

Share this post


Link to post
Share on other sites
Thanks for posting someone elses code from the Tradestation Forum.:haha:

 

Lots of people (Multicharts users for example) don't have access to TS forums, so having an alternate source is handy.

 

Just one thing Tams, this looks like it's a lot more than just a CCI, dont know if you can change the thread title.

Share this post


Link to post
Share on other sites
I am looking for working Adaptive CCI indicators for MetaTrader4.

Can somebody help me?

Would be great!

 

Cheers,

Jazz

 

Nobody have this adaptive CCI in Metatrader, or knows where to get it?

 

Best regards,

Jazz

Share this post


Link to post
Share on other sites
Lots of people (Multicharts users for example) don't have access to TS forums, so having an alternate source is handy.

 

Just one thing Tams, this looks like it's a lot more than just a CCI, dont know if you can change the thread title.

 

the first block of code is the indicator,

the other 2 are functions that the indicator calls.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Similar Content

  • Topics

  • Posts

    • ORLY OReilly Automotive stock, nice top of range breakout, from Stocks to Watch at https://stockconsultant.com/?ORLY
    • Date: 28th March 2025.   Market Selloff Deepens as Tariff Concerns Weigh on Investors     Global stock markets extended their losing streak for a third day as concerns over looming US tariffs and an escalating trade war dampened investor sentiment. The flight to safety saw gold prices surge to a record high, underscoring growing risk aversion. Stock Selloff Intensifies The MSCI World Index recorded its longest losing streak in a month, while Asian equities saw their sharpest decline since late February. US and European stock futures also signalled potential weakness, while cryptocurrency markets retreated and bond yields edged lower. Investors are scaling back their exposure ahead of President Donald Trump’s expected announcement of ‘reciprocal tariffs’ on April 2. His latest move to impose a 25% levy on all foreign-made automobiles has sparked fresh concerns over inflation and economic growth, prompting traders to reassess their strategies. Investor Strategies Shift Market experts are adjusting their portfolios in anticipation of heightened volatility. ‘It’s impossible to predict Trump’s next move,’ said Xin-Yao Ng of Aberdeen Investments. ‘Our focus is on companies that are less vulnerable to tariff policies while taking advantage of market dips to find value opportunities.’ Yield Curve Signals Economic Concerns In the bond market, the spread between 30-year and 5-year US Treasury yields widened to its highest level since early 2022. Investors are bracing for potential Federal Reserve rate cuts if economic growth slows further. Long-term Treasury yields hit a one-month peak as inflation risks tied to tariffs spurred demand for higher-yielding assets. Boston Fed President Susan Collins noted that while tariffs may contribute to short-term price increases, their long-term effects remain uncertain. Gold Hits Record High as Safe-Haven Demand Rises Amid market turbulence, gold prices soared 0.7% on Friday, reaching an all-time high of $3,077.60 per ounce. Major banks have raised their price targets for the precious metal, with Goldman Sachs now forecasting gold to hit $3,300 per ounce by year-end. Looking Ahead As investors digest economic data showing US growth acceleration in Q4, attention will turn to Friday’s release of the personal consumption expenditures (PCE) price index—the Federal Reserve’s preferred inflation measure. This data will be critical in shaping expectations for future Fed policy moves. With markets on edge and trade tensions escalating, investors will closely monitor upcoming developments, particularly Trump’s tariff announcement next week, which could further dictate market direction.   Always trade with strict risk management. Your capital is the single most important aspect of your trading business.   Please note that times displayed based on local time zone and are from time of writing this report.   Click HERE to access the full HFM Economic calendar.   Want to learn to trade and analyse the markets? Join our webinars and get analysis and trading ideas combined with better understanding of how markets work. Click HERE to register for FREE!   Click HERE to READ more Market news.   Andria Pichidi HFMarkets   Disclaimer: This material is provided as a general marketing communication for information purposes only and does not constitute an independent investment research. Nothing in this communication contains, or should be considered as containing, an investment advice or an investment recommendation or a solicitation for the purpose of buying or selling of any financial instrument. All information provided is gathered from reputable sources and any information containing an indication of past performance is not a guarantee or reliable indicator of future performance. Users acknowledge that any investment in Leveraged Products is characterized by a certain degree of uncertainty and that any investment of this nature involves a high level of risk for which the users are solely responsible and liable. We assume no liability for any loss arising from any investment made based on the information provided in this communication. This communication must not be reproduced or further distributed without our prior written permission.
    • Crypto hype is everywhere since it also making new riches as well, i however trade crypto little as compared to other forex trading pairs.
    • The ewallets can be instant withdrawals like skrill etc or they can also pay through crypto but not tested their crypto withdrawals so far.
    • I noticed that this broker has also started crypto cfds to trade. Crypto fever is almost everywhere in my opinion and the traders making good sums of money too.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.