Announcement

Collapse
No announcement yet.

Moving average of 2 or more studies

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

  • Moving average of 2 or more studies

    I made 2 studies and am trying to plot 3 lines:

    Study 1 was a macd of a macd...
    Study 2 was the average of a macd (I used the sma function)...

    All I want to do is plot them...

    1st line was no problem (it was the addition of the 2 studies)...

    2nd line needs a 10 period average of the 1st line (not the study)
    3rd line needs a 20 period average of the 1st line as well...

    I can't figure out how to do it for the life of me... help!

  • #2
    riverx
    If you are using the Formula Wizard to do this try the following.
    Instead of adding Study 1 and Study 2 and then trying to create the averages of the sum - which is not possible in the Formula Wizard - first calculate the averages of each study individually - which you can do in FW as a study on study - then add those averages. The result should be the same.
    Alex
    Last edited by ACM; 11-01-2003, 07:34 PM.

    Comment


    • #3
      riverx
      Following up on my prior message here is an example of what I suggested compared to doing an average of the sum.
      In the enclosed efs two averages are computed, one created by first calculating the individual MAs of MAs and then adding those values (and dividing the sum by 2) and the other by adding the original MAs and then computing an average of that sum.
      As you can see in the image the results are identical as the two plots overlay each other perfectly.
      Alex



      PHP Code:
      var vSMA10 = new MAStudy(100"Close"MAStudy.SIMPLE);
      var 
      vSMA20 = new MAStudy(200"Close"MAStudy.SIMPLE);
      var 
      vSMA10_of_vSMA10 = new MAStudy(100vSMA10MAStudy.MAMAStudy.SIMPLE);//this is study on study ie 10 period SMA of vSMA10
      var vSMA10_of_vSMA20 = new MAStudy(100vSMA20MAStudy.MAMAStudy.SIMPLE);//this is study on study ie 10 period SMA of vSMA20

      var aB null;//used by array needed to create average of var B 

      function preMain() {
          
      setPriceStudy(true);
          
      setStudyTitle("");
          
      setCursorLabelName("exampleA"0);
          
      setCursorLabelName("exampleB"1);
          
      setDefaultBarFgColor(Color.red0);
          
      setDefaultBarFgColor(Color.blue1);
      }

      function 
      main() {

      //The following is the sum/2 of the individual averages computed with a study on study
      var vA = (vSMA10_of_vSMA10.getValue(MAStudy.MA)+vSMA10_of_vSMA20.getValue(MAStudy.MA))/2;
      //end this section

      //The following instead is the average of the sum of the two original averages
      var B= (vSMA10.getValue(MAStudy.MA)+vSMA20.getValue(MAStudy.MA))/2;

      if (
      aB == null) {
              
      aB = new Array(10);
          }
          if (
      getBarState() == BARSTATE_NEWBAR) {
              
      aB.pop();  
              
      aB.unshift(B);
          } else {
          
      aB[0] = B;
          }
          var 
      nSum 0;
          for (
      010; ++i) {
              
      nSum += aB[i];
          }
          var 
      vB nSum/10;    
      //end this section

          
      return new Array(vA,vB);

      Comment

      Working...
      X