Announcement

Collapse
No announcement yet.

MA of a calculated result.

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

  • MA of a calculated result.

    Can someone help? I guess I'm missing something pretty basic here because this doesn't work.
    Also, is it possible to plot the MA on the chart within the same efs. i.e. combine a price study with a non-price study


    // Show the distance from the trend (a moving average of price)
    // as a percent above or below the trend and add a MA of the percent
    var nLength = null;
    var vMA = null;
    var vPCT = null; // distance from trend as a percent
    var nSmooth = null;
    var vDFT= null; // the MA of vPCT



    function preMain()
    {
    setStudyTitle("Distance from Trend");
    setCursorLabelName("DFT",0);
    setDefaultBarFgColor(Color.navy,0);
    addBand(0, PS_SOLID, 1, Color.black);
    addBand(-3, PS_SOLID, 1, Color.lime);
    addBand(3, PS_SOLID, 1, Color.magenta);
    var fp1 = new FunctionParameter("nLength", FunctionParameter.NUMBER);
    fp1.setName("Length");
    fp1.setLowerLimit(1);
    fp1.setDefault(50);
    var fp1 = new FunctionParameter("nSmooth", FunctionParameter.NUMBER);
    fp1.setName("Length");
    fp1.setLowerLimit(1);
    fp1.setDefault(10);

    }
    function main(nLength, nSmooth)
    {
    var vMA = sma(nLength,"close")
    var vPCT = (close()/vMA-1)*100

    var vDFT = new MAStudy(nSmooth, 0, vPCT, MAStudy.Simple);


    return new array(vPCT(0), vDFT(0).getvalue(MAStudy.MA) ) ;
    }

  • #2
    i could be wrong, but i believe your problem is that you are using vPCT as a data set in your moving average, but that cant be done as it is written since it is only a single variable, not a series

    though another little thing is you have your second function parameter set as fp1, which would erase the set values for your first function parameter
    Last edited by kalzenith; 08-25-2008, 08:50 AM.

    Comment


    • #3
      here, ive set your vPCT variable in an efsInternal() so it can be used as a data series




      var vMA = 0;
      var vPCT = 0;
      var vDFT = 0;

      function preMain() {
      setColorPriceBars(false);
      setPriceStudy(false);
      setStudyTitle("Distance from Trend");
      setCursorLabelName("PCT", 0);
      setCursorLabelName("DFT", 1);
      addBand(0, PS_SOLID, 1, Color.black);
      addBand(-3, PS_SOLID, 1, Color.lime);
      addBand(3, PS_SOLID, 1, Color.magenta);
      var fp1 = new FunctionParameter("nLength", FunctionParameter.NUMBER);
      fp1.setName("Length");
      fp1.setLowerLimit(1);
      fp1.setDefault(50);
      var fp2 = new FunctionParameter("nSmooth", FunctionParameter.NUMBER);
      fp2.setName("Smooth");
      fp2.setLowerLimit(1);
      fp2.setDefault(10);

      }
      function main(nLength, nSmooth) {

      vDFT = sma(nSmooth,efsInternal("calc", nLength))

      return new Array (efsInternal("calc", nLength), vDFT)

      }
      function calc(nLength) {
      vMA = sma(nLength)
      vPCT = (close()/vMA-1)*100

      return vPCT
      }
      function postMain() {
      }
      Last edited by kalzenith; 08-25-2008, 12:25 PM.

      Comment


      • #4
        kalzenith

        though another little thing is you have your second function parameter set as fp1, which would erase the set values for your first function parameter
        Actually it will work even if you assign each FunctionParameter() to the same variable as long as the name used in the string parameter of the FunctionParameter() is unique. You can verify this by replacing fp2 with fp1 in the second FunctionParameter used in your code
        Alex


        Originally posted by kalzenith
        i could be wrong, but i believe your problem is that you are using vPCT as a data set in your moving average, but that cant be done as it is written since it is only a single variable, not a series

        though another little thing is you have your second function parameter set as fp1, which would erase the set values for your first function parameter

        Comment


        • #5
          Re: MA of a calculated result.

          pj909
          Adding to the thread to explain why your code was not working and also to suggest an alternative solution

          1) While in this specific case the following line of code
          var vMA = sma(nLength,"close")
          will still work (because the formula engine ignores the second parameter as it does not match any of the expected types) its syntax is however incorrect and it would return a result different from what you would expect if you were using a source other than "close" (for example "high").
          The reason for this can be found in this thread in the examples of the syntax required by the efs2 functions
          Also you may want to review this article in the EFS knowledgeBase for the description and syntax required by the sma() function
          The line of code should have been either
          var vMA = sma(nLength,close())
          or
          var vMA = sma(nLength)


          2) The following line of code
          var vPCT = (close()/vMA-1)*100
          would be better written as follows
          if(vMA.getValue(0)==null) return;//null check to avoid divide by null in following line
          var vPCT = (close(0)/vMA.getValue(0)-1)*100

          Note that this equation returns a value and not a series (more on this in the next point)

          3) In the following line of code
          var vDFT = new MAStudy(nSmooth, 0, vPCT, MAStudy.Simple);
          the first problem is that you are using an efs1 function (you can easily identify the efs1 functions by the use of the "new" constructor which is not required by the equivalent efs2 functions). The efs1 functions can only use "Open", "High", "Low", "Close" or "Volume" and in some cases another efs1 built-in study function as the source. They cannot use custom variables.
          Even if you had used the equivalent efs2 function ie sma() which allows the use of custom variables as the source this would not have worked because as explained above that variable is a value and not a series as required by the sma() function.
          To create a series you need to calculate your custom variable in a separate function (or efs) and then call that function or efs from the main function using either the efsInternal() or efsExternal() functions (see the links to the corresponding articles in the EFS KnowledgeBase). This will create the series which you can then use as a source for the sma() function. For a step by step explanation see this post in reply to a similar question. In that reply you will also find links to other examples on creating studies on studies using efs2 functions

          Lastly consider that in this case you can obtain the same results without the need of a custom equation to calculate vPCFT (and consequently without the need of a separate function that you call using efsInternal() to create the series).
          In fact you can use the Price Oscillator function [ie osc()] which returns the difference between two averages expressed as a percentage. All you need to do is set the first average of the Price Oscillator to 1 and that will be a proxy of price.
          Since the osc() function already returns a series you can then use that directly as the source of the sma() function. The main function of your script would be as shown below
          Hope this provides some further insight
          Alex

          PHP Code:
          function main(nLengthnSmooth) {
              
          vPCFT osc(1,nLength,0,close());
              
          vDFT  sma(nSmooth,vPCFT);
              return new Array (
          vPCFT.getValue(0), vDFT.getValue(0))



          Originally posted by pj909
          Can someone help? I guess I'm missing something pretty basic here because this doesn't work.
          Also, is it possible to plot the MA on the chart within the same efs. i.e. combine a price study with a non-price study


          // Show the distance from the trend (a moving average of price)
          // as a percent above or below the trend and add a MA of the percent
          var nLength = null;
          var vMA = null;
          var vPCT = null; // distance from trend as a percent
          var nSmooth = null;
          var vDFT= null; // the MA of vPCT



          function preMain()
          {
          setStudyTitle("Distance from Trend");
          setCursorLabelName("DFT",0);
          setDefaultBarFgColor(Color.navy,0);
          addBand(0, PS_SOLID, 1, Color.black);
          addBand(-3, PS_SOLID, 1, Color.lime);
          addBand(3, PS_SOLID, 1, Color.magenta);
          var fp1 = new FunctionParameter("nLength", FunctionParameter.NUMBER);
          fp1.setName("Length");
          fp1.setLowerLimit(1);
          fp1.setDefault(50);
          var fp1 = new FunctionParameter("nSmooth", FunctionParameter.NUMBER);
          fp1.setName("Length");
          fp1.setLowerLimit(1);
          fp1.setDefault(10);

          }
          function main(nLength, nSmooth)
          {
          var vMA = sma(nLength,"close")
          var vPCT = (close()/vMA-1)*100

          var vDFT = new MAStudy(nSmooth, 0, vPCT, MAStudy.Simple);


          return new array(vPCT(0), vDFT(0).getvalue(MAStudy.MA) ) ;
          }

          Comment


          • #6
            thank you both for your guidance.
            I see I have a bit of reading to do - really appreicate the links.

            My 2nd question was about combining price study with a non-price study. Basically I want to plot (on the price chart) whatever MA has been selected in addition to the Oscillator in the bottom half. The idea is to not have to enter the MA value twice.

            Comment


            • #7
              pj909
              An efs only plots in the pane in which it is set to run.
              Alex


              Originally posted by pj909
              thank you both for your guidance.
              I see I have a bit of reading to do - really appreicate the links.

              My 2nd question was about combining price study with a non-price study. Basically I want to plot (on the price chart) whatever MA has been selected in addition to the Oscillator in the bottom half. The idea is to not have to enter the MA value twice.

              Comment

              Working...
              X