Announcement

Collapse
No announcement yet.

Moving average line range

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

  • Moving average line range

    Hi all,

    I am working on an indicator that paints the background if two moving averages are are closer than .5 Is this possible? I tried a few searches and was not able to find anything. Could someone tell me the best way to do this? I am trying to understand the best way to write up the logic. Even if this means going away from the formula wizard skeleton below.

    Thank you,
    Chris

    PHP Code:
    var vSMA8 = new MAStudy(80"Close"MAStudy.SIMPLE);
    var 
    vSMA34 = new MAStudy(340"Close"MAStudy.SIMPLE);
    var 
    vLastAlert = -1;

    function 
    preMain() {
        
    setPriceStudy(false);
        
    setStudyTitle("marange");
        
    setCursorLabelName("8"0);
        
    setCursorLabelName("34"1);
        
    setDefaultBarStyle(PS_SOLID0);
        
    setDefaultBarStyle(PS_SOLID1);
        
    setDefaultBarFgColor(Color.blue0);
        
    setDefaultBarFgColor(Color.red1);
        
    setDefaultBarThickness(10);
        
    setDefaultBarThickness(11);
        
    setPlotType(PLOTTYPE_LINE0);
        
    setPlotType(PLOTTYPE_LINE1);

    }

    function 
    main() {
            if (
                
    vSMA8.getValue(MAStudy.MA) <= vSMA34.getValue(MAStudy.MA)+.5
            
    onAction1();

        return new Array(
            
    vSMA8.getValue(MAStudy.MA)*100,
            
    vSMA34.getValue(MAStudy.MA)*100
        
    );

    }

    function 
    postMain() {
    }

        function 
    onAction1() {
            
    setBarBgColor(Color.RGB(0,0,255));
            
    vLastAlert 1;
        } 

  • #2
    Chris
    As it is written now the logic is shifting up the price of the 34 period MA by 0.5 and then coloring the background whenever the 8 period MA goes below that value. What this implies though is that the background will be always colored when the 8 period MA falls below the 34 period MA even if the two averages are separated by more than 0.5.
    If that is the result you want then the logic is fine, but if you instead wish to color the background only when the difference between the averages is equal or less than 0.5 then you will need to replace
    if (vSMA8.getValue(MAStudy.MA) <= vSMA34.getValue(MAStudy.MA)+.5)
    onAction1();

    with
    if (Math.abs(vSMA8.getValue(MAStudy.MA) - vSMA34.getValue(MAStudy.MA)) <= .5)
    onAction1();


    Also if you would rather have user adjustable MAs then click here and download basicMAx2.efs to which you can add the same (or revised) logic implemented in your efs (just replace vSMA8.getVal... with vMA1.getVal.. and vSMA34.getVal.. with vMA2.getVal...)
    Hope this helps
    Alex

    Comment


    • #3
      Thanks for your reply Alex. I changed the formula the way you suggested. The logic of is alot clearer now. The background is all yellow. I tried adding the 1 == 1 statement and it didn't seem to work. Can anyone see what I am missing?

      Thanks,
      Chris

      PHP Code:
      var vSMA8 = new MAStudy(80"Close"MAStudy.SIMPLE);
      var 
      vSMA34 = new MAStudy(340"Close"MAStudy.SIMPLE);
      var 
      vLastAlert = -1;

      function 
      preMain() {

          
      setPriceStudy(false);
          
      setStudyTitle("marange");
          
      setCursorLabelName("8"0);
          
      setCursorLabelName("34"1);
          
      setDefaultBarStyle(PS_SOLID0);
          
      setDefaultBarStyle(PS_SOLID1);
          
      setDefaultBarFgColor(Color.blue0);
          
      setDefaultBarFgColor(Color.red1);
          
      setDefaultBarThickness(10);
          
      setDefaultBarThickness(11);
          
      setPlotType(PLOTTYPE_LINE0);
          
      setPlotType(PLOTTYPE_LINE1);
      //}}EFSWizard_PreMain 51177

      }

      function 
      main() {
        
              if (
                  (
      Math.abs(vSMA8.getValue(MAStudy.MA) - vSMA34.getValue(MAStudy.MA)) <= .25)
              ) 
      onAction1();

          return new Array(
              
      vSMA8.getValue(MAStudy.MA)*100,
              
      vSMA34.getValue(MAStudy.MA)*100
          
      );
      }

      function 
      postMain() {
         }

          function 
      onAction1() {
              
      setBarBgColor(Color.RGB(255,255,0));
              
      vLastAlert 1;
          } 

      Comment


      • #4
        Chris
        Given that in the return statement you are multiplying the averages by 100 I assume you are running this efs on Forex symbols.
        In that case while the values of the averages on the chart will be more than 0.25 apart due to the displaced decimal point the same will not be true in the conditional statement where the difference between the averages is less than 0.25 hence the continuous yellow background.
        Try replacing
        if ((Math.abs(vSMA8.getValue(MAStudy.MA) - vSMA34.getValue(MAStudy.MA)) <= .25))
        onAction1();

        with
        if ((Math.abs((vSMA8.getValue(MAStudy.MA)*100) - (vSMA34.getValue(MAStudy.MA)*100)) <= .25))
        onAction1();

        This should fix the issue
        Alex

        Comment


        • #5
          That multiplier was causing the problem. Thank you for all your help.

          Chris

          Comment

          Working...
          X