Announcement

Collapse
No announcement yet.

MA of userdefined variable

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

  • MA of userdefined variable

    Hi,

    I have an EFS which calcuates a variable, NEWARIABLE, based on price action. I want to calculate a moving average of that variable.

    I tried to do that using the 'new MAStudy' command substituting the variable name for the price that one want to calcualte the MA of.
    eg: new MAStudy(50,0,NEWVARIABLE,MAStudy.SIMPLE)

    This does not appear to work, any ideas.
    I know this should work for MA of a pre-defined function such as RSI.
    Any ideas?

    Tx
    Stuart

  • #2
    You need to use an array..

    You need to populate an array of your NEW VARIABLE, then use that ARRAY to compute your average.

    Arrays

    Provides support for creation of arrays of any data type.

    Array Constructors:

    Array();
    Array( nSize );
    Array( element1, element2, ..., elementN);

    Array Properties:


    ยท nSize: The number of elements in the given array object (optional).

    Array Examples:

    aMyArray = new Array();
    aMyArray = new Array( 20 );
    aMyArray = new Array( 1,5,2,20,99 );
    aMyArray = new Array( "Mon", "Tue", "Wed", "Thu", "Fri" );

    Array Usage:

    //Adding elements to an array
    for (x=0; x<MyCount; x++) {
    aMyArray[x] = close()/open();
    }

    //Retrieving elements from an array
    for (x=0; x<MyCount; x++) {
    nMyValue = aMyArray[x];
    debugPrint("Value: "+nMyValue+"\n");
    }


    Array Methods:

    concat(array2) Concatenates array2 into the array object.

    Array a = new Array(1,2,3);

    Array b = new Array(4,5,6);

    a.concat(b);

    Contents of a is now: { 1, 2, 3, 4, 5, 6 }

    join(sSeparator) Returns a string consisting of all the elements concatenated together.

    Array a = new Array(1,2,3);

    var s = a.join(",");

    /* s = "1,2,3" */

    pop() Removes the last element from the array and returns that element. This method changes the length of the array.

    Array a = new Array(1,2,3);

    var s = a.pop();

    /* s = "3" and Array a now contains (1,2)*/

    push() Adds one more element to the end of an array and returns that last element added. This method changes the length of the array.

    Array a = new Array(1,2,3);

    var s = a.push(6,7);

    /* s = "7" and Array a now contains (1,2,3,6,7)*/

    shift() Removes the first element from an array and returns that element. This method changes the length of the array.

    Array a = new Array(1,2,3);

    var s = a.shift();

    /* s = "1" and Array a now contains (2,3)*/

    unshift() Adds one or more elements to the beginning of the array and returns the new length of the array.

    Array a = new Array(1,2,3);

    var s = a.unshift(0);

    /* s = "4" and Array a now contains (0,1,2,3)*/

    reverse() Reverses all the objects in the array object.

    Array a = new Array(1,2,3,4,5);

    /* Before Reverse, { 1,2,3,4,5} */

    a.reverse();

    /* After Reverse, { 5,4,3,2,1 } */

    slice(nStartIndex, [nEndIndex]) Returns a section of the array from nStartIndex to (optionally) nEndIndex.

    Array src = new Array(1,2,3,4,5,6);

    Array dest = src.slice(2,4);

    /* Contents of dest: { 3,4,5 } */

    splice(nStartIndex, Quantity, [item1, ..., itemN]) Changes the contents of an array, adding new elements while removing old elements.

    Array src = new Array(1,2,3,4,5,6);

    var s = src.splice(2, 0, 9);

    /* Contents of src: {1,2,9,3,4,5,6);

    sort(sortFunction) Sorts the array using sortFunction

    Array a = new Array(2,4,6,1,3,5);

    a.sort();

    /* Contents of a: { 1,2,3,4,5,6 } */

    If you supply a sort function, it must be defined as:

    function mySortFunction(firstarg, secondarg)

    Return a negative value if firstarg < secondarg

    Return a positive value if firstarg > secondarg.

    Otherwise return 0.
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      Hello Stuart,

      Passing a custom variable or price source to a built-in study is not supported. You can however pass the output of a built-in Study to another built-in study. Visit this link for a good example of coding a Study on Study.

      You can also find a some existing formulas that perform a Study on Study in our EFS Library.
      RSI of ROC
      MA of ROC
      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


      • #4
        Specifically, I want to pass a custom variable to a built-in study. In this case I want to calculate the MA of a custom variable.
        So, it appears that I need to write a function to calculate the moving average. ie create an array of the variable I want to study and then do the math on it to return the MA

        This would be a very useful feature to have in esignal, many other charting programs allow custom variables to be passed to built in functions.

        Stuart

        Comment


        • #5
          Hello Stuart,

          I was just informed that we will be able to pass custom price sources to the built-in studies, but not until the 8.0 release.
          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


          • #6
            Stuart,

            I have found that eSignal efs is so flexible that you can do pretty much anything you want. This particular task can be done by defining your data you want to have an MA performed on as an array and passing it to a function, which calculates and returns the MA. While not a built in function, it is equivalent. Here is a my function I use to perform a weighted moving average.
            PHP Code:
            //--------------------------------------------------------------------------------------- 
            function WMA(nn,fnArray) { //weighted moving average
                
            if (nn 3){nn 3;}//prevents division by zero "ouch!!" for nn = 0
                
            var dSum 0.0; var iSum 0.0; var i;
                for(
            0nni++){dSum += fnArray[i]*(nn i); iSum += nn i;}
                return (
            dSum iSum); 
            //End weighted moving average 
            Let me know if you need more info.

            Comment


            • #7
              I just pasted this into my EFS. How do I plot the value? If I ask for any of the outputs, it just tells me they are undefined. I am a beginner. The Tradestation people can code formulas with just a few lines. I am so frustrated.

              I have a value I have calculated. Its a line. Please please someone tell me how to plot a weighted MA of that line. Not part of the code, the whole thing. Enough so that I can plot that MA.

              Comment


              • #8
                This particular task can be done by defining your data you want to have an MA performed on as an array and passing it to a function
                I assume from your description that you did not define your data as an array and then pass it the listed function. Please post your code and I will be more than happy to show you how to do it.

                Comment


                • #9
                  You're right. I did not pass the thing I wanted the MA of into your function. I'm just going to post the whole thing, after the defintions of the colors, etc. This is supposed to plot a subgraph of a line, and a 21 bar, weighted MA of that line. I would be delighted if you would show me how to do it. I took the code for the calculation of the original line, tried to paste in something to do the MA of that, and the last 3 lines were my guess as to how to get this to plot as 2 lines, using something else as a model. Also, BTW, I have no interest in the offset refered to in the main function. I'm just hesitant to take it out.

                  Here it is:

                  var vPrice = null;

                  var vInit = false;

                  var LSMA_Array = new Array();

                  function main(nLength,nOffset)
                  {
                  if (nLength == null) nLength = 34;
                  if (nOffset == null) {
                  nOffset = 0;
                  } else {
                  nOffset = Math.abs(Math.round(nOffset));
                  }

                  if (vInit == false) {
                  vPrice = new Array(nLength);
                  vInit = true;
                  }

                  vClose = close();
                  vHigh = high();
                  vLow = low();

                  if (vClose == null) return;
                  if (vHigh == null) return;
                  if (vLow == null) return;

                  if (getBarState() == BARSTATE_NEWBAR) {
                  if (vPrice[nLength-1] != null) vPrice.pop();
                  vPrice.unshift(vHLC3);
                  }
                  vHLC3 = vClose; //changed
                  vPrice[0] = vHLC3;

                  if (vPrice[nLength-1] == null) return;

                  var Num1 = 0.0;
                  var Num2 = 0.0;
                  var SumBars = nLength * (nLength - 1) * 0.5;
                  var SumSqrBars = (nLength - 1) * nLength * (2 * nLength - 1) / 6;
                  var SumY = 0.0;
                  var Sum1 = 0.0;
                  var Sum2 = 0.0;
                  var Slope = 0.0;
                  var Intercept = 0.0;

                  var r = new Array(); //added
                  r[0] = 0.0;
                  r[1] = 0.0;

                  for (i = 0; i < nLength; i++)
                  {
                  SumY += vPrice[i];
                  Sum1 += i * vPrice[i];
                  }
                  Sum2 = SumBars * SumY;
                  Num1 = nLength * Sum1 - Sum2;
                  Num2 = SumBars * SumBars - nLength * SumSqrBars;
                  if (Num2 != 0) Slope = Num1 / Num2;
                  Intercept = (SumY - Slope * SumBars) / nLength;
                  var LinearRegValue = Intercept + Slope * (nLength - 1);

                  if (getBarState() == BARSTATE_NEWBAR) {
                  if (LSMA_Array[nLength-1] != null) LSMA_Array.pop(); // v1.2
                  LSMA_Array.unshift(LinearRegValue);
                  }

                  LSMA_Array[0] = LinearRegValue;

                  function WMA(nn,fnArray) { //weighted moving average
                  if (nn < 3){nn = 3;}//prevents division by zero "ouch!!" for nn = 0
                  var dSum = 0.0; var iSum = 0.0; var i;
                  for(i = 0; i < nn; i++){dSum += fnArray[i]*(nn - i); iSum += nn - i;}
                  return (dSum / iSum);
                  } //End weighted moving average


                  r[0] = LSMA_Array[0] // changed to 0
                  r[1] = WMA(nn,fnArray)

                  return (r)

                  Comment


                  • #10
                    Actually, it looks like you took another linear regression calc method and used that as the original line calc. Then tried to do a moving average of that? There will be a tremendous amount of lag. I have an alternative method if you don't mind.

                    Comment


                    • #11
                      I'm trying to create a crossover situation, so the lag may be fine. But I'd be interested in any ideas you have.

                      Comment


                      • #12
                        I went ahead and coded what I believe you want. I am sorry I did not use much of what you posted.

                        The results are very interesting. I went ahead and used functions I had generated a while back for linear regression and weighted moving average. I also use a large number of functions which minimizes code in main. So you will see the main code is between lines 32 and 61. The periods for the study can be modified in the edit studies menu, accessable when you right click the graph. You can also change the price source in there.

                        I tried to comment it fairly well, please ask if you have any questions. Change the title to whatever you would like.

                        As a general commentary to anyone that is interested. I was able to lock my PC up regularly with buffer overflows while I was populating the Arrays with calculation results. The way I got around it is that I used .toFixed(3)*1 or other rounding to prevent 16 (or so) digits to the right of the decimal point. This prevented my buffer overflows I was getting which was locking up my machine.
                        Attached Files

                        Comment


                        • #13
                          here is a screen shot
                          Attached Files

                          Comment


                          • #14
                            Weighted Average of Regression plus error bars

                            I have worked with the efs that I had posted earlier and improved it. I changed the regression and weighted moving average period to 16, although it can be modified by right clicking and selecting edit studies.

                            Another change is that the light blue line is the regression value predicted two bars in advance. I also added the standard deviation of the regression from the predicted value and added it to the predicted value to establish the bands.

                            If anyone has any good ideas, please let me know. I will also post in my fileshare area "functions"

                            Attached Files
                            Last edited by Guest; 10-03-2005, 05:34 PM.

                            Comment

                            Working...
                            X