Announcement

Collapse
No announcement yet.

Formula for Adaptive CG Oscillator in Ehlers Book not working. Solution?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Formula for Adaptive CG Oscillator in Ehlers Book not working. Solution?

    Hi,
    i typed in the code from Ehlers book "Cybernetic Analysis for Stocks and Futures". But doesn't work correctly. Somebody an idea?

    Regards
    Robert

  • #2
    Hello Robert,

    If you are getting a syntax or formula errors, post your version and I'll test it for you. If your version just isn't matching some of the examples in the book, you may need to contact the author.
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      Hi Jason,
      i typed it and i think it is the same as in the book (one always thinks so), checked it twice, but i do not get a correct graph at all.
      I get no syntax errors.

      Here is the code:

      /************************************************** ********
      Title: Adaptive CG Oscillator
      Coded By: Chris D. Kryza (Divergence Software Inc.)
      Email: [email protected]
      Incept: 07/09/2003
      Version: 1.0.0

      ================================================== =========
      Fix History:

      07/09/2003 - Initial Release
      1.0.0

      ================================================== =========
      ************************************************** ********/


      //External Variables

      var nBarCount = 0;

      var aPriceArray = new Array();
      var aSmoothArray = new Array();
      var aCycleArray = new Array();
      var aDeltaPhase = new Array();
      var aPeriod = new Array();
      var aInstPeriod = new Array();
      var aQ1 = new Array();
      var aI1 = new Array();
      var aCGArray = new Array();


      function preMain() {
      var x;

      setPriceStudy(false);
      setStudyTitle("Adaptive CG");
      setCursorLabelName("CG",0);
      setCursorLabelName("Trig",1);
      setDefaultBarFgColor(Color.blue,0);
      setDefaultBarFgColor(Color.red, 1);

      // initialize Arrays
      for (x=0;x<70;x++){
      aPriceArray[x] = 0.0;
      aSmoothArray[x] = 0.0;
      aCycleArray[x] = 0.0;
      aQ1[x] = 0.0;
      aI1[x] = 0.0;
      aDeltaPhase[x] = 0.0;
      aPeriod[x] = 0.0;
      aInstPeriod[x] = 0.0;
      aCGArray[x] = 0.0;
      }

      }

      function main(Alpha) {
      var x;
      var nCG = 0;
      var nDC;
      var nIntPeriod;
      var nNum;
      var nDenom;
      var nMedianDelta;


      // initialize parameters if necessary
      if ( Alpha == null ) {
      Alpha = 0.07;
      }

      // study is initializing
      if (getBarState() == BARSTATE_ALLBARS) {
      return null;
      }

      // on each new bar, save array values
      if ( getBarState() == BARSTATE_NEWBAR) {
      nBarCount++;

      aPriceArray.pop();
      aPriceArray.unshift(0);

      aSmoothArray.pop();
      aSmoothArray.unshift(0);

      aCycleArray.pop();
      aCycleArray.unshift(0);

      aQ1.pop();
      aQ1.unshift(0);

      aI1.pop();
      aI1.unshift(0);

      aDeltaPhase.pop();
      aDeltaPhase.unshift(0);

      aInstPeriod.pop();
      aInstPeriod.unshift(0);

      aPeriod.pop();
      aPeriod.unshift(0);

      aCGArray.pop();
      aCGArray.unshift(0);


      }

      aPriceArray[0] = ( high()+low() ) / 2;

      aSmoothArray[0] = ( aPriceArray[0] + 2*aPriceArray[1] + 2*aPriceArray[2] + aPriceArray[3] ) / 6;

      if ( nBarCount < 7 ) {
      aCycleArray[0] = ( aPriceArray[0] - 2*aPriceArray[1] + aPriceArray[2] ) / 4;
      }

      else {
      aCycleArray[0] = ( 1 - 0.5*Alpha ) * ( 1 - 0.5*Alpha )
      * ( aSmoothArray[0] - 2*aSmoothArray[1]
      + aSmoothArray[2] ) + 2*(1-Alpha) * aCycleArray[1]
      - (1-Alpha) * (1-Alpha)* aCycleArray[2];
      }

      aQ1[0] = (0.0962*aCycleArray[0]
      + 0.5769*aCycleArray[2]
      - 0.5769*aCycleArray[4]
      - 0.0962*aCycleArray[6] )
      * (0.5 + 0.08 * aInstPeriod[1] );
      aI1[0] = aCycleArray[3];

      if ( aQ1[0] != 0 && aQ1[1] != 0 ) {
      aDeltaPhase[0] = (aI1[0]/aQ1[0]
      - aI1[1]/aQ1[1]) / (1
      + aI1[0]*aI1[1]/(aQ1[0]*aQ1[1]));
      }
      if ( aDeltaPhase[0] < 0.1 ) aDeltaPhase[0] = 0.1;
      if ( aDeltaPhase[0] > 1.1 ) aDeltaPhase[0] = 1.1;

      nMedianDelta = Median (5, aDeltaPhase );

      if ( nMedianDelta == 0 ) {
      nDC = 15;
      }
      else {
      nDC = 6.28318 / nMedianDelta + 0.5;
      }

      aInstPeriod[0] = 0.33 * nDC + 0.67 * aInstPeriod[1];
      aPeriod[0] = 0.15*aInstPeriod[0] + 0.85*aPeriod[1];

      nIntPeriod = Math.floor((4*aPeriod[0] + 3*aPeriod[1] + 2*aPeriod[3] * aPeriod[4] ) / 20);

      nNum = 0;
      nDenom = 0;

      for ( x =0;x<nIntPeriod;x++) {
      nNum += ( 1.0 + x) * ( aPriceArray[x] );
      nDenom += ( aPriceArray[x] );
      }

      if ( nDenom != 0 ) nCG = -nNum/nDenom + ( nIntPeriod+1 ) / 2;
      aCGArray[0] = nCG;


      // return the calculated values
      if (!isNaN (aCGArray[0] )) {
      return new Array ( aCGArray[0], aCGArray[1] );
      }

      }

      function Median (nBars, aArray ) {
      var aTmp = new Array();
      var nTmp;
      var result;
      var x;

      // transfer elements to temp array
      x = 0;
      while( x < nBars ) {
      aTmp[x] = aArray[x++];
      }
      //sort array in asc order
      aTmp.sort (SortAsc);

      // if odd # of elements, just take middle
      if ( nBars % 2 != 0 ) {
      result = aTmp[ (nBars+1)/2]
      aTmp = null;
      return( result );
      }
      // if even # of elements, take average of two middle elements
      else {
      nTmp = nBars/2;
      result = (aTmp[nTmp] + aTmp[nTmp+1])/2;
      aTmp = null;
      return ( result );
      }
      }

      function SortAsc ( arg1, arg2 ) {
      if (arg1<arg2) {
      return(-1)
      }
      else {
      return(1);
      }
      }


      Regards
      Robert

      Comment


      • #4
        Hello Robert,

        I don't have the book so it's difficult for me to pinpoint what the difference might be. Is it possible the example you are comparing this formula to is using a different value for Alpha than the default 0.07?
        Jason K.
        Project Manager
        eSignal - an Interactive Data company

        EFS KnowledgeBase
        JavaScript for EFS Video Series
        EFS Beginner Tutorial Series
        EFS Glossary
        Custom EFS Development Policy

        New User Orientation

        Comment


        • #5
          Jason,
          i already played with different figurs, but the book says 0.07.
          This is the chart:


          Regards
          Robert

          P.S. a few hours ago it just showed a little horizontal line, now this

          Comment


          • #6
            Jason,
            i found the other picture again:

            Comment


            • #7
              Hello Robert,

              Your chart images aren’t visible on-line because your image urls are pointing to your local PC. To post an image from your PC, you need to attach the image using the "Browse" button when replying on-line.
              Jason K.
              Project Manager
              eSignal - an Interactive Data company

              EFS KnowledgeBase
              JavaScript for EFS Video Series
              EFS Beginner Tutorial Series
              EFS Glossary
              Custom EFS Development Policy

              New User Orientation

              Comment


              • #8
                Robert,
                I copied and pasted your code into an efs and the first thing I noticed was that on the very first line you have a space between the / and the * like this / *. It is supposed to be /* to correctly begin a comment line or group.

                So, having fixed that, I got a graph. But I have not read the book or seen any example of the study, so I don't know what it is supposed to be doing. I will say, that it does look a bit odd.

                Anyway, in the hope it is something so simple, try fixing those first two characters.

                Good luck, Bob

                Comment


                • #9
                  Bob,
                  i didn't have the space in the code.
                  Don't know how it gets there, but i found an error in line 154, as i made a * instead of a +.
                  This is the correct line:
                  nIntPeriod = Math.floor((4*aPeriod[0] + 3*aPeriod[1] + 2*aPeriod[3] + aPeriod[4] ) / 20);

                  Now it works.

                  Regards
                  Robert

                  Comment

                  Working...
                  X