Announcement

Collapse
No announcement yet.

Moving Average Range

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

  • Moving Average Range

    Hi
    I am trying to make an indicator that wil paint the bar of a chart when a few moving averages are in a tight range. So if the 8, 13, 21 and 34ema are all in a 5 point range of each other it will color those bars. Am I writing this correctly? Any thoughts would be greatly appreciated.

    Chris

    PHP Code:
    var vEMA8 = new MAStudy(80"Close"MAStudy.EXPONENTIAL);
    var 
    vEMA13 = new MAStudy(130"Close"MAStudy.EXPONENTIAL);
    var 
    vEMA21 = new MAStudy(210"Close"MAStudy.EXPONENTIAL);
    var 
    vEMA34 = new MAStudy(340"Close"MAStudy.EXPONENTIAL);
    var 
    vLastAlert = -1;

    function 
    preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("movavgrg");

    }

    function 
    main() {

            if (
                
    AbsValue(vEMA8.getValue(MAStudy.MA)-vEMA13.getValue(MAStudy.MA)) <= 5.0 &&
                
    AbsValue(vEMA21.getValue(MAStudy.MA)-vEMA34.getValue(MAStudy.MA)) <= 5.0
            
    onAction1();

    }

    function 
    postMain() {
    }

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

  • #2
    Chris
    In the mean time here is a corrected version of your efs.

    PHP Code:
    var vEMA8  = new MAStudy(80"Close"MAStudy.EXPONENTIAL);
    var 
    vEMA13 = new MAStudy(130"Close"MAStudy.EXPONENTIAL);
    var 
    vEMA21 = new MAStudy(210"Close"MAStudy.EXPONENTIAL);
    var 
    vEMA34 = new MAStudy(340"Close"MAStudy.EXPONENTIAL);
    var 
    vLastAlert = -1;

    function 
    preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("movavgrg");
        
    setColorPriceBars(true);
        
    setDefaultPriceBarColor(Color.black);
    }

    function 
    main() {

        if (
    Math.abs(vEMA8.getValue(MAStudy.MA)-vEMA13.getValue(MAStudy.MA)) <= 5.0 &&
            
    Math.abs(vEMA21.getValue(MAStudy.MA)-vEMA34.getValue(MAStudy.MA)) <= 5.0)
                
    onAction1();
    }


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

    Also do all the averages have to be within the 5 point range? If that is the case then I am not positive that this efs will show that.
    Perhaps you may want to first calculate the mean of the averages then create a 2.5 point band around the mean that must contain all 4 averages. That should ensure that each average is no more than 5 points away from all the other ones.
    Alex

    Comment


    • #3
      Alex,
      I think your idea about the 2.5 point band should work good and definately alot cleaner that the 5 point range idea. Just out of curiosity, is there a way to make an array and have each parameter tested against the array? Like:

      arrray1 qw \ 8 13 21 34 \;

      ema8 <= array1; #so it tests each element in the array
      ema13 ...the same for the other elements

      I know the language is off, I have been working a little with perl for a sys ad project, so I got some of the concepts from there.

      Thanks for your help,
      Chris

      Comment


      • #4
        Chris,

        I put together a little efs that will demonstrate how to accomplish what you want. The JavaScript Math.max function does not work with arrays based on my experience. I created a function called aMax to obtain the max of an array. The step that checks for the max between the array and a number is tmp = Math.max(aMax(sample),4);. To look at the output of the efs, open the Formula Output window, or left mouseclick the button on the screen titled click here to see the aMax Trace.txt file

        I marked the applicable steps that you need with //here
        I hope this helps

        PHP Code:
        var barcount 0;
        var 
        sample = new Array(5,6,7,8,9,8,7,6,5,4,3,2);
        function 
        preMain(){
         
        }
        function 
        main(){

            if (
        getBarState() == BARSTATE_ALLBARS) {//true once during initial load and subsequent reload
                
        barcount 0;
            var 
        tmp =     Math.max(sample,4);
            
        trace ("sample = "+sample);
            
        trace ("12: tmp = "+tmp);
            
        tmp Math.max(aMax(sample),4);//here 
            
        trace ("14: tmp = "+tmp);

                
            }
            
        //if (getCurrentBarIndex() < -2 )return;
            
            
        if (getBarState() == BARSTATE_NEWBAR) {     

                
            }
            if (
        getCurrentBarIndex() == -){}
            

        //put stuff u want implemented every tick in here

        //
        }
        function 
        aMax(a){//here 
            
        var a1 a.length;var a2 = -9999;//here 
            
        for(i=0;ia1;i++){//here 
                
        a2 Math.max(a[i],a2);//here 
            
        }//here 
            
        return a2;//here 
        }//here 

        //########### trace section required to be copied complete ##############
        var traceon true;// previous tracefile is overwritten if this is true
        //var traceon = false;// previous tracefile is added to if this is false
        var nStudy "aMax Trace.txt";// -  name to be used to name tracefile and button
        var    tracefile = new File(nStudy);
        function 
        trace(data1){//if (getCurrentBarIndex() < -13)return;
            
        debugPrintln(data1);//comment this line out if you do not want the output echoed to the formula output window

            
        if (traceon){//first use is true, overwrites previous trace file
                
        traceon false;
                
        tracefile.open("wt+");tracefile.close();//opens and overwrites previous file
            
        }
            
        tracefile.open("at+");
            
        tracefile.writeln(data1);
            
        tracefile.close();
            if (
        trace.caller.name!="preMain" && trace.caller.name!="postMain")tButton();//call to the button that allows 
                                                                                        //opening text file
        }
        function 
        tButton(){
        drawTextPixel(5010, (" click here to see the "+nStudy+" file " "@URL=C:/Program Files/eSignal/FormulaOutput/"+nStudy),  
        Color.rednullText.RELATIVETOLEFT Text.RELATIVETOBOTTOM|Text.FRAME"Comic Sans MS"13,"trace11");
        }
        //########## section required to be copied complete ################ 

        Comment


        • #5
          Hi Steve,

          This script is awesome! Thank you for taking the time to put it togeather.

          Chris

          Comment


          • #6
            Chris,

            You are welcome, I appreciate the feedback

            Comment

            Working...
            X