Announcement

Collapse
No announcement yet.

Sum of an SMA of an SMA

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

  • Sum of an SMA of an SMA

    What I'm looking to generate is this:

    1. the 5sma of (Open-CLose)*Volume
    2. then to get the 5sma of number (1) ...(the 5sma of the 5sma)
    3. then to get the 13 period sum of number (1) ....(summation of the last 13 bars of number 1).

    I'm trying to get an efs to generate and return all three of those values which would then be used (called) by another efs to analyze the three pieces of data.

    I've spent about three weeks trying to code this, and the recursive nature of it has turned my brain inside out. While I believe I may have it right, I'm very sure that it is extremely inefficient since I'm manually shifting all the data in the arrays back 1 by 1 as I generate a new bar of data.

    Any help would be appreciated.

  • #2
    Does this return the right values for Itme 1? If so, then we can work on multiplying them and completing Item 1, then work on Item 2.

    //{{EFSWizard_Description
    //
    // This formula was intially generated by the Alert Wizard
    // but modified by DJL )
    //}}EFSWizard_Description 7532


    //{{EFSWizard_Declarations

    var vSMA5_of_Close = new MAStudy(5, 0, "Close", MAStudy.SIMPLE);
    var vSMA5_of_Open = new MAStudy(5, 0, "Open", MAStudy.SIMPLE);
    var vSMA5_of_Volume = new MAStudy(5, 0, "Volume", MAStudy.SIMPLE);
    var vLastAlert = -1;

    //}}EFSWizard_Declarations 17947


    function preMain() {
    /**
    * This function is called only once, before any of the bars are loaded.
    * Place any study or EFS configuration commands here.
    */
    //{{EFSWizard_PreMain
    setPriceStudy(false);
    setStudyTitle("t-witch");
    setCursorLabelName("5smaC", 0);
    setCursorLabelName("5smaO", 1);
    setCursorLabelName("5smaV", 2);
    setDefaultBarStyle(PS_SOLID, 0);
    setDefaultBarStyle(PS_SOLID, 1);
    setDefaultBarStyle(PS_SOLID, 2);
    setDefaultBarFgColor(Color.red, 0);
    setDefaultBarFgColor(Color.red, 1);
    setDefaultBarFgColor(Color.red, 2);
    setDefaultBarThickness(1, 0);
    setDefaultBarThickness(1, 1);
    setDefaultBarThickness(1, 2);
    setPlotType(PLOTTYPE_LINE, 0);
    setPlotType(PLOTTYPE_LINE, 1);
    setPlotType(PLOTTYPE_LINE, 2);
    //}}EFSWizard_PreMain 51467

    }

    function main() {
    /**
    * The main() function is called once per bar on all previous bars, once per
    * each incoming completed bar, and if you don't have 'setComputeOnClose(true)'
    * in your preMain(), it is also called on every tick.
    */

    //{{EFSWizard_Expressions
    //{{EFSWizard_Expression_1
    //}}EFSWizard_Expression_1 0

    //}}EFSWizard_Expressions 9063


    //{{EFSWizard_Return
    return new Array
    (
    vSMA5_of_Close.getValue(MAStudy.MA),
    vSMA5_of_Open.getValue(MAStudy.MA),
    vSMA5_of_Volume.getValue(MAStudy.MA)
    );
    //}}EFSWizard_Return 10571

    }

    Comment


    • #3
      Check my math on this one, if it is correct Item 1 is done...

      //{{EFSWizard_Description
      //
      // This formula was generated by the Alert Wizard
      // and edited by DJL
      //}}EFSWizard_Description 7532


      //{{EFSWizard_Declarations

      var vSMA5_of_Close = new MAStudy(5, 0, "Close", MAStudy.SIMPLE);
      var vSMA5_of_Open = new MAStudy(5, 0, "Open", MAStudy.SIMPLE);
      var vSMA5_of_Volume = new MAStudy(5, 0, "Volume", MAStudy.SIMPLE);
      var vLastAlert = -1;

      //}}EFSWizard_Declarations 17947


      function preMain() {
      /**
      * This function is called only once, before any of the bars are loaded.
      * Place any study or EFS configuration commands here.
      */
      //{{EFSWizard_PreMain
      setPriceStudy(false);
      setStudyTitle("t-witch");
      setCursorLabelName("5smaC", 0);
      setCursorLabelName("5smaO", 1);
      setCursorLabelName("5smaV", 2);
      setDefaultBarStyle(PS_SOLID, 0);
      setDefaultBarStyle(PS_SOLID, 1);
      setDefaultBarStyle(PS_SOLID, 2);
      setDefaultBarFgColor(Color.red, 0);
      setDefaultBarFgColor(Color.red, 1);
      setDefaultBarFgColor(Color.red, 2);
      setDefaultBarThickness(1, 0);
      setDefaultBarThickness(1, 1);
      setDefaultBarThickness(1, 2);
      setPlotType(PLOTTYPE_LINE, 0);
      setPlotType(PLOTTYPE_LINE, 1);
      setPlotType(PLOTTYPE_LINE, 2);
      //}}EFSWizard_PreMain 51467

      }

      function main() {
      debugClear();
      /**

      * The main() function is called once per bar on all previous bars, once per
      * each incoming completed bar, and if you don't have 'setComputeOnClose(true)'
      * in your preMain(), it is also called on every tick.
      */

      //{{EFSWizard_Expressions
      //{{EFSWizard_Expression_1
      //}}EFSWizard_Expression_1 0

      //}}EFSWizard_Expressions 9063
      var deltaOC=vSMA5_of_Close.getValue(MAStudy.MA)-vSMA5_of_Open.getValue(MAStudy.MA);
      var VolMA=vSMA5_of_Volume.getValue(MAStudy.MA)/10000000;

      //{{EFSWizard_Return
      return (deltaOC*VolMA);
      //}}EFSWizard_Return 10571

      }

      Comment


      • #4
        This seems to be pretty close to what you wanted

        //{{EFSWizard_Description
        //
        // This formula was generated by the Alert Wizard
        // but parts were lifted from other efs' written by JasonK and others, thanks )
        //}}EFSWizard_Description 7532


        //{{EFSWizard_Declarations

        var vSMA5_of_Close = new MAStudy(5, 0, "Close", MAStudy.SIMPLE);
        var vSMA5_of_Open = new MAStudy(5, 0, "Open", MAStudy.SIMPLE);
        var vSMA5_of_Volume = new MAStudy(5, 0, "Volume", MAStudy.SIMPLE);
        var vLastAlert = -1;
        var myArray = new Array(4);
        var vA = null;

        //}}EFSWizard_Declarations 17947


        function preMain() {
        /**
        * This function is called only once, before any of the bars are loaded.
        * Place any study or EFS configuration commands here.
        */
        //{{EFSWizard_PreMain
        setPriceStudy(false);
        setStudyTitle("t-witch");
        setCursorLabelName("5smao-C*V", 0);
        setCursorLabelName("5sma(5smao-C*V)", 1);
        setCursorLabelName("5smaV", 2);
        setDefaultBarStyle(PS_SOLID, 0);
        setDefaultBarStyle(PS_SOLID, 1);
        setDefaultBarStyle(PS_SOLID, 2);
        setDefaultBarFgColor(Color.red, 0);
        setDefaultBarFgColor(Color.blue, 1);
        setDefaultBarFgColor(Color.red, 2);
        setDefaultBarThickness(1, 0);
        setDefaultBarThickness(2, 1);
        setDefaultBarThickness(1, 2);
        setPlotType(PLOTTYPE_LINE, 0);
        setPlotType(PLOTTYPE_LINE, 1);
        setPlotType(PLOTTYPE_LINE, 2);
        //}}EFSWizard_PreMain 51467

        }

        function main() {
        debugClear();
        /**

        * The main() function is called once per bar on all previous bars, once per
        * each incoming completed bar, and if you don't have 'setComputeOnClose(true)'
        * in your preMain(), it is also called on every tick.
        */

        //{{EFSWizard_Expressions
        //{{EFSWizard_Expression_1
        //}}EFSWizard_Expression_1 0

        //}}EFSWizard_Expressions 9063
        var deltaOC=vSMA5_of_Close.getValue(MAStudy.MA)-vSMA5_of_Open.getValue(MAStudy.MA);
        var VolMA=vSMA5_of_Volume.getValue(MAStudy.MA)/10000000;
        var twitch=deltaOC*VolMA;

        if (getBarState() == BARSTATE_NEWBAR && vA != null) {
        myArray.pop();
        myArray.unshift(vA);
        }

        vA = twitch * 1;
        if (vA == null)
        return;

        var vAma = null;
        if (myArray[3] != null) {
        var dSum = 0;
        for (i = 0; i < 4; ++i) {
        dSum += myArray[i];
        }
        vAma = (dSum + vA)/5;
        }

        //{{EFSWizard_Return
        return new Array(deltaOC*VolMA, vAma);
        //}}EFSWizard_Return 10571

        }

        Comment


        • #5
          David,

          Thanks for taking the time to do that, it looks good.
          It seems that I was taking about twice the code to do the same amount of work. The whole issue of arrays is what is killing me.

          So now, to get the last 13 bars of the variable 'twitch' is where I really lose it, since what we're basically looking for is 13*5 bars of data. I tried initializing an array and a global counter which I kept incrementing until the array was full (zero through 12), and then started the summation, but there has got to be a better way.

          Thanks again for the help.

          Comment


          • #6
            I think you are on the right track, but my brain just lost it, so I afraid from here on you are on your own.

            I think the last section needs to be repeated, adding up the last 13 values of the value calucaluted just previously.

            Comment


            • #7
              David,
              Powered by a couple jugs of expresso this morning, I took another look at the code and noticed that it was creating a 4sma rather than 5sma, so I changed those values in the FOR statement.

              What I cannot find anywhere in my Javascript 1.3 book are the commands for

              myArray.pop();
              myArray.unshift(vA);

              I don't really understand what the .pop and .unshift commands are or what they do. Can you explain or point me to any documentation that describes them?

              As for the Sum13 value, here is what I came up with (I think its right.....although someone may have a more efficient way of coding it):

              //---------------------------------------------------------------
              // Return the sum of the last 13 values of the 5sma of Open-Close*Volume
              //---------------------------------------------------------------

              var vSMA5_of_Close = new MAStudy(5, 0, "Close", MAStudy.SIMPLE);
              var vSMA5_of_Open = new MAStudy(5, 0, "Open", MAStudy.SIMPLE);
              var vSMA5_of_Volume = new MAStudy(5, 0, "Volume", MAStudy.SIMPLE);
              var vLastAlert = -1;
              var myArray = new Array(5);
              var sum13 = new Array(12);
              var sumc = 0;
              var sum13val = 0;
              var vA = null;

              function preMain() {
              setPriceStudy(false);
              setStudyTitle("13SUM OCV");
              setDefaultBarStyle(PS_SOLID, 0);
              setDefaultBarFgColor(Color.cyan, 0);
              setDefaultBarThickness(3, 0);
              setPlotType(PLOTTYPE_HISTOGRAM, 0);
              addBand(0, PS_DOT, 2, Color.RGB(0,255,128));
              }

              function main()
              {
              debugClear();

              var deltaOC = vSMA5_of_Close.getValue(MAStudy.MA) - vSMA5_of_Open.getValue(MAStudy.MA);
              var VolMA = vSMA5_of_Volume.getValue(MAStudy.MA)/10000000;
              var twitch = deltaOC*VolMA;

              if (getBarState() == BARSTATE_NEWBAR && vA != null)
              {
              myArray.pop();
              myArray.unshift(vA);
              }

              vA = twitch * 1;
              if (vA == null) return;

              var vAma = null;

              if (myArray[4] != null)
              {
              var dSum = 0;
              for (i = 0; i < 5; ++i)
              {
              dSum += myArray[i];
              }
              vAma = (dSum + vA)/5;
              }

              // Generate 13 SUM
              // Fill an array with the last 13 values of 5SMA of Open-Close*Volume for 13 bars,
              // when array is filled, sum it up
              //---------------------------------------------------------------------------------
              if (sumc < 13) // haven't filled all 13 slots of data yet
              {
              sum13[sumc] = twitch;
              sumc = sumc + 1;

              if (sumc == 13) // have filled array, sum it up
              {
              for (j = 0; j < 13; j++)
              sum13val = sum13val + sum13[j];
              }
              else return; // have not filled it, return
              }

              // Array with 13 bars of 5sma need to be shifted over and add latest value to end of array
              // Then sum it up.
              //---------------------------
              for (i = 0; i < 12; ++i) // Shift array values over by 1
              sum13[i] = sum13[i+1];

              sum13[12] = twitch; // place this bars 5sma of Open-Close*vol into last array slot
              for (j = 0; j < 13; j++) // Sum the 13 array values
              sum13val = sum13val + sum13[j];

              return (sum13val);
              }

              Comment


              • #8
                http://www.devguru.com/Technologies/...ckref/Pop.html

                talks about pop and unshift

                http://www.devguru.com/Technologies/...f/Unshift.html

                but what you did looks like a good first step - does the math work out ifyou do a long hand double check?

                Comment

                Working...
                X