Announcement

Collapse
No announcement yet.

Some simple scripts

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    The attached efs will calculate the overnight high and low of a security.
    In Edit Studies you can set the Start time (default is 16:15) and End time (default 9:30). A third option - set On by default - extends the overnight high/low lines into the current day (shown in the enclosed image)
    Alex

    Attached Files

    Comment


    • #17
      The attached efs will compute and plot an MA or EMA (default is MA) of a user defined variable.
      Brief comments and an example are provided in the script. User can select the length and type of the average through Edit Studies
      Alex
      Attached Files

      Comment


      • #18
        With regards to the MAorEMAof_Template.efs posted in the prior message it should be noted that it is not always necessary to write all the code of the variable for which one wants to compute an MA or EMA. If an efs already exists that calculates that variable one can use call() or callFunction() to retrieve it.

        For example assume we want to compute the MA of the Chaikin Money Flow study (cmf.efs) that is available in the Library subfolder of Formulas. Rather than transferring all the code necessary to compute the Chaikin Money Flow into the MAorEMAof_Template.efs we would simply use call() to retrieve that value.
        Here is how the MAorEMAof_Template.efs script would need to be written

        PHP Code:
        function preMain() {

            
        setPriceStudy(false);
            
        setStudyTitle("MA-EMA");
            
        setCursorLabelName("MA-EMA"0);
            
        setDefaultBarStyle(PS_SOLID0);
            
        setDefaultBarFgColor(Color.blue0);
            
        setDefaultBarThickness(10);
            
        setPlotType(PLOTTYPE_LINE0);
                
            var 
        fp1 = new FunctionParameter("MALength"FunctionParameter.NUMBER);
            
        fp1.setLowerLimit(1);        
            
        fp1.setDefault(10);
            
            var 
        fp2 = new FunctionParameter("MAType"FunctionParameter.STRING);
            
        fp2.addOption("Exponential");
            
        fp2.addOption("Simple");
            
        fp2.setDefault("Simple"); 
        }

        var 
        bPrimed false
        var 
        vEMA null;
        var 
        vEMA1 null;   
        var 
        dPercent 0.0;
        var 
        vValue 0.0;
        var 
        aValue null;
        var 
        vAvg null;//used in the example provided

        function main(MALength,MAType) {

            var 
        i;
            var 
        vSum 0.0;
            
            
        //insert below the calculations required for the user defined variable (example provided)
            
            
        vAvg call("/Library/cmf.efs");//this line calls the cmf.efs study
            
        if (vAvg==null) return; //null check
            
            //end section for user defined variable

            
        vValue vAvg//replace vAvg with the user defined variable

            
        if(vValue == null) {
                return;
            }
            
            if (
        aValue == null) {
                
        aValue = new Array(MALength);
            }
            if (
        getBarState() == BARSTATE_NEWBAR) {
                
        aValue.pop();  
                
        aValue.unshift(vValue);
            } else {
            
        aValue[0] = vValue;
            }

            for(
        0MALengthi++) {
                
        vSum += aValue[i];
            }
            
            var 
        vMA = (vSum/MALength);
            
            
            if(
        MAType=="Exponential"){
                if(
        aValue[MALength-1] != nullvEMA EMA(MALengthaValue); 
                return 
        vEMA;
            }else{
                return 
        vMA;
            }
        }

        function 
        EMA(MALengthaValue) {

            var 
        dSum 0.0;

            if(
        getBarState() == BARSTATE_ALLBARS || bPrimed == false) {
                
        dPercent = (2.0 / (MALength 1.0));
                
        bPrimed false;
            }

            if (
        getBarState() == BARSTATE_NEWBAR) {
                
        vEMA1 vEMA;
            }

            if(
        bPrimed == false) {
                for(var 
        0MALengthi++) {
                    
        dSum += aValue[i];
                }
                
        bPrimed true;
                return (
        dSum MALength);
            } else {
                return (((
        vValue vEMA1) * dPercent) + vEMA1);
            }

        In the example above we "called" the cmf.efs using only the original parameters. However if we also wanted to be able to modify the parameters of that study we would add a few lines such as in the example below

        PHP Code:
        function preMain() {

            
        setPriceStudy(false);
            
        setStudyTitle("MA-EMA");
            
        setCursorLabelName("MA-EMA"0);
            
        setDefaultBarStyle(PS_SOLID0);
            
        setDefaultBarFgColor(Color.blue0);
            
        setDefaultBarThickness(10);
            
        setPlotType(PLOTTYPE_LINE0);
                
            var 
        fp1 = new FunctionParameter("MALength"FunctionParameter.NUMBER);
            
        fp1.setLowerLimit(1);        
            
        fp1.setDefault(10);
            
            var 
        fp2 = new FunctionParameter("MAType"FunctionParameter.STRING);
            
        fp2.addOption("Exponential");
            
        fp2.addOption("Simple");
            
        fp2.setDefault("Simple");      
        }

        var 
        bPrimed false
        var 
        vEMA null;
        var 
        vEMA1 null;   
        var 
        dPercent 0.0;
        var 
        vValue 0.0;
        var 
        aValue null;
        var 
        vAvg null;//used in the example provided

        function main(MALength,MAType,nInputLength) { //added parameter

            
        var i;
            var 
        vSum 0.0;
            
            
        //insert below the calculations required for the user defined variable (example provided)
            
            
        if(nInputLength == null) {// this section adds the parameter that will be passed back to cmf.efs
                
        nInputLength 21;
            }
            
            
        vAvg call("/Library/cmf.efs",nInputLength);//this line calls the cmf.efs study
                                                         //and passes the parameter
            
        if (vAvg==null) return; //null check
            
            //end section for user defined variable

            
        vValue vAvg//replace vAvg with the user defined variable

            
        if(vValue == null) {
                return;
            }
            
            if (
        aValue == null) {
                
        aValue = new Array(MALength);
            }
            if (
        getBarState() == BARSTATE_NEWBAR) {
                
        aValue.pop();  
                
        aValue.unshift(vValue);
            } else {
            
        aValue[0] = vValue;
            }

            for(
        0MALengthi++) {
                
        vSum += aValue[i];
            }
            
            var 
        vMA = (vSum/MALength);
            
            
            if(
        MAType=="Exponential"){
                if(
        aValue[MALength-1] != nullvEMA EMA(MALengthaValue); 
                return 
        vEMA;
            }else{
                return 
        vMA;
            }
        }

        function 
        EMA(MALengthaValue) {

            var 
        dSum 0.0;

            if(
        getBarState() == BARSTATE_ALLBARS || bPrimed == false) {
                
        dPercent = (2.0 / (MALength 1.0));
                
        bPrimed false;
            }

            if (
        getBarState() == BARSTATE_NEWBAR) {
                
        vEMA1 vEMA;
            }

            if(
        bPrimed == false) {
                for(var 
        0MALengthi++) {
                    
        dSum += aValue[i];
                }
                
        bPrimed true;
                return (
        dSum MALength);
            } else {
                return (((
        vValue vEMA1) * dPercent) + vEMA1);
            }

        Here below is the result ie the MA of the Chaikin Money Flow study (bottom indicator pane).



        In other words MAorEMAof_Template.efs allows one to easily calculate an MA or EMA of virtually any study for which there is an available efs.
        For more information on how to use call() or callFunction() mentioned earlier in this post see this thread.
        Alex

        Comment


        • #19
          The attached revision of MAorEMAof_template.efs posted earlier in this thread now includes the option to retrieve historical values of the MA (or EMA). Notes on the required syntax are included in the script.
          Alex
          Attached Files

          Comment


          • #20
            The attached efs paints the Kagi bars according to the last reversal. If the last reversal was up it will paint both Up and Down bars in lime, if the last reversal was down it will paint them in red. The colors can be selected through Edit Studies
            The enclosed images show how the plot is colored by the efs (top image) and by the chart default (bottom image)
            Alex



            Attached Files

            Comment


            • #21
              The attached revision of MAorEMAof_template2.efs posted earlier in this thread now includes the option for a Weighted moving average and has been optimized to be more efficient
              Alex
              Attached Files

              Comment


              • #22
                The attached revision of MA-EMA-WMAof_template.efs posted in the prior message now includes an offset parameter
                Alex
                Attached Files

                Comment

                Working...
                X